]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/hid/wacom_wac.c
Merge tag 'sunxi-fixes-for-4.12' of https://git.kernel.org/pub/scm/linux/kernel/git...
[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         else if (wacom->touch_input)
1578                 dev_dbg(wacom->touch_input->dev.parent,
1579                         "%s: received report #%d\n", __func__, data[0]);
1580
1581         switch (len) {
1582         case WACOM_PKGLEN_TPC1FG:
1583                 return wacom_tpc_single_touch(wacom, len);
1584
1585         case WACOM_PKGLEN_TPC2FG:
1586                 return wacom_tpc_mt_touch(wacom);
1587
1588         case WACOM_PKGLEN_PENABLED:
1589                 return wacom_tpc_pen(wacom);
1590
1591         default:
1592                 switch (data[0]) {
1593                 case WACOM_REPORT_TPC1FG:
1594                 case WACOM_REPORT_TPCHID:
1595                 case WACOM_REPORT_TPCST:
1596                 case WACOM_REPORT_TPC1FGE:
1597                         return wacom_tpc_single_touch(wacom, len);
1598
1599                 case WACOM_REPORT_TPCMT:
1600                 case WACOM_REPORT_TPCMT2:
1601                         return wacom_mt_touch(wacom);
1602
1603                 case WACOM_REPORT_PENABLED:
1604                         return wacom_tpc_pen(wacom);
1605                 }
1606         }
1607
1608         return 0;
1609 }
1610
1611 int wacom_equivalent_usage(int usage)
1612 {
1613         if ((usage & HID_USAGE_PAGE) == WACOM_HID_UP_WACOMDIGITIZER) {
1614                 int subpage = (usage & 0xFF00) << 8;
1615                 int subusage = (usage & 0xFF);
1616
1617                 if (subpage == WACOM_HID_SP_PAD ||
1618                     subpage == WACOM_HID_SP_BUTTON ||
1619                     subpage == WACOM_HID_SP_DIGITIZER ||
1620                     subpage == WACOM_HID_SP_DIGITIZERINFO ||
1621                     usage == WACOM_HID_WD_SENSE ||
1622                     usage == WACOM_HID_WD_SERIALHI ||
1623                     usage == WACOM_HID_WD_TOOLTYPE ||
1624                     usage == WACOM_HID_WD_DISTANCE ||
1625                     usage == WACOM_HID_WD_TOUCHSTRIP ||
1626                     usage == WACOM_HID_WD_TOUCHSTRIP2 ||
1627                     usage == WACOM_HID_WD_TOUCHRING ||
1628                     usage == WACOM_HID_WD_TOUCHRINGSTATUS) {
1629                         return usage;
1630                 }
1631
1632                 if (subpage == HID_UP_UNDEFINED)
1633                         subpage = HID_UP_DIGITIZER;
1634
1635                 return subpage | subusage;
1636         }
1637
1638         if ((usage & HID_USAGE_PAGE) == WACOM_HID_UP_WACOMTOUCH) {
1639                 int subpage = (usage & 0xFF00) << 8;
1640                 int subusage = (usage & 0xFF);
1641
1642                 if (subpage == HID_UP_UNDEFINED)
1643                         subpage = WACOM_HID_SP_DIGITIZER;
1644
1645                 return subpage | subusage;
1646         }
1647
1648         return usage;
1649 }
1650
1651 static void wacom_map_usage(struct input_dev *input, struct hid_usage *usage,
1652                 struct hid_field *field, __u8 type, __u16 code, int fuzz)
1653 {
1654         struct wacom *wacom = input_get_drvdata(input);
1655         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1656         struct wacom_features *features = &wacom_wac->features;
1657         int fmin = field->logical_minimum;
1658         int fmax = field->logical_maximum;
1659         unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
1660         int resolution_code = code;
1661
1662         if (equivalent_usage == HID_DG_TWIST) {
1663                 resolution_code = ABS_RZ;
1664         }
1665
1666         if (equivalent_usage == HID_GD_X) {
1667                 fmin += features->offset_left;
1668                 fmax -= features->offset_right;
1669         }
1670         if (equivalent_usage == HID_GD_Y) {
1671                 fmin += features->offset_top;
1672                 fmax -= features->offset_bottom;
1673         }
1674
1675         usage->type = type;
1676         usage->code = code;
1677
1678         set_bit(type, input->evbit);
1679
1680         switch (type) {
1681         case EV_ABS:
1682                 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
1683                 input_abs_set_res(input, code,
1684                                   hidinput_calc_abs_res(field, resolution_code));
1685                 break;
1686         case EV_KEY:
1687                 input_set_capability(input, EV_KEY, code);
1688                 break;
1689         case EV_MSC:
1690                 input_set_capability(input, EV_MSC, code);
1691                 break;
1692         case EV_SW:
1693                 input_set_capability(input, EV_SW, code);
1694                 break;
1695         }
1696 }
1697
1698 static void wacom_wac_pad_usage_mapping(struct hid_device *hdev,
1699                 struct hid_field *field, struct hid_usage *usage)
1700 {
1701         struct wacom *wacom = hid_get_drvdata(hdev);
1702         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1703         struct wacom_features *features = &wacom_wac->features;
1704         struct input_dev *input = wacom_wac->pad_input;
1705         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1706
1707         switch (equivalent_usage) {
1708         case WACOM_HID_WD_BATTERY_LEVEL:
1709         case WACOM_HID_WD_BATTERY_CHARGING:
1710                 features->quirks |= WACOM_QUIRK_BATTERY;
1711                 break;
1712         case WACOM_HID_WD_ACCELEROMETER_X:
1713                 __set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
1714                 wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 0);
1715                 features->device_type |= WACOM_DEVICETYPE_PAD;
1716                 break;
1717         case WACOM_HID_WD_ACCELEROMETER_Y:
1718                 __set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
1719                 wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 0);
1720                 features->device_type |= WACOM_DEVICETYPE_PAD;
1721                 break;
1722         case WACOM_HID_WD_ACCELEROMETER_Z:
1723                 __set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
1724                 wacom_map_usage(input, usage, field, EV_ABS, ABS_Z, 0);
1725                 features->device_type |= WACOM_DEVICETYPE_PAD;
1726                 break;
1727         case WACOM_HID_WD_BUTTONCENTER:
1728                 wacom->generic_has_leds = true;
1729                 /* fall through */
1730         case WACOM_HID_WD_BUTTONHOME:
1731         case WACOM_HID_WD_BUTTONUP:
1732         case WACOM_HID_WD_BUTTONDOWN:
1733         case WACOM_HID_WD_BUTTONLEFT:
1734         case WACOM_HID_WD_BUTTONRIGHT:
1735                 wacom_map_usage(input, usage, field, EV_KEY,
1736                                 wacom_numbered_button_to_key(features->numbered_buttons),
1737                                 0);
1738                 features->numbered_buttons++;
1739                 features->device_type |= WACOM_DEVICETYPE_PAD;
1740                 break;
1741         case WACOM_HID_WD_TOUCHONOFF:
1742         case WACOM_HID_WD_MUTE_DEVICE:
1743                 /*
1744                  * This usage, which is used to mute touch events, comes
1745                  * from the pad packet, but is reported on the touch
1746                  * interface. Because the touch interface may not have
1747                  * been created yet, we cannot call wacom_map_usage(). In
1748                  * order to process this usage when we receive it, we set
1749                  * the usage type and code directly.
1750                  */
1751                 wacom_wac->has_mute_touch_switch = true;
1752                 usage->type = EV_SW;
1753                 usage->code = SW_MUTE_DEVICE;
1754                 features->device_type |= WACOM_DEVICETYPE_PAD;
1755                 break;
1756         case WACOM_HID_WD_TOUCHSTRIP:
1757                 wacom_map_usage(input, usage, field, EV_ABS, ABS_RX, 0);
1758                 features->device_type |= WACOM_DEVICETYPE_PAD;
1759                 break;
1760         case WACOM_HID_WD_TOUCHSTRIP2:
1761                 wacom_map_usage(input, usage, field, EV_ABS, ABS_RY, 0);
1762                 features->device_type |= WACOM_DEVICETYPE_PAD;
1763                 break;
1764         case WACOM_HID_WD_TOUCHRING:
1765                 wacom_map_usage(input, usage, field, EV_ABS, ABS_WHEEL, 0);
1766                 features->device_type |= WACOM_DEVICETYPE_PAD;
1767                 break;
1768         case WACOM_HID_WD_TOUCHRINGSTATUS:
1769                 wacom_map_usage(input, usage, field, EV_ABS, ABS_WHEEL, 0);
1770                 features->device_type |= WACOM_DEVICETYPE_PAD;
1771                 break;
1772         case WACOM_HID_WD_BUTTONCONFIG:
1773                 wacom_map_usage(input, usage, field, EV_KEY, KEY_BUTTONCONFIG, 0);
1774                 features->device_type |= WACOM_DEVICETYPE_PAD;
1775                 break;
1776         case WACOM_HID_WD_ONSCREEN_KEYBOARD:
1777                 wacom_map_usage(input, usage, field, EV_KEY, KEY_ONSCREEN_KEYBOARD, 0);
1778                 features->device_type |= WACOM_DEVICETYPE_PAD;
1779                 break;
1780         case WACOM_HID_WD_CONTROLPANEL:
1781                 wacom_map_usage(input, usage, field, EV_KEY, KEY_CONTROLPANEL, 0);
1782                 features->device_type |= WACOM_DEVICETYPE_PAD;
1783                 break;
1784         case WACOM_HID_WD_MODE_CHANGE:
1785                 /* do not overwrite previous data */
1786                 if (!wacom_wac->has_mode_change) {
1787                         wacom_wac->has_mode_change = true;
1788                         wacom_wac->is_direct_mode = true;
1789                 }
1790                 features->device_type |= WACOM_DEVICETYPE_PAD;
1791                 break;
1792         }
1793
1794         switch (equivalent_usage & 0xfffffff0) {
1795         case WACOM_HID_WD_EXPRESSKEY00:
1796                 wacom_map_usage(input, usage, field, EV_KEY,
1797                                 wacom_numbered_button_to_key(features->numbered_buttons),
1798                                 0);
1799                 features->numbered_buttons++;
1800                 features->device_type |= WACOM_DEVICETYPE_PAD;
1801                 break;
1802         }
1803 }
1804
1805 static void wacom_wac_pad_battery_event(struct hid_device *hdev, struct hid_field *field,
1806                 struct hid_usage *usage, __s32 value)
1807 {
1808         struct wacom *wacom = hid_get_drvdata(hdev);
1809         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1810         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1811
1812         switch (equivalent_usage) {
1813         case WACOM_HID_WD_BATTERY_LEVEL:
1814                 wacom_wac->hid_data.battery_capacity = value;
1815                 wacom_wac->hid_data.bat_connected = 1;
1816                 break;
1817
1818         case WACOM_HID_WD_BATTERY_CHARGING:
1819                 wacom_wac->hid_data.bat_charging = value;
1820                 wacom_wac->hid_data.ps_connected = value;
1821                 wacom_wac->hid_data.bat_connected = 1;
1822                 break;
1823         }
1824 }
1825
1826 static void wacom_wac_pad_event(struct hid_device *hdev, struct hid_field *field,
1827                 struct hid_usage *usage, __s32 value)
1828 {
1829         struct wacom *wacom = hid_get_drvdata(hdev);
1830         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1831         struct input_dev *input = wacom_wac->pad_input;
1832         struct wacom_features *features = &wacom_wac->features;
1833         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1834         int i;
1835         bool is_touch_on = value;
1836
1837         /*
1838          * Avoid reporting this event and setting inrange_state if this usage
1839          * hasn't been mapped.
1840          */
1841         if (!usage->type && equivalent_usage != WACOM_HID_WD_MODE_CHANGE)
1842                 return;
1843
1844         if (wacom_equivalent_usage(field->physical) == HID_DG_TABLETFUNCTIONKEY) {
1845                 if (usage->hid != WACOM_HID_WD_TOUCHRING)
1846                         wacom_wac->hid_data.inrange_state |= value;
1847         }
1848
1849         switch (equivalent_usage) {
1850         case WACOM_HID_WD_TOUCHRINGSTATUS:
1851                 if (!value)
1852                         input_event(input, usage->type, usage->code, 0);
1853                 break;
1854
1855         case WACOM_HID_WD_MUTE_DEVICE:
1856                 if (wacom_wac->shared->touch_input && value) {
1857                         wacom_wac->shared->is_touch_on = !wacom_wac->shared->is_touch_on;
1858                         is_touch_on = wacom_wac->shared->is_touch_on;
1859                 }
1860
1861                 /* fall through*/
1862         case WACOM_HID_WD_TOUCHONOFF:
1863                 if (wacom_wac->shared->touch_input) {
1864                         input_report_switch(wacom_wac->shared->touch_input,
1865                                             SW_MUTE_DEVICE, !is_touch_on);
1866                         input_sync(wacom_wac->shared->touch_input);
1867                 }
1868                 break;
1869
1870         case WACOM_HID_WD_MODE_CHANGE:
1871                 if (wacom_wac->is_direct_mode != value) {
1872                         wacom_wac->is_direct_mode = value;
1873                         wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_MODE_CHANGE);
1874                 }
1875                 break;
1876
1877         case WACOM_HID_WD_BUTTONCENTER:
1878                 for (i = 0; i < wacom->led.count; i++)
1879                         wacom_update_led(wacom, features->numbered_buttons,
1880                                          value, i);
1881                  /* fall through*/
1882         default:
1883                 input_event(input, usage->type, usage->code, value);
1884                 if (value)
1885                         wacom_wac->hid_data.pad_input_event_flag = true;
1886                 break;
1887         }
1888 }
1889
1890 static void wacom_wac_pad_pre_report(struct hid_device *hdev,
1891                 struct hid_report *report)
1892 {
1893         struct wacom *wacom = hid_get_drvdata(hdev);
1894         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1895
1896         wacom_wac->hid_data.inrange_state = 0;
1897 }
1898
1899 static void wacom_wac_pad_battery_report(struct hid_device *hdev,
1900                 struct hid_report *report)
1901 {
1902         struct wacom *wacom = hid_get_drvdata(hdev);
1903         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1904         struct wacom_features *features = &wacom_wac->features;
1905
1906         if (features->quirks & WACOM_QUIRK_BATTERY) {
1907                 int capacity = wacom_wac->hid_data.battery_capacity;
1908                 bool charging = wacom_wac->hid_data.bat_charging;
1909                 bool connected = wacom_wac->hid_data.bat_connected;
1910                 bool powered = wacom_wac->hid_data.ps_connected;
1911
1912                 wacom_notify_battery(wacom_wac, capacity, charging,
1913                                      connected, powered);
1914         }
1915 }
1916
1917 static void wacom_wac_pad_report(struct hid_device *hdev,
1918                 struct hid_report *report)
1919 {
1920         struct wacom *wacom = hid_get_drvdata(hdev);
1921         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1922         struct input_dev *input = wacom_wac->pad_input;
1923         bool active = wacom_wac->hid_data.inrange_state != 0;
1924
1925         /* report prox for expresskey events */
1926         if ((wacom_equivalent_usage(report->field[0]->physical) == HID_DG_TABLETFUNCTIONKEY) &&
1927             wacom_wac->hid_data.pad_input_event_flag) {
1928                 input_event(input, EV_ABS, ABS_MISC, active ? PAD_DEVICE_ID : 0);
1929                 input_sync(input);
1930                 if (!active)
1931                         wacom_wac->hid_data.pad_input_event_flag = false;
1932         }
1933
1934 }
1935
1936 static void wacom_wac_pen_usage_mapping(struct hid_device *hdev,
1937                 struct hid_field *field, struct hid_usage *usage)
1938 {
1939         struct wacom *wacom = hid_get_drvdata(hdev);
1940         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1941         struct wacom_features *features = &wacom_wac->features;
1942         struct input_dev *input = wacom_wac->pen_input;
1943         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1944
1945         switch (equivalent_usage) {
1946         case HID_GD_X:
1947                 wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 4);
1948                 break;
1949         case HID_GD_Y:
1950                 wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 4);
1951                 break;
1952         case WACOM_HID_WD_DISTANCE:
1953         case HID_GD_Z:
1954                 wacom_map_usage(input, usage, field, EV_ABS, ABS_DISTANCE, 0);
1955                 break;
1956         case HID_DG_TIPPRESSURE:
1957                 wacom_map_usage(input, usage, field, EV_ABS, ABS_PRESSURE, 0);
1958                 break;
1959         case HID_DG_INRANGE:
1960                 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOOL_PEN, 0);
1961                 break;
1962         case HID_DG_BATTERYSTRENGTH:
1963                 features->quirks |= WACOM_QUIRK_BATTERY;
1964                 break;
1965         case HID_DG_INVERT:
1966                 wacom_map_usage(input, usage, field, EV_KEY,
1967                                 BTN_TOOL_RUBBER, 0);
1968                 break;
1969         case HID_DG_TILT_X:
1970                 wacom_map_usage(input, usage, field, EV_ABS, ABS_TILT_X, 0);
1971                 break;
1972         case HID_DG_TILT_Y:
1973                 wacom_map_usage(input, usage, field, EV_ABS, ABS_TILT_Y, 0);
1974                 break;
1975         case HID_DG_TWIST:
1976                 wacom_map_usage(input, usage, field, EV_ABS, ABS_Z, 0);
1977                 break;
1978         case HID_DG_ERASER:
1979         case HID_DG_TIPSWITCH:
1980                 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
1981                 break;
1982         case HID_DG_BARRELSWITCH:
1983                 wacom_map_usage(input, usage, field, EV_KEY, BTN_STYLUS, 0);
1984                 break;
1985         case HID_DG_BARRELSWITCH2:
1986                 wacom_map_usage(input, usage, field, EV_KEY, BTN_STYLUS2, 0);
1987                 break;
1988         case HID_DG_TOOLSERIALNUMBER:
1989                 wacom_map_usage(input, usage, field, EV_MSC, MSC_SERIAL, 0);
1990                 break;
1991         case WACOM_HID_WD_SENSE:
1992                 features->quirks |= WACOM_QUIRK_SENSE;
1993                 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOOL_PEN, 0);
1994                 break;
1995         case WACOM_HID_WD_SERIALHI:
1996                 wacom_map_usage(input, usage, field, EV_ABS, ABS_MISC, 0);
1997                 set_bit(EV_KEY, input->evbit);
1998                 input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
1999                 input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER);
2000                 input_set_capability(input, EV_KEY, BTN_TOOL_BRUSH);
2001                 input_set_capability(input, EV_KEY, BTN_TOOL_PENCIL);
2002                 input_set_capability(input, EV_KEY, BTN_TOOL_AIRBRUSH);
2003                 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT)) {
2004                         input_set_capability(input, EV_KEY, BTN_TOOL_MOUSE);
2005                         input_set_capability(input, EV_KEY, BTN_TOOL_LENS);
2006                 }
2007                 break;
2008         case WACOM_HID_WD_FINGERWHEEL:
2009                 wacom_map_usage(input, usage, field, EV_ABS, ABS_WHEEL, 0);
2010                 break;
2011         }
2012 }
2013
2014 static void wacom_wac_pen_event(struct hid_device *hdev, struct hid_field *field,
2015                 struct hid_usage *usage, __s32 value)
2016 {
2017         struct wacom *wacom = hid_get_drvdata(hdev);
2018         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2019         struct wacom_features *features = &wacom_wac->features;
2020         struct input_dev *input = wacom_wac->pen_input;
2021         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2022
2023         switch (equivalent_usage) {
2024         case HID_GD_Z:
2025                 /*
2026                  * HID_GD_Z "should increase as the control's position is
2027                  * moved from high to low", while ABS_DISTANCE instead
2028                  * increases in value as the tool moves from low to high.
2029                  */
2030                 value = field->logical_maximum - value;
2031                 break;
2032         case HID_DG_INRANGE:
2033                 wacom_wac->hid_data.inrange_state = value;
2034                 if (!(features->quirks & WACOM_QUIRK_SENSE))
2035                         wacom_wac->hid_data.sense_state = value;
2036                 return;
2037         case HID_DG_BATTERYSTRENGTH:
2038                 wacom_wac->hid_data.battery_capacity = value;
2039                 wacom_wac->hid_data.bat_connected = 1;
2040                 break;
2041         case HID_DG_INVERT:
2042                 wacom_wac->hid_data.invert_state = value;
2043                 return;
2044         case HID_DG_ERASER:
2045         case HID_DG_TIPSWITCH:
2046                 wacom_wac->hid_data.tipswitch |= value;
2047                 return;
2048         case HID_DG_TOOLSERIALNUMBER:
2049                 wacom_wac->serial[0] = (wacom_wac->serial[0] & ~0xFFFFFFFFULL);
2050                 wacom_wac->serial[0] |= (__u32)value;
2051                 return;
2052         case WACOM_HID_WD_SENSE:
2053                 wacom_wac->hid_data.sense_state = value;
2054                 return;
2055         case WACOM_HID_WD_SERIALHI:
2056                 wacom_wac->serial[0] = (wacom_wac->serial[0] & 0xFFFFFFFF);
2057                 wacom_wac->serial[0] |= ((__u64)value) << 32;
2058                 /*
2059                  * Non-USI EMR devices may contain additional tool type
2060                  * information here. See WACOM_HID_WD_TOOLTYPE case for
2061                  * more details.
2062                  */
2063                 if (value >> 20 == 1) {
2064                         wacom_wac->id[0] |= value & 0xFFFFF;
2065                 }
2066                 return;
2067         case WACOM_HID_WD_TOOLTYPE:
2068                 /*
2069                  * Some devices (MobileStudio Pro, and possibly later
2070                  * devices as well) do not return the complete tool
2071                  * type in their WACOM_HID_WD_TOOLTYPE usage. Use a
2072                  * bitwise OR so the complete value can be built
2073                  * up over time :(
2074                  */
2075                 wacom_wac->id[0] |= value;
2076                 return;
2077         case WACOM_HID_WD_OFFSETLEFT:
2078                 if (features->offset_left && value != features->offset_left)
2079                         hid_warn(hdev, "%s: overriding exising left offset "
2080                                  "%d -> %d\n", __func__, value,
2081                                  features->offset_left);
2082                 features->offset_left = value;
2083                 return;
2084         case WACOM_HID_WD_OFFSETRIGHT:
2085                 if (features->offset_right && value != features->offset_right)
2086                         hid_warn(hdev, "%s: overriding exising right offset "
2087                                  "%d -> %d\n", __func__, value,
2088                                  features->offset_right);
2089                 features->offset_right = value;
2090                 return;
2091         case WACOM_HID_WD_OFFSETTOP:
2092                 if (features->offset_top && value != features->offset_top)
2093                         hid_warn(hdev, "%s: overriding exising top offset "
2094                                  "%d -> %d\n", __func__, value,
2095                                  features->offset_top);
2096                 features->offset_top = value;
2097                 return;
2098         case WACOM_HID_WD_OFFSETBOTTOM:
2099                 if (features->offset_bottom && value != features->offset_bottom)
2100                         hid_warn(hdev, "%s: overriding exising bottom offset "
2101                                  "%d -> %d\n", __func__, value,
2102                                  features->offset_bottom);
2103                 features->offset_bottom = value;
2104                 return;
2105         }
2106
2107         /* send pen events only when touch is up or forced out
2108          * or touch arbitration is off
2109          */
2110         if (!usage->type || delay_pen_events(wacom_wac))
2111                 return;
2112
2113         /* send pen events only when the pen is in/entering/leaving proximity */
2114         if (!wacom_wac->hid_data.inrange_state && !wacom_wac->tool[0])
2115                 return;
2116
2117         input_event(input, usage->type, usage->code, value);
2118 }
2119
2120 static void wacom_wac_pen_pre_report(struct hid_device *hdev,
2121                 struct hid_report *report)
2122 {
2123         return;
2124 }
2125
2126 static void wacom_wac_pen_report(struct hid_device *hdev,
2127                 struct hid_report *report)
2128 {
2129         struct wacom *wacom = hid_get_drvdata(hdev);
2130         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2131         struct input_dev *input = wacom_wac->pen_input;
2132         bool prox = wacom_wac->hid_data.inrange_state;
2133         bool range = wacom_wac->hid_data.sense_state;
2134
2135         if (!wacom_wac->tool[0] && prox) { /* first in prox */
2136                 /* Going into proximity select tool */
2137                 if (wacom_wac->hid_data.invert_state)
2138                         wacom_wac->tool[0] = BTN_TOOL_RUBBER;
2139                 else if (wacom_wac->id[0])
2140                         wacom_wac->tool[0] = wacom_intuos_get_tool_type(wacom_wac->id[0]);
2141                 else
2142                         wacom_wac->tool[0] = BTN_TOOL_PEN;
2143         }
2144
2145         /* keep pen state for touch events */
2146         wacom_wac->shared->stylus_in_proximity = range;
2147
2148         if (!delay_pen_events(wacom_wac) && wacom_wac->tool[0]) {
2149                 int id = wacom_wac->id[0];
2150
2151                 /*
2152                  * Non-USI EMR tools should have their IDs mangled to
2153                  * match the legacy behavior of wacom_intuos_general
2154                  */
2155                 if (wacom_wac->serial[0] >> 52 == 1)
2156                         id = wacom_intuos_id_mangle(id);
2157
2158                 /*
2159                  * To ensure compatibility with xf86-input-wacom, we should
2160                  * report the BTN_TOOL_* event prior to the ABS_MISC or
2161                  * MSC_SERIAL events.
2162                  */
2163                 input_report_key(input, BTN_TOUCH,
2164                                 wacom_wac->hid_data.tipswitch);
2165                 input_report_key(input, wacom_wac->tool[0], prox);
2166                 if (wacom_wac->serial[0]) {
2167                         input_event(input, EV_MSC, MSC_SERIAL, wacom_wac->serial[0]);
2168                         input_report_abs(input, ABS_MISC, id);
2169                 }
2170
2171                 wacom_wac->hid_data.tipswitch = false;
2172
2173                 input_sync(input);
2174         }
2175
2176         if (!prox) {
2177                 wacom_wac->tool[0] = 0;
2178                 wacom_wac->id[0] = 0;
2179         }
2180 }
2181
2182 static void wacom_wac_finger_usage_mapping(struct hid_device *hdev,
2183                 struct hid_field *field, struct hid_usage *usage)
2184 {
2185         struct wacom *wacom = hid_get_drvdata(hdev);
2186         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2187         struct input_dev *input = wacom_wac->touch_input;
2188         unsigned touch_max = wacom_wac->features.touch_max;
2189         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2190
2191         switch (equivalent_usage) {
2192         case HID_GD_X:
2193                 if (touch_max == 1)
2194                         wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 4);
2195                 else
2196                         wacom_map_usage(input, usage, field, EV_ABS,
2197                                         ABS_MT_POSITION_X, 4);
2198                 break;
2199         case HID_GD_Y:
2200                 if (touch_max == 1)
2201                         wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 4);
2202                 else
2203                         wacom_map_usage(input, usage, field, EV_ABS,
2204                                         ABS_MT_POSITION_Y, 4);
2205                 break;
2206         case HID_DG_WIDTH:
2207         case HID_DG_HEIGHT:
2208                 wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MAJOR, 0);
2209                 wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MINOR, 0);
2210                 input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
2211                 break;
2212         case HID_DG_TIPSWITCH:
2213                 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
2214                 break;
2215         case HID_DG_CONTACTCOUNT:
2216                 wacom_wac->hid_data.cc_report = field->report->id;
2217                 wacom_wac->hid_data.cc_index = field->index;
2218                 wacom_wac->hid_data.cc_value_index = usage->usage_index;
2219                 break;
2220         case HID_DG_CONTACTID:
2221                 if ((field->logical_maximum - field->logical_minimum) < touch_max) {
2222                         /*
2223                          * The HID descriptor for G11 sensors leaves logical
2224                          * maximum set to '1' despite it being a multitouch
2225                          * device. Override to a sensible number.
2226                          */
2227                         field->logical_maximum = 255;
2228                 }
2229                 break;
2230         }
2231 }
2232
2233 static void wacom_wac_finger_slot(struct wacom_wac *wacom_wac,
2234                 struct input_dev *input)
2235 {
2236         struct hid_data *hid_data = &wacom_wac->hid_data;
2237         bool mt = wacom_wac->features.touch_max > 1;
2238         bool prox = hid_data->tipswitch &&
2239                     report_touch_events(wacom_wac);
2240
2241         if (wacom_wac->shared->has_mute_touch_switch &&
2242             !wacom_wac->shared->is_touch_on) {
2243                 if (!wacom_wac->shared->touch_down)
2244                         return;
2245                 prox = 0;
2246         }
2247
2248         wacom_wac->hid_data.num_received++;
2249         if (wacom_wac->hid_data.num_received > wacom_wac->hid_data.num_expected)
2250                 return;
2251
2252         if (mt) {
2253                 int slot;
2254
2255                 slot = input_mt_get_slot_by_key(input, hid_data->id);
2256                 input_mt_slot(input, slot);
2257                 input_mt_report_slot_state(input, MT_TOOL_FINGER, prox);
2258         }
2259         else {
2260                 input_report_key(input, BTN_TOUCH, prox);
2261         }
2262
2263         if (prox) {
2264                 input_report_abs(input, mt ? ABS_MT_POSITION_X : ABS_X,
2265                                  hid_data->x);
2266                 input_report_abs(input, mt ? ABS_MT_POSITION_Y : ABS_Y,
2267                                  hid_data->y);
2268
2269                 if (test_bit(ABS_MT_TOUCH_MAJOR, input->absbit)) {
2270                         input_report_abs(input, ABS_MT_TOUCH_MAJOR, max(hid_data->width, hid_data->height));
2271                         input_report_abs(input, ABS_MT_TOUCH_MINOR, min(hid_data->width, hid_data->height));
2272                         if (hid_data->width != hid_data->height)
2273                                 input_report_abs(input, ABS_MT_ORIENTATION, hid_data->width <= hid_data->height ? 0 : 1);
2274                 }
2275         }
2276 }
2277
2278 static void wacom_wac_finger_event(struct hid_device *hdev,
2279                 struct hid_field *field, struct hid_usage *usage, __s32 value)
2280 {
2281         struct wacom *wacom = hid_get_drvdata(hdev);
2282         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2283         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2284
2285         switch (equivalent_usage) {
2286         case HID_GD_X:
2287                 wacom_wac->hid_data.x = value;
2288                 break;
2289         case HID_GD_Y:
2290                 wacom_wac->hid_data.y = value;
2291                 break;
2292         case HID_DG_WIDTH:
2293                 wacom_wac->hid_data.width = value;
2294                 break;
2295         case HID_DG_HEIGHT:
2296                 wacom_wac->hid_data.height = value;
2297                 break;
2298         case HID_DG_CONTACTID:
2299                 wacom_wac->hid_data.id = value;
2300                 break;
2301         case HID_DG_TIPSWITCH:
2302                 wacom_wac->hid_data.tipswitch = value;
2303                 break;
2304         }
2305
2306
2307         if (usage->usage_index + 1 == field->report_count) {
2308                 if (equivalent_usage == wacom_wac->hid_data.last_slot_field)
2309                         wacom_wac_finger_slot(wacom_wac, wacom_wac->touch_input);
2310         }
2311 }
2312
2313 static void wacom_wac_finger_pre_report(struct hid_device *hdev,
2314                 struct hid_report *report)
2315 {
2316         struct wacom *wacom = hid_get_drvdata(hdev);
2317         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2318         struct hid_data* hid_data = &wacom_wac->hid_data;
2319         int i;
2320
2321         for (i = 0; i < report->maxfield; i++) {
2322                 struct hid_field *field = report->field[i];
2323                 int j;
2324
2325                 for (j = 0; j < field->maxusage; j++) {
2326                         struct hid_usage *usage = &field->usage[j];
2327                         unsigned int equivalent_usage =
2328                                 wacom_equivalent_usage(usage->hid);
2329
2330                         switch (equivalent_usage) {
2331                         case HID_GD_X:
2332                         case HID_GD_Y:
2333                         case HID_DG_WIDTH:
2334                         case HID_DG_HEIGHT:
2335                         case HID_DG_CONTACTID:
2336                         case HID_DG_INRANGE:
2337                         case HID_DG_INVERT:
2338                         case HID_DG_TIPSWITCH:
2339                                 hid_data->last_slot_field = equivalent_usage;
2340                                 break;
2341                         case HID_DG_CONTACTCOUNT:
2342                                 hid_data->cc_report = report->id;
2343                                 hid_data->cc_index = i;
2344                                 hid_data->cc_value_index = j;
2345                                 break;
2346                         }
2347                 }
2348         }
2349
2350         if (hid_data->cc_report != 0 &&
2351             hid_data->cc_index >= 0) {
2352                 struct hid_field *field = report->field[hid_data->cc_index];
2353                 int value = field->value[hid_data->cc_value_index];
2354                 if (value)
2355                         hid_data->num_expected = value;
2356         }
2357         else {
2358                 hid_data->num_expected = wacom_wac->features.touch_max;
2359         }
2360 }
2361
2362 static void wacom_wac_finger_report(struct hid_device *hdev,
2363                 struct hid_report *report)
2364 {
2365         struct wacom *wacom = hid_get_drvdata(hdev);
2366         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2367         struct input_dev *input = wacom_wac->touch_input;
2368         unsigned touch_max = wacom_wac->features.touch_max;
2369
2370         /* If more packets of data are expected, give us a chance to
2371          * process them rather than immediately syncing a partial
2372          * update.
2373          */
2374         if (wacom_wac->hid_data.num_received < wacom_wac->hid_data.num_expected)
2375                 return;
2376
2377         if (touch_max > 1)
2378                 input_mt_sync_frame(input);
2379
2380         input_sync(input);
2381         wacom_wac->hid_data.num_received = 0;
2382
2383         /* keep touch state for pen event */
2384         wacom_wac->shared->touch_down = wacom_wac_finger_count_touches(wacom_wac);
2385 }
2386
2387 void wacom_wac_usage_mapping(struct hid_device *hdev,
2388                 struct hid_field *field, struct hid_usage *usage)
2389 {
2390         struct wacom *wacom = hid_get_drvdata(hdev);
2391         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2392         struct wacom_features *features = &wacom_wac->features;
2393
2394         if (WACOM_DIRECT_DEVICE(field))
2395                 features->device_type |= WACOM_DEVICETYPE_DIRECT;
2396
2397         if (WACOM_PAD_FIELD(field))
2398                 wacom_wac_pad_usage_mapping(hdev, field, usage);
2399         else if (WACOM_PEN_FIELD(field))
2400                 wacom_wac_pen_usage_mapping(hdev, field, usage);
2401         else if (WACOM_FINGER_FIELD(field))
2402                 wacom_wac_finger_usage_mapping(hdev, field, usage);
2403 }
2404
2405 void wacom_wac_event(struct hid_device *hdev, struct hid_field *field,
2406                 struct hid_usage *usage, __s32 value)
2407 {
2408         struct wacom *wacom = hid_get_drvdata(hdev);
2409
2410         if (wacom->wacom_wac.features.type != HID_GENERIC)
2411                 return;
2412
2413         if (value > field->logical_maximum || value < field->logical_minimum)
2414                 return;
2415
2416         if (WACOM_PAD_FIELD(field)) {
2417                 wacom_wac_pad_battery_event(hdev, field, usage, value);
2418                 if (wacom->wacom_wac.pad_input)
2419                         wacom_wac_pad_event(hdev, field, usage, value);
2420         } else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2421                 wacom_wac_pen_event(hdev, field, usage, value);
2422         else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
2423                 wacom_wac_finger_event(hdev, field, usage, value);
2424 }
2425
2426 static void wacom_report_events(struct hid_device *hdev, struct hid_report *report)
2427 {
2428         int r;
2429
2430         for (r = 0; r < report->maxfield; r++) {
2431                 struct hid_field *field;
2432                 unsigned count, n;
2433
2434                 field = report->field[r];
2435                 count = field->report_count;
2436
2437                 if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
2438                         continue;
2439
2440                 for (n = 0; n < count; n++)
2441                         wacom_wac_event(hdev, field, &field->usage[n], field->value[n]);
2442         }
2443 }
2444
2445 void wacom_wac_report(struct hid_device *hdev, struct hid_report *report)
2446 {
2447         struct wacom *wacom = hid_get_drvdata(hdev);
2448         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2449         struct hid_field *field = report->field[0];
2450
2451         if (wacom_wac->features.type != HID_GENERIC)
2452                 return;
2453
2454         if (WACOM_PAD_FIELD(field) && wacom->wacom_wac.pad_input)
2455                 wacom_wac_pad_pre_report(hdev, report);
2456         else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2457                 wacom_wac_pen_pre_report(hdev, report);
2458         else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
2459                 wacom_wac_finger_pre_report(hdev, report);
2460
2461         wacom_report_events(hdev, report);
2462
2463         /*
2464          * Non-input reports may be sent prior to the device being
2465          * completely initialized. Since only their events need
2466          * to be processed, exit after 'wacom_report_events' has
2467          * been called to prevent potential crashes in the report-
2468          * processing functions.
2469          */
2470         if (report->type != HID_INPUT_REPORT)
2471                 return;
2472
2473         if (WACOM_PAD_FIELD(field)) {
2474                 wacom_wac_pad_battery_report(hdev, report);
2475                 if (wacom->wacom_wac.pad_input)
2476                         wacom_wac_pad_report(hdev, report);
2477         } else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2478                 wacom_wac_pen_report(hdev, report);
2479         else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
2480                 wacom_wac_finger_report(hdev, report);
2481 }
2482
2483 static int wacom_bpt_touch(struct wacom_wac *wacom)
2484 {
2485         struct wacom_features *features = &wacom->features;
2486         struct input_dev *input = wacom->touch_input;
2487         struct input_dev *pad_input = wacom->pad_input;
2488         unsigned char *data = wacom->data;
2489         int i;
2490
2491         if (data[0] != 0x02)
2492             return 0;
2493
2494         for (i = 0; i < 2; i++) {
2495                 int offset = (data[1] & 0x80) ? (8 * i) : (9 * i);
2496                 bool touch = report_touch_events(wacom)
2497                            && (data[offset + 3] & 0x80);
2498
2499                 input_mt_slot(input, i);
2500                 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
2501                 if (touch) {
2502                         int x = get_unaligned_be16(&data[offset + 3]) & 0x7ff;
2503                         int y = get_unaligned_be16(&data[offset + 5]) & 0x7ff;
2504                         if (features->quirks & WACOM_QUIRK_BBTOUCH_LOWRES) {
2505                                 x <<= 5;
2506                                 y <<= 5;
2507                         }
2508                         input_report_abs(input, ABS_MT_POSITION_X, x);
2509                         input_report_abs(input, ABS_MT_POSITION_Y, y);
2510                 }
2511         }
2512
2513         input_mt_sync_frame(input);
2514
2515         input_report_key(pad_input, BTN_LEFT, (data[1] & 0x08) != 0);
2516         input_report_key(pad_input, BTN_FORWARD, (data[1] & 0x04) != 0);
2517         input_report_key(pad_input, BTN_BACK, (data[1] & 0x02) != 0);
2518         input_report_key(pad_input, BTN_RIGHT, (data[1] & 0x01) != 0);
2519         wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
2520
2521         return 1;
2522 }
2523
2524 static void wacom_bpt3_touch_msg(struct wacom_wac *wacom, unsigned char *data)
2525 {
2526         struct wacom_features *features = &wacom->features;
2527         struct input_dev *input = wacom->touch_input;
2528         bool touch = data[1] & 0x80;
2529         int slot = input_mt_get_slot_by_key(input, data[0]);
2530
2531         if (slot < 0)
2532                 return;
2533
2534         touch = touch && report_touch_events(wacom);
2535
2536         input_mt_slot(input, slot);
2537         input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
2538
2539         if (touch) {
2540                 int x = (data[2] << 4) | (data[4] >> 4);
2541                 int y = (data[3] << 4) | (data[4] & 0x0f);
2542                 int width, height;
2543
2544                 if (features->type >= INTUOSPS && features->type <= INTUOSHT2) {
2545                         width  = data[5] * 100;
2546                         height = data[6] * 100;
2547                 } else {
2548                         /*
2549                          * "a" is a scaled-down area which we assume is
2550                          * roughly circular and which can be described as:
2551                          * a=(pi*r^2)/C.
2552                          */
2553                         int a = data[5];
2554                         int x_res = input_abs_get_res(input, ABS_MT_POSITION_X);
2555                         int y_res = input_abs_get_res(input, ABS_MT_POSITION_Y);
2556                         width = 2 * int_sqrt(a * WACOM_CONTACT_AREA_SCALE);
2557                         height = width * y_res / x_res;
2558                 }
2559
2560                 input_report_abs(input, ABS_MT_POSITION_X, x);
2561                 input_report_abs(input, ABS_MT_POSITION_Y, y);
2562                 input_report_abs(input, ABS_MT_TOUCH_MAJOR, width);
2563                 input_report_abs(input, ABS_MT_TOUCH_MINOR, height);
2564         }
2565 }
2566
2567 static void wacom_bpt3_button_msg(struct wacom_wac *wacom, unsigned char *data)
2568 {
2569         struct input_dev *input = wacom->pad_input;
2570         struct wacom_features *features = &wacom->features;
2571
2572         if (features->type == INTUOSHT || features->type == INTUOSHT2) {
2573                 input_report_key(input, BTN_LEFT, (data[1] & 0x02) != 0);
2574                 input_report_key(input, BTN_BACK, (data[1] & 0x08) != 0);
2575         } else {
2576                 input_report_key(input, BTN_BACK, (data[1] & 0x02) != 0);
2577                 input_report_key(input, BTN_LEFT, (data[1] & 0x08) != 0);
2578         }
2579         input_report_key(input, BTN_FORWARD, (data[1] & 0x04) != 0);
2580         input_report_key(input, BTN_RIGHT, (data[1] & 0x01) != 0);
2581 }
2582
2583 static int wacom_bpt3_touch(struct wacom_wac *wacom)
2584 {
2585         unsigned char *data = wacom->data;
2586         int count = data[1] & 0x07;
2587         int  touch_changed = 0, i;
2588
2589         if (data[0] != 0x02)
2590             return 0;
2591
2592         /* data has up to 7 fixed sized 8-byte messages starting at data[2] */
2593         for (i = 0; i < count; i++) {
2594                 int offset = (8 * i) + 2;
2595                 int msg_id = data[offset];
2596
2597                 if (msg_id >= 2 && msg_id <= 17) {
2598                         wacom_bpt3_touch_msg(wacom, data + offset);
2599                         touch_changed++;
2600                 } else if (msg_id == 128)
2601                         wacom_bpt3_button_msg(wacom, data + offset);
2602
2603         }
2604
2605         /* only update touch if we actually have a touchpad and touch data changed */
2606         if (wacom->touch_input && touch_changed) {
2607                 input_mt_sync_frame(wacom->touch_input);
2608                 wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
2609         }
2610
2611         return 1;
2612 }
2613
2614 static int wacom_bpt_pen(struct wacom_wac *wacom)
2615 {
2616         struct wacom_features *features = &wacom->features;
2617         struct input_dev *input = wacom->pen_input;
2618         unsigned char *data = wacom->data;
2619         int prox = 0, x = 0, y = 0, p = 0, d = 0, pen = 0, btn1 = 0, btn2 = 0;
2620
2621         if (data[0] != WACOM_REPORT_PENABLED)
2622             return 0;
2623
2624         prox = (data[1] & 0x20) == 0x20;
2625
2626         /*
2627          * All reports shared between PEN and RUBBER tool must be
2628          * forced to a known starting value (zero) when transitioning to
2629          * out-of-prox.
2630          *
2631          * If not reset then, to userspace, it will look like lost events
2632          * if new tool comes in-prox with same values as previous tool sent.
2633          *
2634          * Hardware does report zero in most out-of-prox cases but not all.
2635          */
2636         if (!wacom->shared->stylus_in_proximity) {
2637                 if (data[1] & 0x08) {
2638                         wacom->tool[0] = BTN_TOOL_RUBBER;
2639                         wacom->id[0] = ERASER_DEVICE_ID;
2640                 } else {
2641                         wacom->tool[0] = BTN_TOOL_PEN;
2642                         wacom->id[0] = STYLUS_DEVICE_ID;
2643                 }
2644         }
2645
2646         wacom->shared->stylus_in_proximity = prox;
2647         if (delay_pen_events(wacom))
2648                 return 0;
2649
2650         if (prox) {
2651                 x = le16_to_cpup((__le16 *)&data[2]);
2652                 y = le16_to_cpup((__le16 *)&data[4]);
2653                 p = le16_to_cpup((__le16 *)&data[6]);
2654                 /*
2655                  * Convert distance from out prox to distance from tablet.
2656                  * distance will be greater than distance_max once
2657                  * touching and applying pressure; do not report negative
2658                  * distance.
2659                  */
2660                 if (data[8] <= features->distance_max)
2661                         d = features->distance_max - data[8];
2662
2663                 pen = data[1] & 0x01;
2664                 btn1 = data[1] & 0x02;
2665                 btn2 = data[1] & 0x04;
2666         } else {
2667                 wacom->id[0] = 0;
2668         }
2669
2670         input_report_key(input, BTN_TOUCH, pen);
2671         input_report_key(input, BTN_STYLUS, btn1);
2672         input_report_key(input, BTN_STYLUS2, btn2);
2673
2674         input_report_abs(input, ABS_X, x);
2675         input_report_abs(input, ABS_Y, y);
2676         input_report_abs(input, ABS_PRESSURE, p);
2677         input_report_abs(input, ABS_DISTANCE, d);
2678
2679         input_report_key(input, wacom->tool[0], prox); /* PEN or RUBBER */
2680         input_report_abs(input, ABS_MISC, wacom->id[0]); /* TOOL ID */
2681
2682         return 1;
2683 }
2684
2685 static int wacom_bpt_irq(struct wacom_wac *wacom, size_t len)
2686 {
2687         struct wacom_features *features = &wacom->features;
2688
2689         if ((features->type == INTUOSHT2) &&
2690             (features->device_type & WACOM_DEVICETYPE_PEN))
2691                 return wacom_intuos_irq(wacom);
2692         else if (len == WACOM_PKGLEN_BBTOUCH)
2693                 return wacom_bpt_touch(wacom);
2694         else if (len == WACOM_PKGLEN_BBTOUCH3)
2695                 return wacom_bpt3_touch(wacom);
2696         else if (len == WACOM_PKGLEN_BBFUN || len == WACOM_PKGLEN_BBPEN)
2697                 return wacom_bpt_pen(wacom);
2698
2699         return 0;
2700 }
2701
2702 static void wacom_bamboo_pad_pen_event(struct wacom_wac *wacom,
2703                 unsigned char *data)
2704 {
2705         unsigned char prefix;
2706
2707         /*
2708          * We need to reroute the event from the debug interface to the
2709          * pen interface.
2710          * We need to add the report ID to the actual pen report, so we
2711          * temporary overwrite the first byte to prevent having to kzalloc/kfree
2712          * and memcpy the report.
2713          */
2714         prefix = data[0];
2715         data[0] = WACOM_REPORT_BPAD_PEN;
2716
2717         /*
2718          * actually reroute the event.
2719          * No need to check if wacom->shared->pen is valid, hid_input_report()
2720          * will check for us.
2721          */
2722         hid_input_report(wacom->shared->pen, HID_INPUT_REPORT, data,
2723                          WACOM_PKGLEN_PENABLED, 1);
2724
2725         data[0] = prefix;
2726 }
2727
2728 static int wacom_bamboo_pad_touch_event(struct wacom_wac *wacom,
2729                 unsigned char *data)
2730 {
2731         struct input_dev *input = wacom->touch_input;
2732         unsigned char *finger_data, prefix;
2733         unsigned id;
2734         int x, y;
2735         bool valid;
2736
2737         prefix = data[0];
2738
2739         for (id = 0; id < wacom->features.touch_max; id++) {
2740                 valid = !!(prefix & BIT(id)) &&
2741                         report_touch_events(wacom);
2742
2743                 input_mt_slot(input, id);
2744                 input_mt_report_slot_state(input, MT_TOOL_FINGER, valid);
2745
2746                 if (!valid)
2747                         continue;
2748
2749                 finger_data = data + 1 + id * 3;
2750                 x = finger_data[0] | ((finger_data[1] & 0x0f) << 8);
2751                 y = (finger_data[2] << 4) | (finger_data[1] >> 4);
2752
2753                 input_report_abs(input, ABS_MT_POSITION_X, x);
2754                 input_report_abs(input, ABS_MT_POSITION_Y, y);
2755         }
2756
2757         input_mt_sync_frame(input);
2758
2759         input_report_key(input, BTN_LEFT, prefix & 0x40);
2760         input_report_key(input, BTN_RIGHT, prefix & 0x80);
2761
2762         /* keep touch state for pen event */
2763         wacom->shared->touch_down = !!prefix && report_touch_events(wacom);
2764
2765         return 1;
2766 }
2767
2768 static int wacom_bamboo_pad_irq(struct wacom_wac *wacom, size_t len)
2769 {
2770         unsigned char *data = wacom->data;
2771
2772         if (!((len == WACOM_PKGLEN_BPAD_TOUCH) ||
2773               (len == WACOM_PKGLEN_BPAD_TOUCH_USB)) ||
2774             (data[0] != WACOM_REPORT_BPAD_TOUCH))
2775                 return 0;
2776
2777         if (data[1] & 0x01)
2778                 wacom_bamboo_pad_pen_event(wacom, &data[1]);
2779
2780         if (data[1] & 0x02)
2781                 return wacom_bamboo_pad_touch_event(wacom, &data[9]);
2782
2783         return 0;
2784 }
2785
2786 static int wacom_wireless_irq(struct wacom_wac *wacom, size_t len)
2787 {
2788         unsigned char *data = wacom->data;
2789         int connected;
2790
2791         if (len != WACOM_PKGLEN_WIRELESS || data[0] != WACOM_REPORT_WL)
2792                 return 0;
2793
2794         connected = data[1] & 0x01;
2795         if (connected) {
2796                 int pid, battery, charging;
2797
2798                 if ((wacom->shared->type == INTUOSHT ||
2799                     wacom->shared->type == INTUOSHT2) &&
2800                     wacom->shared->touch_input &&
2801                     wacom->shared->touch_max) {
2802                         input_report_switch(wacom->shared->touch_input,
2803                                         SW_MUTE_DEVICE, data[5] & 0x40);
2804                         input_sync(wacom->shared->touch_input);
2805                 }
2806
2807                 pid = get_unaligned_be16(&data[6]);
2808                 battery = (data[5] & 0x3f) * 100 / 31;
2809                 charging = !!(data[5] & 0x80);
2810                 if (wacom->pid != pid) {
2811                         wacom->pid = pid;
2812                         wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
2813                 }
2814
2815                 wacom_notify_battery(wacom, battery, charging, 1, 0);
2816
2817         } else if (wacom->pid != 0) {
2818                 /* disconnected while previously connected */
2819                 wacom->pid = 0;
2820                 wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
2821                 wacom_notify_battery(wacom, 0, 0, 0, 0);
2822         }
2823
2824         return 0;
2825 }
2826
2827 static int wacom_status_irq(struct wacom_wac *wacom_wac, size_t len)
2828 {
2829         struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
2830         struct wacom_features *features = &wacom_wac->features;
2831         unsigned char *data = wacom_wac->data;
2832
2833         if (data[0] != WACOM_REPORT_USB)
2834                 return 0;
2835
2836         if ((features->type == INTUOSHT ||
2837             features->type == INTUOSHT2) &&
2838             wacom_wac->shared->touch_input &&
2839             features->touch_max) {
2840                 input_report_switch(wacom_wac->shared->touch_input,
2841                                     SW_MUTE_DEVICE, data[8] & 0x40);
2842                 input_sync(wacom_wac->shared->touch_input);
2843         }
2844
2845         if (data[9] & 0x02) { /* wireless module is attached */
2846                 int battery = (data[8] & 0x3f) * 100 / 31;
2847                 bool charging = !!(data[8] & 0x80);
2848
2849                 wacom_notify_battery(wacom_wac, battery, charging,
2850                                      battery || charging, 1);
2851
2852                 if (!wacom->battery.battery &&
2853                     !(features->quirks & WACOM_QUIRK_BATTERY)) {
2854                         features->quirks |= WACOM_QUIRK_BATTERY;
2855                         wacom_schedule_work(wacom_wac, WACOM_WORKER_BATTERY);
2856                 }
2857         }
2858         else if ((features->quirks & WACOM_QUIRK_BATTERY) &&
2859                  wacom->battery.battery) {
2860                 features->quirks &= ~WACOM_QUIRK_BATTERY;
2861                 wacom_schedule_work(wacom_wac, WACOM_WORKER_BATTERY);
2862                 wacom_notify_battery(wacom_wac, 0, 0, 0, 0);
2863         }
2864         return 0;
2865 }
2866
2867 void wacom_wac_irq(struct wacom_wac *wacom_wac, size_t len)
2868 {
2869         bool sync;
2870
2871         switch (wacom_wac->features.type) {
2872         case PENPARTNER:
2873                 sync = wacom_penpartner_irq(wacom_wac);
2874                 break;
2875
2876         case PL:
2877                 sync = wacom_pl_irq(wacom_wac);
2878                 break;
2879
2880         case WACOM_G4:
2881         case GRAPHIRE:
2882         case GRAPHIRE_BT:
2883         case WACOM_MO:
2884                 sync = wacom_graphire_irq(wacom_wac);
2885                 break;
2886
2887         case PTU:
2888                 sync = wacom_ptu_irq(wacom_wac);
2889                 break;
2890
2891         case DTU:
2892                 sync = wacom_dtu_irq(wacom_wac);
2893                 break;
2894
2895         case DTUS:
2896         case DTUSX:
2897                 sync = wacom_dtus_irq(wacom_wac);
2898                 break;
2899
2900         case INTUOS:
2901         case INTUOS3S:
2902         case INTUOS3:
2903         case INTUOS3L:
2904         case INTUOS4S:
2905         case INTUOS4:
2906         case INTUOS4L:
2907         case CINTIQ:
2908         case WACOM_BEE:
2909         case WACOM_13HD:
2910         case WACOM_21UX2:
2911         case WACOM_22HD:
2912         case WACOM_24HD:
2913         case WACOM_27QHD:
2914         case DTK:
2915         case CINTIQ_HYBRID:
2916         case CINTIQ_COMPANION_2:
2917                 sync = wacom_intuos_irq(wacom_wac);
2918                 break;
2919
2920         case INTUOS4WL:
2921                 sync = wacom_intuos_bt_irq(wacom_wac, len);
2922                 break;
2923
2924         case WACOM_24HDT:
2925         case WACOM_27QHDT:
2926                 sync = wacom_24hdt_irq(wacom_wac);
2927                 break;
2928
2929         case INTUOS5S:
2930         case INTUOS5:
2931         case INTUOS5L:
2932         case INTUOSPS:
2933         case INTUOSPM:
2934         case INTUOSPL:
2935                 if (len == WACOM_PKGLEN_BBTOUCH3)
2936                         sync = wacom_bpt3_touch(wacom_wac);
2937                 else if (wacom_wac->data[0] == WACOM_REPORT_USB)
2938                         sync = wacom_status_irq(wacom_wac, len);
2939                 else
2940                         sync = wacom_intuos_irq(wacom_wac);
2941                 break;
2942
2943         case INTUOSP2_BT:
2944                 sync = wacom_intuos_pro2_bt_irq(wacom_wac, len);
2945                 break;
2946
2947         case TABLETPC:
2948         case TABLETPCE:
2949         case TABLETPC2FG:
2950         case MTSCREEN:
2951         case MTTPC:
2952         case MTTPC_B:
2953                 sync = wacom_tpc_irq(wacom_wac, len);
2954                 break;
2955
2956         case BAMBOO_PT:
2957         case BAMBOO_PEN:
2958         case BAMBOO_TOUCH:
2959         case INTUOSHT:
2960         case INTUOSHT2:
2961                 if (wacom_wac->data[0] == WACOM_REPORT_USB)
2962                         sync = wacom_status_irq(wacom_wac, len);
2963                 else
2964                         sync = wacom_bpt_irq(wacom_wac, len);
2965                 break;
2966
2967         case BAMBOO_PAD:
2968                 sync = wacom_bamboo_pad_irq(wacom_wac, len);
2969                 break;
2970
2971         case WIRELESS:
2972                 sync = wacom_wireless_irq(wacom_wac, len);
2973                 break;
2974
2975         case REMOTE:
2976                 sync = false;
2977                 if (wacom_wac->data[0] == WACOM_REPORT_DEVICE_LIST)
2978                         wacom_remote_status_irq(wacom_wac, len);
2979                 else
2980                         sync = wacom_remote_irq(wacom_wac, len);
2981                 break;
2982
2983         default:
2984                 sync = false;
2985                 break;
2986         }
2987
2988         if (sync) {
2989                 if (wacom_wac->pen_input)
2990                         input_sync(wacom_wac->pen_input);
2991                 if (wacom_wac->touch_input)
2992                         input_sync(wacom_wac->touch_input);
2993                 if (wacom_wac->pad_input)
2994                         input_sync(wacom_wac->pad_input);
2995         }
2996 }
2997
2998 static void wacom_setup_basic_pro_pen(struct wacom_wac *wacom_wac)
2999 {
3000         struct input_dev *input_dev = wacom_wac->pen_input;
3001
3002         input_set_capability(input_dev, EV_MSC, MSC_SERIAL);
3003
3004         __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3005         __set_bit(BTN_STYLUS, input_dev->keybit);
3006         __set_bit(BTN_STYLUS2, input_dev->keybit);
3007
3008         input_set_abs_params(input_dev, ABS_DISTANCE,
3009                              0, wacom_wac->features.distance_max, wacom_wac->features.distance_fuzz, 0);
3010 }
3011
3012 static void wacom_setup_cintiq(struct wacom_wac *wacom_wac)
3013 {
3014         struct input_dev *input_dev = wacom_wac->pen_input;
3015         struct wacom_features *features = &wacom_wac->features;
3016
3017         wacom_setup_basic_pro_pen(wacom_wac);
3018
3019         __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3020         __set_bit(BTN_TOOL_BRUSH, input_dev->keybit);
3021         __set_bit(BTN_TOOL_PENCIL, input_dev->keybit);
3022         __set_bit(BTN_TOOL_AIRBRUSH, input_dev->keybit);
3023
3024         input_set_abs_params(input_dev, ABS_WHEEL, 0, 1023, 0, 0);
3025         input_set_abs_params(input_dev, ABS_TILT_X, -64, 63, features->tilt_fuzz, 0);
3026         input_abs_set_res(input_dev, ABS_TILT_X, 57);
3027         input_set_abs_params(input_dev, ABS_TILT_Y, -64, 63, features->tilt_fuzz, 0);
3028         input_abs_set_res(input_dev, ABS_TILT_Y, 57);
3029 }
3030
3031 static void wacom_setup_intuos(struct wacom_wac *wacom_wac)
3032 {
3033         struct input_dev *input_dev = wacom_wac->pen_input;
3034
3035         input_set_capability(input_dev, EV_REL, REL_WHEEL);
3036
3037         wacom_setup_cintiq(wacom_wac);
3038
3039         __set_bit(BTN_LEFT, input_dev->keybit);
3040         __set_bit(BTN_RIGHT, input_dev->keybit);
3041         __set_bit(BTN_MIDDLE, input_dev->keybit);
3042         __set_bit(BTN_SIDE, input_dev->keybit);
3043         __set_bit(BTN_EXTRA, input_dev->keybit);
3044         __set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
3045         __set_bit(BTN_TOOL_LENS, input_dev->keybit);
3046
3047         input_set_abs_params(input_dev, ABS_RZ, -900, 899, 0, 0);
3048         input_abs_set_res(input_dev, ABS_RZ, 287);
3049         input_set_abs_params(input_dev, ABS_THROTTLE, -1023, 1023, 0, 0);
3050 }
3051
3052 void wacom_setup_device_quirks(struct wacom *wacom)
3053 {
3054         struct wacom_features *features = &wacom->wacom_wac.features;
3055
3056         /* The pen and pad share the same interface on most devices */
3057         if (features->type == GRAPHIRE_BT || features->type == WACOM_G4 ||
3058             features->type == DTUS ||
3059             (features->type >= INTUOS3S && features->type <= WACOM_MO)) {
3060                 if (features->device_type & WACOM_DEVICETYPE_PEN)
3061                         features->device_type |= WACOM_DEVICETYPE_PAD;
3062         }
3063
3064         /* touch device found but size is not defined. use default */
3065         if (features->device_type & WACOM_DEVICETYPE_TOUCH && !features->x_max) {
3066                 features->x_max = 1023;
3067                 features->y_max = 1023;
3068         }
3069
3070         /*
3071          * Intuos5/Pro and Bamboo 3rd gen have no useful data about its
3072          * touch interface in its HID descriptor. If this is the touch
3073          * interface (PacketSize of WACOM_PKGLEN_BBTOUCH3), override the
3074          * tablet values.
3075          */
3076         if ((features->type >= INTUOS5S && features->type <= INTUOSPL) ||
3077                 (features->type >= INTUOSHT && features->type <= BAMBOO_PT)) {
3078                 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
3079                         if (features->touch_max)
3080                                 features->device_type |= WACOM_DEVICETYPE_TOUCH;
3081                         if (features->type >= INTUOSHT && features->type <= BAMBOO_PT)
3082                                 features->device_type |= WACOM_DEVICETYPE_PAD;
3083
3084                         features->x_max = 4096;
3085                         features->y_max = 4096;
3086                 }
3087                 else if (features->pktlen == WACOM_PKGLEN_BBTOUCH) {
3088                         features->device_type |= WACOM_DEVICETYPE_PAD;
3089                 }
3090         }
3091
3092         /*
3093          * Hack for the Bamboo One:
3094          * the device presents a PAD/Touch interface as most Bamboos and even
3095          * sends ghosts PAD data on it. However, later, we must disable this
3096          * ghost interface, and we can not detect it unless we set it here
3097          * to WACOM_DEVICETYPE_PAD or WACOM_DEVICETYPE_TOUCH.
3098          */
3099         if (features->type == BAMBOO_PEN &&
3100             features->pktlen == WACOM_PKGLEN_BBTOUCH3)
3101                 features->device_type |= WACOM_DEVICETYPE_PAD;
3102
3103         /*
3104          * Raw Wacom-mode pen and touch events both come from interface
3105          * 0, whose HID descriptor has an application usage of 0xFF0D
3106          * (i.e., WACOM_HID_WD_DIGITIZER). We route pen packets back
3107          * out through the HID_GENERIC device created for interface 1,
3108          * so rewrite this one to be of type WACOM_DEVICETYPE_TOUCH.
3109          */
3110         if (features->type == BAMBOO_PAD)
3111                 features->device_type = WACOM_DEVICETYPE_TOUCH;
3112
3113         if (features->type == REMOTE)
3114                 features->device_type = WACOM_DEVICETYPE_PAD;
3115
3116         if (features->type == INTUOSP2_BT) {
3117                 features->device_type |= WACOM_DEVICETYPE_PEN |
3118                                          WACOM_DEVICETYPE_PAD |
3119                                          WACOM_DEVICETYPE_TOUCH;
3120                 features->quirks |= WACOM_QUIRK_BATTERY;
3121         }
3122
3123         switch (features->type) {
3124         case PL:
3125         case DTU:
3126         case DTUS:
3127         case DTUSX:
3128         case WACOM_21UX2:
3129         case WACOM_22HD:
3130         case DTK:
3131         case WACOM_24HD:
3132         case WACOM_27QHD:
3133         case CINTIQ_HYBRID:
3134         case CINTIQ_COMPANION_2:
3135         case CINTIQ:
3136         case WACOM_BEE:
3137         case WACOM_13HD:
3138         case WACOM_24HDT:
3139         case WACOM_27QHDT:
3140         case TABLETPC:
3141         case TABLETPCE:
3142         case TABLETPC2FG:
3143         case MTSCREEN:
3144         case MTTPC:
3145         case MTTPC_B:
3146                 features->device_type |= WACOM_DEVICETYPE_DIRECT;
3147                 break;
3148         }
3149
3150         if (wacom->hdev->bus == BUS_BLUETOOTH)
3151                 features->quirks |= WACOM_QUIRK_BATTERY;
3152
3153         /* quirk for bamboo touch with 2 low res touches */
3154         if ((features->type == BAMBOO_PT || features->type == BAMBOO_TOUCH) &&
3155             features->pktlen == WACOM_PKGLEN_BBTOUCH) {
3156                 features->x_max <<= 5;
3157                 features->y_max <<= 5;
3158                 features->x_fuzz <<= 5;
3159                 features->y_fuzz <<= 5;
3160                 features->quirks |= WACOM_QUIRK_BBTOUCH_LOWRES;
3161         }
3162
3163         if (features->type == WIRELESS) {
3164                 if (features->device_type == WACOM_DEVICETYPE_WL_MONITOR) {
3165                         features->quirks |= WACOM_QUIRK_BATTERY;
3166                 }
3167         }
3168
3169         if (features->type == REMOTE)
3170                 features->device_type |= WACOM_DEVICETYPE_WL_MONITOR;
3171 }
3172
3173 int wacom_setup_pen_input_capabilities(struct input_dev *input_dev,
3174                                    struct wacom_wac *wacom_wac)
3175 {
3176         struct wacom_features *features = &wacom_wac->features;
3177
3178         input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
3179
3180         if (!(features->device_type & WACOM_DEVICETYPE_PEN))
3181                 return -ENODEV;
3182
3183         if (features->device_type & WACOM_DEVICETYPE_DIRECT)
3184                 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
3185         else
3186                 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
3187
3188         if (features->type == HID_GENERIC)
3189                 /* setup has already been done */
3190                 return 0;
3191
3192         __set_bit(BTN_TOUCH, input_dev->keybit);
3193         __set_bit(ABS_MISC, input_dev->absbit);
3194
3195         input_set_abs_params(input_dev, ABS_X, 0 + features->offset_left,
3196                              features->x_max - features->offset_right,
3197                              features->x_fuzz, 0);
3198         input_set_abs_params(input_dev, ABS_Y, 0 + features->offset_top,
3199                              features->y_max - features->offset_bottom,
3200                              features->y_fuzz, 0);
3201         input_set_abs_params(input_dev, ABS_PRESSURE, 0,
3202                 features->pressure_max, features->pressure_fuzz, 0);
3203
3204         /* penabled devices have fixed resolution for each model */
3205         input_abs_set_res(input_dev, ABS_X, features->x_resolution);
3206         input_abs_set_res(input_dev, ABS_Y, features->y_resolution);
3207
3208         switch (features->type) {
3209         case GRAPHIRE_BT:
3210                 __clear_bit(ABS_MISC, input_dev->absbit);
3211
3212         case WACOM_MO:
3213         case WACOM_G4:
3214                 input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3215                                               features->distance_max,
3216                                               features->distance_fuzz, 0);
3217                 /* fall through */
3218
3219         case GRAPHIRE:
3220                 input_set_capability(input_dev, EV_REL, REL_WHEEL);
3221
3222                 __set_bit(BTN_LEFT, input_dev->keybit);
3223                 __set_bit(BTN_RIGHT, input_dev->keybit);
3224                 __set_bit(BTN_MIDDLE, input_dev->keybit);
3225
3226                 __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3227                 __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3228                 __set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
3229                 __set_bit(BTN_STYLUS, input_dev->keybit);
3230                 __set_bit(BTN_STYLUS2, input_dev->keybit);
3231                 break;
3232
3233         case WACOM_27QHD:
3234         case WACOM_24HD:
3235         case DTK:
3236         case WACOM_22HD:
3237         case WACOM_21UX2:
3238         case WACOM_BEE:
3239         case CINTIQ:
3240         case WACOM_13HD:
3241         case CINTIQ_HYBRID:
3242         case CINTIQ_COMPANION_2:
3243                 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3244                 input_abs_set_res(input_dev, ABS_Z, 287);
3245                 wacom_setup_cintiq(wacom_wac);
3246                 break;
3247
3248         case INTUOS3:
3249         case INTUOS3L:
3250         case INTUOS3S:
3251         case INTUOS4:
3252         case INTUOS4WL:
3253         case INTUOS4L:
3254         case INTUOS4S:
3255                 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3256                 input_abs_set_res(input_dev, ABS_Z, 287);
3257                 /* fall through */
3258
3259         case INTUOS:
3260                 wacom_setup_intuos(wacom_wac);
3261                 break;
3262
3263         case INTUOS5:
3264         case INTUOS5L:
3265         case INTUOSPM:
3266         case INTUOSPL:
3267         case INTUOS5S:
3268         case INTUOSPS:
3269         case INTUOSP2_BT:
3270                 input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3271                                       features->distance_max,
3272                                       features->distance_fuzz, 0);
3273
3274                 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
3275                 input_abs_set_res(input_dev, ABS_Z, 287);
3276
3277                 wacom_setup_intuos(wacom_wac);
3278                 break;
3279
3280         case WACOM_24HDT:
3281         case WACOM_27QHDT:
3282         case MTSCREEN:
3283         case MTTPC:
3284         case MTTPC_B:
3285         case TABLETPC2FG:
3286         case TABLETPC:
3287         case TABLETPCE:
3288                 __clear_bit(ABS_MISC, input_dev->absbit);
3289                 /* fall through */
3290
3291         case DTUS:
3292         case DTUSX:
3293         case PL:
3294         case DTU:
3295                 __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3296                 __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3297                 __set_bit(BTN_STYLUS, input_dev->keybit);
3298                 __set_bit(BTN_STYLUS2, input_dev->keybit);
3299                 break;
3300
3301         case PTU:
3302                 __set_bit(BTN_STYLUS2, input_dev->keybit);
3303                 /* fall through */
3304
3305         case PENPARTNER:
3306                 __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3307                 __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3308                 __set_bit(BTN_STYLUS, input_dev->keybit);
3309                 break;
3310
3311         case INTUOSHT:
3312         case BAMBOO_PT:
3313         case BAMBOO_PEN:
3314         case INTUOSHT2:
3315                 if (features->type == INTUOSHT2) {
3316                         wacom_setup_basic_pro_pen(wacom_wac);
3317                 } else {
3318                         __clear_bit(ABS_MISC, input_dev->absbit);
3319                         __set_bit(BTN_TOOL_PEN, input_dev->keybit);
3320                         __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
3321                         __set_bit(BTN_STYLUS, input_dev->keybit);
3322                         __set_bit(BTN_STYLUS2, input_dev->keybit);
3323                         input_set_abs_params(input_dev, ABS_DISTANCE, 0,
3324                                       features->distance_max,
3325                                       features->distance_fuzz, 0);
3326                 }
3327                 break;
3328         case BAMBOO_PAD:
3329                 __clear_bit(ABS_MISC, input_dev->absbit);
3330                 break;
3331         }
3332         return 0;
3333 }
3334
3335 int wacom_setup_touch_input_capabilities(struct input_dev *input_dev,
3336                                          struct wacom_wac *wacom_wac)
3337 {
3338         struct wacom_features *features = &wacom_wac->features;
3339
3340         input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
3341
3342         if (!(features->device_type & WACOM_DEVICETYPE_TOUCH))
3343                 return -ENODEV;
3344
3345         if (features->device_type & WACOM_DEVICETYPE_DIRECT)
3346                 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
3347         else
3348                 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
3349
3350         if (features->type == HID_GENERIC)
3351                 /* setup has already been done */
3352                 return 0;
3353
3354         __set_bit(BTN_TOUCH, input_dev->keybit);
3355
3356         if (features->touch_max == 1) {
3357                 input_set_abs_params(input_dev, ABS_X, 0,
3358                         features->x_max, features->x_fuzz, 0);
3359                 input_set_abs_params(input_dev, ABS_Y, 0,
3360                         features->y_max, features->y_fuzz, 0);
3361                 input_abs_set_res(input_dev, ABS_X,
3362                                   features->x_resolution);
3363                 input_abs_set_res(input_dev, ABS_Y,
3364                                   features->y_resolution);
3365         }
3366         else if (features->touch_max > 1) {
3367                 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
3368                         features->x_max, features->x_fuzz, 0);
3369                 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
3370                         features->y_max, features->y_fuzz, 0);
3371                 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
3372                                   features->x_resolution);
3373                 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
3374                                   features->y_resolution);
3375         }
3376
3377         switch (features->type) {
3378         case INTUOSP2_BT:
3379                 input_dev->evbit[0] |= BIT_MASK(EV_SW);
3380                 __set_bit(SW_MUTE_DEVICE, input_dev->swbit);
3381
3382                 if (wacom_wac->shared->touch->product == 0x361) {
3383                         input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3384                                              0, 12440, 4, 0);
3385                         input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
3386                                              0, 8640, 4, 0);
3387                 }
3388                 else if (wacom_wac->shared->touch->product == 0x360) {
3389                         input_set_abs_params(input_dev, ABS_MT_POSITION_X,
3390                                              0, 8960, 4, 0);
3391                         input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
3392                                              0, 5920, 4, 0);
3393                 }
3394                 input_abs_set_res(input_dev, ABS_MT_POSITION_X, 40);
3395                 input_abs_set_res(input_dev, ABS_MT_POSITION_X, 40);
3396
3397                 /* fall through */
3398
3399         case INTUOS5:
3400         case INTUOS5L:
3401         case INTUOSPM:
3402         case INTUOSPL:
3403         case INTUOS5S:
3404         case INTUOSPS:
3405                 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, features->x_max, 0, 0);
3406                 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0, features->y_max, 0, 0);
3407                 input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_POINTER);
3408                 break;
3409
3410         case WACOM_24HDT:
3411                 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, features->x_max, 0, 0);
3412                 input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, features->x_max, 0, 0);
3413                 input_set_abs_params(input_dev, ABS_MT_WIDTH_MINOR, 0, features->y_max, 0, 0);
3414                 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
3415                 /* fall through */
3416
3417         case WACOM_27QHDT:
3418         case MTSCREEN:
3419         case MTTPC:
3420         case MTTPC_B:
3421         case TABLETPC2FG:
3422                 input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_DIRECT);
3423                 /*fall through */
3424
3425         case TABLETPC:
3426         case TABLETPCE:
3427                 break;
3428
3429         case INTUOSHT:
3430         case INTUOSHT2:
3431                 input_dev->evbit[0] |= BIT_MASK(EV_SW);
3432                 __set_bit(SW_MUTE_DEVICE, input_dev->swbit);
3433                 /* fall through */
3434
3435         case BAMBOO_PT:
3436         case BAMBOO_TOUCH:
3437                 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
3438                         input_set_abs_params(input_dev,
3439                                      ABS_MT_TOUCH_MAJOR,
3440                                      0, features->x_max, 0, 0);
3441                         input_set_abs_params(input_dev,
3442                                      ABS_MT_TOUCH_MINOR,
3443                                      0, features->y_max, 0, 0);
3444                 }
3445                 input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_POINTER);
3446                 break;
3447
3448         case BAMBOO_PAD:
3449                 input_mt_init_slots(input_dev, features->touch_max,
3450                                     INPUT_MT_POINTER);
3451                 __set_bit(BTN_LEFT, input_dev->keybit);
3452                 __set_bit(BTN_RIGHT, input_dev->keybit);
3453                 break;
3454         }
3455         return 0;
3456 }
3457
3458 static int wacom_numbered_button_to_key(int n)
3459 {
3460         if (n < 10)
3461                 return BTN_0 + n;
3462         else if (n < 16)
3463                 return BTN_A + (n-10);
3464         else if (n < 18)
3465                 return BTN_BASE + (n-16);
3466         else
3467                 return 0;
3468 }
3469
3470 static void wacom_setup_numbered_buttons(struct input_dev *input_dev,
3471                                 int button_count)
3472 {
3473         int i;
3474
3475         for (i = 0; i < button_count; i++) {
3476                 int key = wacom_numbered_button_to_key(i);
3477
3478                 if (key)
3479                         __set_bit(key, input_dev->keybit);
3480         }
3481 }
3482
3483 static void wacom_24hd_update_leds(struct wacom *wacom, int mask, int group)
3484 {
3485         struct wacom_led *led;
3486         int i;
3487         bool updated = false;
3488
3489         /*
3490          * 24HD has LED group 1 to the left and LED group 0 to the right.
3491          * So group 0 matches the second half of the buttons and thus the mask
3492          * needs to be shifted.
3493          */
3494         if (group == 0)
3495                 mask >>= 8;
3496
3497         for (i = 0; i < 3; i++) {
3498                 led = wacom_led_find(wacom, group, i);
3499                 if (!led) {
3500                         hid_err(wacom->hdev, "can't find LED %d in group %d\n",
3501                                 i, group);
3502                         continue;
3503                 }
3504                 if (!updated && mask & BIT(i)) {
3505                         led->held = true;
3506                         led_trigger_event(&led->trigger, LED_FULL);
3507                 } else {
3508                         led->held = false;
3509                 }
3510         }
3511 }
3512
3513 static bool wacom_is_led_toggled(struct wacom *wacom, int button_count,
3514                                  int mask, int group)
3515 {
3516         int button_per_group;
3517
3518         /*
3519          * 21UX2 has LED group 1 to the left and LED group 0
3520          * to the right. We need to reverse the group to match this
3521          * historical behavior.
3522          */
3523         if (wacom->wacom_wac.features.type == WACOM_21UX2)
3524                 group = 1 - group;
3525
3526         button_per_group = button_count/wacom->led.count;
3527
3528         return mask & (1 << (group * button_per_group));
3529 }
3530
3531 static void wacom_update_led(struct wacom *wacom, int button_count, int mask,
3532                              int group)
3533 {
3534         struct wacom_led *led, *next_led;
3535         int cur;
3536         bool pressed;
3537
3538         if (wacom->wacom_wac.features.type == WACOM_24HD)
3539                 return wacom_24hd_update_leds(wacom, mask, group);
3540
3541         pressed = wacom_is_led_toggled(wacom, button_count, mask, group);
3542         cur = wacom->led.groups[group].select;
3543
3544         led = wacom_led_find(wacom, group, cur);
3545         if (!led) {
3546                 hid_err(wacom->hdev, "can't find current LED %d in group %d\n",
3547                         cur, group);
3548                 return;
3549         }
3550
3551         if (!pressed) {
3552                 led->held = false;
3553                 return;
3554         }
3555
3556         if (led->held && pressed)
3557                 return;
3558
3559         next_led = wacom_led_next(wacom, led);
3560         if (!next_led) {
3561                 hid_err(wacom->hdev, "can't find next LED in group %d\n",
3562                         group);
3563                 return;
3564         }
3565         if (next_led == led)
3566                 return;
3567
3568         next_led->held = true;
3569         led_trigger_event(&next_led->trigger,
3570                           wacom_leds_brightness_get(next_led));
3571 }
3572
3573 static void wacom_report_numbered_buttons(struct input_dev *input_dev,
3574                                 int button_count, int mask)
3575 {
3576         struct wacom *wacom = input_get_drvdata(input_dev);
3577         int i;
3578
3579         for (i = 0; i < wacom->led.count; i++)
3580                 wacom_update_led(wacom,  button_count, mask, i);
3581
3582         for (i = 0; i < button_count; i++) {
3583                 int key = wacom_numbered_button_to_key(i);
3584
3585                 if (key)
3586                         input_report_key(input_dev, key, mask & (1 << i));
3587         }
3588 }
3589
3590 int wacom_setup_pad_input_capabilities(struct input_dev *input_dev,
3591                                    struct wacom_wac *wacom_wac)
3592 {
3593         struct wacom_features *features = &wacom_wac->features;
3594
3595         if ((features->type == HID_GENERIC) && features->numbered_buttons > 0)
3596                 features->device_type |= WACOM_DEVICETYPE_PAD;
3597
3598         if (!(features->device_type & WACOM_DEVICETYPE_PAD))
3599                 return -ENODEV;
3600
3601         if (features->type == REMOTE && input_dev == wacom_wac->pad_input)
3602                 return -ENODEV;
3603
3604         input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
3605
3606         /* kept for making legacy xf86-input-wacom working with the wheels */
3607         __set_bit(ABS_MISC, input_dev->absbit);
3608
3609         /* kept for making legacy xf86-input-wacom accepting the pad */
3610         if (!(input_dev->absinfo && (input_dev->absinfo[ABS_X].minimum ||
3611               input_dev->absinfo[ABS_X].maximum)))
3612                 input_set_abs_params(input_dev, ABS_X, 0, 1, 0, 0);
3613         if (!(input_dev->absinfo && (input_dev->absinfo[ABS_Y].minimum ||
3614               input_dev->absinfo[ABS_Y].maximum)))
3615                 input_set_abs_params(input_dev, ABS_Y, 0, 1, 0, 0);
3616
3617         /* kept for making udev and libwacom accepting the pad */
3618         __set_bit(BTN_STYLUS, input_dev->keybit);
3619
3620         wacom_setup_numbered_buttons(input_dev, features->numbered_buttons);
3621
3622         switch (features->type) {
3623
3624         case CINTIQ_HYBRID:
3625         case CINTIQ_COMPANION_2:
3626         case DTK:
3627         case DTUS:
3628         case GRAPHIRE_BT:
3629                 break;
3630
3631         case WACOM_MO:
3632                 __set_bit(BTN_BACK, input_dev->keybit);
3633                 __set_bit(BTN_LEFT, input_dev->keybit);
3634                 __set_bit(BTN_FORWARD, input_dev->keybit);
3635                 __set_bit(BTN_RIGHT, input_dev->keybit);
3636                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
3637                 break;
3638
3639         case WACOM_G4:
3640                 __set_bit(BTN_BACK, input_dev->keybit);
3641                 __set_bit(BTN_FORWARD, input_dev->keybit);
3642                 input_set_capability(input_dev, EV_REL, REL_WHEEL);
3643                 break;
3644
3645         case WACOM_24HD:
3646                 __set_bit(KEY_PROG1, input_dev->keybit);
3647                 __set_bit(KEY_PROG2, input_dev->keybit);
3648                 __set_bit(KEY_PROG3, input_dev->keybit);
3649
3650                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
3651                 input_set_abs_params(input_dev, ABS_THROTTLE, 0, 71, 0, 0);
3652                 break;
3653
3654         case WACOM_27QHD:
3655                 __set_bit(KEY_PROG1, input_dev->keybit);
3656                 __set_bit(KEY_PROG2, input_dev->keybit);
3657                 __set_bit(KEY_PROG3, input_dev->keybit);
3658                 input_set_abs_params(input_dev, ABS_X, -2048, 2048, 0, 0);
3659                 input_abs_set_res(input_dev, ABS_X, 1024); /* points/g */
3660                 input_set_abs_params(input_dev, ABS_Y, -2048, 2048, 0, 0);
3661                 input_abs_set_res(input_dev, ABS_Y, 1024);
3662                 input_set_abs_params(input_dev, ABS_Z, -2048, 2048, 0, 0);
3663                 input_abs_set_res(input_dev, ABS_Z, 1024);
3664                 __set_bit(INPUT_PROP_ACCELEROMETER, input_dev->propbit);
3665                 break;
3666
3667         case WACOM_22HD:
3668                 __set_bit(KEY_PROG1, input_dev->keybit);
3669                 __set_bit(KEY_PROG2, input_dev->keybit);
3670                 __set_bit(KEY_PROG3, input_dev->keybit);
3671                 /* fall through */
3672
3673         case WACOM_21UX2:
3674         case WACOM_BEE:
3675         case CINTIQ:
3676                 input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
3677                 input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
3678                 break;
3679
3680         case WACOM_13HD:
3681                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
3682                 break;
3683
3684         case INTUOS3:
3685         case INTUOS3L:
3686                 input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
3687                 /* fall through */
3688
3689         case INTUOS3S:
3690                 input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
3691                 break;
3692
3693         case INTUOS5:
3694         case INTUOS5L:
3695         case INTUOSPM:
3696         case INTUOSPL:
3697         case INTUOS5S:
3698         case INTUOSPS:
3699         case INTUOSP2_BT:
3700                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
3701                 break;
3702
3703         case INTUOS4WL:
3704                 /*
3705                  * For Bluetooth devices, the udev rule does not work correctly
3706                  * for pads unless we add a stylus capability, which forces
3707                  * ID_INPUT_TABLET to be set.
3708                  */
3709                 __set_bit(BTN_STYLUS, input_dev->keybit);
3710                 /* fall through */
3711
3712         case INTUOS4:
3713         case INTUOS4L:
3714         case INTUOS4S:
3715                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
3716                 break;
3717
3718         case INTUOSHT:
3719         case BAMBOO_PT:
3720         case BAMBOO_TOUCH:
3721         case INTUOSHT2:
3722                 __clear_bit(ABS_MISC, input_dev->absbit);
3723
3724                 __set_bit(BTN_LEFT, input_dev->keybit);
3725                 __set_bit(BTN_FORWARD, input_dev->keybit);
3726                 __set_bit(BTN_BACK, input_dev->keybit);
3727                 __set_bit(BTN_RIGHT, input_dev->keybit);
3728
3729                 break;
3730
3731         case REMOTE:
3732                 input_set_capability(input_dev, EV_MSC, MSC_SERIAL);
3733                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
3734                 break;
3735
3736         case HID_GENERIC:
3737                 break;
3738
3739         default:
3740                 /* no pad supported */
3741                 return -ENODEV;
3742         }
3743         return 0;
3744 }
3745
3746 static const struct wacom_features wacom_features_0x00 =
3747         { "Wacom Penpartner", 5040, 3780, 255, 0,
3748           PENPARTNER, WACOM_PENPRTN_RES, WACOM_PENPRTN_RES };
3749 static const struct wacom_features wacom_features_0x10 =
3750         { "Wacom Graphire", 10206, 7422, 511, 63,
3751           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3752 static const struct wacom_features wacom_features_0x81 =
3753         { "Wacom Graphire BT", 16704, 12064, 511, 32,
3754           GRAPHIRE_BT, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES, 2 };
3755 static const struct wacom_features wacom_features_0x11 =
3756         { "Wacom Graphire2 4x5", 10206, 7422, 511, 63,
3757           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3758 static const struct wacom_features wacom_features_0x12 =
3759         { "Wacom Graphire2 5x7", 13918, 10206, 511, 63,
3760           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3761 static const struct wacom_features wacom_features_0x13 =
3762         { "Wacom Graphire3", 10208, 7424, 511, 63,
3763           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3764 static const struct wacom_features wacom_features_0x14 =
3765         { "Wacom Graphire3 6x8", 16704, 12064, 511, 63,
3766           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3767 static const struct wacom_features wacom_features_0x15 =
3768         { "Wacom Graphire4 4x5", 10208, 7424, 511, 63,
3769           WACOM_G4, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3770 static const struct wacom_features wacom_features_0x16 =
3771         { "Wacom Graphire4 6x8", 16704, 12064, 511, 63,
3772           WACOM_G4, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3773 static const struct wacom_features wacom_features_0x17 =
3774         { "Wacom BambooFun 4x5", 14760, 9225, 511, 63,
3775           WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3776 static const struct wacom_features wacom_features_0x18 =
3777         { "Wacom BambooFun 6x8", 21648, 13530, 511, 63,
3778           WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3779 static const struct wacom_features wacom_features_0x19 =
3780         { "Wacom Bamboo1 Medium", 16704, 12064, 511, 63,
3781           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3782 static const struct wacom_features wacom_features_0x60 =
3783         { "Wacom Volito", 5104, 3712, 511, 63,
3784           GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
3785 static const struct wacom_features wacom_features_0x61 =
3786         { "Wacom PenStation2", 3250, 2320, 255, 63,
3787           GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
3788 static const struct wacom_features wacom_features_0x62 =
3789         { "Wacom Volito2 4x5", 5104, 3712, 511, 63,
3790           GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
3791 static const struct wacom_features wacom_features_0x63 =
3792         { "Wacom Volito2 2x3", 3248, 2320, 511, 63,
3793           GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
3794 static const struct wacom_features wacom_features_0x64 =
3795         { "Wacom PenPartner2", 3250, 2320, 511, 63,
3796           GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
3797 static const struct wacom_features wacom_features_0x65 =
3798         { "Wacom Bamboo", 14760, 9225, 511, 63,
3799           WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3800 static const struct wacom_features wacom_features_0x69 =
3801         { "Wacom Bamboo1", 5104, 3712, 511, 63,
3802           GRAPHIRE, WACOM_PENPRTN_RES, WACOM_PENPRTN_RES };
3803 static const struct wacom_features wacom_features_0x6A =
3804         { "Wacom Bamboo1 4x6", 14760, 9225, 1023, 63,
3805           GRAPHIRE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3806 static const struct wacom_features wacom_features_0x6B =
3807         { "Wacom Bamboo1 5x8", 21648, 13530, 1023, 63,
3808           GRAPHIRE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3809 static const struct wacom_features wacom_features_0x20 =
3810         { "Wacom Intuos 4x5", 12700, 10600, 1023, 31,
3811           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3812 static const struct wacom_features wacom_features_0x21 =
3813         { "Wacom Intuos 6x8", 20320, 16240, 1023, 31,
3814           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3815 static const struct wacom_features wacom_features_0x22 =
3816         { "Wacom Intuos 9x12", 30480, 24060, 1023, 31,
3817           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3818 static const struct wacom_features wacom_features_0x23 =
3819         { "Wacom Intuos 12x12", 30480, 31680, 1023, 31,
3820           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3821 static const struct wacom_features wacom_features_0x24 =
3822         { "Wacom Intuos 12x18", 45720, 31680, 1023, 31,
3823           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3824 static const struct wacom_features wacom_features_0x30 =
3825         { "Wacom PL400", 5408, 4056, 255, 0,
3826           PL, WACOM_PL_RES, WACOM_PL_RES };
3827 static const struct wacom_features wacom_features_0x31 =
3828         { "Wacom PL500", 6144, 4608, 255, 0,
3829           PL, WACOM_PL_RES, WACOM_PL_RES };
3830 static const struct wacom_features wacom_features_0x32 =
3831         { "Wacom PL600", 6126, 4604, 255, 0,
3832           PL, WACOM_PL_RES, WACOM_PL_RES };
3833 static const struct wacom_features wacom_features_0x33 =
3834         { "Wacom PL600SX", 6260, 5016, 255, 0,
3835           PL, WACOM_PL_RES, WACOM_PL_RES };
3836 static const struct wacom_features wacom_features_0x34 =
3837         { "Wacom PL550", 6144, 4608, 511, 0,
3838           PL, WACOM_PL_RES, WACOM_PL_RES };
3839 static const struct wacom_features wacom_features_0x35 =
3840         { "Wacom PL800", 7220, 5780, 511, 0,
3841           PL, WACOM_PL_RES, WACOM_PL_RES };
3842 static const struct wacom_features wacom_features_0x37 =
3843         { "Wacom PL700", 6758, 5406, 511, 0,
3844           PL, WACOM_PL_RES, WACOM_PL_RES };
3845 static const struct wacom_features wacom_features_0x38 =
3846         { "Wacom PL510", 6282, 4762, 511, 0,
3847           PL, WACOM_PL_RES, WACOM_PL_RES };
3848 static const struct wacom_features wacom_features_0x39 =
3849         { "Wacom DTU710", 34080, 27660, 511, 0,
3850           PL, WACOM_PL_RES, WACOM_PL_RES };
3851 static const struct wacom_features wacom_features_0xC4 =
3852         { "Wacom DTF521", 6282, 4762, 511, 0,
3853           PL, WACOM_PL_RES, WACOM_PL_RES };
3854 static const struct wacom_features wacom_features_0xC0 =
3855         { "Wacom DTF720", 6858, 5506, 511, 0,
3856           PL, WACOM_PL_RES, WACOM_PL_RES };
3857 static const struct wacom_features wacom_features_0xC2 =
3858         { "Wacom DTF720a", 6858, 5506, 511, 0,
3859           PL, WACOM_PL_RES, WACOM_PL_RES };
3860 static const struct wacom_features wacom_features_0x03 =
3861         { "Wacom Cintiq Partner", 20480, 15360, 511, 0,
3862           PTU, WACOM_PL_RES, WACOM_PL_RES };
3863 static const struct wacom_features wacom_features_0x41 =
3864         { "Wacom Intuos2 4x5", 12700, 10600, 1023, 31,
3865           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3866 static const struct wacom_features wacom_features_0x42 =
3867         { "Wacom Intuos2 6x8", 20320, 16240, 1023, 31,
3868           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3869 static const struct wacom_features wacom_features_0x43 =
3870         { "Wacom Intuos2 9x12", 30480, 24060, 1023, 31,
3871           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3872 static const struct wacom_features wacom_features_0x44 =
3873         { "Wacom Intuos2 12x12", 30480, 31680, 1023, 31,
3874           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3875 static const struct wacom_features wacom_features_0x45 =
3876         { "Wacom Intuos2 12x18", 45720, 31680, 1023, 31,
3877           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3878 static const struct wacom_features wacom_features_0xB0 =
3879         { "Wacom Intuos3 4x5", 25400, 20320, 1023, 63,
3880           INTUOS3S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 4 };
3881 static const struct wacom_features wacom_features_0xB1 =
3882         { "Wacom Intuos3 6x8", 40640, 30480, 1023, 63,
3883           INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3884 static const struct wacom_features wacom_features_0xB2 =
3885         { "Wacom Intuos3 9x12", 60960, 45720, 1023, 63,
3886           INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3887 static const struct wacom_features wacom_features_0xB3 =
3888         { "Wacom Intuos3 12x12", 60960, 60960, 1023, 63,
3889           INTUOS3L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3890 static const struct wacom_features wacom_features_0xB4 =
3891         { "Wacom Intuos3 12x19", 97536, 60960, 1023, 63,
3892           INTUOS3L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3893 static const struct wacom_features wacom_features_0xB5 =
3894         { "Wacom Intuos3 6x11", 54204, 31750, 1023, 63,
3895           INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3896 static const struct wacom_features wacom_features_0xB7 =
3897         { "Wacom Intuos3 4x6", 31496, 19685, 1023, 63,
3898           INTUOS3S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 4 };
3899 static const struct wacom_features wacom_features_0xB8 =
3900         { "Wacom Intuos4 4x6", 31496, 19685, 2047, 63,
3901           INTUOS4S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7 };
3902 static const struct wacom_features wacom_features_0xB9 =
3903         { "Wacom Intuos4 6x9", 44704, 27940, 2047, 63,
3904           INTUOS4, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3905 static const struct wacom_features wacom_features_0xBA =
3906         { "Wacom Intuos4 8x13", 65024, 40640, 2047, 63,
3907           INTUOS4L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3908 static const struct wacom_features wacom_features_0xBB =
3909         { "Wacom Intuos4 12x19", 97536, 60960, 2047, 63,
3910           INTUOS4L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3911 static const struct wacom_features wacom_features_0xBC =
3912         { "Wacom Intuos4 WL", 40640, 25400, 2047, 63,
3913           INTUOS4, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3914 static const struct wacom_features wacom_features_0xBD =
3915         { "Wacom Intuos4 WL", 40640, 25400, 2047, 63,
3916           INTUOS4WL, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3917 static const struct wacom_features wacom_features_0x26 =
3918         { "Wacom Intuos5 touch S", 31496, 19685, 2047, 63,
3919           INTUOS5S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7, .touch_max = 16 };
3920 static const struct wacom_features wacom_features_0x27 =
3921         { "Wacom Intuos5 touch M", 44704, 27940, 2047, 63,
3922           INTUOS5, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16 };
3923 static const struct wacom_features wacom_features_0x28 =
3924         { "Wacom Intuos5 touch L", 65024, 40640, 2047, 63,
3925           INTUOS5L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16 };
3926 static const struct wacom_features wacom_features_0x29 =
3927         { "Wacom Intuos5 S", 31496, 19685, 2047, 63,
3928           INTUOS5S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7 };
3929 static const struct wacom_features wacom_features_0x2A =
3930         { "Wacom Intuos5 M", 44704, 27940, 2047, 63,
3931           INTUOS5, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3932 static const struct wacom_features wacom_features_0x314 =
3933         { "Wacom Intuos Pro S", 31496, 19685, 2047, 63,
3934           INTUOSPS, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7, .touch_max = 16,
3935           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3936 static const struct wacom_features wacom_features_0x315 =
3937         { "Wacom Intuos Pro M", 44704, 27940, 2047, 63,
3938           INTUOSPM, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16,
3939           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3940 static const struct wacom_features wacom_features_0x317 =
3941         { "Wacom Intuos Pro L", 65024, 40640, 2047, 63,
3942           INTUOSPL, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16,
3943           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3944 static const struct wacom_features wacom_features_0xF4 =
3945         { "Wacom Cintiq 24HD", 104480, 65600, 2047, 63,
3946           WACOM_24HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 16,
3947           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3948           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
3949 static const struct wacom_features wacom_features_0xF8 =
3950         { "Wacom Cintiq 24HD touch", 104480, 65600, 2047, 63, /* Pen */
3951           WACOM_24HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 16,
3952           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3953           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3954           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0xf6 };
3955 static const struct wacom_features wacom_features_0xF6 =
3956         { "Wacom Cintiq 24HD touch", .type = WACOM_24HDT, /* Touch */
3957           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0xf8, .touch_max = 10,
3958           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3959 static const struct wacom_features wacom_features_0x32A =
3960         { "Wacom Cintiq 27QHD", 120140, 67920, 2047, 63,
3961           WACOM_27QHD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 0,
3962           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3963           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
3964 static const struct wacom_features wacom_features_0x32B =
3965         { "Wacom Cintiq 27QHD touch", 120140, 67920, 2047, 63,
3966           WACOM_27QHD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 0,
3967           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3968           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3969           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x32C };
3970 static const struct wacom_features wacom_features_0x32C =
3971         { "Wacom Cintiq 27QHD touch", .type = WACOM_27QHDT,
3972           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x32B, .touch_max = 10 };
3973 static const struct wacom_features wacom_features_0x3F =
3974         { "Wacom Cintiq 21UX", 87200, 65600, 1023, 63,
3975           CINTIQ, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3976 static const struct wacom_features wacom_features_0xC5 =
3977         { "Wacom Cintiq 20WSX", 86680, 54180, 1023, 63,
3978           WACOM_BEE, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 10 };
3979 static const struct wacom_features wacom_features_0xC6 =
3980         { "Wacom Cintiq 12WX", 53020, 33440, 1023, 63,
3981           WACOM_BEE, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 10 };
3982 static const struct wacom_features wacom_features_0x304 =
3983         { "Wacom Cintiq 13HD", 59552, 33848, 1023, 63,
3984           WACOM_13HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
3985           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3986           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
3987 static const struct wacom_features wacom_features_0x333 =
3988         { "Wacom Cintiq 13HD touch", 59552, 33848, 2047, 63,
3989           WACOM_13HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
3990           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3991           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3992           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x335 };
3993 static const struct wacom_features wacom_features_0x335 =
3994         { "Wacom Cintiq 13HD touch", .type = WACOM_24HDT, /* Touch */
3995           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x333, .touch_max = 10,
3996           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3997 static const struct wacom_features wacom_features_0xC7 =
3998         { "Wacom DTU1931", 37832, 30305, 511, 0,
3999           PL, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4000 static const struct wacom_features wacom_features_0xCE =
4001         { "Wacom DTU2231", 47864, 27011, 511, 0,
4002           DTU, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4003           .check_for_hid_type = true, .hid_type = HID_TYPE_USBMOUSE };
4004 static const struct wacom_features wacom_features_0xF0 =
4005         { "Wacom DTU1631", 34623, 19553, 511, 0,
4006           DTU, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4007 static const struct wacom_features wacom_features_0xFB =
4008         { "Wacom DTU1031", 22096, 13960, 511, 0,
4009           DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
4010           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4011           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4012 static const struct wacom_features wacom_features_0x32F =
4013         { "Wacom DTU1031X", 22672, 12928, 511, 0,
4014           DTUSX, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 0,
4015           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4016           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4017 static const struct wacom_features wacom_features_0x336 =
4018         { "Wacom DTU1141", 23672, 13403, 1023, 0,
4019           DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
4020           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4021           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4022 static const struct wacom_features wacom_features_0x57 =
4023         { "Wacom DTK2241", 95840, 54260, 2047, 63,
4024           DTK, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 6,
4025           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4026           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4027 static const struct wacom_features wacom_features_0x59 = /* Pen */
4028         { "Wacom DTH2242", 95840, 54260, 2047, 63,
4029           DTK, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 6,
4030           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4031           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4032           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5D };
4033 static const struct wacom_features wacom_features_0x5D = /* Touch */
4034         { "Wacom DTH2242",       .type = WACOM_24HDT,
4035           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x59, .touch_max = 10,
4036           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4037 static const struct wacom_features wacom_features_0xCC =
4038         { "Wacom Cintiq 21UX2", 87200, 65600, 2047, 63,
4039           WACOM_21UX2, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
4040           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4041           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4042 static const struct wacom_features wacom_features_0xFA =
4043         { "Wacom Cintiq 22HD", 95840, 54260, 2047, 63,
4044           WACOM_22HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
4045           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4046           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
4047 static const struct wacom_features wacom_features_0x5B =
4048         { "Wacom Cintiq 22HDT", 95840, 54260, 2047, 63,
4049           WACOM_22HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
4050           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4051           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4052           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5e };
4053 static const struct wacom_features wacom_features_0x5E =
4054         { "Wacom Cintiq 22HDT", .type = WACOM_24HDT,
4055           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5b, .touch_max = 10,
4056           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4057 static const struct wacom_features wacom_features_0x90 =
4058         { "Wacom ISDv4 90", 26202, 16325, 255, 0,
4059           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4060 static const struct wacom_features wacom_features_0x93 =
4061         { "Wacom ISDv4 93", 26202, 16325, 255, 0,
4062           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4063 static const struct wacom_features wacom_features_0x97 =
4064         { "Wacom ISDv4 97", 26202, 16325, 511, 0,
4065           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4066 static const struct wacom_features wacom_features_0x9A =
4067         { "Wacom ISDv4 9A", 26202, 16325, 255, 0,
4068           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4069 static const struct wacom_features wacom_features_0x9F =
4070         { "Wacom ISDv4 9F", 26202, 16325, 255, 0,
4071           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4072 static const struct wacom_features wacom_features_0xE2 =
4073         { "Wacom ISDv4 E2", 26202, 16325, 255, 0,
4074           TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4075 static const struct wacom_features wacom_features_0xE3 =
4076         { "Wacom ISDv4 E3", 26202, 16325, 255, 0,
4077           TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4078 static const struct wacom_features wacom_features_0xE5 =
4079         { "Wacom ISDv4 E5", 26202, 16325, 255, 0,
4080           MTSCREEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4081 static const struct wacom_features wacom_features_0xE6 =
4082         { "Wacom ISDv4 E6", 27760, 15694, 255, 0,
4083           TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4084 static const struct wacom_features wacom_features_0xEC =
4085         { "Wacom ISDv4 EC", 25710, 14500, 255, 0,
4086           TABLETPC,    WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4087 static const struct wacom_features wacom_features_0xED =
4088         { "Wacom ISDv4 ED", 26202, 16325, 255, 0,
4089           TABLETPCE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4090 static const struct wacom_features wacom_features_0xEF =
4091         { "Wacom ISDv4 EF", 26202, 16325, 255, 0,
4092           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4093 static const struct wacom_features wacom_features_0x100 =
4094         { "Wacom ISDv4 100", 26202, 16325, 255, 0,
4095           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4096 static const struct wacom_features wacom_features_0x101 =
4097         { "Wacom ISDv4 101", 26202, 16325, 255, 0,
4098           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4099 static const struct wacom_features wacom_features_0x10D =
4100         { "Wacom ISDv4 10D", 26202, 16325, 255, 0,
4101           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4102 static const struct wacom_features wacom_features_0x10E =
4103         { "Wacom ISDv4 10E", 27760, 15694, 255, 0,
4104           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4105 static const struct wacom_features wacom_features_0x10F =
4106         { "Wacom ISDv4 10F", 27760, 15694, 255, 0,
4107           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4108 static const struct wacom_features wacom_features_0x116 =
4109         { "Wacom ISDv4 116", 26202, 16325, 255, 0,
4110           TABLETPCE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4111 static const struct wacom_features wacom_features_0x12C =
4112         { "Wacom ISDv4 12C", 27848, 15752, 2047, 0,
4113           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4114 static const struct wacom_features wacom_features_0x4001 =
4115         { "Wacom ISDv4 4001", 26202, 16325, 255, 0,
4116           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4117 static const struct wacom_features wacom_features_0x4004 =
4118         { "Wacom ISDv4 4004", 11060, 6220, 255, 0,
4119           MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4120 static const struct wacom_features wacom_features_0x5000 =
4121         { "Wacom ISDv4 5000", 27848, 15752, 1023, 0,
4122           MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4123 static const struct wacom_features wacom_features_0x5002 =
4124         { "Wacom ISDv4 5002", 29576, 16724, 1023, 0,
4125           MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4126 static const struct wacom_features wacom_features_0x47 =
4127         { "Wacom Intuos2 6x8", 20320, 16240, 1023, 31,
4128           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4129 static const struct wacom_features wacom_features_0x84 =
4130         { "Wacom Wireless Receiver", .type = WIRELESS, .touch_max = 16 };
4131 static const struct wacom_features wacom_features_0xD0 =
4132         { "Wacom Bamboo 2FG", 14720, 9200, 1023, 31,
4133           BAMBOO_TOUCH, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4134 static const struct wacom_features wacom_features_0xD1 =
4135         { "Wacom Bamboo 2FG 4x5", 14720, 9200, 1023, 31,
4136           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4137 static const struct wacom_features wacom_features_0xD2 =
4138         { "Wacom Bamboo Craft", 14720, 9200, 1023, 31,
4139           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4140 static const struct wacom_features wacom_features_0xD3 =
4141         { "Wacom Bamboo 2FG 6x8", 21648, 13700, 1023, 31,
4142           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4143 static const struct wacom_features wacom_features_0xD4 =
4144         { "Wacom Bamboo Pen", 14720, 9200, 1023, 31,
4145           BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4146 static const struct wacom_features wacom_features_0xD5 =
4147         { "Wacom Bamboo Pen 6x8", 21648, 13700, 1023, 31,
4148           BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4149 static const struct wacom_features wacom_features_0xD6 =
4150         { "Wacom BambooPT 2FG 4x5", 14720, 9200, 1023, 31,
4151           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4152 static const struct wacom_features wacom_features_0xD7 =
4153         { "Wacom BambooPT 2FG Small", 14720, 9200, 1023, 31,
4154           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4155 static const struct wacom_features wacom_features_0xD8 =
4156         { "Wacom Bamboo Comic 2FG", 21648, 13700, 1023, 31,
4157           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4158 static const struct wacom_features wacom_features_0xDA =
4159         { "Wacom Bamboo 2FG 4x5 SE", 14720, 9200, 1023, 31,
4160           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4161 static const struct wacom_features wacom_features_0xDB =
4162         { "Wacom Bamboo 2FG 6x8 SE", 21648, 13700, 1023, 31,
4163           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
4164 static const struct wacom_features wacom_features_0xDD =
4165         { "Wacom Bamboo Connect", 14720, 9200, 1023, 31,
4166           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4167 static const struct wacom_features wacom_features_0xDE =
4168         { "Wacom Bamboo 16FG 4x5", 14720, 9200, 1023, 31,
4169           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16 };
4170 static const struct wacom_features wacom_features_0xDF =
4171         { "Wacom Bamboo 16FG 6x8", 21648, 13700, 1023, 31,
4172           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16 };
4173 static const struct wacom_features wacom_features_0x300 =
4174         { "Wacom Bamboo One S", 14720, 9225, 1023, 31,
4175           BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4176 static const struct wacom_features wacom_features_0x301 =
4177         { "Wacom Bamboo One M", 21648, 13530, 1023, 31,
4178           BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4179 static const struct wacom_features wacom_features_0x302 =
4180         { "Wacom Intuos PT S", 15200, 9500, 1023, 31,
4181           INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4182           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4183 static const struct wacom_features wacom_features_0x303 =
4184         { "Wacom Intuos PT M", 21600, 13500, 1023, 31,
4185           INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4186           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4187 static const struct wacom_features wacom_features_0x30E =
4188         { "Wacom Intuos S", 15200, 9500, 1023, 31,
4189           INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4190           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4191 static const struct wacom_features wacom_features_0x6004 =
4192         { "ISD-V4", 12800, 8000, 255, 0,
4193           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
4194 static const struct wacom_features wacom_features_0x307 =
4195         { "Wacom ISDv5 307", 59552, 33848, 2047, 63,
4196           CINTIQ_HYBRID, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4197           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4198           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4199           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x309 };
4200 static const struct wacom_features wacom_features_0x309 =
4201         { "Wacom ISDv5 309", .type = WACOM_24HDT, /* Touch */
4202           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x0307, .touch_max = 10,
4203           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4204 static const struct wacom_features wacom_features_0x30A =
4205         { "Wacom ISDv5 30A", 59552, 33848, 2047, 63,
4206           CINTIQ_HYBRID, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
4207           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4208           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4209           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x30C };
4210 static const struct wacom_features wacom_features_0x30C =
4211         { "Wacom ISDv5 30C", .type = WACOM_24HDT, /* Touch */
4212           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x30A, .touch_max = 10,
4213           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4214 static const struct wacom_features wacom_features_0x318 =
4215         { "Wacom USB Bamboo PAD", 4095, 4095, /* Touch */
4216           .type = BAMBOO_PAD, 35, 48, .touch_max = 4 };
4217 static const struct wacom_features wacom_features_0x319 =
4218         { "Wacom Wireless Bamboo PAD", 4095, 4095, /* Touch */
4219           .type = BAMBOO_PAD, 35, 48, .touch_max = 4 };
4220 static const struct wacom_features wacom_features_0x325 =
4221         { "Wacom ISDv5 325", 59552, 33848, 2047, 63,
4222           CINTIQ_COMPANION_2, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 11,
4223           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4224           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
4225           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x326 };
4226 static const struct wacom_features wacom_features_0x326 = /* Touch */
4227         { "Wacom ISDv5 326", .type = HID_GENERIC, .oVid = USB_VENDOR_ID_WACOM,
4228           .oPid = 0x325 };
4229 static const struct wacom_features wacom_features_0x323 =
4230         { "Wacom Intuos P M", 21600, 13500, 1023, 31,
4231           INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4232           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4233 static const struct wacom_features wacom_features_0x331 =
4234         { "Wacom Express Key Remote", .type = REMOTE,
4235           .numbered_buttons = 18, .check_for_hid_type = true,
4236           .hid_type = HID_TYPE_USBNONE };
4237 static const struct wacom_features wacom_features_0x33B =
4238         { "Wacom Intuos S 2", 15200, 9500, 2047, 63,
4239           INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4240           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4241 static const struct wacom_features wacom_features_0x33C =
4242         { "Wacom Intuos PT S 2", 15200, 9500, 2047, 63,
4243           INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4244           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4245 static const struct wacom_features wacom_features_0x33D =
4246         { "Wacom Intuos P M 2", 21600, 13500, 2047, 63,
4247           INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
4248           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4249 static const struct wacom_features wacom_features_0x33E =
4250         { "Wacom Intuos PT M 2", 21600, 13500, 2047, 63,
4251           INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
4252           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
4253 static const struct wacom_features wacom_features_0x343 =
4254         { "Wacom DTK1651", 34816, 19759, 1023, 0,
4255           DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
4256           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET,
4257           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
4258 static const struct wacom_features wacom_features_0x360 =
4259         { "Wacom Intuos Pro M", 44800, 29600, 8191, 63,
4260           INTUOSP2_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 10 };
4261 static const struct wacom_features wacom_features_0x361 =
4262         { "Wacom Intuos Pro L", 62200, 43200, 8191, 63,
4263           INTUOSP2_BT, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 10 };
4264
4265 static const struct wacom_features wacom_features_HID_ANY_ID =
4266         { "Wacom HID", .type = HID_GENERIC, .oVid = HID_ANY_ID, .oPid = HID_ANY_ID };
4267
4268 #define USB_DEVICE_WACOM(prod)                                          \
4269         HID_DEVICE(BUS_USB, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4270         .driver_data = (kernel_ulong_t)&wacom_features_##prod
4271
4272 #define BT_DEVICE_WACOM(prod)                                           \
4273         HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4274         .driver_data = (kernel_ulong_t)&wacom_features_##prod
4275
4276 #define I2C_DEVICE_WACOM(prod)                                          \
4277         HID_DEVICE(BUS_I2C, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
4278         .driver_data = (kernel_ulong_t)&wacom_features_##prod
4279
4280 #define USB_DEVICE_LENOVO(prod)                                 \
4281         HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, prod),                     \
4282         .driver_data = (kernel_ulong_t)&wacom_features_##prod
4283
4284 const struct hid_device_id wacom_ids[] = {
4285         { USB_DEVICE_WACOM(0x00) },
4286         { USB_DEVICE_WACOM(0x03) },
4287         { USB_DEVICE_WACOM(0x10) },
4288         { USB_DEVICE_WACOM(0x11) },
4289         { USB_DEVICE_WACOM(0x12) },
4290         { USB_DEVICE_WACOM(0x13) },
4291         { USB_DEVICE_WACOM(0x14) },
4292         { USB_DEVICE_WACOM(0x15) },
4293         { USB_DEVICE_WACOM(0x16) },
4294         { USB_DEVICE_WACOM(0x17) },
4295         { USB_DEVICE_WACOM(0x18) },
4296         { USB_DEVICE_WACOM(0x19) },
4297         { USB_DEVICE_WACOM(0x20) },
4298         { USB_DEVICE_WACOM(0x21) },
4299         { USB_DEVICE_WACOM(0x22) },
4300         { USB_DEVICE_WACOM(0x23) },
4301         { USB_DEVICE_WACOM(0x24) },
4302         { USB_DEVICE_WACOM(0x26) },
4303         { USB_DEVICE_WACOM(0x27) },
4304         { USB_DEVICE_WACOM(0x28) },
4305         { USB_DEVICE_WACOM(0x29) },
4306         { USB_DEVICE_WACOM(0x2A) },
4307         { USB_DEVICE_WACOM(0x30) },
4308         { USB_DEVICE_WACOM(0x31) },
4309         { USB_DEVICE_WACOM(0x32) },
4310         { USB_DEVICE_WACOM(0x33) },
4311         { USB_DEVICE_WACOM(0x34) },
4312         { USB_DEVICE_WACOM(0x35) },
4313         { USB_DEVICE_WACOM(0x37) },
4314         { USB_DEVICE_WACOM(0x38) },
4315         { USB_DEVICE_WACOM(0x39) },
4316         { USB_DEVICE_WACOM(0x3F) },
4317         { USB_DEVICE_WACOM(0x41) },
4318         { USB_DEVICE_WACOM(0x42) },
4319         { USB_DEVICE_WACOM(0x43) },
4320         { USB_DEVICE_WACOM(0x44) },
4321         { USB_DEVICE_WACOM(0x45) },
4322         { USB_DEVICE_WACOM(0x47) },
4323         { USB_DEVICE_WACOM(0x57) },
4324         { USB_DEVICE_WACOM(0x59) },
4325         { USB_DEVICE_WACOM(0x5B) },
4326         { USB_DEVICE_WACOM(0x5D) },
4327         { USB_DEVICE_WACOM(0x5E) },
4328         { USB_DEVICE_WACOM(0x60) },
4329         { USB_DEVICE_WACOM(0x61) },
4330         { USB_DEVICE_WACOM(0x62) },
4331         { USB_DEVICE_WACOM(0x63) },
4332         { USB_DEVICE_WACOM(0x64) },
4333         { USB_DEVICE_WACOM(0x65) },
4334         { USB_DEVICE_WACOM(0x69) },
4335         { USB_DEVICE_WACOM(0x6A) },
4336         { USB_DEVICE_WACOM(0x6B) },
4337         { BT_DEVICE_WACOM(0x81) },
4338         { USB_DEVICE_WACOM(0x84) },
4339         { USB_DEVICE_WACOM(0x90) },
4340         { USB_DEVICE_WACOM(0x93) },
4341         { USB_DEVICE_WACOM(0x97) },
4342         { USB_DEVICE_WACOM(0x9A) },
4343         { USB_DEVICE_WACOM(0x9F) },
4344         { USB_DEVICE_WACOM(0xB0) },
4345         { USB_DEVICE_WACOM(0xB1) },
4346         { USB_DEVICE_WACOM(0xB2) },
4347         { USB_DEVICE_WACOM(0xB3) },
4348         { USB_DEVICE_WACOM(0xB4) },
4349         { USB_DEVICE_WACOM(0xB5) },
4350         { USB_DEVICE_WACOM(0xB7) },
4351         { USB_DEVICE_WACOM(0xB8) },
4352         { USB_DEVICE_WACOM(0xB9) },
4353         { USB_DEVICE_WACOM(0xBA) },
4354         { USB_DEVICE_WACOM(0xBB) },
4355         { USB_DEVICE_WACOM(0xBC) },
4356         { BT_DEVICE_WACOM(0xBD) },
4357         { USB_DEVICE_WACOM(0xC0) },
4358         { USB_DEVICE_WACOM(0xC2) },
4359         { USB_DEVICE_WACOM(0xC4) },
4360         { USB_DEVICE_WACOM(0xC5) },
4361         { USB_DEVICE_WACOM(0xC6) },
4362         { USB_DEVICE_WACOM(0xC7) },
4363         { USB_DEVICE_WACOM(0xCC) },
4364         { USB_DEVICE_WACOM(0xCE) },
4365         { USB_DEVICE_WACOM(0xD0) },
4366         { USB_DEVICE_WACOM(0xD1) },
4367         { USB_DEVICE_WACOM(0xD2) },
4368         { USB_DEVICE_WACOM(0xD3) },
4369         { USB_DEVICE_WACOM(0xD4) },
4370         { USB_DEVICE_WACOM(0xD5) },
4371         { USB_DEVICE_WACOM(0xD6) },
4372         { USB_DEVICE_WACOM(0xD7) },
4373         { USB_DEVICE_WACOM(0xD8) },
4374         { USB_DEVICE_WACOM(0xDA) },
4375         { USB_DEVICE_WACOM(0xDB) },
4376         { USB_DEVICE_WACOM(0xDD) },
4377         { USB_DEVICE_WACOM(0xDE) },
4378         { USB_DEVICE_WACOM(0xDF) },
4379         { USB_DEVICE_WACOM(0xE2) },
4380         { USB_DEVICE_WACOM(0xE3) },
4381         { USB_DEVICE_WACOM(0xE5) },
4382         { USB_DEVICE_WACOM(0xE6) },
4383         { USB_DEVICE_WACOM(0xEC) },
4384         { USB_DEVICE_WACOM(0xED) },
4385         { USB_DEVICE_WACOM(0xEF) },
4386         { USB_DEVICE_WACOM(0xF0) },
4387         { USB_DEVICE_WACOM(0xF4) },
4388         { USB_DEVICE_WACOM(0xF6) },
4389         { USB_DEVICE_WACOM(0xF8) },
4390         { USB_DEVICE_WACOM(0xFA) },
4391         { USB_DEVICE_WACOM(0xFB) },
4392         { USB_DEVICE_WACOM(0x100) },
4393         { USB_DEVICE_WACOM(0x101) },
4394         { USB_DEVICE_WACOM(0x10D) },
4395         { USB_DEVICE_WACOM(0x10E) },
4396         { USB_DEVICE_WACOM(0x10F) },
4397         { USB_DEVICE_WACOM(0x116) },
4398         { USB_DEVICE_WACOM(0x12C) },
4399         { USB_DEVICE_WACOM(0x300) },
4400         { USB_DEVICE_WACOM(0x301) },
4401         { USB_DEVICE_WACOM(0x302) },
4402         { USB_DEVICE_WACOM(0x303) },
4403         { USB_DEVICE_WACOM(0x304) },
4404         { USB_DEVICE_WACOM(0x307) },
4405         { USB_DEVICE_WACOM(0x309) },
4406         { USB_DEVICE_WACOM(0x30A) },
4407         { USB_DEVICE_WACOM(0x30C) },
4408         { USB_DEVICE_WACOM(0x30E) },
4409         { USB_DEVICE_WACOM(0x314) },
4410         { USB_DEVICE_WACOM(0x315) },
4411         { USB_DEVICE_WACOM(0x317) },
4412         { USB_DEVICE_WACOM(0x318) },
4413         { USB_DEVICE_WACOM(0x319) },
4414         { USB_DEVICE_WACOM(0x323) },
4415         { USB_DEVICE_WACOM(0x325) },
4416         { USB_DEVICE_WACOM(0x326) },
4417         { USB_DEVICE_WACOM(0x32A) },
4418         { USB_DEVICE_WACOM(0x32B) },
4419         { USB_DEVICE_WACOM(0x32C) },
4420         { USB_DEVICE_WACOM(0x32F) },
4421         { USB_DEVICE_WACOM(0x331) },
4422         { USB_DEVICE_WACOM(0x333) },
4423         { USB_DEVICE_WACOM(0x335) },
4424         { USB_DEVICE_WACOM(0x336) },
4425         { USB_DEVICE_WACOM(0x33B) },
4426         { USB_DEVICE_WACOM(0x33C) },
4427         { USB_DEVICE_WACOM(0x33D) },
4428         { USB_DEVICE_WACOM(0x33E) },
4429         { USB_DEVICE_WACOM(0x343) },
4430         { BT_DEVICE_WACOM(0x360) },
4431         { BT_DEVICE_WACOM(0x361) },
4432         { USB_DEVICE_WACOM(0x4001) },
4433         { USB_DEVICE_WACOM(0x4004) },
4434         { USB_DEVICE_WACOM(0x5000) },
4435         { USB_DEVICE_WACOM(0x5002) },
4436         { USB_DEVICE_LENOVO(0x6004) },
4437
4438         { USB_DEVICE_WACOM(HID_ANY_ID) },
4439         { I2C_DEVICE_WACOM(HID_ANY_ID) },
4440         { BT_DEVICE_WACOM(HID_ANY_ID) },
4441         { }
4442 };
4443 MODULE_DEVICE_TABLE(hid, wacom_ids);