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