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