Merge tag 'nfsd-6.2-6' of git://git.kernel.org/pub/scm/linux/kernel/git/cel/linux
[linux-block.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                         int sw_state = wacom_wac->hid_data.barrelswitch |
2525                                        (wacom_wac->hid_data.barrelswitch2 << 1);
2526                         wacom_wac->hid_data.barrelswitch = sw_state == 1;
2527                         wacom_wac->hid_data.barrelswitch2 = sw_state == 2;
2528                         wacom_wac->hid_data.barrelswitch3 = sw_state == 3;
2529                 }
2530                 input_report_key(input, BTN_STYLUS, wacom_wac->hid_data.barrelswitch);
2531                 input_report_key(input, BTN_STYLUS2, wacom_wac->hid_data.barrelswitch2);
2532                 input_report_key(input, BTN_STYLUS3, wacom_wac->hid_data.barrelswitch3);
2533
2534                 /*
2535                  * Non-USI EMR tools should have their IDs mangled to
2536                  * match the legacy behavior of wacom_intuos_general
2537                  */
2538                 if (wacom_wac->serial[0] >> 52 == 1)
2539                         id = wacom_intuos_id_mangle(id);
2540
2541                 /*
2542                  * To ensure compatibility with xf86-input-wacom, we should
2543                  * report the BTN_TOOL_* event prior to the ABS_MISC or
2544                  * MSC_SERIAL events.
2545                  */
2546                 input_report_key(input, BTN_TOUCH,
2547                                 wacom_wac->hid_data.tipswitch);
2548                 input_report_key(input, wacom_wac->tool[0], sense);
2549                 if (wacom_wac->serial[0]) {
2550                         input_event(input, EV_MSC, MSC_SERIAL, wacom_wac->serial[0]);
2551                         input_report_abs(input, ABS_MISC, sense ? id : 0);
2552                 }
2553
2554                 wacom_wac->hid_data.tipswitch = false;
2555
2556                 input_sync(input);
2557         }
2558
2559         if (!sense) {
2560                 wacom_wac->tool[0] = 0;
2561                 wacom_wac->id[0] = 0;
2562                 wacom_wac->serial[0] = 0;
2563         }
2564 }
2565
2566 static void wacom_wac_finger_usage_mapping(struct hid_device *hdev,
2567                 struct hid_field *field, struct hid_usage *usage)
2568 {
2569         struct wacom *wacom = hid_get_drvdata(hdev);
2570         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2571         struct input_dev *input = wacom_wac->touch_input;
2572         unsigned touch_max = wacom_wac->features.touch_max;
2573         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2574
2575         switch (equivalent_usage) {
2576         case HID_GD_X:
2577                 if (touch_max == 1)
2578                         wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 4);
2579                 else
2580                         wacom_map_usage(input, usage, field, EV_ABS,
2581                                         ABS_MT_POSITION_X, 4);
2582                 break;
2583         case HID_GD_Y:
2584                 if (touch_max == 1)
2585                         wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 4);
2586                 else
2587                         wacom_map_usage(input, usage, field, EV_ABS,
2588                                         ABS_MT_POSITION_Y, 4);
2589                 break;
2590         case HID_DG_WIDTH:
2591         case HID_DG_HEIGHT:
2592                 wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MAJOR, 0);
2593                 wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MINOR, 0);
2594                 input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
2595                 break;
2596         case HID_DG_TIPSWITCH:
2597                 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
2598                 break;
2599         case HID_DG_CONTACTCOUNT:
2600                 wacom_wac->hid_data.cc_report = field->report->id;
2601                 wacom_wac->hid_data.cc_index = field->index;
2602                 wacom_wac->hid_data.cc_value_index = usage->usage_index;
2603                 break;
2604         case HID_DG_CONTACTID:
2605                 if ((field->logical_maximum - field->logical_minimum) < touch_max) {
2606                         /*
2607                          * The HID descriptor for G11 sensors leaves logical
2608                          * maximum set to '1' despite it being a multitouch
2609                          * device. Override to a sensible number.
2610                          */
2611                         field->logical_maximum = 255;
2612                 }
2613                 break;
2614         case HID_DG_SCANTIME:
2615                 wacom_map_usage(input, usage, field, EV_MSC, MSC_TIMESTAMP, 0);
2616                 break;
2617         }
2618 }
2619
2620 static void wacom_wac_finger_slot(struct wacom_wac *wacom_wac,
2621                 struct input_dev *input)
2622 {
2623         struct hid_data *hid_data = &wacom_wac->hid_data;
2624         bool mt = wacom_wac->features.touch_max > 1;
2625         bool prox = hid_data->tipswitch &&
2626                     report_touch_events(wacom_wac);
2627
2628         if (touch_is_muted(wacom_wac)) {
2629                 if (!wacom_wac->shared->touch_down)
2630                         return;
2631                 prox = false;
2632         }
2633
2634         wacom_wac->hid_data.num_received++;
2635         if (wacom_wac->hid_data.num_received > wacom_wac->hid_data.num_expected)
2636                 return;
2637
2638         if (mt) {
2639                 int slot;
2640
2641                 slot = input_mt_get_slot_by_key(input, hid_data->id);
2642                 if (slot < 0) {
2643                         return;
2644                 } else {
2645                         struct input_mt_slot *ps = &input->mt->slots[slot];
2646                         int mt_id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
2647
2648                         if (!prox && mt_id < 0) {
2649                                 // No data to send for this slot; short-circuit
2650                                 return;
2651                         }
2652                 }
2653
2654                 input_mt_slot(input, slot);
2655                 input_mt_report_slot_state(input, MT_TOOL_FINGER, prox);
2656         }
2657         else {
2658                 input_report_key(input, BTN_TOUCH, prox);
2659         }
2660
2661         if (prox) {
2662                 input_report_abs(input, mt ? ABS_MT_POSITION_X : ABS_X,
2663                                  hid_data->x);
2664                 input_report_abs(input, mt ? ABS_MT_POSITION_Y : ABS_Y,
2665                                  hid_data->y);
2666
2667                 if (test_bit(ABS_MT_TOUCH_MAJOR, input->absbit)) {
2668                         input_report_abs(input, ABS_MT_TOUCH_MAJOR, max(hid_data->width, hid_data->height));
2669                         input_report_abs(input, ABS_MT_TOUCH_MINOR, min(hid_data->width, hid_data->height));
2670                         if (hid_data->width != hid_data->height)
2671                                 input_report_abs(input, ABS_MT_ORIENTATION, hid_data->width <= hid_data->height ? 0 : 1);
2672                 }
2673         }
2674 }
2675
2676 static bool wacom_wac_slot_is_active(struct input_dev *dev, int key)
2677 {
2678         struct input_mt *mt = dev->mt;
2679         struct input_mt_slot *s;
2680
2681         if (!mt)
2682                 return false;
2683
2684         for (s = mt->slots; s != mt->slots + mt->num_slots; s++) {
2685                 if (s->key == key &&
2686                         input_mt_get_value(s, ABS_MT_TRACKING_ID) >= 0) {
2687                         return true;
2688                 }
2689         }
2690
2691         return false;
2692 }
2693
2694 static void wacom_wac_finger_event(struct hid_device *hdev,
2695                 struct hid_field *field, struct hid_usage *usage, __s32 value)
2696 {
2697         struct wacom *wacom = hid_get_drvdata(hdev);
2698         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2699         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2700         struct wacom_features *features = &wacom->wacom_wac.features;
2701
2702         if (touch_is_muted(wacom_wac) && !wacom_wac->shared->touch_down)
2703                 return;
2704
2705         if (wacom_wac->is_invalid_bt_frame)
2706                 return;
2707
2708         switch (equivalent_usage) {
2709         case HID_DG_CONFIDENCE:
2710                 wacom_wac->hid_data.confidence = value;
2711                 break;
2712         case HID_GD_X:
2713                 wacom_wac->hid_data.x = value;
2714                 break;
2715         case HID_GD_Y:
2716                 wacom_wac->hid_data.y = value;
2717                 break;
2718         case HID_DG_WIDTH:
2719                 wacom_wac->hid_data.width = value;
2720                 break;
2721         case HID_DG_HEIGHT:
2722                 wacom_wac->hid_data.height = value;
2723                 break;
2724         case HID_DG_CONTACTID:
2725                 wacom_wac->hid_data.id = value;
2726                 break;
2727         case HID_DG_TIPSWITCH:
2728                 wacom_wac->hid_data.tipswitch = value;
2729                 break;
2730         case WACOM_HID_WT_REPORT_VALID:
2731                 wacom_wac->is_invalid_bt_frame = !value;
2732                 return;
2733         case HID_DG_CONTACTMAX:
2734                 if (!features->touch_max) {
2735                         features->touch_max = value;
2736                 } else {
2737                         hid_warn(hdev, "%s: ignoring attempt to overwrite non-zero touch_max "
2738                                  "%d -> %d\n", __func__, features->touch_max, value);
2739                 }
2740                 return;
2741         }
2742
2743         if (usage->usage_index + 1 == field->report_count) {
2744                 if (equivalent_usage == wacom_wac->hid_data.last_slot_field) {
2745                         bool touch_removed = wacom_wac_slot_is_active(wacom_wac->touch_input,
2746                                 wacom_wac->hid_data.id) && !wacom_wac->hid_data.tipswitch;
2747
2748                         if (wacom_wac->hid_data.confidence || touch_removed) {
2749                                 wacom_wac_finger_slot(wacom_wac, wacom_wac->touch_input);
2750                         }
2751                 }
2752         }
2753 }
2754
2755 static void wacom_wac_finger_pre_report(struct hid_device *hdev,
2756                 struct hid_report *report)
2757 {
2758         struct wacom *wacom = hid_get_drvdata(hdev);
2759         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2760         struct hid_data* hid_data = &wacom_wac->hid_data;
2761         int i;
2762
2763         if (touch_is_muted(wacom_wac) && !wacom_wac->shared->touch_down)
2764                 return;
2765
2766         wacom_wac->is_invalid_bt_frame = false;
2767
2768         hid_data->confidence = true;
2769
2770         hid_data->cc_report = 0;
2771         hid_data->cc_index = -1;
2772         hid_data->cc_value_index = -1;
2773
2774         for (i = 0; i < report->maxfield; i++) {
2775                 struct hid_field *field = report->field[i];
2776                 int j;
2777
2778                 for (j = 0; j < field->maxusage; j++) {
2779                         struct hid_usage *usage = &field->usage[j];
2780                         unsigned int equivalent_usage =
2781                                 wacom_equivalent_usage(usage->hid);
2782
2783                         switch (equivalent_usage) {
2784                         case HID_GD_X:
2785                         case HID_GD_Y:
2786                         case HID_DG_WIDTH:
2787                         case HID_DG_HEIGHT:
2788                         case HID_DG_CONTACTID:
2789                         case HID_DG_INRANGE:
2790                         case HID_DG_INVERT:
2791                         case HID_DG_TIPSWITCH:
2792                                 hid_data->last_slot_field = equivalent_usage;
2793                                 break;
2794                         case HID_DG_CONTACTCOUNT:
2795                                 hid_data->cc_report = report->id;
2796                                 hid_data->cc_index = i;
2797                                 hid_data->cc_value_index = j;
2798                                 break;
2799                         }
2800                 }
2801         }
2802
2803         if (hid_data->cc_report != 0 &&
2804             hid_data->cc_index >= 0) {
2805                 struct hid_field *field = report->field[hid_data->cc_index];
2806                 int value = field->value[hid_data->cc_value_index];
2807                 if (value) {
2808                         hid_data->num_expected = value;
2809                         hid_data->num_received = 0;
2810                 }
2811         }
2812         else {
2813                 hid_data->num_expected = wacom_wac->features.touch_max;
2814                 hid_data->num_received = 0;
2815         }
2816 }
2817
2818 static void wacom_wac_finger_report(struct hid_device *hdev,
2819                 struct hid_report *report)
2820 {
2821         struct wacom *wacom = hid_get_drvdata(hdev);
2822         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2823         struct input_dev *input = wacom_wac->touch_input;
2824         unsigned touch_max = wacom_wac->features.touch_max;
2825
2826         /* if there was nothing to process, don't send an empty sync */
2827         if (wacom_wac->hid_data.num_expected == 0)
2828                 return;
2829
2830         /* If more packets of data are expected, give us a chance to
2831          * process them rather than immediately syncing a partial
2832          * update.
2833          */
2834         if (wacom_wac->hid_data.num_received < wacom_wac->hid_data.num_expected)
2835                 return;
2836
2837         if (touch_max > 1)
2838                 input_mt_sync_frame(input);
2839
2840         input_sync(input);
2841         wacom_wac->hid_data.num_received = 0;
2842         wacom_wac->hid_data.num_expected = 0;
2843
2844         /* keep touch state for pen event */
2845         wacom_wac->shared->touch_down = wacom_wac_finger_count_touches(wacom_wac);
2846 }
2847
2848 void wacom_wac_usage_mapping(struct hid_device *hdev,
2849                 struct hid_field *field, struct hid_usage *usage)
2850 {
2851         struct wacom *wacom = hid_get_drvdata(hdev);
2852         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2853         struct wacom_features *features = &wacom_wac->features;
2854
2855         if (WACOM_DIRECT_DEVICE(field))
2856                 features->device_type |= WACOM_DEVICETYPE_DIRECT;
2857
2858         /* usage tests must precede field tests */
2859         if (WACOM_BATTERY_USAGE(usage))
2860                 wacom_wac_battery_usage_mapping(hdev, field, usage);
2861         else if (WACOM_PAD_FIELD(field))
2862                 wacom_wac_pad_usage_mapping(hdev, field, usage);
2863         else if (WACOM_PEN_FIELD(field))
2864                 wacom_wac_pen_usage_mapping(hdev, field, usage);
2865         else if (WACOM_FINGER_FIELD(field))
2866                 wacom_wac_finger_usage_mapping(hdev, field, usage);
2867 }
2868
2869 void wacom_wac_event(struct hid_device *hdev, struct hid_field *field,
2870                 struct hid_usage *usage, __s32 value)
2871 {
2872         struct wacom *wacom = hid_get_drvdata(hdev);
2873
2874         if (wacom->wacom_wac.features.type != HID_GENERIC)
2875                 return;
2876
2877         if (value > field->logical_maximum || value < field->logical_minimum)
2878                 return;
2879
2880         /* usage tests must precede field tests */
2881         if (WACOM_BATTERY_USAGE(usage))
2882                 wacom_wac_battery_event(hdev, field, usage, value);
2883         else if (WACOM_PAD_FIELD(field))
2884                 wacom_wac_pad_event(hdev, field, usage, value);
2885         else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2886                 wacom_wac_pen_event(hdev, field, usage, value);
2887         else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
2888                 wacom_wac_finger_event(hdev, field, usage, value);
2889 }
2890
2891 static void wacom_report_events(struct hid_device *hdev,
2892                                 struct hid_report *report, int collection_index,
2893                                 int field_index)
2894 {
2895         int r;
2896
2897         for (r = field_index; r < report->maxfield; r++) {
2898                 struct hid_field *field;
2899                 unsigned count, n;
2900
2901                 field = report->field[r];
2902                 count = field->report_count;
2903
2904                 if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
2905                         continue;
2906
2907                 for (n = 0 ; n < count; n++) {
2908                         if (field->usage[n].collection_index == collection_index)
2909                                 wacom_wac_event(hdev, field, &field->usage[n],
2910                                                 field->value[n]);
2911                         else
2912                                 return;
2913                 }
2914         }
2915 }
2916
2917 static int wacom_wac_collection(struct hid_device *hdev, struct hid_report *report,
2918                          int collection_index, struct hid_field *field,
2919                          int field_index)
2920 {
2921         struct wacom *wacom = hid_get_drvdata(hdev);
2922
2923         wacom_report_events(hdev, report, collection_index, field_index);
2924
2925         /*
2926          * Non-input reports may be sent prior to the device being
2927          * completely initialized. Since only their events need
2928          * to be processed, exit after 'wacom_report_events' has
2929          * been called to prevent potential crashes in the report-
2930          * processing functions.
2931          */
2932         if (report->type != HID_INPUT_REPORT)
2933                 return -1;
2934
2935         if (WACOM_PAD_FIELD(field))
2936                 return 0;
2937         else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2938                 wacom_wac_pen_report(hdev, report);
2939         else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
2940                 wacom_wac_finger_report(hdev, report);
2941
2942         return 0;
2943 }
2944
2945 void wacom_wac_report(struct hid_device *hdev, struct hid_report *report)
2946 {
2947         struct wacom *wacom = hid_get_drvdata(hdev);
2948         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2949         struct hid_field *field;
2950         bool pad_in_hid_field = false, pen_in_hid_field = false,
2951                 finger_in_hid_field = false, true_pad = false;
2952         int r;
2953         int prev_collection = -1;
2954
2955         if (wacom_wac->features.type != HID_GENERIC)
2956                 return;
2957
2958         for (r = 0; r < report->maxfield; r++) {
2959                 field = report->field[r];
2960
2961                 if (WACOM_PAD_FIELD(field))
2962                         pad_in_hid_field = true;
2963                 if (WACOM_PEN_FIELD(field))
2964                         pen_in_hid_field = true;
2965                 if (WACOM_FINGER_FIELD(field))
2966                         finger_in_hid_field = true;
2967                 if (wacom_equivalent_usage(field->physical) == HID_DG_TABLETFUNCTIONKEY)
2968                         true_pad = true;
2969         }
2970
2971         wacom_wac_battery_pre_report(hdev, report);
2972
2973         if (pad_in_hid_field && wacom->wacom_wac.pad_input)
2974                 wacom_wac_pad_pre_report(hdev, report);
2975         if (pen_in_hid_field && wacom->wacom_wac.pen_input)
2976                 wacom_wac_pen_pre_report(hdev, report);
2977         if (finger_in_hid_field && wacom->wacom_wac.touch_input)
2978                 wacom_wac_finger_pre_report(hdev, report);
2979
2980         for (r = 0; r < report->maxfield; r++) {
2981                 field = report->field[r];
2982
2983                 if (field->usage[0].collection_index != prev_collection) {
2984                         if (wacom_wac_collection(hdev, report,
2985                                 field->usage[0].collection_index, field, r) < 0)
2986                                 return;
2987                         prev_collection = field->usage[0].collection_index;
2988                 }
2989         }
2990
2991         wacom_wac_battery_report(hdev, report);
2992
2993         if (true_pad && wacom->wacom_wac.pad_input)
2994                 wacom_wac_pad_report(hdev, report, field);
2995 }
2996
2997 static int wacom_bpt_touch(struct wacom_wac *wacom)
2998 {
2999         struct wacom_features *features = &wacom->features;
3000         struct input_dev *input = wacom->touch_input;
3001         struct input_dev *pad_input = wacom->pad_input;
3002         unsigned char *data = wacom->data;
3003         int i;
3004
3005         if (data[0] != 0x02)
3006             return 0;
3007
3008         for (i = 0; i < 2; i++) {
3009                 int offset = (data[1] & 0x80) ? (8 * i) : (9 * i);
3010                 bool touch = report_touch_events(wacom)
3011                            && (data[offset + 3] & 0x80);
3012
3013                 input_mt_slot(input, i);
3014                 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
3015                 if (touch) {
3016                         int x = get_unaligned_be16(&data[offset + 3]) & 0x7ff;
3017                         int y = get_unaligned_be16(&data[offset + 5]) & 0x7ff;
3018                         if (features->quirks & WACOM_QUIRK_BBTOUCH_LOWRES) {
3019                                 x <<= 5;
3020                                 y <<= 5;
3021                         }
3022                         input_report_abs(input, ABS_MT_POSITION_X, x);
3023                         input_report_abs(input, ABS_MT_POSITION_Y, y);
3024                 }
3025         }
3026
3027         input_mt_sync_frame(input);
3028
3029         input_report_key(pad_input, BTN_LEFT, (data[1] & 0x08) != 0);
3030         input_report_key(pad_input, BTN_FORWARD, (data[1] & 0x04) != 0);
3031         input_report_key(pad_input, BTN_BACK, (data[1] & 0x02) != 0);
3032         input_report_key(pad_input, BTN_RIGHT, (data[1] & 0x01) != 0);
3033         wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
3034
3035         return 1;
3036 }
3037
3038 static void wacom_bpt3_touch_msg(struct wacom_wac *wacom, unsigned char *data)
3039 {
3040         struct wacom_features *features = &wacom->features;
3041         struct input_dev *input = wacom->touch_input;
3042         bool touch = data[1] & 0x80;
3043         int slot = input_mt_get_slot_by_key(input, data[0]);
3044
3045         if (slot < 0)
3046                 return;
3047
3048         touch = touch && report_touch_events(wacom);
3049
3050         input_mt_slot(input, slot);
3051         input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
3052
3053         if (touch) {
3054                 int x = (data[2] << 4) | (data[4] >> 4);
3055                 int y = (data[3] << 4) | (data[4] & 0x0f);
3056                 int width, height;
3057
3058                 if (features->type >= INTUOSPS && features->type <= INTUOSHT2) {
3059                         width  = data[5] * 100;
3060                         height = data[6] * 100;
3061                 } else {
3062                         /*
3063                          * "a" is a scaled-down area which we assume is
3064                          * roughly circular and which can be described as:
3065                          * a=(pi*r^2)/C.
3066                          */
3067                         int a = data[5];
3068                         int x_res = input_abs_get_res(input, ABS_MT_POSITION_X);
3069                         int y_res = input_abs_get_res(input, ABS_MT_POSITION_Y);
3070                         width = 2 * int_sqrt(a * WACOM_CONTACT_AREA_SCALE);
3071                         height = width * y_res / x_res;
3072                 }
3073
3074                 input_report_abs(input, ABS_MT_POSITION_X, x);
3075                 input_report_abs(input, ABS_MT_POSITION_Y, y);
3076                 input_report_abs(input, ABS_MT_TOUCH_MAJOR, width);
3077                 input_report_abs(input, ABS_MT_TOUCH_MINOR, height);
3078         }
3079 }
3080
3081 static void wacom_bpt3_button_msg(struct wacom_wac *wacom, unsigned char *data)
3082 {
3083         struct input_dev *input = wacom->pad_input;
3084         struct wacom_features *features = &wacom->features;
3085
3086         if (features->type == INTUOSHT || features->type == INTUOSHT2) {
3087                 input_report_key(input, BTN_LEFT, (data[1] & 0x02) != 0);
3088                 input_report_key(input, BTN_BACK, (data[1] & 0x08) != 0);
3089         } else {
3090                 input_report_key(input, BTN_BACK, (data[1] & 0x02) != 0);
3091                 input_report_key(input, BTN_LEFT, (data[1] & 0x08) != 0);
3092         }
3093         input_report_key(input, BTN_FORWARD, (data[1] & 0x04) != 0);
3094         input_report_key(input, BTN_RIGHT, (data[1] & 0x01) != 0);
3095 }
3096
3097 static int wacom_bpt3_touch(struct wacom_wac *wacom)
3098 {
3099         unsigned char *data = wacom->data;
3100         int count = data[1] & 0x07;
3101         int  touch_changed = 0, i;
3102
3103         if (data[0] != 0x02)
3104             return 0;
3105
3106         /* data has up to 7 fixed sized 8-byte messages starting at data[2] */
3107         for (i = 0; i < count; i++) {
3108                 int offset = (8 * i) + 2;
3109                 int msg_id = data[offset];
3110
3111                 if (msg_id >= 2 && msg_id <= 17) {
3112                         wacom_bpt3_touch_msg(wacom, data + offset);
3113                         touch_changed++;
3114                 } else if (msg_id == 128)
3115                         wacom_bpt3_button_msg(wacom, data + offset);
3116
3117         }
3118
3119         /* only update touch if we actually have a touchpad and touch data changed */
3120         if (wacom->touch_input && touch_changed) {
3121                 input_mt_sync_frame(wacom->touch_input);
3122                 wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
3123         }
3124
3125         return 1;
3126 }
3127
3128 static int wacom_bpt_pen(struct wacom_wac *wacom)
3129 {
3130         struct wacom_features *features = &wacom->features;
3131         struct input_dev *input = wacom->pen_input;
3132         unsigned char *data = wacom->data;
3133         int x = 0, y = 0, p = 0, d = 0;
3134         bool pen = false, btn1 = false, btn2 = false;
3135         bool range, prox, rdy;
3136
3137         if (data[0] != WACOM_REPORT_PENABLED)
3138             return 0;
3139
3140         range = (data[1] & 0x80) == 0x80;
3141         prox = (data[1] & 0x40) == 0x40;
3142         rdy = (data[1] & 0x20) == 0x20;
3143
3144         wacom->shared->stylus_in_proximity = range;
3145         if (delay_pen_events(wacom))
3146                 return 0;
3147
3148         if (rdy) {
3149                 p = le16_to_cpup((__le16 *)&data[6]);
3150                 pen = data[1] & 0x01;
3151                 btn1 = data[1] & 0x02;
3152                 btn2 = data[1] & 0x04;
3153         }
3154         if (prox) {
3155                 x = le16_to_cpup((__le16 *)&data[2]);
3156                 y = le16_to_cpup((__le16 *)&data[4]);
3157
3158                 if (data[1] & 0x08) {
3159                         wacom->tool[0] = BTN_TOOL_RUBBER;
3160                         wacom->id[0] = ERASER_DEVICE_ID;
3161                 } else {
3162                         wacom->tool[0] = BTN_TOOL_PEN;
3163                         wacom->id[0] = STYLUS_DEVICE_ID;
3164                 }
3165                 wacom->reporting_data = true;
3166         }
3167         if (range) {
3168                 /*
3169                  * Convert distance from out prox to distance from tablet.
3170                  * distance will be greater than distance_max once
3171                  * touching and applying pressure; do not report negative
3172                  * distance.
3173                  */
3174                 if (data[8] <= features->distance_max)
3175                         d = features->distance_max - data[8];
3176         } else {
3177                 wacom->id[0] = 0;
3178         }
3179
3180         if (wacom->reporting_data) {
3181                 input_report_key(input, BTN_TOUCH, pen);
3182                 input_report_key(input, BTN_STYLUS, btn1);
3183                 input_report_key(input, BTN_STYLUS2, btn2);
3184
3185                 if (prox || !range) {
3186                         input_report_abs(input, ABS_X, x);
3187                         input_report_abs(input, ABS_Y, y);
3188                 }
3189                 input_report_abs(input, ABS_PRESSURE, p);
3190                 input_report_abs(input, ABS_DISTANCE, d);
3191
3192                 input_report_key(input, wacom->tool[0], range); /* PEN or RUBBER */
3193                 input_report_abs(input, ABS_MISC, wacom->id[0]); /* TOOL ID */
3194         }
3195
3196         if (!range) {
3197                 wacom->reporting_data = false;
3198         }
3199
3200         return 1;
3201 }
3202
3203 static int wacom_bpt_irq(struct wacom_wac *wacom, size_t len)
3204 {
3205         struct wacom_features *features = &wacom->features;
3206
3207         if ((features->type == INTUOSHT2) &&
3208             (features->device_type & WACOM_DEVICETYPE_PEN))
3209                 return wacom_intuos_irq(wacom);
3210         else if (len == WACOM_PKGLEN_BBTOUCH)
3211                 return wacom_bpt_touch(wacom);
3212         else if (len == WACOM_PKGLEN_BBTOUCH3)
3213                 return wacom_bpt3_touch(wacom);
3214         else if (len == WACOM_PKGLEN_BBFUN || len == WACOM_PKGLEN_BBPEN)
3215                 return wacom_bpt_pen(wacom);
3216
3217         return 0;
3218 }
3219
3220 static void wacom_bamboo_pad_pen_event(struct wacom_wac *wacom,
3221                 unsigned char *data)
3222 {
3223         unsigned char prefix;
3224
3225         /*
3226          * We need to reroute the event from the debug interface to the
3227          * pen interface.
3228          * We need to add the report ID to the actual pen report, so we
3229          * temporary overwrite the first byte to prevent having to kzalloc/kfree
3230          * and memcpy the report.
3231          */
3232         prefix = data[0];
3233         data[0] = WACOM_REPORT_BPAD_PEN;
3234
3235         /*
3236          * actually reroute the event.
3237          * No need to check if wacom->shared->pen is valid, hid_input_report()
3238          * will check for us.
3239          */
3240         hid_input_report(wacom->shared->pen, HID_INPUT_REPORT, data,
3241                          WACOM_PKGLEN_PENABLED, 1);
3242
3243         data[0] = prefix;
3244 }
3245
3246 static int wacom_bamboo_pad_touch_event(struct wacom_wac *wacom,
3247                 unsigned char *data)
3248 {
3249         struct input_dev *input = wacom->touch_input;
3250         unsigned char *finger_data, prefix;
3251         unsigned id;
3252         int x, y;
3253         bool valid;
3254
3255         prefix = data[0];
3256
3257         for (id = 0; id < wacom->features.touch_max; id++) {
3258                 valid = !!(prefix & BIT(id)) &&
3259                         report_touch_events(wacom);
3260
3261                 input_mt_slot(input, id);
3262                 input_mt_report_slot_state(input, MT_TOOL_FINGER, valid);
3263
3264                 if (!valid)
3265                         continue;
3266
3267                 finger_data = data + 1 + id * 3;
3268                 x = finger_data[0] | ((finger_data[1] & 0x0f) << 8);
3269                 y = (finger_data[2] << 4) | (finger_data[1] >> 4);
3270
3271                 input_report_abs(input, ABS_MT_POSITION_X, x);
3272                 input_report_abs(input, ABS_MT_POSITION_Y, y);
3273         }
3274
3275         input_mt_sync_frame(input);
3276
3277         input_report_key(input, BTN_LEFT, prefix & 0x40);
3278         input_report_key(input, BTN_RIGHT, prefix & 0x80);
3279
3280         /* keep touch state for pen event */
3281         wacom->shared->touch_down = !!prefix && report_touch_events(wacom);
3282
3283         return 1;
3284 }
3285
3286 static int wacom_bamboo_pad_irq(struct wacom_wac *wacom, size_t len)
3287 {
3288         unsigned char *data = wacom->data;
3289
3290         if (!((len == WACOM_PKGLEN_BPAD_TOUCH) ||
3291               (len == WACOM_PKGLEN_BPAD_TOUCH_USB)) ||
3292             (data[0] != WACOM_REPORT_BPAD_TOUCH))
3293                 return 0;
3294
3295         if (data[1] & 0x01)
3296                 wacom_bamboo_pad_pen_event(wacom, &data[1]);
3297
3298         if (data[1] & 0x02)
3299                 return wacom_bamboo_pad_touch_event(wacom, &data[9]);
3300
3301         return 0;
3302 }
3303
3304 static int wacom_wireless_irq(struct wacom_wac *wacom, size_t len)
3305 {
3306         unsigned char *data = wacom->data;
3307         int connected;
3308
3309         if (len != WACOM_PKGLEN_WIRELESS || data[0] != WACOM_REPORT_WL)
3310                 return 0;
3311
3312         connected = data[1] & 0x01;
3313         if (connected) {
3314                 int pid, battery, charging;
3315
3316                 if ((wacom->shared->type == INTUOSHT ||
3317                     wacom->shared->type == INTUOSHT2) &&
3318                     wacom->shared->touch_input &&
3319                     wacom->shared->touch_max) {
3320                         input_report_switch(wacom->shared->touch_input,
3321                                         SW_MUTE_DEVICE, data[5] & 0x40);
3322                         input_sync(wacom->shared->touch_input);
3323                 }
3324
3325                 pid = get_unaligned_be16(&data[6]);
3326                 battery = (data[5] & 0x3f) * 100 / 31;
3327                 charging = !!(data[5] & 0x80);
3328                 if (wacom->pid != pid) {
3329                         wacom->pid = pid;
3330                         wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
3331                 }
3332
3333                 wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
3334                                      battery, charging, 1, 0);
3335
3336         } else if (wacom->pid != 0) {
3337                 /* disconnected while previously connected */
3338                 wacom->pid = 0;
3339                 wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
3340                 wacom_notify_battery(wacom, POWER_SUPPLY_STATUS_UNKNOWN, 0, 0, 0, 0);
3341         }
3342
3343         return 0;
3344 }
3345
3346 static int wacom_status_irq(struct wacom_wac *wacom_wac, size_t len)
3347 {
3348         struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
3349         struct wacom_features *features = &wacom_wac->features;
3350         unsigned char *data = wacom_wac->data;
3351
3352         if (data[0] != WACOM_REPORT_USB)
3353                 return 0;
3354
3355         if ((features->type == INTUOSHT ||
3356             features->type == INTUOSHT2) &&
3357             wacom_wac->shared->touch_input &&
3358             features->touch_max) {
3359                 input_report_switch(wacom_wac->shared->touch_input,
3360                                     SW_MUTE_DEVICE, data[8] & 0x40);
3361                 input_sync(wacom_wac->shared->touch_input);
3362         }
3363
3364         if (data[9] & 0x02) { /* wireless module is attached */
3365                 int battery = (data[8] & 0x3f) * 100 / 31;
3366                 bool charging = !!(data[8] & 0x80);
3367
3368                 wacom_notify_battery(wacom_wac, WACOM_POWER_SUPPLY_STATUS_AUTO,
3369                                      battery, charging, battery || charging, 1);
3370
3371                 if (!wacom->battery.battery &&
3372                     !(features->quirks & WACOM_QUIRK_BATTERY)) {
3373                         features->quirks |= WACOM_QUIRK_BATTERY;
3374                         wacom_schedule_work(wacom_wac, WACOM_WORKER_BATTERY);
3375                 }
3376         }
3377         else if ((features->quirks & WACOM_QUIRK_BATTERY) &&
3378                  wacom->battery.battery) {
3379                 features->quirks &= ~WACOM_QUIRK_BATTERY;
3380                 wacom_schedule_work(wacom_wac, WACOM_WORKER_BATTERY);
3381                 wacom_notify_battery(wacom_wac, POWER_SUPPLY_STATUS_UNKNOWN, 0, 0, 0, 0);
3382         }
3383         return 0;
3384 }
3385
3386 void wacom_wac_irq(struct wacom_wac *wacom_wac, size_t len)
3387 {
3388         bool sync;
3389
3390         switch (wacom_wac->features.type) {
3391         case PENPARTNER:
3392                 sync = wacom_penpartner_irq(wacom_wac);
3393                 break;
3394
3395         case PL:
3396                 sync = wacom_pl_irq(wacom_wac);
3397                 break;
3398
3399         case WACOM_G4:
3400         case GRAPHIRE:
3401         case GRAPHIRE_BT:
3402         case WACOM_MO:
3403                 sync = wacom_graphire_irq(wacom_wac);
3404                 break;
3405
3406         case PTU:
3407                 sync = wacom_ptu_irq(wacom_wac);
3408                 break;
3409
3410         case DTU:
3411                 sync = wacom_dtu_irq(wacom_wac);
3412                 break;
3413
3414         case DTUS:
3415         case DTUSX:
3416                 sync = wacom_dtus_irq(wacom_wac);
3417                 break;
3418
3419         case INTUOS:
3420         case INTUOS3S:
3421         case INTUOS3:
3422         case INTUOS3L:
3423         case INTUOS4S:
3424         case INTUOS4:
3425         case INTUOS4L:
3426         case CINTIQ:
3427         case WACOM_BEE:
3428         case WACOM_13HD:
3429         case WACOM_21UX2:
3430         case WACOM_22HD:
3431         case WACOM_24HD:
3432         case WACOM_27QHD:
3433         case DTK:
3434         case CINTIQ_HYBRID:
3435         case CINTIQ_COMPANION_2:
3436                 sync = wacom_intuos_irq(wacom_wac);
3437                 break;
3438
3439         case INTUOS4WL:
3440                 sync = wacom_intuos_bt_irq(wacom_wac, len);
3441                 break;
3442
3443         case WACOM_24HDT:
3444         case WACOM_27QHDT:
3445                 sync = wacom_24hdt_irq(wacom_wac);
3446                 break;
3447
3448         case INTUOS5S:
3449         case INTUOS5:
3450         case INTUOS5L:
3451         case INTUOSPS:
3452         case INTUOSPM:
3453         case INTUOSPL:
3454                 if (len == WACOM_PKGLEN_BBTOUCH3)
3455                         sync = wacom_bpt3_touch(wacom_wac);
3456                 else if (wacom_wac->data[0] == WACOM_REPORT_USB)
3457                         sync = wacom_status_irq(wacom_wac, len);
3458                 else
3459                         sync = wacom_intuos_irq(wacom_wac);
3460                 break;
3461
3462         case INTUOSP2_BT:
3463         case INTUOSP2S_BT:
3464         case INTUOSHT3_BT:
3465                 sync = wacom_intuos_pro2_bt_irq(wacom_wac, len);
3466                 break;
3467
3468         case TABLETPC:
3469         case TABLETPCE:
3470         case TABLETPC2FG:
3471         case MTSCREEN:
3472         case MTTPC:
3473         case MTTPC_B:
3474                 sync = wacom_tpc_irq(wacom_wac, len);
3475                 break;
3476
3477         case BAMBOO_PT:
3478         case BAMBOO_PEN:
3479         case BAMBOO_TOUCH:
3480         case INTUOSHT:
3481         case INTUOSHT2:
3482                 if (wacom_wac->data[0] == WACOM_REPORT_USB)
3483                         sync = wacom_status_irq(wacom_wac, len);
3484                 else
3485                         sync = wacom_bpt_irq(wacom_wac, len);
3486                 break;
3487
3488         case BAMBOO_PAD:
3489                 sync = wacom_bamboo_pad_irq(wacom_wac, len);
3490                 break;
3491
3492         case WIRELESS:
3493                 sync = wacom_wireless_irq(wacom_wac, len);
3494                 break;
3495
3496         case REMOTE:
3497                 sync = false;
3498                 if (wacom_wac->data[0] == WACOM_REPORT_DEVICE_LIST)
3499                         wacom_remote_status_irq(wacom_wac, len);
3500                 else
3501                         sync = wacom_remote_irq(wacom_wac, len);
3502                 break;
3503
3504         default:
3505                 sync = false;
3506                 break;
3507         }
3508
3509         if (sync) {
3510                 if (wacom_wac->pen_input)
3511                         input_sync(wacom_wac->pen_input);
3512                 if (wacom_wac->touch_input)
3513                         input_sync(wacom_wac->touch_input);
3514                 if (wacom_wac->pad_input)
3515                         input_sync(wacom_wac->pad_input);
3516         }
3517 }
3518
3519 static void wacom_setup_basic_pro_pen(struct wacom_wac *wacom_wac)
3520 {
3521         struct input_dev *input_dev = wacom_wac->pen_input;
3522
3523         input_set_capability(input_dev, EV_MSC, MSC_SERIAL);
3524
3525         __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3526         __set_bit(BTN_STYLUS, input_dev->keybit);
3527         __set_bit(BTN_STYLUS2, input_dev->keybit);
3528
3529         input_set_abs_params(input_dev, ABS_DISTANCE,
3530                              0, wacom_wac->features.distance_max, wacom_wac->features.distance_fuzz, 0);
3531 }
3532
3533 static void wacom_setup_cintiq(struct wacom_wac *wacom_wac)
3534 {
3535         struct input_dev *input_dev = wacom_wac->pen_input;
3536         struct wacom_features *features = &wacom_wac->features;
3537
3538         wacom_setup_basic_pro_pen(wacom_wac);
3539
3540         __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3541         __set_bit(BTN_TOOL_BRUSH, input_dev->keybit);
3542         __set_bit(BTN_TOOL_PENCIL, input_dev->keybit);
3543         __set_bit(BTN_TOOL_AIRBRUSH, input_dev->keybit);
3544
3545         input_set_abs_params(input_dev, ABS_WHEEL, 0, 1023, 0, 0);
3546         input_set_abs_params(input_dev, ABS_TILT_X, -64, 63, features->tilt_fuzz, 0);
3547         input_abs_set_res(input_dev, ABS_TILT_X, 57);
3548         input_set_abs_params(input_dev, ABS_TILT_Y, -64, 63, features->tilt_fuzz, 0);
3549         input_abs_set_res(input_dev, ABS_TILT_Y, 57);
3550 }
3551
3552 static void wacom_setup_intuos(struct wacom_wac *wacom_wac)
3553 {
3554         struct input_dev *input_dev = wacom_wac->pen_input;
3555
3556         input_set_capability(input_dev, EV_REL, REL_WHEEL);
3557
3558         wacom_setup_cintiq(wacom_wac);
3559
3560         __set_bit(BTN_LEFT, input_dev->keybit);
3561         __set_bit(BTN_RIGHT, input_dev->keybit);
3562         __set_bit(BTN_MIDDLE, input_dev->keybit);
3563         __set_bit(BTN_SIDE, input_dev->keybit);
3564         __set_bit(BTN_EXTRA, input_dev->keybit);
3565         __set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
3566         __set_bit(BTN_TOOL_LENS, input_dev->keybit);
3567
3568         input_set_abs_params(input_dev, ABS_RZ, -900, 899, 0, 0);
3569         input_abs_set_res(input_dev, ABS_RZ, 287);
3570         input_set_abs_params(input_dev, ABS_THROTTLE, -1023, 1023, 0, 0);
3571 }
3572
3573 void wacom_setup_device_quirks(struct wacom *wacom)
3574 {
3575         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
3576         struct wacom_features *features = &wacom->wacom_wac.features;
3577
3578         /* The pen and pad share the same interface on most devices */
3579         if (features->type == GRAPHIRE_BT || features->type == WACOM_G4 ||
3580             features->type == DTUS ||
3581             (features->type >= INTUOS3S && features->type <= WACOM_MO)) {
3582                 if (features->device_type & WACOM_DEVICETYPE_PEN)
3583                         features->device_type |= WACOM_DEVICETYPE_PAD;
3584         }
3585
3586         /* touch device found but size is not defined. use default */
3587         if (features->device_type & WACOM_DEVICETYPE_TOUCH && !features->x_max) {
3588                 features->x_max = 1023;
3589                 features->y_max = 1023;
3590         }
3591
3592         /*
3593          * Intuos5/Pro and Bamboo 3rd gen have no useful data about its
3594          * touch interface in its HID descriptor. If this is the touch
3595          * interface (PacketSize of WACOM_PKGLEN_BBTOUCH3), override the
3596          * tablet values.
3597          */
3598         if ((features->type >= INTUOS5S && features->type <= INTUOSPL) ||
3599                 (features->type >= INTUOSHT && features->type <= BAMBOO_PT)) {
3600                 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
3601                         if (features->touch_max)
3602                                 features->device_type |= WACOM_DEVICETYPE_TOUCH;
3603                         if (features->type >= INTUOSHT && features->type <= BAMBOO_PT)
3604                                 features->device_type |= WACOM_DEVICETYPE_PAD;
3605
3606                         if (features->type == INTUOSHT2) {
3607                                 features->x_max = features->x_max / 10;
3608                                 features->y_max = features->y_max / 10;
3609                         }
3610                         else {
3611                                 features->x_max = 4096;
3612                                 features->y_max = 4096;
3613                         }
3614                 }
3615                 else if (features->pktlen == WACOM_PKGLEN_BBTOUCH) {
3616                         features->device_type |= WACOM_DEVICETYPE_PAD;
3617                 }
3618         }
3619
3620         /*
3621          * Hack for the Bamboo One:
3622          * the device presents a PAD/Touch interface as most Bamboos and even
3623          * sends ghosts PAD data on it. However, later, we must disable this
3624          * ghost interface, and we can not detect it unless we set it here
3625          * to WACOM_DEVICETYPE_PAD or WACOM_DEVICETYPE_TOUCH.
3626          */
3627         if (features->type == BAMBOO_PEN &&
3628             features->pktlen == WACOM_PKGLEN_BBTOUCH3)
3629                 features->device_type |= WACOM_DEVICETYPE_PAD;
3630
3631         /*
3632          * Raw Wacom-mode pen and touch events both come from interface
3633          * 0, whose HID descriptor has an application usage of 0xFF0D
3634          * (i.e., WACOM_HID_WD_DIGITIZER). We route pen packets back
3635          * out through the HID_GENERIC device created for interface 1,
3636          * so rewrite this one to be of type WACOM_DEVICETYPE_TOUCH.
3637          */
3638         if (features->type == BAMBOO_PAD)
3639                 features->device_type = WACOM_DEVICETYPE_TOUCH;
3640
3641         if (features->type == REMOTE)
3642                 features->device_type = WACOM_DEVICETYPE_PAD;
3643
3644         if (features->type == INTUOSP2_BT ||
3645             features->type == INTUOSP2S_BT) {
3646                 features->device_type |= WACOM_DEVICETYPE_PEN |
3647                                          WACOM_DEVICETYPE_PAD |
3648                                          WACOM_DEVICETYPE_TOUCH;
3649                 features->quirks |= WACOM_QUIRK_BATTERY;
3650         }
3651
3652         if (features->type == INTUOSHT3_BT) {
3653                 features->device_type |= WACOM_DEVICETYPE_PEN |
3654                                          WACOM_DEVICETYPE_PAD;
3655                 features->quirks |= WACOM_QUIRK_BATTERY;
3656         }
3657
3658         switch (features->type) {
3659         case PL:
3660         case DTU:
3661         case DTUS:
3662         case DTUSX:
3663         case WACOM_21UX2:
3664         case WACOM_22HD:
3665         case DTK:
3666         case WACOM_24HD:
3667         case WACOM_27QHD:
3668         case CINTIQ_HYBRID:
3669         case CINTIQ_COMPANION_2:
3670         case CINTIQ:
3671         case WACOM_BEE:
3672         case WACOM_13HD:
3673         case WACOM_24HDT:
3674         case WACOM_27QHDT:
3675         case TABLETPC:
3676         case TABLETPCE:
3677         case TABLETPC2FG:
3678         case MTSCREEN:
3679         case MTTPC:
3680         case MTTPC_B:
3681                 features->device_type |= WACOM_DEVICETYPE_DIRECT;
3682                 break;
3683         }
3684
3685         if (wacom->hdev->bus == BUS_BLUETOOTH)
3686                 features->quirks |= WACOM_QUIRK_BATTERY;
3687
3688         /* quirk for bamboo touch with 2 low res touches */
3689         if ((features->type == BAMBOO_PT || features->type == BAMBOO_TOUCH) &&
3690             features->pktlen == WACOM_PKGLEN_BBTOUCH) {
3691                 features->x_max <<= 5;
3692                 features->y_max <<= 5;
3693                 features->x_fuzz <<= 5;
3694                 features->y_fuzz <<= 5;
3695                 features->quirks |= WACOM_QUIRK_BBTOUCH_LOWRES;
3696         }
3697
3698         if (features->type == WIRELESS) {
3699                 if (features->device_type == WACOM_DEVICETYPE_WL_MONITOR) {
3700                         features->quirks |= WACOM_QUIRK_BATTERY;
3701                 }
3702         }
3703
3704         if (features->type == REMOTE)
3705                 features->device_type |= WACOM_DEVICETYPE_WL_MONITOR;
3706
3707         /* HID descriptor for DTK-2451 / DTH-2452 claims to report lots
3708          * of things it shouldn't. Lets fix up the damage...
3709          */
3710         if (wacom->hdev->product == 0x382 || wacom->hdev->product == 0x37d) {
3711                 features->quirks &= ~WACOM_QUIRK_TOOLSERIAL;
3712                 __clear_bit(BTN_TOOL_BRUSH, wacom_wac->pen_input->keybit);
3713                 __clear_bit(BTN_TOOL_PENCIL, wacom_wac->pen_input->keybit);
3714                 __clear_bit(BTN_TOOL_AIRBRUSH, wacom_wac->pen_input->keybit);
3715                 __clear_bit(ABS_Z, wacom_wac->pen_input->absbit);
3716                 __clear_bit(ABS_DISTANCE, wacom_wac->pen_input->absbit);
3717                 __clear_bit(ABS_TILT_X, wacom_wac->pen_input->absbit);
3718                 __clear_bit(ABS_TILT_Y, wacom_wac->pen_input->absbit);
3719                 __clear_bit(ABS_WHEEL, wacom_wac->pen_input->absbit);
3720                 __clear_bit(ABS_MISC, wacom_wac->pen_input->absbit);
3721                 __clear_bit(MSC_SERIAL, wacom_wac->pen_input->mscbit);
3722                 __clear_bit(EV_MSC, wacom_wac->pen_input->evbit);
3723         }
3724 }
3725
3726 int wacom_setup_pen_input_capabilities(struct input_dev *input_dev,
3727                                    struct wacom_wac *wacom_wac)
3728 {
3729         struct wacom_features *features = &wacom_wac->features;
3730
3731         if (!(features->device_type & WACOM_DEVICETYPE_PEN))
3732                 return -ENODEV;
3733
3734         if (features->device_type & WACOM_DEVICETYPE_DIRECT)
3735                 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
3736         else
3737                 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
3738
3739         if (features->type == HID_GENERIC)
3740                 /* setup has already been done */
3741                 return 0;
3742
3743         input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
3744         __set_bit(BTN_TOUCH, input_dev->keybit);
3745         __set_bit(ABS_MISC, input_dev->absbit);
3746
3747         input_set_abs_params(input_dev, ABS_X, 0 + features->offset_left,
3748                              features->x_max - features->offset_right,
3749                              features->x_fuzz, 0);
3750         input_set_abs_params(input_dev, ABS_Y, 0 + features->offset_top,
3751                              features->y_max - features->offset_bottom,
3752                              features->y_fuzz, 0);
3753         input_set_abs_params(input_dev, ABS_PRESSURE, 0,
3754                 features->pressure_max, features->pressure_fuzz, 0);
3755
3756         /* penabled devices have fixed resolution for each model */
3757         input_abs_set_res(input_dev, ABS_X, features->x_resolution);
3758         input_abs_set_res(input_dev, ABS_Y, features->y_resolution);
3759
3760         switch (features->type) {
3761         case GRAPHIRE_BT:
3762                 __clear_bit(ABS_MISC, input_dev->absbit);
3763                 fallthrough;
3764
3765         case WACOM_MO:
3766         case WACOM_G4:
3767                 input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3768                                               features->distance_max,
3769                                               features->distance_fuzz, 0);
3770                 fallthrough;
3771
3772         case GRAPHIRE:
3773                 input_set_capability(input_dev, EV_REL, REL_WHEEL);
3774
3775                 __set_bit(BTN_LEFT, input_dev->keybit);
3776                 __set_bit(BTN_RIGHT, input_dev->keybit);
3777                 __set_bit(BTN_MIDDLE, input_dev->keybit);
3778
3779                 __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3780                 __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3781                 __set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
3782                 __set_bit(BTN_STYLUS, input_dev->keybit);
3783                 __set_bit(BTN_STYLUS2, input_dev->keybit);
3784                 break;
3785
3786         case WACOM_27QHD:
3787         case WACOM_24HD:
3788         case DTK:
3789         case WACOM_22HD:
3790         case WACOM_21UX2:
3791         case WACOM_BEE:
3792         case CINTIQ:
3793         case WACOM_13HD:
3794         case CINTIQ_HYBRID:
3795         case CINTIQ_COMPANION_2:
3796                 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3797                 input_abs_set_res(input_dev, ABS_Z, 287);
3798                 wacom_setup_cintiq(wacom_wac);
3799                 break;
3800
3801         case INTUOS3:
3802         case INTUOS3L:
3803         case INTUOS3S:
3804         case INTUOS4:
3805         case INTUOS4WL:
3806         case INTUOS4L:
3807         case INTUOS4S:
3808                 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3809                 input_abs_set_res(input_dev, ABS_Z, 287);
3810                 fallthrough;
3811
3812         case INTUOS:
3813                 wacom_setup_intuos(wacom_wac);
3814                 break;
3815
3816         case INTUOS5:
3817         case INTUOS5L:
3818         case INTUOSPM:
3819         case INTUOSPL:
3820         case INTUOS5S:
3821         case INTUOSPS:
3822         case INTUOSP2_BT:
3823         case INTUOSP2S_BT:
3824                 input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3825                                       features->distance_max,
3826                                       features->distance_fuzz, 0);
3827
3828                 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3829                 input_abs_set_res(input_dev, ABS_Z, 287);
3830
3831                 wacom_setup_intuos(wacom_wac);
3832                 break;
3833
3834         case WACOM_24HDT:
3835         case WACOM_27QHDT:
3836         case MTSCREEN:
3837         case MTTPC:
3838         case MTTPC_B:
3839         case TABLETPC2FG:
3840         case TABLETPC:
3841         case TABLETPCE:
3842                 __clear_bit(ABS_MISC, input_dev->absbit);
3843                 fallthrough;
3844
3845         case DTUS:
3846         case DTUSX:
3847         case PL:
3848         case DTU:
3849                 __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3850                 __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3851                 __set_bit(BTN_STYLUS, input_dev->keybit);
3852                 __set_bit(BTN_STYLUS2, input_dev->keybit);
3853                 break;
3854
3855         case PTU:
3856                 __set_bit(BTN_STYLUS2, input_dev->keybit);
3857                 fallthrough;
3858
3859         case PENPARTNER:
3860                 __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3861                 __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3862                 __set_bit(BTN_STYLUS, input_dev->keybit);
3863                 break;
3864
3865         case INTUOSHT:
3866         case BAMBOO_PT:
3867         case BAMBOO_PEN:
3868         case INTUOSHT2:
3869         case INTUOSHT3_BT:
3870                 if (features->type == INTUOSHT2 ||
3871                     features->type == INTUOSHT3_BT) {
3872                         wacom_setup_basic_pro_pen(wacom_wac);
3873                 } else {
3874                         __clear_bit(ABS_MISC, input_dev->absbit);
3875                         __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3876                         __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3877                         __set_bit(BTN_STYLUS, input_dev->keybit);
3878                         __set_bit(BTN_STYLUS2, input_dev->keybit);
3879                         input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3880                                       features->distance_max,
3881                                       features->distance_fuzz, 0);
3882                 }
3883                 break;
3884         case BAMBOO_PAD:
3885                 __clear_bit(ABS_MISC, input_dev->absbit);
3886                 break;
3887         }
3888         return 0;
3889 }
3890
3891 int wacom_setup_touch_input_capabilities(struct input_dev *input_dev,
3892                                          struct wacom_wac *wacom_wac)
3893 {
3894         struct wacom_features *features = &wacom_wac->features;
3895
3896         if (!(features->device_type & WACOM_DEVICETYPE_TOUCH))
3897                 return -ENODEV;
3898
3899         if (features->device_type & WACOM_DEVICETYPE_DIRECT)
3900                 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
3901         else
3902                 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
3903
3904         if (features->type == HID_GENERIC)
3905                 /* setup has already been done */
3906                 return 0;
3907
3908         input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
3909         __set_bit(BTN_TOUCH, input_dev->keybit);
3910
3911         if (features->touch_max == 1) {
3912                 input_set_abs_params(input_dev, ABS_X, 0,
3913                         features->x_max, features->x_fuzz, 0);
3914                 input_set_abs_params(input_dev, ABS_Y, 0,
3915                         features->y_max, features->y_fuzz, 0);
3916                 input_abs_set_res(input_dev, ABS_X,
3917                                   features->x_resolution);
3918                 input_abs_set_res(input_dev, ABS_Y,
3919                                   features->y_resolution);
3920         }
3921         else if (features->touch_max > 1) {
3922                 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
3923                         features->x_max, features->x_fuzz, 0);
3924                 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
3925                         features->y_max, features->y_fuzz, 0);
3926                 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
3927                                   features->x_resolution);
3928                 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
3929                                   features->y_resolution);
3930         }
3931
3932         switch (features->type) {
3933         case INTUOSP2_BT:
3934         case INTUOSP2S_BT:
3935                 input_dev->evbit[0] |= BIT_MASK(EV_SW);
3936                 __set_bit(SW_MUTE_DEVICE, input_dev->swbit);
3937
3938                 if (wacom_wac->shared->touch->product == 0x361) {
3939                         input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3940                                              0, 12440, 4, 0);
3941                         input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
3942                                              0, 8640, 4, 0);
3943                 }
3944                 else if (wacom_wac->shared->touch->product == 0x360) {
3945                         input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3946                                              0, 8960, 4, 0);
3947                         input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
3948                                              0, 5920, 4, 0);
3949                 }
3950                 else if (wacom_wac->shared->touch->product == 0x393) {
3951                         input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3952                                              0, 6400, 4, 0);
3953                         input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
3954                                              0, 4000, 4, 0);
3955                 }
3956                 input_abs_set_res(input_dev, ABS_MT_POSITION_X, 40);
3957                 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, 40);
3958
3959                 fallthrough;
3960
3961         case INTUOS5:
3962         case INTUOS5L:
3963         case INTUOSPM:
3964         case INTUOSPL:
3965         case INTUOS5S:
3966         case INTUOSPS:
3967                 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, features->x_max, 0, 0);
3968                 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0, features->y_max, 0, 0);
3969                 input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_POINTER);
3970                 break;
3971
3972         case WACOM_24HDT:
3973                 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, features->x_max, 0, 0);
3974                 input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, features->x_max, 0, 0);
3975                 input_set_abs_params(input_dev, ABS_MT_WIDTH_MINOR, 0, features->y_max, 0, 0);
3976                 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
3977                 fallthrough;
3978
3979         case WACOM_27QHDT:
3980                 if (wacom_wac->shared->touch->product == 0x32C ||
3981                     wacom_wac->shared->touch->product == 0xF6) {
3982                         input_dev->evbit[0] |= BIT_MASK(EV_SW);
3983                         __set_bit(SW_MUTE_DEVICE, input_dev->swbit);
3984                         wacom_wac->has_mute_touch_switch = true;
3985                         wacom_wac->is_soft_touch_switch = true;
3986                 }
3987                 fallthrough;
3988
3989         case MTSCREEN:
3990         case MTTPC:
3991         case MTTPC_B:
3992         case TABLETPC2FG:
3993                 input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_DIRECT);
3994                 fallthrough;
3995
3996         case TABLETPC:
3997         case TABLETPCE:
3998                 break;
3999
4000         case INTUOSHT:
4001         case INTUOSHT2:
4002                 input_dev->evbit[0] |= BIT_MASK(EV_SW);
4003                 __set_bit(SW_MUTE_DEVICE, input_dev->swbit);
4004                 fallthrough;
4005
4006         case BAMBOO_PT:
4007         case BAMBOO_TOUCH:
4008                 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
4009                         input_set_abs_params(input_dev,
4010                                      ABS_MT_TOUCH_MAJOR,
4011                                      0, features->x_max, 0, 0);
4012                         input_set_abs_params(input_dev,
4013                                      ABS_MT_TOUCH_MINOR,
4014                                      0, features->y_max, 0, 0);
4015                 }
4016                 input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_POINTER);
4017                 break;
4018
4019         case BAMBOO_PAD:
4020                 input_mt_init_slots(input_dev, features->touch_max,
4021                                     INPUT_MT_POINTER);
4022                 __set_bit(BTN_LEFT, input_dev->keybit);
4023                 __set_bit(BTN_RIGHT, input_dev->keybit);
4024                 break;
4025         }
4026         return 0;
4027 }
4028
4029 static int wacom_numbered_button_to_key(int n)
4030 {
4031         if (n < 10)
4032                 return BTN_0 + n;
4033         else if (n < 16)
4034                 return BTN_A + (n-10);
4035         else if (n < 18)
4036                 return BTN_BASE + (n-16);
4037         else
4038                 return 0;
4039 }
4040
4041 static void wacom_setup_numbered_buttons(struct input_dev *input_dev,
4042                                 int button_count)
4043 {
4044         int i;
4045
4046         for (i = 0; i < button_count; i++) {
4047                 int key = wacom_numbered_button_to_key(i);
4048
4049                 if (key)
4050                         __set_bit(key, input_dev->keybit);
4051         }
4052 }
4053
4054 static void wacom_24hd_update_leds(struct wacom *wacom, int mask, int group)
4055 {
4056         struct wacom_led *led;
4057         int i;
4058         bool updated = false;
4059
4060         /*
4061          * 24HD has LED group 1 to the left and LED group 0 to the right.
4062          * So group 0 matches the second half of the buttons and thus the mask
4063          * needs to be shifted.
4064          */
4065         if (group == 0)
4066                 mask >>= 8;
4067
4068         for (i = 0; i < 3; i++) {
4069                 led = wacom_led_find(wacom, group, i);
4070                 if (!led) {
4071                         hid_err(wacom->hdev, "can't find LED %d in group %d\n",
4072                                 i, group);
4073                         continue;
4074                 }
4075                 if (!updated && mask & BIT(i)) {
4076                         led->held = true;
4077                         led_trigger_event(&led->trigger, LED_FULL);
4078                 } else {
4079                         led->held = false;
4080                 }
4081         }
4082 }
4083
4084 static bool wacom_is_led_toggled(struct wacom *wacom, int button_count,
4085                                  int mask, int group)
4086 {
4087         int group_button;
4088
4089         /*
4090          * 21UX2 has LED group 1 to the left and LED group 0
4091          * to the right. We need to reverse the group to match this
4092          * historical behavior.
4093          */
4094         if (wacom->wacom_wac.features.type == WACOM_21UX2)
4095                 group = 1 - group;
4096
4097         group_button = group * (button_count/wacom->led.count);
4098
4099         if (wacom->wacom_wac.features.type == INTUOSP2_BT)
4100                 group_button = 8;
4101
4102         return mask & (1 << group_button);
4103 }
4104
4105 static void wacom_update_led(struct wacom *wacom, int button_count, int mask,
4106                              int group)
4107 {
4108         struct wacom_led *led, *next_led;
4109         int cur;
4110         bool pressed;
4111
4112         if (wacom->wacom_wac.features.type == WACOM_24HD)
4113                 return wacom_24hd_update_leds(wacom, mask, group);
4114
4115         pressed = wacom_is_led_toggled(wacom, button_count, mask, group);
4116         cur = wacom->led.groups[group].select;
4117
4118         led = wacom_led_find(wacom, group, cur);
4119         if (!led) {
4120                 hid_err(wacom->hdev, "can't find current LED %d in group %d\n",
4121                         cur, group);
4122                 return;
4123         }
4124
4125         if (!pressed) {
4126                 led->held = false;
4127                 return;
4128         }
4129
4130         if (led->held && pressed)
4131                 return;
4132
4133         next_led = wacom_led_next(wacom, led);
4134         if (!next_led) {
4135                 hid_err(wacom->hdev, "can't find next LED in group %d\n",
4136                         group);
4137                 return;
4138         }
4139         if (next_led == led)
4140                 return;
4141
4142         next_led->held = true;
4143         led_trigger_event(&next_led->trigger,
4144                           wacom_leds_brightness_get(next_led));
4145 }
4146
4147 static void wacom_report_numbered_buttons(struct input_dev *input_dev,
4148                                 int button_count, int mask)
4149 {
4150         struct wacom *wacom = input_get_drvdata(input_dev);
4151         int i;
4152
4153         for (i = 0; i < wacom->led.count; i++)
4154                 wacom_update_led(wacom,  button_count, mask, i);
4155
4156         for (i = 0; i < button_count; i++) {
4157                 int key = wacom_numbered_button_to_key(i);
4158
4159                 if (key)
4160                         input_report_key(input_dev, key, mask & (1 << i));
4161         }
4162 }
4163
4164 int wacom_setup_pad_input_capabilities(struct input_dev *input_dev,
4165                                    struct wacom_wac *wacom_wac)
4166 {
4167         struct wacom_features *features = &wacom_wac->features;
4168
4169         if ((features->type == HID_GENERIC) && features->numbered_buttons > 0)
4170                 features->device_type |= WACOM_DEVICETYPE_PAD;
4171
4172         if (!(features->device_type & WACOM_DEVICETYPE_PAD))
4173                 return -ENODEV;
4174
4175         if (features->type == REMOTE && input_dev == wacom_wac->pad_input)
4176                 return -ENODEV;
4177
4178         input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
4179
4180         /* kept for making legacy xf86-input-wacom working with the wheels */
4181         __set_bit(ABS_MISC, input_dev->absbit);
4182
4183         /* kept for making legacy xf86-input-wacom accepting the pad */
4184         if (!(input_dev->absinfo && (input_dev->absinfo[ABS_X].minimum ||
4185               input_dev->absinfo[ABS_X].maximum)))
4186                 input_set_abs_params(input_dev, ABS_X, 0, 1, 0, 0);
4187         if (!(input_dev->absinfo && (input_dev->absinfo[ABS_Y].minimum ||
4188               input_dev->absinfo[ABS_Y].maximum)))
4189                 input_set_abs_params(input_dev, ABS_Y, 0, 1, 0, 0);
4190
4191         /* kept for making udev and libwacom accepting the pad */
4192         __set_bit(BTN_STYLUS, input_dev->keybit);
4193
4194         wacom_setup_numbered_buttons(input_dev, features->numbered_buttons);
4195
4196         switch (features->type) {
4197
4198         case CINTIQ_HYBRID:
4199         case CINTIQ_COMPANION_2:
4200         case DTK:
4201         case DTUS:
4202         case GRAPHIRE_BT:
4203                 break;
4204
4205         case WACOM_MO:
4206                 __set_bit(BTN_BACK, input_dev->keybit);
4207                 __set_bit(BTN_LEFT, input_dev->keybit);
4208                 __set_bit(BTN_FORWARD, input_dev->keybit);
4209                 __set_bit(BTN_RIGHT, input_dev->keybit);
4210                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4211                 break;
4212
4213         case WACOM_G4:
4214                 __set_bit(BTN_BACK, input_dev->keybit);
4215                 __set_bit(BTN_FORWARD, input_dev->keybit);
4216                 input_set_capability(input_dev, EV_REL, REL_WHEEL);
4217                 break;
4218
4219         case WACOM_24HD:
4220                 __set_bit(KEY_PROG1, input_dev->keybit);
4221                 __set_bit(KEY_PROG2, input_dev->keybit);
4222                 __set_bit(KEY_PROG3, input_dev->keybit);
4223
4224                 __set_bit(KEY_ONSCREEN_KEYBOARD, input_dev->keybit);
4225                 __set_bit(KEY_INFO, input_dev->keybit);
4226
4227                 if (!features->oPid)
4228                         __set_bit(KEY_BUTTONCONFIG, input_dev->keybit);
4229
4230                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4231                 input_set_abs_params(input_dev, ABS_THROTTLE, 0, 71, 0, 0);
4232                 break;
4233
4234         case WACOM_27QHD:
4235                 __set_bit(KEY_PROG1, input_dev->keybit);
4236                 __set_bit(KEY_PROG2, input_dev->keybit);
4237                 __set_bit(KEY_PROG3, input_dev->keybit);
4238
4239                 __set_bit(KEY_ONSCREEN_KEYBOARD, input_dev->keybit);
4240                 __set_bit(KEY_BUTTONCONFIG, input_dev->keybit);
4241
4242                 if (!features->oPid)
4243                         __set_bit(KEY_CONTROLPANEL, input_dev->keybit);
4244                 input_set_abs_params(input_dev, ABS_X, -2048, 2048, 0, 0);
4245                 input_abs_set_res(input_dev, ABS_X, 1024); /* points/g */
4246                 input_set_abs_params(input_dev, ABS_Y, -2048, 2048, 0, 0);
4247                 input_abs_set_res(input_dev, ABS_Y, 1024);
4248                 input_set_abs_params(input_dev, ABS_Z, -2048, 2048, 0, 0);
4249                 input_abs_set_res(input_dev, ABS_Z, 1024);
4250                 __set_bit(INPUT_PROP_ACCELEROMETER, input_dev->propbit);
4251                 break;
4252
4253         case WACOM_22HD:
4254                 __set_bit(KEY_PROG1, input_dev->keybit);
4255                 __set_bit(KEY_PROG2, input_dev->keybit);
4256                 __set_bit(KEY_PROG3, input_dev->keybit);
4257
4258                 __set_bit(KEY_BUTTONCONFIG, input_dev->keybit);
4259                 __set_bit(KEY_INFO, input_dev->keybit);
4260                 fallthrough;
4261
4262         case WACOM_21UX2:
4263         case WACOM_BEE:
4264         case CINTIQ:
4265                 input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
4266                 input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
4267                 break;
4268
4269         case WACOM_13HD:
4270                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4271                 break;
4272
4273         case INTUOS3:
4274         case INTUOS3L:
4275                 input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
4276                 fallthrough;
4277
4278         case INTUOS3S:
4279                 input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
4280                 break;
4281
4282         case INTUOS5:
4283         case INTUOS5L:
4284         case INTUOSPM:
4285         case INTUOSPL:
4286         case INTUOS5S:
4287         case INTUOSPS:
4288         case INTUOSP2_BT:
4289         case INTUOSP2S_BT:
4290                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4291                 break;
4292
4293         case INTUOS4WL:
4294                 /*
4295                  * For Bluetooth devices, the udev rule does not work correctly
4296                  * for pads unless we add a stylus capability, which forces
4297                  * ID_INPUT_TABLET to be set.
4298                  */
4299                 __set_bit(BTN_STYLUS, input_dev->keybit);
4300                 fallthrough;
4301
4302         case INTUOS4:
4303         case INTUOS4L:
4304         case INTUOS4S:
4305                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4306                 break;
4307
4308         case INTUOSHT:
4309         case BAMBOO_PT:
4310         case BAMBOO_TOUCH:
4311         case INTUOSHT2:
4312                 __clear_bit(ABS_MISC, input_dev->absbit);
4313
4314                 __set_bit(BTN_LEFT, input_dev->keybit);
4315                 __set_bit(BTN_FORWARD, input_dev->keybit);
4316                 __set_bit(BTN_BACK, input_dev->keybit);
4317                 __set_bit(BTN_RIGHT, input_dev->keybit);
4318
4319                 break;
4320
4321         case REMOTE:
4322                 input_set_capability(input_dev, EV_MSC, MSC_SERIAL);
4323                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
4324                 break;
4325
4326         case INTUOSHT3_BT:
4327         case HID_GENERIC:
4328                 break;
4329
4330         default:
4331                 /* no pad supported */
4332                 return -ENODEV;
4333         }
4334         return 0;
4335 }
4336
4337 static const struct wacom_features wacom_features_0x00 =
4338         { "Wacom Penpartner", 5040, 3780, 255, 0,
4339           PENPARTNER, WACOM_PENPRTN_RES, WACOM_PENPRTN_RES };
4340 static const struct wacom_features wacom_features_0x10 =
4341         { "Wacom Graphire", 10206, 7422, 511, 63,
4342           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4343 static const struct wacom_features wacom_features_0x81 =
4344         { "Wacom Graphire BT", 16704, 12064, 511, 32,
4345           GRAPHIRE_BT, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES, 2 };
4346 static const struct wacom_features wacom_features_0x11 =
4347         { "Wacom Graphire2 4x5", 10206, 7422, 511, 63,
4348           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4349 static const struct wacom_features wacom_features_0x12 =
4350         { "Wacom Graphire2 5x7", 13918, 10206, 511, 63,
4351           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4352 static const struct wacom_features wacom_features_0x13 =
4353         { "Wacom Graphire3", 10208, 7424, 511, 63,
4354           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4355 static const struct wacom_features wacom_features_0x14 =
4356         { "Wacom Graphire3 6x8", 16704, 12064, 511, 63,
4357           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4358 static const struct wacom_features wacom_features_0x15 =
4359         { "Wacom Graphire4 4x5", 10208, 7424, 511, 63,
4360           WACOM_G4, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4361 static const struct wacom_features wacom_features_0x16 =
4362         { "Wacom Graphire4 6x8", 16704, 12064, 511, 63,
4363           WACOM_G4, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4364 static const struct wacom_features wacom_features_0x17 =
4365         { "Wacom BambooFun 4x5", 14760, 9225, 511, 63,
4366           WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4367 static const struct wacom_features wacom_features_0x18 =
4368         { "Wacom BambooFun 6x8", 21648, 13530, 511, 63,
4369           WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4370 static const struct wacom_features wacom_features_0x19 =
4371         { "Wacom Bamboo1 Medium", 16704, 12064, 511, 63,
4372           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
4373 static const struct wacom_features wacom_features_0x60 =
4374         { "Wacom Volito", 5104, 3712, 511, 63,
4375           GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
4376 static const struct wacom_features wacom_features_0x61 =
4377         { "Wacom PenStation2", 3250, 2320, 255, 63,
4378           GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
4379 static const struct wacom_features wacom_features_0x62 =
4380         { "Wacom Volito2 4x5", 5104, 3712, 511, 63,
4381           GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
4382 static const struct wacom_features wacom_features_0x63 =
4383         { "Wacom Volito2 2x3", 3248, 2320, 511, 63,
4384           GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
4385 static const struct wacom_features wacom_features_0x64 =
4386         { "Wacom PenPartner2", 3250, 2320, 511, 63,
4387           GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
4388 static const struct wacom_features wacom_features_0x65 =
4389         { "Wacom Bamboo", 14760, 9225, 511, 63,
4390           WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4391 static const struct wacom_features wacom_features_0x69 =
4392         { "Wacom Bamboo1", 5104, 3712, 511, 63,
4393           GRAPHIRE, WACOM_PENPRTN_RES, WACOM_PENPRTN_RES };
4394 static const struct wacom_features wacom_features_0x6A =
4395         { "Wacom Bamboo1 4x6", 14760, 9225, 1023, 63,
4396           GRAPHIRE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4397 static const struct wacom_features wacom_features_0x6B =
4398         { "Wacom Bamboo1 5x8", 21648, 13530, 1023, 63,
4399           GRAPHIRE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4400 static const struct wacom_features wacom_features_0x20 =
4401         { "Wacom Intuos 4x5", 12700, 10600, 1023, 31,
4402           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4403 static const struct wacom_features wacom_features_0x21 =
4404         { "Wacom Intuos 6x8", 20320, 16240, 1023, 31,
4405           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4406 static const struct wacom_features wacom_features_0x22 =
4407         { "Wacom Intuos 9x12", 30480, 24060, 1023, 31,
4408           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4409 static const struct wacom_features wacom_features_0x23 =
4410         { "Wacom Intuos 12x12", 30480, 31680, 1023, 31,
4411           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4412 static const struct wacom_features wacom_features_0x24 =
4413         { "Wacom Intuos 12x18", 45720, 31680, 1023, 31,
4414           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4415 static const struct wacom_features wacom_features_0x30 =
4416         { "Wacom PL400", 5408, 4056, 255, 0,
4417           PL, WACOM_PL_RES, WACOM_PL_RES };
4418 static const struct wacom_features wacom_features_0x31 =
4419         { "Wacom PL500", 6144, 4608, 255, 0,
4420           PL, WACOM_PL_RES, WACOM_PL_RES };
4421 static const struct wacom_features wacom_features_0x32 =
4422         { "Wacom PL600", 6126, 4604, 255, 0,
4423           PL, WACOM_PL_RES, WACOM_PL_RES };
4424 static const struct wacom_features wacom_features_0x33 =
4425         { "Wacom PL600SX", 6260, 5016, 255, 0,
4426           PL, WACOM_PL_RES, WACOM_PL_RES };
4427 static const struct wacom_features wacom_features_0x34 =
4428         { "Wacom PL550", 6144, 4608, 511, 0,
4429           PL, WACOM_PL_RES, WACOM_PL_RES };
4430 static const struct wacom_features wacom_features_0x35 =
4431         { "Wacom PL800", 7220, 5780, 511, 0,
4432           PL, WACOM_PL_RES, WACOM_PL_RES };
4433 static const struct wacom_features wacom_features_0x37 =
4434         { "Wacom PL700", 6758, 5406, 511, 0,
4435           PL, WACOM_PL_RES, WACOM_PL_RES };
4436 static const struct wacom_features wacom_features_0x38 =
4437         { "Wacom PL510", 6282, 4762, 511, 0,
4438           PL, WACOM_PL_RES, WACOM_PL_RES };
4439 static const struct wacom_features wacom_features_0x39 =
4440         { "Wacom DTU710", 34080, 27660, 511, 0,
4441           PL, WACOM_PL_RES, WACOM_PL_RES };
4442 static const struct wacom_features wacom_features_0xC4 =
4443         { "Wacom DTF521", 6282, 4762, 511, 0,
4444           PL, WACOM_PL_RES, WACOM_PL_RES };
4445 static const struct wacom_features wacom_features_0xC0 =
4446         { "Wacom DTF720", 6858, 5506, 511, 0,
4447           PL, WACOM_PL_RES, WACOM_PL_RES };
4448 static const struct wacom_features wacom_features_0xC2 =
4449         { "Wacom DTF720a", 6858, 5506, 511, 0,
4450           PL, WACOM_PL_RES, WACOM_PL_RES };
4451 static const struct wacom_features wacom_features_0x03 =
4452         { "Wacom Cintiq Partner", 20480, 15360, 511, 0,
4453           PTU, WACOM_PL_RES, WACOM_PL_RES };
4454 static const struct wacom_features wacom_features_0x41 =
4455         { "Wacom Intuos2 4x5", 12700, 10600, 1023, 31,
4456           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4457 static const struct wacom_features wacom_features_0x42 =
4458         { "Wacom Intuos2 6x8", 20320, 16240, 1023, 31,
4459           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4460 static const struct wacom_features wacom_features_0x43 =
4461         { "Wacom Intuos2 9x12", 30480, 24060, 1023, 31,
4462           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4463 static const struct wacom_features wacom_features_0x44 =
4464         { "Wacom Intuos2 12x12", 30480, 31680, 1023, 31,
4465           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4466 static const struct wacom_features wacom_features_0x45 =
4467         { "Wacom Intuos2 12x18", 45720, 31680, 1023, 31,
4468           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4469 static const struct wacom_features wacom_features_0xB0 =
4470         { "Wacom Intuos3 4x5", 25400, 20320, 1023, 63,
4471           INTUOS3S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 4 };
4472 static const struct wacom_features wacom_features_0xB1 =
4473         { "Wacom Intuos3 6x8", 40640, 30480, 1023, 63,
4474           INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4475 static const struct wacom_features wacom_features_0xB2 =
4476         { "Wacom Intuos3 9x12", 60960, 45720, 1023, 63,
4477           INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4478 static const struct wacom_features wacom_features_0xB3 =
4479         { "Wacom Intuos3 12x12", 60960, 60960, 1023, 63,
4480           INTUOS3L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4481 static const struct wacom_features wacom_features_0xB4 =
4482         { "Wacom Intuos3 12x19", 97536, 60960, 1023, 63,
4483           INTUOS3L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4484 static const struct wacom_features wacom_features_0xB5 =
4485         { "Wacom Intuos3 6x11", 54204, 31750, 1023, 63,
4486           INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4487 static const struct wacom_features wacom_features_0xB7 =
4488         { "Wacom Intuos3 4x6", 31496, 19685, 1023, 63,
4489           INTUOS3S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 4 };
4490 static const struct wacom_features wacom_features_0xB8 =
4491         { "Wacom Intuos4 4x6", 31496, 19685, 2047, 63,
4492           INTUOS4S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7 };
4493 static const struct wacom_features wacom_features_0xB9 =
4494         { "Wacom Intuos4 6x9", 44704, 27940, 2047, 63,
4495           INTUOS4, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4496 static const struct wacom_features wacom_features_0xBA =
4497         { "Wacom Intuos4 8x13", 65024, 40640, 2047, 63,
4498           INTUOS4L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4499 static const struct wacom_features wacom_features_0xBB =
4500         { "Wacom Intuos4 12x19", 97536, 60960, 2047, 63,
4501           INTUOS4L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4502 static const struct wacom_features wacom_features_0xBC =
4503         { "Wacom Intuos4 WL", 40640, 25400, 2047, 63,
4504           INTUOS4, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4505 static const struct wacom_features wacom_features_0xBD =
4506         { "Wacom Intuos4 WL", 40640, 25400, 2047, 63,
4507           INTUOS4WL, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4508 static const struct wacom_features wacom_features_0x26 =
4509         { "Wacom Intuos5 touch S", 31496, 19685, 2047, 63,
4510           INTUOS5S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7, .touch_max = 16 };
4511 static const struct wacom_features wacom_features_0x27 =
4512         { "Wacom Intuos5 touch M", 44704, 27940, 2047, 63,
4513           INTUOS5, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16 };
4514 static const struct wacom_features wacom_features_0x28 =
4515         { "Wacom Intuos5 touch L", 65024, 40640, 2047, 63,
4516           INTUOS5L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16 };
4517 static const struct wacom_features wacom_features_0x29 =
4518         { "Wacom Intuos5 S", 31496, 19685, 2047, 63,
4519           INTUOS5S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7 };
4520 static const struct wacom_features wacom_features_0x2A =
4521         { "Wacom Intuos5 M", 44704, 27940, 2047, 63,
4522           INTUOS5, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
4523 static const struct wacom_features wacom_features_0x314 =
4524         { "Wacom Intuos Pro S", 31496, 19685, 2047, 63,
4525           INTUOSPS, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7, .touch_max = 16,
4526           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4527 static const struct wacom_features wacom_features_0x315 =
4528         { "Wacom Intuos Pro M", 44704, 27940, 2047, 63,
4529           INTUOSPM, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16,
4530           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4531 static const struct wacom_features wacom_features_0x317 =
4532         { "Wacom Intuos Pro L", 65024, 40640, 2047, 63,
4533           INTUOSPL, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16,
4534           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4535 static const struct wacom_features wacom_features_0xF4 =
4536         { "Wacom Cintiq 24HD", 104480, 65600, 2047, 63,
4537           WACOM_24HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 16,
4538           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4539           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4540 static const struct wacom_features wacom_features_0xF8 =
4541         { "Wacom Cintiq 24HD touch", 104480, 65600, 2047, 63, /* Pen */
4542           WACOM_24HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 16,
4543           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4544           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4545           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0xf6 };
4546 static const struct wacom_features wacom_features_0xF6 =
4547         { "Wacom Cintiq 24HD touch", .type = WACOM_24HDT, /* Touch */
4548           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0xf8, .touch_max = 10,
4549           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4550 static const struct wacom_features wacom_features_0x32A =
4551         { "Wacom Cintiq 27QHD", 120140, 67920, 2047, 63,
4552           WACOM_27QHD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 0,
4553           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4554           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4555 static const struct wacom_features wacom_features_0x32B =
4556         { "Wacom Cintiq 27QHD touch", 120140, 67920, 2047, 63,
4557           WACOM_27QHD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 0,
4558           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4559           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4560           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x32C };
4561 static const struct wacom_features wacom_features_0x32C =
4562         { "Wacom Cintiq 27QHD touch", .type = WACOM_27QHDT,
4563           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x32B, .touch_max = 10 };
4564 static const struct wacom_features wacom_features_0x3F =
4565         { "Wacom Cintiq 21UX", 87200, 65600, 1023, 63,
4566           CINTIQ, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
4567 static const struct wacom_features wacom_features_0xC5 =
4568         { "Wacom Cintiq 20WSX", 86680, 54180, 1023, 63,
4569           WACOM_BEE, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 10 };
4570 static const struct wacom_features wacom_features_0xC6 =
4571         { "Wacom Cintiq 12WX", 53020, 33440, 1023, 63,
4572           WACOM_BEE, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 10 };
4573 static const struct wacom_features wacom_features_0x304 =
4574         { "Wacom Cintiq 13HD", 59552, 33848, 1023, 63,
4575           WACOM_13HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4576           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4577           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4578 static const struct wacom_features wacom_features_0x333 =
4579         { "Wacom Cintiq 13HD touch", 59552, 33848, 2047, 63,
4580           WACOM_13HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4581           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4582           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4583           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x335 };
4584 static const struct wacom_features wacom_features_0x335 =
4585         { "Wacom Cintiq 13HD touch", .type = WACOM_24HDT, /* Touch */
4586           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x333, .touch_max = 10,
4587           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4588 static const struct wacom_features wacom_features_0xC7 =
4589         { "Wacom DTU1931", 37832, 30305, 511, 0,
4590           PL, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4591 static const struct wacom_features wacom_features_0xCE =
4592         { "Wacom DTU2231", 47864, 27011, 511, 0,
4593           DTU, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4594           .check_for_hid_type = true, .hid_type = HID_TYPE_USBMOUSE };
4595 static const struct wacom_features wacom_features_0xF0 =
4596         { "Wacom DTU1631", 34623, 19553, 511, 0,
4597           DTU, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4598 static const struct wacom_features wacom_features_0xFB =
4599         { "Wacom DTU1031", 22096, 13960, 511, 0,
4600           DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
4601           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4602           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4603 static const struct wacom_features wacom_features_0x32F =
4604         { "Wacom DTU1031X", 22672, 12928, 511, 0,
4605           DTUSX, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 0,
4606           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4607           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4608 static const struct wacom_features wacom_features_0x336 =
4609         { "Wacom DTU1141", 23672, 13403, 1023, 0,
4610           DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
4611           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4612           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4613 static const struct wacom_features wacom_features_0x57 =
4614         { "Wacom DTK2241", 95840, 54260, 2047, 63,
4615           DTK, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 6,
4616           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4617           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4618 static const struct wacom_features wacom_features_0x59 = /* Pen */
4619         { "Wacom DTH2242", 95840, 54260, 2047, 63,
4620           DTK, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 6,
4621           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4622           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4623           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5D };
4624 static const struct wacom_features wacom_features_0x5D = /* Touch */
4625         { "Wacom DTH2242",       .type = WACOM_24HDT,
4626           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x59, .touch_max = 10,
4627           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4628 static const struct wacom_features wacom_features_0xCC =
4629         { "Wacom Cintiq 21UX2", 87200, 65600, 2047, 63,
4630           WACOM_21UX2, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
4631           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4632           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4633 static const struct wacom_features wacom_features_0xFA =
4634         { "Wacom Cintiq 22HD", 95840, 54260, 2047, 63,
4635           WACOM_22HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
4636           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4637           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4638 static const struct wacom_features wacom_features_0x5B =
4639         { "Wacom Cintiq 22HDT", 95840, 54260, 2047, 63,
4640           WACOM_22HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
4641           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4642           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4643           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5e };
4644 static const struct wacom_features wacom_features_0x5E =
4645         { "Wacom Cintiq 22HDT", .type = WACOM_24HDT,
4646           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5b, .touch_max = 10,
4647           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4648 static const struct wacom_features wacom_features_0x90 =
4649         { "Wacom ISDv4 90", 26202, 16325, 255, 0,
4650           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
4651 static const struct wacom_features wacom_features_0x93 =
4652         { "Wacom ISDv4 93", 26202, 16325, 255, 0,
4653           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
4654 static const struct wacom_features wacom_features_0x97 =
4655         { "Wacom ISDv4 97", 26202, 16325, 511, 0,
4656           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
4657 static const struct wacom_features wacom_features_0x9A =
4658         { "Wacom ISDv4 9A", 26202, 16325, 255, 0,
4659           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
4660 static const struct wacom_features wacom_features_0x9F =
4661         { "Wacom ISDv4 9F", 26202, 16325, 255, 0,
4662           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
4663 static const struct wacom_features wacom_features_0xE2 =
4664         { "Wacom ISDv4 E2", 26202, 16325, 255, 0,
4665           TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4666 static const struct wacom_features wacom_features_0xE3 =
4667         { "Wacom ISDv4 E3", 26202, 16325, 255, 0,
4668           TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4669 static const struct wacom_features wacom_features_0xE5 =
4670         { "Wacom ISDv4 E5", 26202, 16325, 255, 0,
4671           MTSCREEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4672 static const struct wacom_features wacom_features_0xE6 =
4673         { "Wacom ISDv4 E6", 27760, 15694, 255, 0,
4674           TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4675 static const struct wacom_features wacom_features_0xEC =
4676         { "Wacom ISDv4 EC", 25710, 14500, 255, 0,
4677           TABLETPC,    WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
4678 static const struct wacom_features wacom_features_0xED =
4679         { "Wacom ISDv4 ED", 26202, 16325, 255, 0,
4680           TABLETPCE, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
4681 static const struct wacom_features wacom_features_0xEF =
4682         { "Wacom ISDv4 EF", 26202, 16325, 255, 0,
4683           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
4684 static const struct wacom_features wacom_features_0x100 =
4685         { "Wacom ISDv4 100", 26202, 16325, 255, 0,
4686           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4687 static const struct wacom_features wacom_features_0x101 =
4688         { "Wacom ISDv4 101", 26202, 16325, 255, 0,
4689           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4690 static const struct wacom_features wacom_features_0x10D =
4691         { "Wacom ISDv4 10D", 26202, 16325, 255, 0,
4692           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4693 static const struct wacom_features wacom_features_0x10E =
4694         { "Wacom ISDv4 10E", 27760, 15694, 255, 0,
4695           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4696 static const struct wacom_features wacom_features_0x10F =
4697         { "Wacom ISDv4 10F", 27760, 15694, 255, 0,
4698           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4699 static const struct wacom_features wacom_features_0x116 =
4700         { "Wacom ISDv4 116", 26202, 16325, 255, 0,
4701           TABLETPCE, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 1 };
4702 static const struct wacom_features wacom_features_0x12C =
4703         { "Wacom ISDv4 12C", 27848, 15752, 2047, 0,
4704           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES }; /* Pen-only */
4705 static const struct wacom_features wacom_features_0x4001 =
4706         { "Wacom ISDv4 4001", 26202, 16325, 255, 0,
4707           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4708 static const struct wacom_features wacom_features_0x4004 =
4709         { "Wacom ISDv4 4004", 11060, 6220, 255, 0,
4710           MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4711 static const struct wacom_features wacom_features_0x5000 =
4712         { "Wacom ISDv4 5000", 27848, 15752, 1023, 0,
4713           MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4714 static const struct wacom_features wacom_features_0x5002 =
4715         { "Wacom ISDv4 5002", 29576, 16724, 1023, 0,
4716           MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4717 static const struct wacom_features wacom_features_0x47 =
4718         { "Wacom Intuos2 6x8", 20320, 16240, 1023, 31,
4719           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4720 static const struct wacom_features wacom_features_0x84 =
4721         { "Wacom Wireless Receiver", .type = WIRELESS, .touch_max = 16 };
4722 static const struct wacom_features wacom_features_0xD0 =
4723         { "Wacom Bamboo 2FG", 14720, 9200, 1023, 31,
4724           BAMBOO_TOUCH, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4725 static const struct wacom_features wacom_features_0xD1 =
4726         { "Wacom Bamboo 2FG 4x5", 14720, 9200, 1023, 31,
4727           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4728 static const struct wacom_features wacom_features_0xD2 =
4729         { "Wacom Bamboo Craft", 14720, 9200, 1023, 31,
4730           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4731 static const struct wacom_features wacom_features_0xD3 =
4732         { "Wacom Bamboo 2FG 6x8", 21648, 13700, 1023, 31,
4733           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4734 static const struct wacom_features wacom_features_0xD4 =
4735         { "Wacom Bamboo Pen", 14720, 9200, 1023, 31,
4736           BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4737 static const struct wacom_features wacom_features_0xD5 =
4738         { "Wacom Bamboo Pen 6x8", 21648, 13700, 1023, 31,
4739           BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4740 static const struct wacom_features wacom_features_0xD6 =
4741         { "Wacom BambooPT 2FG 4x5", 14720, 9200, 1023, 31,
4742           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4743 static const struct wacom_features wacom_features_0xD7 =
4744         { "Wacom BambooPT 2FG Small", 14720, 9200, 1023, 31,
4745           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4746 static const struct wacom_features wacom_features_0xD8 =
4747         { "Wacom Bamboo Comic 2FG", 21648, 13700, 1023, 31,
4748           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4749 static const struct wacom_features wacom_features_0xDA =
4750         { "Wacom Bamboo 2FG 4x5 SE", 14720, 9200, 1023, 31,
4751           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4752 static const struct wacom_features wacom_features_0xDB =
4753         { "Wacom Bamboo 2FG 6x8 SE", 21648, 13700, 1023, 31,
4754           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4755 static const struct wacom_features wacom_features_0xDD =
4756         { "Wacom Bamboo Connect", 14720, 9200, 1023, 31,
4757           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4758 static const struct wacom_features wacom_features_0xDE =
4759         { "Wacom Bamboo 16FG 4x5", 14720, 9200, 1023, 31,
4760           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16 };
4761 static const struct wacom_features wacom_features_0xDF =
4762         { "Wacom Bamboo 16FG 6x8", 21648, 13700, 1023, 31,
4763           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16 };
4764 static const struct wacom_features wacom_features_0x300 =
4765         { "Wacom Bamboo One S", 14720, 9225, 1023, 31,
4766           BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4767 static const struct wacom_features wacom_features_0x301 =
4768         { "Wacom Bamboo One M", 21648, 13530, 1023, 31,
4769           BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4770 static const struct wacom_features wacom_features_0x302 =
4771         { "Wacom Intuos PT S", 15200, 9500, 1023, 31,
4772           INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4773           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4774 static const struct wacom_features wacom_features_0x303 =
4775         { "Wacom Intuos PT M", 21600, 13500, 1023, 31,
4776           INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4777           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4778 static const struct wacom_features wacom_features_0x30E =
4779         { "Wacom Intuos S", 15200, 9500, 1023, 31,
4780           INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4781           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4782 static const struct wacom_features wacom_features_0x6004 =
4783         { "ISD-V4", 12800, 8000, 255, 0,
4784           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4785 static const struct wacom_features wacom_features_0x307 =
4786         { "Wacom ISDv5 307", 59552, 33848, 2047, 63,
4787           CINTIQ_HYBRID, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4788           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4789           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4790           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x309 };
4791 static const struct wacom_features wacom_features_0x309 =
4792         { "Wacom ISDv5 309", .type = WACOM_24HDT, /* Touch */
4793           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x0307, .touch_max = 10,
4794           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4795 static const struct wacom_features wacom_features_0x30A =
4796         { "Wacom ISDv5 30A", 59552, 33848, 2047, 63,
4797           CINTIQ_HYBRID, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4798           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4799           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4800           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x30C };
4801 static const struct wacom_features wacom_features_0x30C =
4802         { "Wacom ISDv5 30C", .type = WACOM_24HDT, /* Touch */
4803           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x30A, .touch_max = 10,
4804           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4805 static const struct wacom_features wacom_features_0x318 =
4806         { "Wacom USB Bamboo PAD", 4095, 4095, /* Touch */
4807           .type = BAMBOO_PAD, 35, 48, .touch_max = 4 };
4808 static const struct wacom_features wacom_features_0x319 =
4809         { "Wacom Wireless Bamboo PAD", 4095, 4095, /* Touch */
4810           .type = BAMBOO_PAD, 35, 48, .touch_max = 4 };
4811 static const struct wacom_features wacom_features_0x325 =
4812         { "Wacom ISDv5 325", 59552, 33848, 2047, 63,
4813           CINTIQ_COMPANION_2, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 11,
4814           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4815           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4816           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x326 };
4817 static const struct wacom_features wacom_features_0x326 = /* Touch */
4818         { "Wacom ISDv5 326", .type = HID_GENERIC, .oVid = USB_VENDOR_ID_WACOM,
4819           .oPid = 0x325 };
4820 static const struct wacom_features wacom_features_0x323 =
4821         { "Wacom Intuos P M", 21600, 13500, 1023, 31,
4822           INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4823           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4824 static const struct wacom_features wacom_features_0x331 =
4825         { "Wacom Express Key Remote", .type = REMOTE,
4826           .numbered_buttons = 18, .check_for_hid_type = true,
4827           .hid_type = HID_TYPE_USBNONE };
4828 static const struct wacom_features wacom_features_0x33B =
4829         { "Wacom Intuos S 2", 15200, 9500, 2047, 63,
4830           INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4831           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4832 static const struct wacom_features wacom_features_0x33C =
4833         { "Wacom Intuos PT S 2", 15200, 9500, 2047, 63,
4834           INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4835           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4836 static const struct wacom_features wacom_features_0x33D =
4837         { "Wacom Intuos P M 2", 21600, 13500, 2047, 63,
4838           INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4839           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4840 static const struct wacom_features wacom_features_0x33E =
4841         { "Wacom Intuos PT M 2", 21600, 13500, 2047, 63,
4842           INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4843           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4844 static const struct wacom_features wacom_features_0x343 =
4845         { "Wacom DTK1651", 34816, 19759, 1023, 0,
4846           DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
4847           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4848           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4849 static const struct wacom_features wacom_features_0x360 =
4850         { "Wacom Intuos Pro M", 44800, 29600, 8191, 63,
4851           INTUOSP2_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 10 };
4852 static const struct wacom_features wacom_features_0x361 =
4853         { "Wacom Intuos Pro L", 62200, 43200, 8191, 63,
4854           INTUOSP2_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 10 };
4855 static const struct wacom_features wacom_features_0x377 =
4856         { "Wacom Intuos BT S", 15200, 9500, 4095, 63,
4857           INTUOSHT3_BT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4 };
4858 static const struct wacom_features wacom_features_0x379 =
4859         { "Wacom Intuos BT M", 21600, 13500, 4095, 63,
4860           INTUOSHT3_BT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4 };
4861 static const struct wacom_features wacom_features_0x37A =
4862         { "Wacom One by Wacom S", 15200, 9500, 2047, 63,
4863           BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4864 static const struct wacom_features wacom_features_0x37B =
4865         { "Wacom One by Wacom M", 21600, 13500, 2047, 63,
4866           BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4867 static const struct wacom_features wacom_features_0x393 =
4868         { "Wacom Intuos Pro S", 31920, 19950, 8191, 63,
4869           INTUOSP2S_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7,
4870           .touch_max = 10 };
4871 static const struct wacom_features wacom_features_0x3c6 =
4872         { "Wacom Intuos BT S", 15200, 9500, 4095, 63,
4873           INTUOSHT3_BT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4 };
4874 static const struct wacom_features wacom_features_0x3c8 =
4875         { "Wacom Intuos BT M", 21600, 13500, 4095, 63,
4876           INTUOSHT3_BT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4 };
4877 static const struct wacom_features wacom_features_0x3dd =
4878         { "Wacom Intuos Pro S", 31920, 19950, 8191, 63,
4879           INTUOSP2S_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7,
4880           .touch_max = 10 };
4881
4882 static const struct wacom_features wacom_features_HID_ANY_ID =
4883         { "Wacom HID", .type = HID_GENERIC, .oVid = HID_ANY_ID, .oPid = HID_ANY_ID };
4884
4885 static const struct wacom_features wacom_features_0x94 =
4886         { "Wacom Bootloader", .type = BOOTLOADER };
4887
4888 #define USB_DEVICE_WACOM(prod)                                          \
4889         HID_DEVICE(BUS_USB, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4890         .driver_data = (kernel_ulong_t)&wacom_features_##prod
4891
4892 #define BT_DEVICE_WACOM(prod)                                           \
4893         HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4894         .driver_data = (kernel_ulong_t)&wacom_features_##prod
4895
4896 #define I2C_DEVICE_WACOM(prod)                                          \
4897         HID_DEVICE(BUS_I2C, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4898         .driver_data = (kernel_ulong_t)&wacom_features_##prod
4899
4900 #define USB_DEVICE_LENOVO(prod)                                 \
4901         HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, prod),                     \
4902         .driver_data = (kernel_ulong_t)&wacom_features_##prod
4903
4904 const struct hid_device_id wacom_ids[] = {
4905         { USB_DEVICE_WACOM(0x00) },
4906         { USB_DEVICE_WACOM(0x03) },
4907         { USB_DEVICE_WACOM(0x10) },
4908         { USB_DEVICE_WACOM(0x11) },
4909         { USB_DEVICE_WACOM(0x12) },
4910         { USB_DEVICE_WACOM(0x13) },
4911         { USB_DEVICE_WACOM(0x14) },
4912         { USB_DEVICE_WACOM(0x15) },
4913         { USB_DEVICE_WACOM(0x16) },
4914         { USB_DEVICE_WACOM(0x17) },
4915         { USB_DEVICE_WACOM(0x18) },
4916         { USB_DEVICE_WACOM(0x19) },
4917         { USB_DEVICE_WACOM(0x20) },
4918         { USB_DEVICE_WACOM(0x21) },
4919         { USB_DEVICE_WACOM(0x22) },
4920         { USB_DEVICE_WACOM(0x23) },
4921         { USB_DEVICE_WACOM(0x24) },
4922         { USB_DEVICE_WACOM(0x26) },
4923         { USB_DEVICE_WACOM(0x27) },
4924         { USB_DEVICE_WACOM(0x28) },
4925         { USB_DEVICE_WACOM(0x29) },
4926         { USB_DEVICE_WACOM(0x2A) },
4927         { USB_DEVICE_WACOM(0x30) },
4928         { USB_DEVICE_WACOM(0x31) },
4929         { USB_DEVICE_WACOM(0x32) },
4930         { USB_DEVICE_WACOM(0x33) },
4931         { USB_DEVICE_WACOM(0x34) },
4932         { USB_DEVICE_WACOM(0x35) },
4933         { USB_DEVICE_WACOM(0x37) },
4934         { USB_DEVICE_WACOM(0x38) },
4935         { USB_DEVICE_WACOM(0x39) },
4936         { USB_DEVICE_WACOM(0x3F) },
4937         { USB_DEVICE_WACOM(0x41) },
4938         { USB_DEVICE_WACOM(0x42) },
4939         { USB_DEVICE_WACOM(0x43) },
4940         { USB_DEVICE_WACOM(0x44) },
4941         { USB_DEVICE_WACOM(0x45) },
4942         { USB_DEVICE_WACOM(0x47) },
4943         { USB_DEVICE_WACOM(0x57) },
4944         { USB_DEVICE_WACOM(0x59) },
4945         { USB_DEVICE_WACOM(0x5B) },
4946         { USB_DEVICE_WACOM(0x5D) },
4947         { USB_DEVICE_WACOM(0x5E) },
4948         { USB_DEVICE_WACOM(0x60) },
4949         { USB_DEVICE_WACOM(0x61) },
4950         { USB_DEVICE_WACOM(0x62) },
4951         { USB_DEVICE_WACOM(0x63) },
4952         { USB_DEVICE_WACOM(0x64) },
4953         { USB_DEVICE_WACOM(0x65) },
4954         { USB_DEVICE_WACOM(0x69) },
4955         { USB_DEVICE_WACOM(0x6A) },
4956         { USB_DEVICE_WACOM(0x6B) },
4957         { BT_DEVICE_WACOM(0x81) },
4958         { USB_DEVICE_WACOM(0x84) },
4959         { USB_DEVICE_WACOM(0x90) },
4960         { USB_DEVICE_WACOM(0x93) },
4961         { USB_DEVICE_WACOM(0x94) },
4962         { USB_DEVICE_WACOM(0x97) },
4963         { USB_DEVICE_WACOM(0x9A) },
4964         { USB_DEVICE_WACOM(0x9F) },
4965         { USB_DEVICE_WACOM(0xB0) },
4966         { USB_DEVICE_WACOM(0xB1) },
4967         { USB_DEVICE_WACOM(0xB2) },
4968         { USB_DEVICE_WACOM(0xB3) },
4969         { USB_DEVICE_WACOM(0xB4) },
4970         { USB_DEVICE_WACOM(0xB5) },
4971         { USB_DEVICE_WACOM(0xB7) },
4972         { USB_DEVICE_WACOM(0xB8) },
4973         { USB_DEVICE_WACOM(0xB9) },
4974         { USB_DEVICE_WACOM(0xBA) },
4975         { USB_DEVICE_WACOM(0xBB) },
4976         { USB_DEVICE_WACOM(0xBC) },
4977         { BT_DEVICE_WACOM(0xBD) },
4978         { USB_DEVICE_WACOM(0xC0) },
4979         { USB_DEVICE_WACOM(0xC2) },
4980         { USB_DEVICE_WACOM(0xC4) },
4981         { USB_DEVICE_WACOM(0xC5) },
4982         { USB_DEVICE_WACOM(0xC6) },
4983         { USB_DEVICE_WACOM(0xC7) },
4984         { USB_DEVICE_WACOM(0xCC) },
4985         { USB_DEVICE_WACOM(0xCE) },
4986         { USB_DEVICE_WACOM(0xD0) },
4987         { USB_DEVICE_WACOM(0xD1) },
4988         { USB_DEVICE_WACOM(0xD2) },
4989         { USB_DEVICE_WACOM(0xD3) },
4990         { USB_DEVICE_WACOM(0xD4) },
4991         { USB_DEVICE_WACOM(0xD5) },
4992         { USB_DEVICE_WACOM(0xD6) },
4993         { USB_DEVICE_WACOM(0xD7) },
4994         { USB_DEVICE_WACOM(0xD8) },
4995         { USB_DEVICE_WACOM(0xDA) },
4996         { USB_DEVICE_WACOM(0xDB) },
4997         { USB_DEVICE_WACOM(0xDD) },
4998         { USB_DEVICE_WACOM(0xDE) },
4999         { USB_DEVICE_WACOM(0xDF) },
5000         { USB_DEVICE_WACOM(0xE2) },
5001         { USB_DEVICE_WACOM(0xE3) },
5002         { USB_DEVICE_WACOM(0xE5) },
5003         { USB_DEVICE_WACOM(0xE6) },
5004         { USB_DEVICE_WACOM(0xEC) },
5005         { USB_DEVICE_WACOM(0xED) },
5006         { USB_DEVICE_WACOM(0xEF) },
5007         { USB_DEVICE_WACOM(0xF0) },
5008         { USB_DEVICE_WACOM(0xF4) },
5009         { USB_DEVICE_WACOM(0xF6) },
5010         { USB_DEVICE_WACOM(0xF8) },
5011         { USB_DEVICE_WACOM(0xFA) },
5012         { USB_DEVICE_WACOM(0xFB) },
5013         { USB_DEVICE_WACOM(0x100) },
5014         { USB_DEVICE_WACOM(0x101) },
5015         { USB_DEVICE_WACOM(0x10D) },
5016         { USB_DEVICE_WACOM(0x10E) },
5017         { USB_DEVICE_WACOM(0x10F) },
5018         { USB_DEVICE_WACOM(0x116) },
5019         { USB_DEVICE_WACOM(0x12C) },
5020         { USB_DEVICE_WACOM(0x300) },
5021         { USB_DEVICE_WACOM(0x301) },
5022         { USB_DEVICE_WACOM(0x302) },
5023         { USB_DEVICE_WACOM(0x303) },
5024         { USB_DEVICE_WACOM(0x304) },
5025         { USB_DEVICE_WACOM(0x307) },
5026         { USB_DEVICE_WACOM(0x309) },
5027         { USB_DEVICE_WACOM(0x30A) },
5028         { USB_DEVICE_WACOM(0x30C) },
5029         { USB_DEVICE_WACOM(0x30E) },
5030         { USB_DEVICE_WACOM(0x314) },
5031         { USB_DEVICE_WACOM(0x315) },
5032         { USB_DEVICE_WACOM(0x317) },
5033         { USB_DEVICE_WACOM(0x318) },
5034         { USB_DEVICE_WACOM(0x319) },
5035         { USB_DEVICE_WACOM(0x323) },
5036         { USB_DEVICE_WACOM(0x325) },
5037         { USB_DEVICE_WACOM(0x326) },
5038         { USB_DEVICE_WACOM(0x32A) },
5039         { USB_DEVICE_WACOM(0x32B) },
5040         { USB_DEVICE_WACOM(0x32C) },
5041         { USB_DEVICE_WACOM(0x32F) },
5042         { USB_DEVICE_WACOM(0x331) },
5043         { USB_DEVICE_WACOM(0x333) },
5044         { USB_DEVICE_WACOM(0x335) },
5045         { USB_DEVICE_WACOM(0x336) },
5046         { USB_DEVICE_WACOM(0x33B) },
5047         { USB_DEVICE_WACOM(0x33C) },
5048         { USB_DEVICE_WACOM(0x33D) },
5049         { USB_DEVICE_WACOM(0x33E) },
5050         { USB_DEVICE_WACOM(0x343) },
5051         { BT_DEVICE_WACOM(0x360) },
5052         { BT_DEVICE_WACOM(0x361) },
5053         { BT_DEVICE_WACOM(0x377) },
5054         { BT_DEVICE_WACOM(0x379) },
5055         { USB_DEVICE_WACOM(0x37A) },
5056         { USB_DEVICE_WACOM(0x37B) },
5057         { BT_DEVICE_WACOM(0x393) },
5058         { BT_DEVICE_WACOM(0x3c6) },
5059         { BT_DEVICE_WACOM(0x3c8) },
5060         { BT_DEVICE_WACOM(0x3dd) },
5061         { USB_DEVICE_WACOM(0x4001) },
5062         { USB_DEVICE_WACOM(0x4004) },
5063         { USB_DEVICE_WACOM(0x5000) },
5064         { USB_DEVICE_WACOM(0x5002) },
5065         { USB_DEVICE_LENOVO(0x6004) },
5066
5067         { USB_DEVICE_WACOM(HID_ANY_ID) },
5068         { I2C_DEVICE_WACOM(HID_ANY_ID) },
5069         { BT_DEVICE_WACOM(HID_ANY_ID) },
5070         { }
5071 };
5072 MODULE_DEVICE_TABLE(hid, wacom_ids);