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