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