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