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