Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livep...
[muen/linux.git] / drivers / hid / wacom_wac.c
1 /*
2  * drivers/input/tablet/wacom_wac.c
3  *
4  *  USB Wacom tablet support - Wacom specific code
5  *
6  */
7
8 /*
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  */
14
15 #include "wacom_wac.h"
16 #include "wacom.h"
17 #include <linux/input/mt.h>
18
19 /* resolution for penabled devices */
20 #define WACOM_PL_RES            20
21 #define WACOM_PENPRTN_RES       40
22 #define WACOM_VOLITO_RES        50
23 #define WACOM_GRAPHIRE_RES      80
24 #define WACOM_INTUOS_RES        100
25 #define WACOM_INTUOS3_RES       200
26
27 /* Newer Cintiq and DTU have an offset between tablet and screen areas */
28 #define WACOM_DTU_OFFSET        200
29 #define WACOM_CINTIQ_OFFSET     400
30
31 /*
32  * Scale factor relating reported contact size to logical contact area.
33  * 2^14/pi is a good approximation on Intuos5 and 3rd-gen Bamboo
34  */
35 #define WACOM_CONTACT_AREA_SCALE 2607
36
37 static bool touch_arbitration = 1;
38 module_param(touch_arbitration, bool, 0644);
39 MODULE_PARM_DESC(touch_arbitration, " on (Y) off (N)");
40
41 static void wacom_report_numbered_buttons(struct input_dev *input_dev,
42                                 int button_count, int mask);
43
44 static int wacom_numbered_button_to_key(int n);
45
46 static void wacom_update_led(struct wacom *wacom, int button_count, int mask,
47                              int group);
48 /*
49  * Percent of battery capacity for Graphire.
50  * 8th value means AC online and show 100% capacity.
51  */
52 static unsigned short batcap_gr[8] = { 1, 15, 25, 35, 50, 70, 100, 100 };
53
54 /*
55  * Percent of battery capacity for Intuos4 WL, AC has a separate bit.
56  */
57 static unsigned short batcap_i4[8] = { 1, 15, 30, 45, 60, 70, 85, 100 };
58
59 static void __wacom_notify_battery(struct wacom_battery *battery,
60                                    int bat_status, int bat_capacity,
61                                    bool bat_charging, bool bat_connected,
62                                    bool ps_connected)
63 {
64         bool changed = battery->bat_status       != bat_status    ||
65                        battery->battery_capacity != bat_capacity  ||
66                        battery->bat_charging     != bat_charging  ||
67                        battery->bat_connected    != bat_connected ||
68                        battery->ps_connected     != ps_connected;
69
70         if (changed) {
71                 battery->bat_status = bat_status;
72                 battery->battery_capacity = bat_capacity;
73                 battery->bat_charging = bat_charging;
74                 battery->bat_connected = bat_connected;
75                 battery->ps_connected = ps_connected;
76
77                 if (battery->battery)
78                         power_supply_changed(battery->battery);
79         }
80 }
81
82 static void wacom_notify_battery(struct wacom_wac *wacom_wac,
83         int bat_status, int bat_capacity, bool bat_charging,
84         bool bat_connected, bool ps_connected)
85 {
86         struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
87
88         __wacom_notify_battery(&wacom->battery, bat_status, bat_capacity,
89                                bat_charging, bat_connected, ps_connected);
90 }
91
92 static int wacom_penpartner_irq(struct wacom_wac *wacom)
93 {
94         unsigned char *data = wacom->data;
95         struct input_dev *input = wacom->pen_input;
96
97         switch (data[0]) {
98         case 1:
99                 if (data[5] & 0x80) {
100                         wacom->tool[0] = (data[5] & 0x20) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
101                         wacom->id[0] = (data[5] & 0x20) ? ERASER_DEVICE_ID : STYLUS_DEVICE_ID;
102                         input_report_key(input, wacom->tool[0], 1);
103                         input_report_abs(input, ABS_MISC, wacom->id[0]); /* report tool id */
104                         input_report_abs(input, ABS_X, get_unaligned_le16(&data[1]));
105                         input_report_abs(input, ABS_Y, get_unaligned_le16(&data[3]));
106                         input_report_abs(input, ABS_PRESSURE, (signed char)data[6] + 127);
107                         input_report_key(input, BTN_TOUCH, ((signed char)data[6] > -127));
108                         input_report_key(input, BTN_STYLUS, (data[5] & 0x40));
109                 } else {
110                         input_report_key(input, wacom->tool[0], 0);
111                         input_report_abs(input, ABS_MISC, 0); /* report tool id */
112                         input_report_abs(input, ABS_PRESSURE, -1);
113                         input_report_key(input, BTN_TOUCH, 0);
114                 }
115                 break;
116
117         case 2:
118                 input_report_key(input, BTN_TOOL_PEN, 1);
119                 input_report_abs(input, ABS_MISC, STYLUS_DEVICE_ID); /* report tool id */
120                 input_report_abs(input, ABS_X, get_unaligned_le16(&data[1]));
121                 input_report_abs(input, ABS_Y, get_unaligned_le16(&data[3]));
122                 input_report_abs(input, ABS_PRESSURE, (signed char)data[6] + 127);
123                 input_report_key(input, BTN_TOUCH, ((signed char)data[6] > -80) && !(data[5] & 0x20));
124                 input_report_key(input, BTN_STYLUS, (data[5] & 0x40));
125                 break;
126
127         default:
128                 dev_dbg(input->dev.parent,
129                         "%s: received unknown report #%d\n", __func__, data[0]);
130                 return 0;
131         }
132
133         return 1;
134 }
135
136 static int wacom_pl_irq(struct wacom_wac *wacom)
137 {
138         struct wacom_features *features = &wacom->features;
139         unsigned char *data = wacom->data;
140         struct input_dev *input = wacom->pen_input;
141         int prox, pressure;
142
143         if (data[0] != WACOM_REPORT_PENABLED) {
144                 dev_dbg(input->dev.parent,
145                         "%s: received unknown report #%d\n", __func__, data[0]);
146                 return 0;
147         }
148
149         prox = data[1] & 0x40;
150
151         if (!wacom->id[0]) {
152                 if ((data[0] & 0x10) || (data[4] & 0x20)) {
153                         wacom->tool[0] = BTN_TOOL_RUBBER;
154                         wacom->id[0] = ERASER_DEVICE_ID;
155                 }
156                 else {
157                         wacom->tool[0] = BTN_TOOL_PEN;
158                         wacom->id[0] = STYLUS_DEVICE_ID;
159                 }
160         }
161
162         /* If the eraser is in prox, STYLUS2 is always set. If we
163          * mis-detected the type and notice that STYLUS2 isn't set
164          * then force the eraser out of prox and let the pen in.
165          */
166         if (wacom->tool[0] == BTN_TOOL_RUBBER && !(data[4] & 0x20)) {
167                 input_report_key(input, BTN_TOOL_RUBBER, 0);
168                 input_report_abs(input, ABS_MISC, 0);
169                 input_sync(input);
170                 wacom->tool[0] = BTN_TOOL_PEN;
171                 wacom->id[0] = STYLUS_DEVICE_ID;
172         }
173
174         if (prox) {
175                 pressure = (signed char)((data[7] << 1) | ((data[4] >> 2) & 1));
176                 if (features->pressure_max > 255)
177                         pressure = (pressure << 1) | ((data[4] >> 6) & 1);
178                 pressure += (features->pressure_max + 1) / 2;
179
180                 input_report_abs(input, ABS_X, data[3] | (data[2] << 7) | ((data[1] & 0x03) << 14));
181                 input_report_abs(input, ABS_Y, data[6] | (data[5] << 7) | ((data[4] & 0x03) << 14));
182                 input_report_abs(input, ABS_PRESSURE, pressure);
183
184                 input_report_key(input, BTN_TOUCH, data[4] & 0x08);
185                 input_report_key(input, BTN_STYLUS, data[4] & 0x10);
186                 /* Only allow the stylus2 button to be reported for the pen tool. */
187                 input_report_key(input, BTN_STYLUS2, (wacom->tool[0] == BTN_TOOL_PEN) && (data[4] & 0x20));
188         }
189
190         if (!prox)
191                 wacom->id[0] = 0;
192         input_report_key(input, wacom->tool[0], prox);
193         input_report_abs(input, ABS_MISC, wacom->id[0]);
194         return 1;
195 }
196
197 static int wacom_ptu_irq(struct wacom_wac *wacom)
198 {
199         unsigned char *data = wacom->data;
200         struct input_dev *input = wacom->pen_input;
201
202         if (data[0] != WACOM_REPORT_PENABLED) {
203                 dev_dbg(input->dev.parent,
204                         "%s: received unknown report #%d\n", __func__, data[0]);
205                 return 0;
206         }
207
208         if (data[1] & 0x04) {
209                 input_report_key(input, BTN_TOOL_RUBBER, data[1] & 0x20);
210                 input_report_key(input, BTN_TOUCH, data[1] & 0x08);
211                 wacom->id[0] = ERASER_DEVICE_ID;
212         } else {
213                 input_report_key(input, BTN_TOOL_PEN, data[1] & 0x20);
214                 input_report_key(input, BTN_TOUCH, data[1] & 0x01);
215                 wacom->id[0] = STYLUS_DEVICE_ID;
216         }
217         input_report_abs(input, ABS_MISC, wacom->id[0]); /* report tool id */
218         input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
219         input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
220         input_report_abs(input, ABS_PRESSURE, le16_to_cpup((__le16 *)&data[6]));
221         input_report_key(input, BTN_STYLUS, data[1] & 0x02);
222         input_report_key(input, BTN_STYLUS2, data[1] & 0x10);
223         return 1;
224 }
225
226 static int wacom_dtu_irq(struct wacom_wac *wacom)
227 {
228         unsigned char *data = wacom->data;
229         struct input_dev *input = wacom->pen_input;
230         int prox = data[1] & 0x20;
231
232         dev_dbg(input->dev.parent,
233                 "%s: received report #%d", __func__, data[0]);
234
235         if (prox) {
236                 /* Going into proximity select tool */
237                 wacom->tool[0] = (data[1] & 0x0c) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
238                 if (wacom->tool[0] == BTN_TOOL_PEN)
239                         wacom->id[0] = STYLUS_DEVICE_ID;
240                 else
241                         wacom->id[0] = ERASER_DEVICE_ID;
242         }
243         input_report_key(input, BTN_STYLUS, data[1] & 0x02);
244         input_report_key(input, BTN_STYLUS2, data[1] & 0x10);
245         input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
246         input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
247         input_report_abs(input, ABS_PRESSURE, ((data[7] & 0x01) << 8) | data[6]);
248         input_report_key(input, BTN_TOUCH, data[1] & 0x05);
249         if (!prox) /* out-prox */
250                 wacom->id[0] = 0;
251         input_report_key(input, wacom->tool[0], prox);
252         input_report_abs(input, ABS_MISC, wacom->id[0]);
253         return 1;
254 }
255
256 static int wacom_dtus_irq(struct wacom_wac *wacom)
257 {
258         char *data = wacom->data;
259         struct input_dev *input = wacom->pen_input;
260         unsigned short prox, pressure = 0;
261
262         if (data[0] != WACOM_REPORT_DTUS && data[0] != WACOM_REPORT_DTUSPAD) {
263                 dev_dbg(input->dev.parent,
264                         "%s: received unknown report #%d", __func__, data[0]);
265                 return 0;
266         } else if (data[0] == WACOM_REPORT_DTUSPAD) {
267                 input = wacom->pad_input;
268                 input_report_key(input, BTN_0, (data[1] & 0x01));
269                 input_report_key(input, BTN_1, (data[1] & 0x02));
270                 input_report_key(input, BTN_2, (data[1] & 0x04));
271                 input_report_key(input, BTN_3, (data[1] & 0x08));
272                 input_report_abs(input, ABS_MISC,
273                                  data[1] & 0x0f ? PAD_DEVICE_ID : 0);
274                 return 1;
275         } else {
276                 prox = data[1] & 0x80;
277                 if (prox) {
278                         switch ((data[1] >> 3) & 3) {
279                         case 1: /* Rubber */
280                                 wacom->tool[0] = BTN_TOOL_RUBBER;
281                                 wacom->id[0] = ERASER_DEVICE_ID;
282                                 break;
283
284                         case 2: /* Pen */
285                                 wacom->tool[0] = BTN_TOOL_PEN;
286                                 wacom->id[0] = STYLUS_DEVICE_ID;
287                                 break;
288                         }
289                 }
290
291                 input_report_key(input, BTN_STYLUS, data[1] & 0x20);
292                 input_report_key(input, BTN_STYLUS2, data[1] & 0x40);
293                 input_report_abs(input, ABS_X, get_unaligned_be16(&data[3]));
294                 input_report_abs(input, ABS_Y, get_unaligned_be16(&data[5]));
295                 pressure = ((data[1] & 0x03) << 8) | (data[2] & 0xff);
296                 input_report_abs(input, ABS_PRESSURE, pressure);
297                 input_report_key(input, BTN_TOUCH, pressure > 10);
298
299                 if (!prox) /* out-prox */
300                         wacom->id[0] = 0;
301                 input_report_key(input, wacom->tool[0], prox);
302                 input_report_abs(input, ABS_MISC, wacom->id[0]);
303                 return 1;
304         }
305 }
306
307 static int wacom_graphire_irq(struct wacom_wac *wacom)
308 {
309         struct wacom_features *features = &wacom->features;
310         unsigned char *data = wacom->data;
311         struct input_dev *input = wacom->pen_input;
312         struct input_dev *pad_input = wacom->pad_input;
313         int battery_capacity, ps_connected;
314         int prox;
315         int rw = 0;
316         int retval = 0;
317
318         if (features->type == GRAPHIRE_BT) {
319                 if (data[0] != WACOM_REPORT_PENABLED_BT) {
320                         dev_dbg(input->dev.parent,
321                                 "%s: received unknown report #%d\n", __func__,
322                                 data[0]);
323                         goto exit;
324                 }
325         } else if (data[0] != WACOM_REPORT_PENABLED) {
326                 dev_dbg(input->dev.parent,
327                         "%s: received unknown report #%d\n", __func__, data[0]);
328                 goto exit;
329         }
330
331         prox = data[1] & 0x80;
332         if (prox || wacom->id[0]) {
333                 if (prox) {
334                         switch ((data[1] >> 5) & 3) {
335
336                         case 0: /* Pen */
337                                 wacom->tool[0] = BTN_TOOL_PEN;
338                                 wacom->id[0] = STYLUS_DEVICE_ID;
339                                 break;
340
341                         case 1: /* Rubber */
342                                 wacom->tool[0] = BTN_TOOL_RUBBER;
343                                 wacom->id[0] = ERASER_DEVICE_ID;
344                                 break;
345
346                         case 2: /* Mouse with wheel */
347                                 input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
348                                 /* fall through */
349
350                         case 3: /* Mouse without wheel */
351                                 wacom->tool[0] = BTN_TOOL_MOUSE;
352                                 wacom->id[0] = CURSOR_DEVICE_ID;
353                                 break;
354                         }
355                 }
356                 input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
357                 input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
358                 if (wacom->tool[0] != BTN_TOOL_MOUSE) {
359                         if (features->type == GRAPHIRE_BT)
360                                 input_report_abs(input, ABS_PRESSURE, data[6] |
361                                         (((__u16) (data[1] & 0x08)) << 5));
362                         else
363                                 input_report_abs(input, ABS_PRESSURE, data[6] |
364                                         ((data[7] & 0x03) << 8));
365                         input_report_key(input, BTN_TOUCH, data[1] & 0x01);
366                         input_report_key(input, BTN_STYLUS, data[1] & 0x02);
367                         input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
368                 } else {
369                         input_report_key(input, BTN_LEFT, data[1] & 0x01);
370                         input_report_key(input, BTN_RIGHT, data[1] & 0x02);
371                         if (features->type == WACOM_G4 ||
372                                         features->type == WACOM_MO) {
373                                 input_report_abs(input, ABS_DISTANCE, data[6] & 0x3f);
374                                 rw = (data[7] & 0x04) - (data[7] & 0x03);
375                         } else if (features->type == GRAPHIRE_BT) {
376                                 /* Compute distance between mouse and tablet */
377                                 rw = 44 - (data[6] >> 2);
378                                 rw = clamp_val(rw, 0, 31);
379                                 input_report_abs(input, ABS_DISTANCE, rw);
380                                 if (((data[1] >> 5) & 3) == 2) {
381                                         /* Mouse with wheel */
382                                         input_report_key(input, BTN_MIDDLE,
383                                                         data[1] & 0x04);
384                                         rw = (data[6] & 0x01) ? -1 :
385                                                 (data[6] & 0x02) ? 1 : 0;
386                                 } else {
387                                         rw = 0;
388                                 }
389                         } else {
390                                 input_report_abs(input, ABS_DISTANCE, data[7] & 0x3f);
391                                 rw = -(signed char)data[6];
392                         }
393                         input_report_rel(input, REL_WHEEL, rw);
394                 }
395
396                 if (!prox)
397                         wacom->id[0] = 0;
398                 input_report_abs(input, ABS_MISC, wacom->id[0]); /* report tool id */
399                 input_report_key(input, wacom->tool[0], prox);
400                 input_sync(input); /* sync last event */
401         }
402
403         /* send pad data */
404         switch (features->type) {
405         case WACOM_G4:
406                 prox = data[7] & 0xf8;
407                 if (prox || wacom->id[1]) {
408                         wacom->id[1] = PAD_DEVICE_ID;
409                         input_report_key(pad_input, BTN_BACK, (data[7] & 0x40));
410                         input_report_key(pad_input, BTN_FORWARD, (data[7] & 0x80));
411                         rw = ((data[7] & 0x18) >> 3) - ((data[7] & 0x20) >> 3);
412                         input_report_rel(pad_input, REL_WHEEL, rw);
413                         if (!prox)
414                                 wacom->id[1] = 0;
415                         input_report_abs(pad_input, ABS_MISC, wacom->id[1]);
416                         retval = 1;
417                 }
418                 break;
419
420         case WACOM_MO:
421                 prox = (data[7] & 0xf8) || data[8];
422                 if (prox || wacom->id[1]) {
423                         wacom->id[1] = PAD_DEVICE_ID;
424                         input_report_key(pad_input, BTN_BACK, (data[7] & 0x08));
425                         input_report_key(pad_input, BTN_LEFT, (data[7] & 0x20));
426                         input_report_key(pad_input, BTN_FORWARD, (data[7] & 0x10));
427                         input_report_key(pad_input, BTN_RIGHT, (data[7] & 0x40));
428                         input_report_abs(pad_input, ABS_WHEEL, (data[8] & 0x7f));
429                         if (!prox)
430                                 wacom->id[1] = 0;
431                         input_report_abs(pad_input, ABS_MISC, wacom->id[1]);
432                         retval = 1;
433                 }
434                 break;
435         case GRAPHIRE_BT:
436                 prox = data[7] & 0x03;
437                 if (prox || wacom->id[1]) {
438                         wacom->id[1] = PAD_DEVICE_ID;
439                         input_report_key(pad_input, BTN_0, (data[7] & 0x02));
440                         input_report_key(pad_input, BTN_1, (data[7] & 0x01));
441                         if (!prox)
442                                 wacom->id[1] = 0;
443                         input_report_abs(pad_input, ABS_MISC, wacom->id[1]);
444                         retval = 1;
445                 }
446                 break;
447         }
448
449         /* Store current battery capacity and power supply state */
450         if (features->type == GRAPHIRE_BT) {
451                 rw = (data[7] >> 2 & 0x07);
452                 battery_capacity = batcap_gr[rw];
453                 ps_connected = rw == 7;
454                 wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
455                                      battery_capacity, ps_connected, 1,
456                                      ps_connected);
457         }
458 exit:
459         return retval;
460 }
461
462 static void wacom_intuos_schedule_prox_event(struct wacom_wac *wacom_wac)
463 {
464         struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
465         struct wacom_features *features = &wacom_wac->features;
466         struct hid_report *r;
467         struct hid_report_enum *re;
468
469         re = &(wacom->hdev->report_enum[HID_FEATURE_REPORT]);
470         if (features->type == INTUOSHT2)
471                 r = re->report_id_hash[WACOM_REPORT_INTUOSHT2_ID];
472         else
473                 r = re->report_id_hash[WACOM_REPORT_INTUOS_ID1];
474         if (r) {
475                 hid_hw_request(wacom->hdev, r, HID_REQ_GET_REPORT);
476         }
477 }
478
479 static int wacom_intuos_pad(struct wacom_wac *wacom)
480 {
481         struct wacom_features *features = &wacom->features;
482         unsigned char *data = wacom->data;
483         struct input_dev *input = wacom->pad_input;
484         int i;
485         int buttons = 0, nbuttons = features->numbered_buttons;
486         int keys = 0, nkeys = 0;
487         int ring1 = 0, ring2 = 0;
488         int strip1 = 0, strip2 = 0;
489         bool prox = false;
490
491         /* pad packets. Works as a second tool and is always in prox */
492         if (!(data[0] == WACOM_REPORT_INTUOSPAD || data[0] == WACOM_REPORT_INTUOS5PAD ||
493               data[0] == WACOM_REPORT_CINTIQPAD))
494                 return 0;
495
496         if (features->type >= INTUOS4S && features->type <= INTUOS4L) {
497                 buttons = (data[3] << 1) | (data[2] & 0x01);
498                 ring1 = data[1];
499         } else if (features->type == DTK) {
500                 buttons = data[6];
501         } else if (features->type == WACOM_13HD) {
502                 buttons = (data[4] << 1) | (data[3] & 0x01);
503         } else if (features->type == WACOM_24HD) {
504                 buttons = (data[8] << 8) | data[6];
505                 ring1 = data[1];
506                 ring2 = data[2];
507
508                 /*
509                  * Three "buttons" are available on the 24HD which are
510                  * physically implemented as a touchstrip. Each button
511                  * is approximately 3 bits wide with a 2 bit spacing.
512                  * The raw touchstrip bits are stored at:
513                  *    ((data[3] & 0x1f) << 8) | data[4])
514                  */
515                 nkeys = 3;
516                 keys = ((data[3] & 0x1C) ? 1<<2 : 0) |
517                        ((data[4] & 0xE0) ? 1<<1 : 0) |
518                        ((data[4] & 0x07) ? 1<<0 : 0);
519         } else if (features->type == WACOM_27QHD) {
520                 nkeys = 3;
521                 keys = data[2] & 0x07;
522
523                 input_report_abs(input, ABS_X, be16_to_cpup((__be16 *)&data[4]));
524                 input_report_abs(input, ABS_Y, be16_to_cpup((__be16 *)&data[6]));
525                 input_report_abs(input, ABS_Z, be16_to_cpup((__be16 *)&data[8]));
526         } else if (features->type == CINTIQ_HYBRID) {
527                 /*
528                  * Do not send hardware buttons under Android. They
529                  * are already sent to the system through GPIO (and
530                  * have different meaning).
531                  *
532                  * d-pad right  -> data[4] & 0x10
533                  * d-pad up     -> data[4] & 0x20
534                  * d-pad left   -> data[4] & 0x40
535                  * d-pad down   -> data[4] & 0x80
536                  * d-pad center -> data[3] & 0x01
537                  */
538                 buttons = (data[4] << 1) | (data[3] & 0x01);
539         } else if (features->type == CINTIQ_COMPANION_2) {
540                 /* d-pad right  -> data[4] & 0x10
541                  * d-pad up     -> data[4] & 0x20
542                  * d-pad left   -> data[4] & 0x40
543                  * d-pad down   -> data[4] & 0x80
544                  * d-pad center -> data[3] & 0x01
545                  */
546                 buttons = ((data[2] >> 4) << 7) |
547                           ((data[1] & 0x04) << 6) |
548                           ((data[2] & 0x0F) << 2) |
549                           (data[1] & 0x03);
550         } else if (features->type >= INTUOS5S && features->type <= INTUOSPL) {
551                 /*
552                  * ExpressKeys on Intuos5/Intuos Pro have a capacitive sensor in
553                  * addition to the mechanical switch. Switch data is
554                  * stored in data[4], capacitive data in data[5].
555                  *
556                  * Touch ring mode switch (data[3]) has no capacitive sensor
557                  */
558                 buttons = (data[4] << 1) | (data[3] & 0x01);
559                 ring1 = data[2];
560         } else {
561                 if (features->type == WACOM_21UX2 || features->type == WACOM_22HD) {
562                         buttons = (data[8] << 10) | ((data[7] & 0x01) << 9) |
563                                   (data[6] << 1) | (data[5] & 0x01);
564
565                         if (features->type == WACOM_22HD) {
566                                 nkeys = 3;
567                                 keys = data[9] & 0x07;
568                         }
569                 } else {
570                         buttons = ((data[6] & 0x10) << 5)  |
571                                   ((data[5] & 0x10) << 4)  |
572                                   ((data[6] & 0x0F) << 4)  |
573                                   (data[5] & 0x0F);
574                 }
575                 strip1 = ((data[1] & 0x1f) << 8) | data[2];
576                 strip2 = ((data[3] & 0x1f) << 8) | data[4];
577         }
578
579         prox = (buttons & ~(~0 << nbuttons)) | (keys & ~(~0 << nkeys)) |
580                (ring1 & 0x80) | (ring2 & 0x80) | strip1 | strip2;
581
582         wacom_report_numbered_buttons(input, nbuttons, buttons);
583
584         for (i = 0; i < nkeys; i++)
585                 input_report_key(input, KEY_PROG1 + i, keys & (1 << i));
586
587         input_report_abs(input, ABS_RX, strip1);
588         input_report_abs(input, ABS_RY, strip2);
589
590         input_report_abs(input, ABS_WHEEL,    (ring1 & 0x80) ? (ring1 & 0x7f) : 0);
591         input_report_abs(input, ABS_THROTTLE, (ring2 & 0x80) ? (ring2 & 0x7f) : 0);
592
593         input_report_key(input, wacom->tool[1], prox ? 1 : 0);
594         input_report_abs(input, ABS_MISC, prox ? PAD_DEVICE_ID : 0);
595
596         input_event(input, EV_MSC, MSC_SERIAL, 0xffffffff);
597
598         return 1;
599 }
600
601 static int wacom_intuos_id_mangle(int tool_id)
602 {
603         return (tool_id & ~0xFFF) << 4 | (tool_id & 0xFFF);
604 }
605
606 static int wacom_intuos_get_tool_type(int tool_id)
607 {
608         int tool_type;
609
610         switch (tool_id) {
611         case 0x812: /* Inking pen */
612         case 0x801: /* Intuos3 Inking pen */
613         case 0x12802: /* Intuos4/5 Inking Pen */
614         case 0x012:
615                 tool_type = BTN_TOOL_PENCIL;
616                 break;
617
618         case 0x822: /* Pen */
619         case 0x842:
620         case 0x852:
621         case 0x823: /* Intuos3 Grip Pen */
622         case 0x813: /* Intuos3 Classic Pen */
623         case 0x885: /* Intuos3 Marker Pen */
624         case 0x802: /* Intuos4/5 13HD/24HD General Pen */
625         case 0x804: /* Intuos4/5 13HD/24HD Marker Pen */
626         case 0x8e2: /* IntuosHT2 pen */
627         case 0x022:
628         case 0x10804: /* Intuos4/5 13HD/24HD Art Pen */
629         case 0x14802: /* Intuos4/5 13HD/24HD Classic Pen */
630         case 0x16802: /* Cintiq 13HD Pro Pen */
631         case 0x18802: /* DTH2242 Pen */
632         case 0x10802: /* Intuos4/5 13HD/24HD General Pen */
633                 tool_type = BTN_TOOL_PEN;
634                 break;
635
636         case 0x832: /* Stroke pen */
637         case 0x032:
638                 tool_type = BTN_TOOL_BRUSH;
639                 break;
640
641         case 0x007: /* Mouse 4D and 2D */
642         case 0x09c:
643         case 0x094:
644         case 0x017: /* Intuos3 2D Mouse */
645         case 0x806: /* Intuos4 Mouse */
646                 tool_type = BTN_TOOL_MOUSE;
647                 break;
648
649         case 0x096: /* Lens cursor */
650         case 0x097: /* Intuos3 Lens cursor */
651         case 0x006: /* Intuos4 Lens cursor */
652                 tool_type = BTN_TOOL_LENS;
653                 break;
654
655         case 0x82a: /* Eraser */
656         case 0x84a:
657         case 0x85a:
658         case 0x91a:
659         case 0xd1a:
660         case 0x0fa:
661         case 0x82b: /* Intuos3 Grip Pen Eraser */
662         case 0x81b: /* Intuos3 Classic Pen Eraser */
663         case 0x91b: /* Intuos3 Airbrush Eraser */
664         case 0x80c: /* Intuos4/5 13HD/24HD Marker Pen Eraser */
665         case 0x80a: /* Intuos4/5 13HD/24HD General Pen Eraser */
666         case 0x90a: /* Intuos4/5 13HD/24HD Airbrush Eraser */
667         case 0x1480a: /* Intuos4/5 13HD/24HD Classic Pen Eraser */
668         case 0x1090a: /* Intuos4/5 13HD/24HD Airbrush Eraser */
669         case 0x1080c: /* Intuos4/5 13HD/24HD Art Pen Eraser */
670         case 0x1680a: /* Cintiq 13HD Pro Pen Eraser */
671         case 0x1880a: /* DTH2242 Eraser */
672         case 0x1080a: /* Intuos4/5 13HD/24HD General Pen Eraser */
673                 tool_type = BTN_TOOL_RUBBER;
674                 break;
675
676         case 0xd12:
677         case 0x912:
678         case 0x112:
679         case 0x913: /* Intuos3 Airbrush */
680         case 0x902: /* Intuos4/5 13HD/24HD Airbrush */
681         case 0x10902: /* Intuos4/5 13HD/24HD Airbrush */
682                 tool_type = BTN_TOOL_AIRBRUSH;
683                 break;
684
685         default: /* Unknown tool */
686                 tool_type = BTN_TOOL_PEN;
687                 break;
688         }
689         return tool_type;
690 }
691
692 static int wacom_intuos_inout(struct wacom_wac *wacom)
693 {
694         struct wacom_features *features = &wacom->features;
695         unsigned char *data = wacom->data;
696         struct input_dev *input = wacom->pen_input;
697         int idx = (features->type == INTUOS) ? (data[1] & 0x01) : 0;
698
699         if (!(((data[1] & 0xfc) == 0xc0) ||  /* in prox */
700             ((data[1] & 0xfe) == 0x20) ||    /* in range */
701             ((data[1] & 0xfe) == 0x80)))     /* out prox */
702                 return 0;
703
704         /* Enter report */
705         if ((data[1] & 0xfc) == 0xc0) {
706                 /* serial number of the tool */
707                 wacom->serial[idx] = ((data[3] & 0x0f) << 28) +
708                         (data[4] << 20) + (data[5] << 12) +
709                         (data[6] << 4) + (data[7] >> 4);
710
711                 wacom->id[idx] = (data[2] << 4) | (data[3] >> 4) |
712                      ((data[7] & 0x0f) << 16) | ((data[8] & 0xf0) << 8);
713
714                 wacom->tool[idx] = wacom_intuos_get_tool_type(wacom->id[idx]);
715
716                 wacom->shared->stylus_in_proximity = true;
717                 return 1;
718         }
719
720         /* in Range */
721         if ((data[1] & 0xfe) == 0x20) {
722                 if (features->type != INTUOSHT2)
723                         wacom->shared->stylus_in_proximity = true;
724
725                 /* in Range while exiting */
726                 if (wacom->reporting_data) {
727                         input_report_key(input, BTN_TOUCH, 0);
728                         input_report_abs(input, ABS_PRESSURE, 0);
729                         input_report_abs(input, ABS_DISTANCE, wacom->features.distance_max);
730                         return 2;
731                 }
732                 return 1;
733         }
734
735         /* Exit report */
736         if ((data[1] & 0xfe) == 0x80) {
737                 wacom->shared->stylus_in_proximity = false;
738                 wacom->reporting_data = false;
739
740                 /* don't report exit if we don't know the ID */
741                 if (!wacom->id[idx])
742                         return 1;
743
744                 /*
745                  * Reset all states otherwise we lose the initial states
746                  * when in-prox next time
747                  */
748                 input_report_abs(input, ABS_X, 0);
749                 input_report_abs(input, ABS_Y, 0);
750                 input_report_abs(input, ABS_DISTANCE, 0);
751                 input_report_abs(input, ABS_TILT_X, 0);
752                 input_report_abs(input, ABS_TILT_Y, 0);
753                 if (wacom->tool[idx] >= BTN_TOOL_MOUSE) {
754                         input_report_key(input, BTN_LEFT, 0);
755                         input_report_key(input, BTN_MIDDLE, 0);
756                         input_report_key(input, BTN_RIGHT, 0);
757                         input_report_key(input, BTN_SIDE, 0);
758                         input_report_key(input, BTN_EXTRA, 0);
759                         input_report_abs(input, ABS_THROTTLE, 0);
760                         input_report_abs(input, ABS_RZ, 0);
761                 } else {
762                         input_report_abs(input, ABS_PRESSURE, 0);
763                         input_report_key(input, BTN_STYLUS, 0);
764                         input_report_key(input, BTN_STYLUS2, 0);
765                         input_report_key(input, BTN_TOUCH, 0);
766                         input_report_abs(input, ABS_WHEEL, 0);
767                         if (features->type >= INTUOS3S)
768                                 input_report_abs(input, ABS_Z, 0);
769                 }
770                 input_report_key(input, wacom->tool[idx], 0);
771                 input_report_abs(input, ABS_MISC, 0); /* reset tool id */
772                 input_event(input, EV_MSC, MSC_SERIAL, wacom->serial[idx]);
773                 wacom->id[idx] = 0;
774                 return 2;
775         }
776
777         return 0;
778 }
779
780 static inline bool report_touch_events(struct wacom_wac *wacom)
781 {
782         return (touch_arbitration ? !wacom->shared->stylus_in_proximity : 1);
783 }
784
785 static inline bool delay_pen_events(struct wacom_wac *wacom)
786 {
787         return (wacom->shared->touch_down && touch_arbitration);
788 }
789
790 static int wacom_intuos_general(struct wacom_wac *wacom)
791 {
792         struct wacom_features *features = &wacom->features;
793         unsigned char *data = wacom->data;
794         struct input_dev *input = wacom->pen_input;
795         int idx = (features->type == INTUOS) ? (data[1] & 0x01) : 0;
796         unsigned char type = (data[1] >> 1) & 0x0F;
797         unsigned int x, y, distance, t;
798
799         if (data[0] != WACOM_REPORT_PENABLED && data[0] != WACOM_REPORT_CINTIQ &&
800                 data[0] != WACOM_REPORT_INTUOS_PEN)
801                 return 0;
802
803         if (delay_pen_events(wacom))
804                 return 1;
805
806         /* don't report events if we don't know the tool ID */
807         if (!wacom->id[idx]) {
808                 /* but reschedule a read of the current tool */
809                 wacom_intuos_schedule_prox_event(wacom);
810                 return 1;
811         }
812
813         /*
814          * don't report events for invalid data
815          */
816         /* older I4 styli don't work with new Cintiqs */
817         if ((!((wacom->id[idx] >> 16) & 0x01) &&
818                         (features->type == WACOM_21UX2)) ||
819             /* Only large Intuos support Lense Cursor */
820             (wacom->tool[idx] == BTN_TOOL_LENS &&
821                 (features->type == INTUOS3 ||
822                  features->type == INTUOS3S ||
823                  features->type == INTUOS4 ||
824                  features->type == INTUOS4S ||
825                  features->type == INTUOS5 ||
826                  features->type == INTUOS5S ||
827                  features->type == INTUOSPM ||
828                  features->type == INTUOSPS)) ||
829            /* Cintiq doesn't send data when RDY bit isn't set */
830            (features->type == CINTIQ && !(data[1] & 0x40)))
831                 return 1;
832
833         x = (be16_to_cpup((__be16 *)&data[2]) << 1) | ((data[9] >> 1) & 1);
834         y = (be16_to_cpup((__be16 *)&data[4]) << 1) | (data[9] & 1);
835         distance = data[9] >> 2;
836         if (features->type < INTUOS3S) {
837                 x >>= 1;
838                 y >>= 1;
839                 distance >>= 1;
840         }
841         input_report_abs(input, ABS_X, x);
842         input_report_abs(input, ABS_Y, y);
843         input_report_abs(input, ABS_DISTANCE, distance);
844
845         switch (type) {
846         case 0x00:
847         case 0x01:
848         case 0x02:
849         case 0x03:
850                 /* general pen packet */
851                 t = (data[6] << 3) | ((data[7] & 0xC0) >> 5) | (data[1] & 1);
852                 if (features->pressure_max < 2047)
853                         t >>= 1;
854                 input_report_abs(input, ABS_PRESSURE, t);
855                 if (features->type != INTUOSHT2) {
856                     input_report_abs(input, ABS_TILT_X,
857                                  (((data[7] << 1) & 0x7e) | (data[8] >> 7)) - 64);
858                     input_report_abs(input, ABS_TILT_Y, (data[8] & 0x7f) - 64);
859                 }
860                 input_report_key(input, BTN_STYLUS, data[1] & 2);
861                 input_report_key(input, BTN_STYLUS2, data[1] & 4);
862                 input_report_key(input, BTN_TOUCH, t > 10);
863                 break;
864
865         case 0x0a:
866                 /* airbrush second packet */
867                 input_report_abs(input, ABS_WHEEL,
868                                 (data[6] << 2) | ((data[7] >> 6) & 3));
869                 input_report_abs(input, ABS_TILT_X,
870                                  (((data[7] << 1) & 0x7e) | (data[8] >> 7)) - 64);
871                 input_report_abs(input, ABS_TILT_Y, (data[8] & 0x7f) - 64);
872                 break;
873
874         case 0x05:
875                 /* Rotation packet */
876                 if (features->type >= INTUOS3S) {
877                         /* I3 marker pen rotation */
878                         t = (data[6] << 3) | ((data[7] >> 5) & 7);
879                         t = (data[7] & 0x20) ? ((t > 900) ? ((t-1) / 2 - 1350) :
880                                 ((t-1) / 2 + 450)) : (450 - t / 2) ;
881                         input_report_abs(input, ABS_Z, t);
882                 } else {
883                         /* 4D mouse 2nd packet */
884                         t = (data[6] << 3) | ((data[7] >> 5) & 7);
885                         input_report_abs(input, ABS_RZ, (data[7] & 0x20) ?
886                                 ((t - 1) / 2) : -t / 2);
887                 }
888                 break;
889
890         case 0x04:
891                 /* 4D mouse 1st packet */
892                 input_report_key(input, BTN_LEFT,   data[8] & 0x01);
893                 input_report_key(input, BTN_MIDDLE, data[8] & 0x02);
894                 input_report_key(input, BTN_RIGHT,  data[8] & 0x04);
895
896                 input_report_key(input, BTN_SIDE,   data[8] & 0x20);
897                 input_report_key(input, BTN_EXTRA,  data[8] & 0x10);
898                 t = (data[6] << 2) | ((data[7] >> 6) & 3);
899                 input_report_abs(input, ABS_THROTTLE, (data[8] & 0x08) ? -t : t);
900                 break;
901
902         case 0x06:
903                 /* I4 mouse */
904                 input_report_key(input, BTN_LEFT,   data[6] & 0x01);
905                 input_report_key(input, BTN_MIDDLE, data[6] & 0x02);
906                 input_report_key(input, BTN_RIGHT,  data[6] & 0x04);
907                 input_report_rel(input, REL_WHEEL, ((data[7] & 0x80) >> 7)
908                                  - ((data[7] & 0x40) >> 6));
909                 input_report_key(input, BTN_SIDE,   data[6] & 0x08);
910                 input_report_key(input, BTN_EXTRA,  data[6] & 0x10);
911
912                 input_report_abs(input, ABS_TILT_X,
913                         (((data[7] << 1) & 0x7e) | (data[8] >> 7)) - 64);
914                 input_report_abs(input, ABS_TILT_Y, (data[8] & 0x7f) - 64);
915                 break;
916
917         case 0x08:
918                 if (wacom->tool[idx] == BTN_TOOL_MOUSE) {
919                         /* 2D mouse packet */
920                         input_report_key(input, BTN_LEFT,   data[8] & 0x04);
921                         input_report_key(input, BTN_MIDDLE, data[8] & 0x08);
922                         input_report_key(input, BTN_RIGHT,  data[8] & 0x10);
923                         input_report_rel(input, REL_WHEEL, (data[8] & 0x01)
924                                          - ((data[8] & 0x02) >> 1));
925
926                         /* I3 2D mouse side buttons */
927                         if (features->type >= INTUOS3S && features->type <= INTUOS3L) {
928                                 input_report_key(input, BTN_SIDE,   data[8] & 0x40);
929                                 input_report_key(input, BTN_EXTRA,  data[8] & 0x20);
930                         }
931                 }
932                 else if (wacom->tool[idx] == BTN_TOOL_LENS) {
933                         /* Lens cursor packets */
934                         input_report_key(input, BTN_LEFT,   data[8] & 0x01);
935                         input_report_key(input, BTN_MIDDLE, data[8] & 0x02);
936                         input_report_key(input, BTN_RIGHT,  data[8] & 0x04);
937                         input_report_key(input, BTN_SIDE,   data[8] & 0x10);
938                         input_report_key(input, BTN_EXTRA,  data[8] & 0x08);
939                 }
940                 break;
941
942         case 0x07:
943         case 0x09:
944         case 0x0b:
945         case 0x0c:
946         case 0x0d:
947         case 0x0e:
948         case 0x0f:
949                 /* unhandled */
950                 break;
951         }
952
953         input_report_abs(input, ABS_MISC,
954                          wacom_intuos_id_mangle(wacom->id[idx])); /* report tool id */
955         input_report_key(input, wacom->tool[idx], 1);
956         input_event(input, EV_MSC, MSC_SERIAL, wacom->serial[idx]);
957         wacom->reporting_data = true;
958         return 2;
959 }
960
961 static int wacom_intuos_irq(struct wacom_wac *wacom)
962 {
963         unsigned char *data = wacom->data;
964         struct input_dev *input = wacom->pen_input;
965         int result;
966
967         if (data[0] != WACOM_REPORT_PENABLED &&
968             data[0] != WACOM_REPORT_INTUOS_ID1 &&
969             data[0] != WACOM_REPORT_INTUOS_ID2 &&
970             data[0] != WACOM_REPORT_INTUOSPAD &&
971             data[0] != WACOM_REPORT_INTUOS_PEN &&
972             data[0] != WACOM_REPORT_CINTIQ &&
973             data[0] != WACOM_REPORT_CINTIQPAD &&
974             data[0] != WACOM_REPORT_INTUOS5PAD) {
975                 dev_dbg(input->dev.parent,
976                         "%s: received unknown report #%d\n", __func__, data[0]);
977                 return 0;
978         }
979
980         /* process pad events */
981         result = wacom_intuos_pad(wacom);
982         if (result)
983                 return result;
984
985         /* process in/out prox events */
986         result = wacom_intuos_inout(wacom);
987         if (result)
988                 return result - 1;
989
990         /* process general packets */
991         result = wacom_intuos_general(wacom);
992         if (result)
993                 return result - 1;
994
995         return 0;
996 }
997
998 static int wacom_remote_irq(struct wacom_wac *wacom_wac, size_t len)
999 {
1000         unsigned char *data = wacom_wac->data;
1001         struct input_dev *input;
1002         struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
1003         struct wacom_remote *remote = wacom->remote;
1004         int bat_charging, bat_percent, touch_ring_mode;
1005         __u32 serial;
1006         int i, index = -1;
1007         unsigned long flags;
1008
1009         if (data[0] != WACOM_REPORT_REMOTE) {
1010                 hid_dbg(wacom->hdev, "%s: received unknown report #%d",
1011                         __func__, data[0]);
1012                 return 0;
1013         }
1014
1015         serial = data[3] + (data[4] << 8) + (data[5] << 16);
1016         wacom_wac->id[0] = PAD_DEVICE_ID;
1017
1018         spin_lock_irqsave(&remote->remote_lock, flags);
1019
1020         for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1021                 if (remote->remotes[i].serial == serial) {
1022                         index = i;
1023                         break;
1024                 }
1025         }
1026
1027         if (index < 0 || !remote->remotes[index].registered)
1028                 goto out;
1029
1030         input = remote->remotes[index].input;
1031
1032         input_report_key(input, BTN_0, (data[9] & 0x01));
1033         input_report_key(input, BTN_1, (data[9] & 0x02));
1034         input_report_key(input, BTN_2, (data[9] & 0x04));
1035         input_report_key(input, BTN_3, (data[9] & 0x08));
1036         input_report_key(input, BTN_4, (data[9] & 0x10));
1037         input_report_key(input, BTN_5, (data[9] & 0x20));
1038         input_report_key(input, BTN_6, (data[9] & 0x40));
1039         input_report_key(input, BTN_7, (data[9] & 0x80));
1040
1041         input_report_key(input, BTN_8, (data[10] & 0x01));
1042         input_report_key(input, BTN_9, (data[10] & 0x02));
1043         input_report_key(input, BTN_A, (data[10] & 0x04));
1044         input_report_key(input, BTN_B, (data[10] & 0x08));
1045         input_report_key(input, BTN_C, (data[10] & 0x10));
1046         input_report_key(input, BTN_X, (data[10] & 0x20));
1047         input_report_key(input, BTN_Y, (data[10] & 0x40));
1048         input_report_key(input, BTN_Z, (data[10] & 0x80));
1049
1050         input_report_key(input, BTN_BASE, (data[11] & 0x01));
1051         input_report_key(input, BTN_BASE2, (data[11] & 0x02));
1052
1053         if (data[12] & 0x80)
1054                 input_report_abs(input, ABS_WHEEL, (data[12] & 0x7f));
1055         else
1056                 input_report_abs(input, ABS_WHEEL, 0);
1057
1058         bat_percent = data[7] & 0x7f;
1059         bat_charging = !!(data[7] & 0x80);
1060
1061         if (data[9] | data[10] | (data[11] & 0x03) | data[12])
1062                 input_report_abs(input, ABS_MISC, PAD_DEVICE_ID);
1063         else
1064                 input_report_abs(input, ABS_MISC, 0);
1065
1066         input_event(input, EV_MSC, MSC_SERIAL, serial);
1067
1068         input_sync(input);
1069
1070         /*Which mode select (LED light) is currently on?*/
1071         touch_ring_mode = (data[11] & 0xC0) >> 6;
1072
1073         for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1074                 if (remote->remotes[i].serial == serial)
1075                         wacom->led.groups[i].select = touch_ring_mode;
1076         }
1077
1078         __wacom_notify_battery(&remote->remotes[index].battery,
1079                                 WACOM_POWER_SUPPLY_STATUS_AUTO, bat_percent,
1080                                 bat_charging, 1, bat_charging);
1081
1082 out:
1083         spin_unlock_irqrestore(&remote->remote_lock, flags);
1084         return 0;
1085 }
1086
1087 static void wacom_remote_status_irq(struct wacom_wac *wacom_wac, size_t len)
1088 {
1089         struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
1090         unsigned char *data = wacom_wac->data;
1091         struct wacom_remote *remote = wacom->remote;
1092         struct wacom_remote_data remote_data;
1093         unsigned long flags;
1094         int i, ret;
1095
1096         if (data[0] != WACOM_REPORT_DEVICE_LIST)
1097                 return;
1098
1099         memset(&remote_data, 0, sizeof(struct wacom_remote_data));
1100
1101         for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1102                 int j = i * 6;
1103                 int serial = (data[j+6] << 16) + (data[j+5] << 8) + data[j+4];
1104                 bool connected = data[j+2];
1105
1106                 remote_data.remote[i].serial = serial;
1107                 remote_data.remote[i].connected = connected;
1108         }
1109
1110         spin_lock_irqsave(&remote->remote_lock, flags);
1111
1112         ret = kfifo_in(&remote->remote_fifo, &remote_data, sizeof(remote_data));
1113         if (ret != sizeof(remote_data)) {
1114                 spin_unlock_irqrestore(&remote->remote_lock, flags);
1115                 hid_err(wacom->hdev, "Can't queue Remote status event.\n");
1116                 return;
1117         }
1118
1119         spin_unlock_irqrestore(&remote->remote_lock, flags);
1120
1121         wacom_schedule_work(wacom_wac, WACOM_WORKER_REMOTE);
1122 }
1123
1124 static int int_dist(int x1, int y1, int x2, int y2)
1125 {
1126         int x = x2 - x1;
1127         int y = y2 - y1;
1128
1129         return int_sqrt(x*x + y*y);
1130 }
1131
1132 static void wacom_intuos_bt_process_data(struct wacom_wac *wacom,
1133                 unsigned char *data)
1134 {
1135         memcpy(wacom->data, data, 10);
1136         wacom_intuos_irq(wacom);
1137
1138         input_sync(wacom->pen_input);
1139         if (wacom->pad_input)
1140                 input_sync(wacom->pad_input);
1141 }
1142
1143 static int wacom_intuos_bt_irq(struct wacom_wac *wacom, size_t len)
1144 {
1145         unsigned char data[WACOM_PKGLEN_MAX];
1146         int i = 1;
1147         unsigned power_raw, battery_capacity, bat_charging, ps_connected;
1148
1149         memcpy(data, wacom->data, len);
1150
1151         switch (data[0]) {
1152         case 0x04:
1153                 wacom_intuos_bt_process_data(wacom, data + i);
1154                 i += 10;
1155                 /* fall through */
1156         case 0x03:
1157                 wacom_intuos_bt_process_data(wacom, data + i);
1158                 i += 10;
1159                 wacom_intuos_bt_process_data(wacom, data + i);
1160                 i += 10;
1161                 power_raw = data[i];
1162                 bat_charging = (power_raw & 0x08) ? 1 : 0;
1163                 ps_connected = (power_raw & 0x10) ? 1 : 0;
1164                 battery_capacity = batcap_i4[power_raw & 0x07];
1165                 wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
1166                                      battery_capacity, bat_charging,
1167                                      battery_capacity || bat_charging,
1168                                      ps_connected);
1169                 break;
1170         default:
1171                 dev_dbg(wacom->pen_input->dev.parent,
1172                                 "Unknown report: %d,%d size:%zu\n",
1173                                 data[0], data[1], len);
1174                 return 0;
1175         }
1176         return 0;
1177 }
1178
1179 static int wacom_wac_finger_count_touches(struct wacom_wac *wacom)
1180 {
1181         struct input_dev *input = wacom->touch_input;
1182         unsigned touch_max = wacom->features.touch_max;
1183         int count = 0;
1184         int i;
1185
1186         if (!touch_max)
1187                 return 0;
1188
1189         if (touch_max == 1)
1190                 return test_bit(BTN_TOUCH, input->key) &&
1191                         report_touch_events(wacom);
1192
1193         for (i = 0; i < input->mt->num_slots; i++) {
1194                 struct input_mt_slot *ps = &input->mt->slots[i];
1195                 int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
1196                 if (id >= 0)
1197                         count++;
1198         }
1199
1200         return count;
1201 }
1202
1203 static void wacom_intuos_pro2_bt_pen(struct wacom_wac *wacom)
1204 {
1205         int pen_frame_len, pen_frames;
1206
1207         struct input_dev *pen_input = wacom->pen_input;
1208         unsigned char *data = wacom->data;
1209         int i;
1210
1211         if (wacom->features.type == INTUOSP2_BT) {
1212                 wacom->serial[0] = get_unaligned_le64(&data[99]);
1213                 wacom->id[0]     = get_unaligned_le16(&data[107]);
1214                 pen_frame_len = 14;
1215                 pen_frames = 7;
1216         } else {
1217                 wacom->serial[0] = get_unaligned_le64(&data[33]);
1218                 wacom->id[0]     = get_unaligned_le16(&data[41]);
1219                 pen_frame_len = 8;
1220                 pen_frames = 4;
1221         }
1222
1223         if (wacom->serial[0] >> 52 == 1) {
1224                 /* Add back in missing bits of ID for non-USI pens */
1225                 wacom->id[0] |= (wacom->serial[0] >> 32) & 0xFFFFF;
1226         }
1227         wacom->tool[0]   = wacom_intuos_get_tool_type(wacom_intuos_id_mangle(wacom->id[0]));
1228
1229         for (i = 0; i < pen_frames; i++) {
1230                 unsigned char *frame = &data[i*pen_frame_len + 1];
1231                 bool valid = frame[0] & 0x80;
1232                 bool prox = frame[0] & 0x40;
1233                 bool range = frame[0] & 0x20;
1234
1235                 if (!valid)
1236                         continue;
1237
1238                 if (range) {
1239                         input_report_abs(pen_input, ABS_X, get_unaligned_le16(&frame[1]));
1240                         input_report_abs(pen_input, ABS_Y, get_unaligned_le16(&frame[3]));
1241
1242                         if (wacom->features.type == INTUOSP2_BT) {
1243                                 /* Fix rotation alignment: userspace expects zero at left */
1244                                 int16_t rotation =
1245                                         (int16_t)get_unaligned_le16(&frame[9]);
1246                                 rotation += 1800/4;
1247
1248                                 if (rotation > 899)
1249                                         rotation -= 1800;
1250
1251                                 input_report_abs(pen_input, ABS_TILT_X,
1252                                                  (char)frame[7]);
1253                                 input_report_abs(pen_input, ABS_TILT_Y,
1254                                                  (char)frame[8]);
1255                                 input_report_abs(pen_input, ABS_Z, rotation);
1256                                 input_report_abs(pen_input, ABS_WHEEL,
1257                                                  get_unaligned_le16(&frame[11]));
1258                         }
1259                 }
1260                 input_report_abs(pen_input, ABS_PRESSURE, get_unaligned_le16(&frame[5]));
1261                 if (wacom->features.type == INTUOSP2_BT) {
1262                         input_report_abs(pen_input, ABS_DISTANCE,
1263                                          range ? frame[13] : wacom->features.distance_max);
1264                 } else {
1265                         input_report_abs(pen_input, ABS_DISTANCE,
1266                                          range ? frame[7] : wacom->features.distance_max);
1267                 }
1268
1269                 input_report_key(pen_input, BTN_TOUCH, frame[0] & 0x01);
1270                 input_report_key(pen_input, BTN_STYLUS, frame[0] & 0x02);
1271                 input_report_key(pen_input, BTN_STYLUS2, frame[0] & 0x04);
1272
1273                 input_report_key(pen_input, wacom->tool[0], prox);
1274                 input_event(pen_input, EV_MSC, MSC_SERIAL, wacom->serial[0]);
1275                 input_report_abs(pen_input, ABS_MISC,
1276                                  wacom_intuos_id_mangle(wacom->id[0])); /* report tool id */
1277
1278                 wacom->shared->stylus_in_proximity = prox;
1279
1280                 input_sync(pen_input);
1281         }
1282 }
1283
1284 static void wacom_intuos_pro2_bt_touch(struct wacom_wac *wacom)
1285 {
1286         const int finger_touch_len = 8;
1287         const int finger_frames = 4;
1288         const int finger_frame_len = 43;
1289
1290         struct input_dev *touch_input = wacom->touch_input;
1291         unsigned char *data = wacom->data;
1292         int num_contacts_left = 5;
1293         int i, j;
1294
1295         for (i = 0; i < finger_frames; i++) {
1296                 unsigned char *frame = &data[i*finger_frame_len + 109];
1297                 int current_num_contacts = frame[0] & 0x7F;
1298                 int contacts_to_send;
1299
1300                 if (!(frame[0] & 0x80))
1301                         continue;
1302
1303                 /*
1304                  * First packet resets the counter since only the first
1305                  * packet in series will have non-zero current_num_contacts.
1306                  */
1307                 if (current_num_contacts)
1308                         wacom->num_contacts_left = current_num_contacts;
1309
1310                 contacts_to_send = min(num_contacts_left, wacom->num_contacts_left);
1311
1312                 for (j = 0; j < contacts_to_send; j++) {
1313                         unsigned char *touch = &frame[j*finger_touch_len + 1];
1314                         int slot = input_mt_get_slot_by_key(touch_input, touch[0]);
1315                         int x = get_unaligned_le16(&touch[2]);
1316                         int y = get_unaligned_le16(&touch[4]);
1317                         int w = touch[6] * input_abs_get_res(touch_input, ABS_MT_POSITION_X);
1318                         int h = touch[7] * input_abs_get_res(touch_input, ABS_MT_POSITION_Y);
1319
1320                         if (slot < 0)
1321                                 continue;
1322
1323                         input_mt_slot(touch_input, slot);
1324                         input_mt_report_slot_state(touch_input, MT_TOOL_FINGER, touch[1] & 0x01);
1325                         input_report_abs(touch_input, ABS_MT_POSITION_X, x);
1326                         input_report_abs(touch_input, ABS_MT_POSITION_Y, y);
1327                         input_report_abs(touch_input, ABS_MT_TOUCH_MAJOR, max(w, h));
1328                         input_report_abs(touch_input, ABS_MT_TOUCH_MINOR, min(w, h));
1329                         input_report_abs(touch_input, ABS_MT_ORIENTATION, w > h);
1330                 }
1331
1332                 input_mt_sync_frame(touch_input);
1333
1334                 wacom->num_contacts_left -= contacts_to_send;
1335                 if (wacom->num_contacts_left <= 0) {
1336                         wacom->num_contacts_left = 0;
1337                         wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1338                 }
1339         }
1340
1341         input_report_switch(touch_input, SW_MUTE_DEVICE, !(data[281] >> 7));
1342         input_sync(touch_input);
1343 }
1344
1345 static void wacom_intuos_pro2_bt_pad(struct wacom_wac *wacom)
1346 {
1347         struct input_dev *pad_input = wacom->pad_input;
1348         unsigned char *data = wacom->data;
1349
1350         int buttons = (data[282] << 1) | ((data[281] >> 6) & 0x01);
1351         int ring = data[285] & 0x7F;
1352         bool ringstatus = data[285] & 0x80;
1353         bool prox = buttons || ringstatus;
1354
1355         /* Fix touchring data: userspace expects 0 at left and increasing clockwise */
1356         ring = 71 - ring;
1357         ring += 3*72/16;
1358         if (ring > 71)
1359                 ring -= 72;
1360
1361         wacom_report_numbered_buttons(pad_input, 9, buttons);
1362
1363         input_report_abs(pad_input, ABS_WHEEL, ringstatus ? ring : 0);
1364
1365         input_report_key(pad_input, wacom->tool[1], prox ? 1 : 0);
1366         input_report_abs(pad_input, ABS_MISC, prox ? PAD_DEVICE_ID : 0);
1367         input_event(pad_input, EV_MSC, MSC_SERIAL, 0xffffffff);
1368
1369         input_sync(pad_input);
1370 }
1371
1372 static void wacom_intuos_pro2_bt_battery(struct wacom_wac *wacom)
1373 {
1374         unsigned char *data = wacom->data;
1375
1376         bool chg = data[284] & 0x80;
1377         int battery_status = data[284] & 0x7F;
1378
1379         wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
1380                              battery_status, chg, 1, chg);
1381 }
1382
1383 static void wacom_intuos_gen3_bt_pad(struct wacom_wac *wacom)
1384 {
1385         struct input_dev *pad_input = wacom->pad_input;
1386         unsigned char *data = wacom->data;
1387
1388         int buttons = data[44];
1389
1390         wacom_report_numbered_buttons(pad_input, 4, buttons);
1391
1392         input_report_key(pad_input, wacom->tool[1], buttons ? 1 : 0);
1393         input_report_abs(pad_input, ABS_MISC, buttons ? PAD_DEVICE_ID : 0);
1394         input_event(pad_input, EV_MSC, MSC_SERIAL, 0xffffffff);
1395
1396         input_sync(pad_input);
1397 }
1398
1399 static void wacom_intuos_gen3_bt_battery(struct wacom_wac *wacom)
1400 {
1401         unsigned char *data = wacom->data;
1402
1403         bool chg = data[45] & 0x80;
1404         int battery_status = data[45] & 0x7F;
1405
1406         wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
1407                              battery_status, chg, 1, chg);
1408 }
1409
1410 static int wacom_intuos_pro2_bt_irq(struct wacom_wac *wacom, size_t len)
1411 {
1412         unsigned char *data = wacom->data;
1413
1414         if (data[0] != 0x80 && data[0] != 0x81) {
1415                 dev_dbg(wacom->pen_input->dev.parent,
1416                         "%s: received unknown report #%d\n", __func__, data[0]);
1417                 return 0;
1418         }
1419
1420         wacom_intuos_pro2_bt_pen(wacom);
1421         if (wacom->features.type == INTUOSP2_BT) {
1422                 wacom_intuos_pro2_bt_touch(wacom);
1423                 wacom_intuos_pro2_bt_pad(wacom);
1424                 wacom_intuos_pro2_bt_battery(wacom);
1425         } else {
1426                 wacom_intuos_gen3_bt_pad(wacom);
1427                 wacom_intuos_gen3_bt_battery(wacom);
1428         }
1429         return 0;
1430 }
1431
1432 static int wacom_24hdt_irq(struct wacom_wac *wacom)
1433 {
1434         struct input_dev *input = wacom->touch_input;
1435         unsigned char *data = wacom->data;
1436         int i;
1437         int current_num_contacts = data[61];
1438         int contacts_to_send = 0;
1439         int num_contacts_left = 4; /* maximum contacts per packet */
1440         int byte_per_packet = WACOM_BYTES_PER_24HDT_PACKET;
1441         int y_offset = 2;
1442
1443         if (wacom->features.type == WACOM_27QHDT) {
1444                 current_num_contacts = data[63];
1445                 num_contacts_left = 10;
1446                 byte_per_packet = WACOM_BYTES_PER_QHDTHID_PACKET;
1447                 y_offset = 0;
1448         }
1449
1450         /*
1451          * First packet resets the counter since only the first
1452          * packet in series will have non-zero current_num_contacts.
1453          */
1454         if (current_num_contacts)
1455                 wacom->num_contacts_left = current_num_contacts;
1456
1457         contacts_to_send = min(num_contacts_left, wacom->num_contacts_left);
1458
1459         for (i = 0; i < contacts_to_send; i++) {
1460                 int offset = (byte_per_packet * i) + 1;
1461                 bool touch = (data[offset] & 0x1) && report_touch_events(wacom);
1462                 int slot = input_mt_get_slot_by_key(input, data[offset + 1]);
1463
1464                 if (slot < 0)
1465                         continue;
1466                 input_mt_slot(input, slot);
1467                 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1468
1469                 if (touch) {
1470                         int t_x = get_unaligned_le16(&data[offset + 2]);
1471                         int t_y = get_unaligned_le16(&data[offset + 4 + y_offset]);
1472
1473                         input_report_abs(input, ABS_MT_POSITION_X, t_x);
1474                         input_report_abs(input, ABS_MT_POSITION_Y, t_y);
1475
1476                         if (wacom->features.type != WACOM_27QHDT) {
1477                                 int c_x = get_unaligned_le16(&data[offset + 4]);
1478                                 int c_y = get_unaligned_le16(&data[offset + 8]);
1479                                 int w = get_unaligned_le16(&data[offset + 10]);
1480                                 int h = get_unaligned_le16(&data[offset + 12]);
1481
1482                                 input_report_abs(input, ABS_MT_TOUCH_MAJOR, min(w,h));
1483                                 input_report_abs(input, ABS_MT_WIDTH_MAJOR,
1484                                                  min(w, h) + int_dist(t_x, t_y, c_x, c_y));
1485                                 input_report_abs(input, ABS_MT_WIDTH_MINOR, min(w, h));
1486                                 input_report_abs(input, ABS_MT_ORIENTATION, w > h);
1487                         }
1488                 }
1489         }
1490         input_mt_sync_frame(input);
1491
1492         wacom->num_contacts_left -= contacts_to_send;
1493         if (wacom->num_contacts_left <= 0) {
1494                 wacom->num_contacts_left = 0;
1495                 wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1496         }
1497         return 1;
1498 }
1499
1500 static int wacom_mt_touch(struct wacom_wac *wacom)
1501 {
1502         struct input_dev *input = wacom->touch_input;
1503         unsigned char *data = wacom->data;
1504         int i;
1505         int current_num_contacts = data[2];
1506         int contacts_to_send = 0;
1507         int x_offset = 0;
1508
1509         /* MTTPC does not support Height and Width */
1510         if (wacom->features.type == MTTPC || wacom->features.type == MTTPC_B)
1511                 x_offset = -4;
1512
1513         /*
1514          * First packet resets the counter since only the first
1515          * packet in series will have non-zero current_num_contacts.
1516          */
1517         if (current_num_contacts)
1518                 wacom->num_contacts_left = current_num_contacts;
1519
1520         /* There are at most 5 contacts per packet */
1521         contacts_to_send = min(5, wacom->num_contacts_left);
1522
1523         for (i = 0; i < contacts_to_send; i++) {
1524                 int offset = (WACOM_BYTES_PER_MT_PACKET + x_offset) * i + 3;
1525                 bool touch = (data[offset] & 0x1) && report_touch_events(wacom);
1526                 int id = get_unaligned_le16(&data[offset + 1]);
1527                 int slot = input_mt_get_slot_by_key(input, id);
1528
1529                 if (slot < 0)
1530                         continue;
1531
1532                 input_mt_slot(input, slot);
1533                 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1534                 if (touch) {
1535                         int x = get_unaligned_le16(&data[offset + x_offset + 7]);
1536                         int y = get_unaligned_le16(&data[offset + x_offset + 9]);
1537                         input_report_abs(input, ABS_MT_POSITION_X, x);
1538                         input_report_abs(input, ABS_MT_POSITION_Y, y);
1539                 }
1540         }
1541         input_mt_sync_frame(input);
1542
1543         wacom->num_contacts_left -= contacts_to_send;
1544         if (wacom->num_contacts_left <= 0) {
1545                 wacom->num_contacts_left = 0;
1546                 wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1547         }
1548         return 1;
1549 }
1550
1551 static int wacom_tpc_mt_touch(struct wacom_wac *wacom)
1552 {
1553         struct input_dev *input = wacom->touch_input;
1554         unsigned char *data = wacom->data;
1555         int i;
1556
1557         for (i = 0; i < 2; i++) {
1558                 int p = data[1] & (1 << i);
1559                 bool touch = p && report_touch_events(wacom);
1560
1561                 input_mt_slot(input, i);
1562                 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1563                 if (touch) {
1564                         int x = le16_to_cpup((__le16 *)&data[i * 2 + 2]) & 0x7fff;
1565                         int y = le16_to_cpup((__le16 *)&data[i * 2 + 6]) & 0x7fff;
1566
1567                         input_report_abs(input, ABS_MT_POSITION_X, x);
1568                         input_report_abs(input, ABS_MT_POSITION_Y, y);
1569                 }
1570         }
1571         input_mt_sync_frame(input);
1572
1573         /* keep touch state for pen event */
1574         wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1575
1576         return 1;
1577 }
1578
1579 static int wacom_tpc_single_touch(struct wacom_wac *wacom, size_t len)
1580 {
1581         unsigned char *data = wacom->data;
1582         struct input_dev *input = wacom->touch_input;
1583         bool prox = report_touch_events(wacom);
1584         int x = 0, y = 0;
1585
1586         if (wacom->features.touch_max > 1 || len > WACOM_PKGLEN_TPC2FG)
1587                 return 0;
1588
1589         if (len == WACOM_PKGLEN_TPC1FG) {
1590                 prox = prox && (data[0] & 0x01);
1591                 x = get_unaligned_le16(&data[1]);
1592                 y = get_unaligned_le16(&data[3]);
1593         } else if (len == WACOM_PKGLEN_TPC1FG_B) {
1594                 prox = prox && (data[2] & 0x01);
1595                 x = get_unaligned_le16(&data[3]);
1596                 y = get_unaligned_le16(&data[5]);
1597         } else {
1598                 prox = prox && (data[1] & 0x01);
1599                 x = le16_to_cpup((__le16 *)&data[2]);
1600                 y = le16_to_cpup((__le16 *)&data[4]);
1601         }
1602
1603         if (prox) {
1604                 input_report_abs(input, ABS_X, x);
1605                 input_report_abs(input, ABS_Y, y);
1606         }
1607         input_report_key(input, BTN_TOUCH, prox);
1608
1609         /* keep touch state for pen events */
1610         wacom->shared->touch_down = prox;
1611
1612         return 1;
1613 }
1614
1615 static int wacom_tpc_pen(struct wacom_wac *wacom)
1616 {
1617         unsigned char *data = wacom->data;
1618         struct input_dev *input = wacom->pen_input;
1619         bool prox = data[1] & 0x20;
1620
1621         if (!wacom->shared->stylus_in_proximity) /* first in prox */
1622                 /* Going into proximity select tool */
1623                 wacom->tool[0] = (data[1] & 0x0c) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
1624
1625         /* keep pen state for touch events */
1626         wacom->shared->stylus_in_proximity = prox;
1627
1628         /* send pen events only when touch is up or forced out
1629          * or touch arbitration is off
1630          */
1631         if (!delay_pen_events(wacom)) {
1632                 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
1633                 input_report_key(input, BTN_STYLUS2, data[1] & 0x10);
1634                 input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
1635                 input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
1636                 input_report_abs(input, ABS_PRESSURE, ((data[7] & 0x07) << 8) | data[6]);
1637                 input_report_key(input, BTN_TOUCH, data[1] & 0x05);
1638                 input_report_key(input, wacom->tool[0], prox);
1639                 return 1;
1640         }
1641
1642         return 0;
1643 }
1644
1645 static int wacom_tpc_irq(struct wacom_wac *wacom, size_t len)
1646 {
1647         unsigned char *data = wacom->data;
1648
1649         if (wacom->pen_input) {
1650                 dev_dbg(wacom->pen_input->dev.parent,
1651                         "%s: received report #%d\n", __func__, data[0]);
1652
1653                 if (len == WACOM_PKGLEN_PENABLED ||
1654                     data[0] == WACOM_REPORT_PENABLED)
1655                         return wacom_tpc_pen(wacom);
1656         }
1657         else if (wacom->touch_input) {
1658                 dev_dbg(wacom->touch_input->dev.parent,
1659                         "%s: received report #%d\n", __func__, data[0]);
1660
1661                 switch (len) {
1662                 case WACOM_PKGLEN_TPC1FG:
1663                         return wacom_tpc_single_touch(wacom, len);
1664
1665                 case WACOM_PKGLEN_TPC2FG:
1666                         return wacom_tpc_mt_touch(wacom);
1667
1668                 default:
1669                         switch (data[0]) {
1670                         case WACOM_REPORT_TPC1FG:
1671                         case WACOM_REPORT_TPCHID:
1672                         case WACOM_REPORT_TPCST:
1673                         case WACOM_REPORT_TPC1FGE:
1674                                 return wacom_tpc_single_touch(wacom, len);
1675
1676                         case WACOM_REPORT_TPCMT:
1677                         case WACOM_REPORT_TPCMT2:
1678                                 return wacom_mt_touch(wacom);
1679
1680                         }
1681                 }
1682         }
1683
1684         return 0;
1685 }
1686
1687 static int wacom_offset_rotation(struct input_dev *input, struct hid_usage *usage,
1688                                  int value, int num, int denom)
1689 {
1690         struct input_absinfo *abs = &input->absinfo[usage->code];
1691         int range = (abs->maximum - abs->minimum + 1);
1692
1693         value += num*range/denom;
1694         if (value > abs->maximum)
1695                 value -= range;
1696         else if (value < abs->minimum)
1697                 value += range;
1698         return value;
1699 }
1700
1701 int wacom_equivalent_usage(int usage)
1702 {
1703         if ((usage & HID_USAGE_PAGE) == WACOM_HID_UP_WACOMDIGITIZER) {
1704                 int subpage = (usage & 0xFF00) << 8;
1705                 int subusage = (usage & 0xFF);
1706
1707                 if (subpage == WACOM_HID_SP_PAD ||
1708                     subpage == WACOM_HID_SP_BUTTON ||
1709                     subpage == WACOM_HID_SP_DIGITIZER ||
1710                     subpage == WACOM_HID_SP_DIGITIZERINFO ||
1711                     usage == WACOM_HID_WD_SENSE ||
1712                     usage == WACOM_HID_WD_SERIALHI ||
1713                     usage == WACOM_HID_WD_TOOLTYPE ||
1714                     usage == WACOM_HID_WD_DISTANCE ||
1715                     usage == WACOM_HID_WD_TOUCHSTRIP ||
1716                     usage == WACOM_HID_WD_TOUCHSTRIP2 ||
1717                     usage == WACOM_HID_WD_TOUCHRING ||
1718                     usage == WACOM_HID_WD_TOUCHRINGSTATUS ||
1719                     usage == WACOM_HID_WD_REPORT_VALID) {
1720                         return usage;
1721                 }
1722
1723                 if (subpage == HID_UP_UNDEFINED)
1724                         subpage = HID_UP_DIGITIZER;
1725
1726                 return subpage | subusage;
1727         }
1728
1729         if ((usage & HID_USAGE_PAGE) == WACOM_HID_UP_WACOMTOUCH) {
1730                 int subpage = (usage & 0xFF00) << 8;
1731                 int subusage = (usage & 0xFF);
1732
1733                 if (subpage == HID_UP_UNDEFINED)
1734                         subpage = WACOM_HID_SP_DIGITIZER;
1735
1736                 return subpage | subusage;
1737         }
1738
1739         return usage;
1740 }
1741
1742 static void wacom_map_usage(struct input_dev *input, struct hid_usage *usage,
1743                 struct hid_field *field, __u8 type, __u16 code, int fuzz)
1744 {
1745         struct wacom *wacom = input_get_drvdata(input);
1746         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1747         struct wacom_features *features = &wacom_wac->features;
1748         int fmin = field->logical_minimum;
1749         int fmax = field->logical_maximum;
1750         unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
1751         int resolution_code = code;
1752
1753         if (equivalent_usage == HID_DG_TWIST) {
1754                 resolution_code = ABS_RZ;
1755         }
1756
1757         if (equivalent_usage == HID_GD_X) {
1758                 fmin += features->offset_left;
1759                 fmax -= features->offset_right;
1760         }
1761         if (equivalent_usage == HID_GD_Y) {
1762                 fmin += features->offset_top;
1763                 fmax -= features->offset_bottom;
1764         }
1765
1766         usage->type = type;
1767         usage->code = code;
1768
1769         set_bit(type, input->evbit);
1770
1771         switch (type) {
1772         case EV_ABS:
1773                 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
1774                 input_abs_set_res(input, code,
1775                                   hidinput_calc_abs_res(field, resolution_code));
1776                 break;
1777         case EV_KEY:
1778                 input_set_capability(input, EV_KEY, code);
1779                 break;
1780         case EV_MSC:
1781                 input_set_capability(input, EV_MSC, code);
1782                 break;
1783         case EV_SW:
1784                 input_set_capability(input, EV_SW, code);
1785                 break;
1786         }
1787 }
1788
1789 static void wacom_wac_battery_usage_mapping(struct hid_device *hdev,
1790                 struct hid_field *field, struct hid_usage *usage)
1791 {
1792         struct wacom *wacom = hid_get_drvdata(hdev);
1793         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1794         struct wacom_features *features = &wacom_wac->features;
1795         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1796
1797         switch (equivalent_usage) {
1798         case HID_DG_BATTERYSTRENGTH:
1799         case WACOM_HID_WD_BATTERY_LEVEL:
1800         case WACOM_HID_WD_BATTERY_CHARGING:
1801                 features->quirks |= WACOM_QUIRK_BATTERY;
1802                 break;
1803         }
1804 }
1805
1806 static void wacom_wac_battery_event(struct hid_device *hdev, struct hid_field *field,
1807                 struct hid_usage *usage, __s32 value)
1808 {
1809         struct wacom *wacom = hid_get_drvdata(hdev);
1810         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1811         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1812
1813         switch (equivalent_usage) {
1814         case HID_DG_BATTERYSTRENGTH:
1815                 if (value == 0) {
1816                         wacom_wac->hid_data.bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
1817                 }
1818                 else {
1819                         value = value * 100 / (field->logical_maximum - field->logical_minimum);
1820                         wacom_wac->hid_data.battery_capacity = value;
1821                         wacom_wac->hid_data.bat_connected = 1;
1822                         wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1823                 }
1824                 break;
1825         case WACOM_HID_WD_BATTERY_LEVEL:
1826                 value = value * 100 / (field->logical_maximum - field->logical_minimum);
1827                 wacom_wac->hid_data.battery_capacity = value;
1828                 wacom_wac->hid_data.bat_connected = 1;
1829                 wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1830                 break;
1831         case WACOM_HID_WD_BATTERY_CHARGING:
1832                 wacom_wac->hid_data.bat_charging = value;
1833                 wacom_wac->hid_data.ps_connected = value;
1834                 wacom_wac->hid_data.bat_connected = 1;
1835                 wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
1836                 break;
1837         }
1838 }
1839
1840 static void wacom_wac_battery_pre_report(struct hid_device *hdev,
1841                 struct hid_report *report)
1842 {
1843         return;
1844 }
1845
1846 static void wacom_wac_battery_report(struct hid_device *hdev,
1847                 struct hid_report *report)
1848 {
1849         struct wacom *wacom = hid_get_drvdata(hdev);
1850         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1851         struct wacom_features *features = &wacom_wac->features;
1852
1853         if (features->quirks & WACOM_QUIRK_BATTERY) {
1854                 int status = wacom_wac->hid_data.bat_status;
1855                 int capacity = wacom_wac->hid_data.battery_capacity;
1856                 bool charging = wacom_wac->hid_data.bat_charging;
1857                 bool connected = wacom_wac->hid_data.bat_connected;
1858                 bool powered = wacom_wac->hid_data.ps_connected;
1859
1860                 wacom_notify_battery(wacom_wac, status, capacity, charging,
1861                                      connected, powered);
1862         }
1863 }
1864
1865 static void wacom_wac_pad_usage_mapping(struct hid_device *hdev,
1866                 struct hid_field *field, struct hid_usage *usage)
1867 {
1868         struct wacom *wacom = hid_get_drvdata(hdev);
1869         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1870         struct wacom_features *features = &wacom_wac->features;
1871         struct input_dev *input = wacom_wac->pad_input;
1872         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1873
1874         switch (equivalent_usage) {
1875         case WACOM_HID_WD_ACCELEROMETER_X:
1876                 __set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
1877                 wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 0);
1878                 features->device_type |= WACOM_DEVICETYPE_PAD;
1879                 break;
1880         case WACOM_HID_WD_ACCELEROMETER_Y:
1881                 __set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
1882                 wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 0);
1883                 features->device_type |= WACOM_DEVICETYPE_PAD;
1884                 break;
1885         case WACOM_HID_WD_ACCELEROMETER_Z:
1886                 __set_bit(INPUT_PROP_ACCELEROMETER, input->propbit);
1887                 wacom_map_usage(input, usage, field, EV_ABS, ABS_Z, 0);
1888                 features->device_type |= WACOM_DEVICETYPE_PAD;
1889                 break;
1890         case WACOM_HID_WD_BUTTONCENTER:
1891                 wacom->generic_has_leds = true;
1892                 /* fall through */
1893         case WACOM_HID_WD_BUTTONHOME:
1894         case WACOM_HID_WD_BUTTONUP:
1895         case WACOM_HID_WD_BUTTONDOWN:
1896         case WACOM_HID_WD_BUTTONLEFT:
1897         case WACOM_HID_WD_BUTTONRIGHT:
1898                 wacom_map_usage(input, usage, field, EV_KEY,
1899                                 wacom_numbered_button_to_key(features->numbered_buttons),
1900                                 0);
1901                 features->numbered_buttons++;
1902                 features->device_type |= WACOM_DEVICETYPE_PAD;
1903                 break;
1904         case WACOM_HID_WD_TOUCHONOFF:
1905         case WACOM_HID_WD_MUTE_DEVICE:
1906                 /*
1907                  * This usage, which is used to mute touch events, comes
1908                  * from the pad packet, but is reported on the touch
1909                  * interface. Because the touch interface may not have
1910                  * been created yet, we cannot call wacom_map_usage(). In
1911                  * order to process this usage when we receive it, we set
1912                  * the usage type and code directly.
1913                  */
1914                 wacom_wac->has_mute_touch_switch = true;
1915                 usage->type = EV_SW;
1916                 usage->code = SW_MUTE_DEVICE;
1917                 features->device_type |= WACOM_DEVICETYPE_PAD;
1918                 break;
1919         case WACOM_HID_WD_TOUCHSTRIP:
1920                 wacom_map_usage(input, usage, field, EV_ABS, ABS_RX, 0);
1921                 features->device_type |= WACOM_DEVICETYPE_PAD;
1922                 break;
1923         case WACOM_HID_WD_TOUCHSTRIP2:
1924                 wacom_map_usage(input, usage, field, EV_ABS, ABS_RY, 0);
1925                 features->device_type |= WACOM_DEVICETYPE_PAD;
1926                 break;
1927         case WACOM_HID_WD_TOUCHRING:
1928                 wacom_map_usage(input, usage, field, EV_ABS, ABS_WHEEL, 0);
1929                 features->device_type |= WACOM_DEVICETYPE_PAD;
1930                 break;
1931         case WACOM_HID_WD_TOUCHRINGSTATUS:
1932                 /*
1933                  * Only set up type/code association. Completely mapping
1934                  * this usage may overwrite the axis resolution and range.
1935                  */
1936                 usage->type = EV_ABS;
1937                 usage->code = ABS_WHEEL;
1938                 set_bit(EV_ABS, input->evbit);
1939                 features->device_type |= WACOM_DEVICETYPE_PAD;
1940                 break;
1941         case WACOM_HID_WD_BUTTONCONFIG:
1942                 wacom_map_usage(input, usage, field, EV_KEY, KEY_BUTTONCONFIG, 0);
1943                 features->device_type |= WACOM_DEVICETYPE_PAD;
1944                 break;
1945         case WACOM_HID_WD_ONSCREEN_KEYBOARD:
1946                 wacom_map_usage(input, usage, field, EV_KEY, KEY_ONSCREEN_KEYBOARD, 0);
1947                 features->device_type |= WACOM_DEVICETYPE_PAD;
1948                 break;
1949         case WACOM_HID_WD_CONTROLPANEL:
1950                 wacom_map_usage(input, usage, field, EV_KEY, KEY_CONTROLPANEL, 0);
1951                 features->device_type |= WACOM_DEVICETYPE_PAD;
1952                 break;
1953         case WACOM_HID_WD_MODE_CHANGE:
1954                 /* do not overwrite previous data */
1955                 if (!wacom_wac->has_mode_change) {
1956                         wacom_wac->has_mode_change = true;
1957                         wacom_wac->is_direct_mode = true;
1958                 }
1959                 features->device_type |= WACOM_DEVICETYPE_PAD;
1960                 break;
1961         }
1962
1963         switch (equivalent_usage & 0xfffffff0) {
1964         case WACOM_HID_WD_EXPRESSKEY00:
1965                 wacom_map_usage(input, usage, field, EV_KEY,
1966                                 wacom_numbered_button_to_key(features->numbered_buttons),
1967                                 0);
1968                 features->numbered_buttons++;
1969                 features->device_type |= WACOM_DEVICETYPE_PAD;
1970                 break;
1971         }
1972 }
1973
1974 static void wacom_wac_pad_event(struct hid_device *hdev, struct hid_field *field,
1975                 struct hid_usage *usage, __s32 value)
1976 {
1977         struct wacom *wacom = hid_get_drvdata(hdev);
1978         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1979         struct input_dev *input = wacom_wac->pad_input;
1980         struct wacom_features *features = &wacom_wac->features;
1981         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
1982         int i;
1983         bool do_report = false;
1984
1985         /*
1986          * Avoid reporting this event and setting inrange_state if this usage
1987          * hasn't been mapped.
1988          */
1989         if (!usage->type && equivalent_usage != WACOM_HID_WD_MODE_CHANGE)
1990                 return;
1991
1992         if (wacom_equivalent_usage(field->physical) == HID_DG_TABLETFUNCTIONKEY) {
1993                 if (usage->hid != WACOM_HID_WD_TOUCHRING)
1994                         wacom_wac->hid_data.inrange_state |= value;
1995         }
1996
1997         switch (equivalent_usage) {
1998         case WACOM_HID_WD_TOUCHRING:
1999                 /*
2000                  * Userspace expects touchrings to increase in value with
2001                  * clockwise gestures and have their zero point at the
2002                  * tablet's left. HID events "should" be clockwise-
2003                  * increasing and zero at top, though the MobileStudio
2004                  * Pro and 2nd-gen Intuos Pro don't do this...
2005                  */
2006                 if (hdev->vendor == 0x56a &&
2007                     (hdev->product == 0x34d || hdev->product == 0x34e ||  /* MobileStudio Pro */
2008                      hdev->product == 0x357 || hdev->product == 0x358)) { /* Intuos Pro 2 */
2009                         value = (field->logical_maximum - value);
2010
2011                         if (hdev->product == 0x357 || hdev->product == 0x358)
2012                                 value = wacom_offset_rotation(input, usage, value, 3, 16);
2013                         else if (hdev->product == 0x34d || hdev->product == 0x34e)
2014                                 value = wacom_offset_rotation(input, usage, value, 1, 2);
2015                 }
2016                 else {
2017                         value = wacom_offset_rotation(input, usage, value, 1, 4);
2018                 }
2019                 do_report = true;
2020                 break;
2021         case WACOM_HID_WD_TOUCHRINGSTATUS:
2022                 if (!value)
2023                         input_event(input, usage->type, usage->code, 0);
2024                 break;
2025
2026         case WACOM_HID_WD_MUTE_DEVICE:
2027         case WACOM_HID_WD_TOUCHONOFF:
2028                 if (wacom_wac->shared->touch_input) {
2029                         bool *is_touch_on = &wacom_wac->shared->is_touch_on;
2030
2031                         if (equivalent_usage == WACOM_HID_WD_MUTE_DEVICE && value)
2032                                 *is_touch_on = !(*is_touch_on);
2033                         else if (equivalent_usage == WACOM_HID_WD_TOUCHONOFF)
2034                                 *is_touch_on = value;
2035
2036                         input_report_switch(wacom_wac->shared->touch_input,
2037                                             SW_MUTE_DEVICE, !(*is_touch_on));
2038                         input_sync(wacom_wac->shared->touch_input);
2039                 }
2040                 break;
2041
2042         case WACOM_HID_WD_MODE_CHANGE:
2043                 if (wacom_wac->is_direct_mode != value) {
2044                         wacom_wac->is_direct_mode = value;
2045                         wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_MODE_CHANGE);
2046                 }
2047                 break;
2048
2049         case WACOM_HID_WD_BUTTONCENTER:
2050                 for (i = 0; i < wacom->led.count; i++)
2051                         wacom_update_led(wacom, features->numbered_buttons,
2052                                          value, i);
2053                  /* fall through*/
2054         default:
2055                 do_report = true;
2056                 break;
2057         }
2058
2059         if (do_report) {
2060                 input_event(input, usage->type, usage->code, value);
2061                 if (value)
2062                         wacom_wac->hid_data.pad_input_event_flag = true;
2063         }
2064 }
2065
2066 static void wacom_wac_pad_pre_report(struct hid_device *hdev,
2067                 struct hid_report *report)
2068 {
2069         struct wacom *wacom = hid_get_drvdata(hdev);
2070         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2071
2072         wacom_wac->hid_data.inrange_state = 0;
2073 }
2074
2075 static void wacom_wac_pad_report(struct hid_device *hdev,
2076                 struct hid_report *report, struct hid_field *field)
2077 {
2078         struct wacom *wacom = hid_get_drvdata(hdev);
2079         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2080         struct input_dev *input = wacom_wac->pad_input;
2081         bool active = wacom_wac->hid_data.inrange_state != 0;
2082
2083         /* report prox for expresskey events */
2084         if ((wacom_equivalent_usage(field->physical) == HID_DG_TABLETFUNCTIONKEY) &&
2085             wacom_wac->hid_data.pad_input_event_flag) {
2086                 input_event(input, EV_ABS, ABS_MISC, active ? PAD_DEVICE_ID : 0);
2087                 input_sync(input);
2088                 if (!active)
2089                         wacom_wac->hid_data.pad_input_event_flag = false;
2090         }
2091
2092 }
2093
2094 static void wacom_wac_pen_usage_mapping(struct hid_device *hdev,
2095                 struct hid_field *field, struct hid_usage *usage)
2096 {
2097         struct wacom *wacom = hid_get_drvdata(hdev);
2098         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2099         struct wacom_features *features = &wacom_wac->features;
2100         struct input_dev *input = wacom_wac->pen_input;
2101         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2102
2103         switch (equivalent_usage) {
2104         case HID_GD_X:
2105                 wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 4);
2106                 break;
2107         case HID_GD_Y:
2108                 wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 4);
2109                 break;
2110         case WACOM_HID_WD_DISTANCE:
2111         case HID_GD_Z:
2112                 wacom_map_usage(input, usage, field, EV_ABS, ABS_DISTANCE, 0);
2113                 break;
2114         case HID_DG_TIPPRESSURE:
2115                 wacom_map_usage(input, usage, field, EV_ABS, ABS_PRESSURE, 0);
2116                 break;
2117         case HID_DG_INRANGE:
2118                 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOOL_PEN, 0);
2119                 break;
2120         case HID_DG_INVERT:
2121                 wacom_map_usage(input, usage, field, EV_KEY,
2122                                 BTN_TOOL_RUBBER, 0);
2123                 break;
2124         case HID_DG_TILT_X:
2125                 wacom_map_usage(input, usage, field, EV_ABS, ABS_TILT_X, 0);
2126                 break;
2127         case HID_DG_TILT_Y:
2128                 wacom_map_usage(input, usage, field, EV_ABS, ABS_TILT_Y, 0);
2129                 break;
2130         case HID_DG_TWIST:
2131                 wacom_map_usage(input, usage, field, EV_ABS, ABS_Z, 0);
2132                 break;
2133         case HID_DG_ERASER:
2134         case HID_DG_TIPSWITCH:
2135                 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
2136                 break;
2137         case HID_DG_BARRELSWITCH:
2138                 wacom_map_usage(input, usage, field, EV_KEY, BTN_STYLUS, 0);
2139                 break;
2140         case HID_DG_BARRELSWITCH2:
2141                 wacom_map_usage(input, usage, field, EV_KEY, BTN_STYLUS2, 0);
2142                 break;
2143         case HID_DG_TOOLSERIALNUMBER:
2144                 features->quirks |= WACOM_QUIRK_TOOLSERIAL;
2145                 wacom_map_usage(input, usage, field, EV_MSC, MSC_SERIAL, 0);
2146
2147                 /* Adjust AES usages to match modern convention */
2148                 if (usage->hid == WACOM_HID_WT_SERIALNUMBER && field->report_size == 16) {
2149                         if (field->index + 2 < field->report->maxfield) {
2150                                 struct hid_field *a = field->report->field[field->index + 1];
2151                                 struct hid_field *b = field->report->field[field->index + 2];
2152
2153                                 if (a->maxusage > 0 && a->usage[0].hid == HID_DG_TOOLSERIALNUMBER && a->report_size == 32 &&
2154                                     b->maxusage > 0 && b->usage[0].hid == 0xFF000000 && b->report_size == 8) {
2155                                         features->quirks |= WACOM_QUIRK_AESPEN;
2156                                         usage->hid = WACOM_HID_WD_TOOLTYPE;
2157                                         field->logical_minimum = S16_MIN;
2158                                         field->logical_maximum = S16_MAX;
2159                                         a->logical_minimum = S32_MIN;
2160                                         a->logical_maximum = S32_MAX;
2161                                         b->usage[0].hid = WACOM_HID_WD_SERIALHI;
2162                                         b->logical_minimum = 0;
2163                                         b->logical_maximum = U8_MAX;
2164                                 }
2165                         }
2166                 }
2167                 break;
2168         case WACOM_HID_WD_SENSE:
2169                 features->quirks |= WACOM_QUIRK_SENSE;
2170                 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOOL_PEN, 0);
2171                 break;
2172         case WACOM_HID_WD_SERIALHI:
2173                 wacom_map_usage(input, usage, field, EV_ABS, ABS_MISC, 0);
2174
2175                 if (!(features->quirks & WACOM_QUIRK_AESPEN)) {
2176                         set_bit(EV_KEY, input->evbit);
2177                         input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
2178                         input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER);
2179                         input_set_capability(input, EV_KEY, BTN_TOOL_BRUSH);
2180                         input_set_capability(input, EV_KEY, BTN_TOOL_PENCIL);
2181                         input_set_capability(input, EV_KEY, BTN_TOOL_AIRBRUSH);
2182                         if (!(features->device_type & WACOM_DEVICETYPE_DIRECT)) {
2183                                 input_set_capability(input, EV_KEY, BTN_TOOL_MOUSE);
2184                                 input_set_capability(input, EV_KEY, BTN_TOOL_LENS);
2185                         }
2186                 }
2187                 break;
2188         case WACOM_HID_WD_FINGERWHEEL:
2189                 wacom_map_usage(input, usage, field, EV_ABS, ABS_WHEEL, 0);
2190                 break;
2191         }
2192 }
2193
2194 static void wacom_wac_pen_event(struct hid_device *hdev, struct hid_field *field,
2195                 struct hid_usage *usage, __s32 value)
2196 {
2197         struct wacom *wacom = hid_get_drvdata(hdev);
2198         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2199         struct wacom_features *features = &wacom_wac->features;
2200         struct input_dev *input = wacom_wac->pen_input;
2201         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2202
2203         if (wacom_wac->is_invalid_bt_frame)
2204                 return;
2205
2206         switch (equivalent_usage) {
2207         case HID_GD_Z:
2208                 /*
2209                  * HID_GD_Z "should increase as the control's position is
2210                  * moved from high to low", while ABS_DISTANCE instead
2211                  * increases in value as the tool moves from low to high.
2212                  */
2213                 value = field->logical_maximum - value;
2214                 break;
2215         case HID_DG_INRANGE:
2216                 wacom_wac->hid_data.inrange_state = value;
2217                 if (!(features->quirks & WACOM_QUIRK_SENSE))
2218                         wacom_wac->hid_data.sense_state = value;
2219                 return;
2220         case HID_DG_INVERT:
2221                 wacom_wac->hid_data.invert_state = value;
2222                 return;
2223         case HID_DG_ERASER:
2224         case HID_DG_TIPSWITCH:
2225                 wacom_wac->hid_data.tipswitch |= value;
2226                 return;
2227         case HID_DG_BARRELSWITCH:
2228                 wacom_wac->hid_data.barrelswitch = value;
2229                 return;
2230         case HID_DG_BARRELSWITCH2:
2231                 wacom_wac->hid_data.barrelswitch2 = value;
2232                 return;
2233         case HID_DG_TOOLSERIALNUMBER:
2234                 if (value) {
2235                         wacom_wac->serial[0] = (wacom_wac->serial[0] & ~0xFFFFFFFFULL);
2236                         wacom_wac->serial[0] |= (__u32)value;
2237                 }
2238                 return;
2239         case HID_DG_TWIST:
2240                 /*
2241                  * Userspace expects pen twist to have its zero point when
2242                  * the buttons/finger is on the tablet's left. HID values
2243                  * are zero when buttons are toward the top.
2244                  */
2245                 value = wacom_offset_rotation(input, usage, value, 1, 4);
2246                 break;
2247         case WACOM_HID_WD_SENSE:
2248                 wacom_wac->hid_data.sense_state = value;
2249                 return;
2250         case WACOM_HID_WD_SERIALHI:
2251                 if (value) {
2252                         wacom_wac->serial[0] = (wacom_wac->serial[0] & 0xFFFFFFFF);
2253                         wacom_wac->serial[0] |= ((__u64)value) << 32;
2254                         /*
2255                          * Non-USI EMR devices may contain additional tool type
2256                          * information here. See WACOM_HID_WD_TOOLTYPE case for
2257                          * more details.
2258                          */
2259                         if (value >> 20 == 1) {
2260                                 wacom_wac->id[0] |= value & 0xFFFFF;
2261                         }
2262                 }
2263                 return;
2264         case WACOM_HID_WD_TOOLTYPE:
2265                 /*
2266                  * Some devices (MobileStudio Pro, and possibly later
2267                  * devices as well) do not return the complete tool
2268                  * type in their WACOM_HID_WD_TOOLTYPE usage. Use a
2269                  * bitwise OR so the complete value can be built
2270                  * up over time :(
2271                  */
2272                 wacom_wac->id[0] |= value;
2273                 return;
2274         case WACOM_HID_WD_OFFSETLEFT:
2275                 if (features->offset_left && value != features->offset_left)
2276                         hid_warn(hdev, "%s: overriding existing left offset "
2277                                  "%d -> %d\n", __func__, value,
2278                                  features->offset_left);
2279                 features->offset_left = value;
2280                 return;
2281         case WACOM_HID_WD_OFFSETRIGHT:
2282                 if (features->offset_right && value != features->offset_right)
2283                         hid_warn(hdev, "%s: overriding existing right offset "
2284                                  "%d -> %d\n", __func__, value,
2285                                  features->offset_right);
2286                 features->offset_right = value;
2287                 return;
2288         case WACOM_HID_WD_OFFSETTOP:
2289                 if (features->offset_top && value != features->offset_top)
2290                         hid_warn(hdev, "%s: overriding existing top offset "
2291                                  "%d -> %d\n", __func__, value,
2292                                  features->offset_top);
2293                 features->offset_top = value;
2294                 return;
2295         case WACOM_HID_WD_OFFSETBOTTOM:
2296                 if (features->offset_bottom && value != features->offset_bottom)
2297                         hid_warn(hdev, "%s: overriding existing bottom offset "
2298                                  "%d -> %d\n", __func__, value,
2299                                  features->offset_bottom);
2300                 features->offset_bottom = value;
2301                 return;
2302         case WACOM_HID_WD_REPORT_VALID:
2303                 wacom_wac->is_invalid_bt_frame = !value;
2304                 return;
2305         }
2306
2307         /* send pen events only when touch is up or forced out
2308          * or touch arbitration is off
2309          */
2310         if (!usage->type || delay_pen_events(wacom_wac))
2311                 return;
2312
2313         /* send pen events only when the pen is in range */
2314         if (wacom_wac->hid_data.inrange_state)
2315                 input_event(input, usage->type, usage->code, value);
2316         else if (wacom_wac->shared->stylus_in_proximity && !wacom_wac->hid_data.sense_state)
2317                 input_event(input, usage->type, usage->code, 0);
2318 }
2319
2320 static void wacom_wac_pen_pre_report(struct hid_device *hdev,
2321                 struct hid_report *report)
2322 {
2323         struct wacom *wacom = hid_get_drvdata(hdev);
2324         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2325
2326         wacom_wac->is_invalid_bt_frame = false;
2327         return;
2328 }
2329
2330 static void wacom_wac_pen_report(struct hid_device *hdev,
2331                 struct hid_report *report)
2332 {
2333         struct wacom *wacom = hid_get_drvdata(hdev);
2334         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2335         struct input_dev *input = wacom_wac->pen_input;
2336         bool range = wacom_wac->hid_data.inrange_state;
2337         bool sense = wacom_wac->hid_data.sense_state;
2338
2339         if (wacom_wac->is_invalid_bt_frame)
2340                 return;
2341
2342         if (!wacom_wac->tool[0] && range) { /* first in range */
2343                 /* Going into range select tool */
2344                 if (wacom_wac->hid_data.invert_state)
2345                         wacom_wac->tool[0] = BTN_TOOL_RUBBER;
2346                 else if (wacom_wac->id[0])
2347                         wacom_wac->tool[0] = wacom_intuos_get_tool_type(wacom_wac->id[0]);
2348                 else
2349                         wacom_wac->tool[0] = BTN_TOOL_PEN;
2350         }
2351
2352         /* keep pen state for touch events */
2353         wacom_wac->shared->stylus_in_proximity = sense;
2354
2355         if (!delay_pen_events(wacom_wac) && wacom_wac->tool[0]) {
2356                 int id = wacom_wac->id[0];
2357                 int sw_state = wacom_wac->hid_data.barrelswitch |
2358                                (wacom_wac->hid_data.barrelswitch2 << 1);
2359
2360                 input_report_key(input, BTN_STYLUS, sw_state == 1);
2361                 input_report_key(input, BTN_STYLUS2, sw_state == 2);
2362                 input_report_key(input, BTN_STYLUS3, sw_state == 3);
2363
2364                 /*
2365                  * Non-USI EMR tools should have their IDs mangled to
2366                  * match the legacy behavior of wacom_intuos_general
2367                  */
2368                 if (wacom_wac->serial[0] >> 52 == 1)
2369                         id = wacom_intuos_id_mangle(id);
2370
2371                 /*
2372                  * To ensure compatibility with xf86-input-wacom, we should
2373                  * report the BTN_TOOL_* event prior to the ABS_MISC or
2374                  * MSC_SERIAL events.
2375                  */
2376                 input_report_key(input, BTN_TOUCH,
2377                                 wacom_wac->hid_data.tipswitch);
2378                 input_report_key(input, wacom_wac->tool[0], sense);
2379                 if (wacom_wac->serial[0]) {
2380                         input_event(input, EV_MSC, MSC_SERIAL, wacom_wac->serial[0]);
2381                         input_report_abs(input, ABS_MISC, sense ? id : 0);
2382                 }
2383
2384                 wacom_wac->hid_data.tipswitch = false;
2385
2386                 input_sync(input);
2387         }
2388
2389         if (!sense) {
2390                 wacom_wac->tool[0] = 0;
2391                 wacom_wac->id[0] = 0;
2392                 wacom_wac->serial[0] = 0;
2393         }
2394 }
2395
2396 static void wacom_wac_finger_usage_mapping(struct hid_device *hdev,
2397                 struct hid_field *field, struct hid_usage *usage)
2398 {
2399         struct wacom *wacom = hid_get_drvdata(hdev);
2400         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2401         struct input_dev *input = wacom_wac->touch_input;
2402         unsigned touch_max = wacom_wac->features.touch_max;
2403         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2404
2405         switch (equivalent_usage) {
2406         case HID_GD_X:
2407                 if (touch_max == 1)
2408                         wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 4);
2409                 else
2410                         wacom_map_usage(input, usage, field, EV_ABS,
2411                                         ABS_MT_POSITION_X, 4);
2412                 break;
2413         case HID_GD_Y:
2414                 if (touch_max == 1)
2415                         wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 4);
2416                 else
2417                         wacom_map_usage(input, usage, field, EV_ABS,
2418                                         ABS_MT_POSITION_Y, 4);
2419                 break;
2420         case HID_DG_WIDTH:
2421         case HID_DG_HEIGHT:
2422                 wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MAJOR, 0);
2423                 wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MINOR, 0);
2424                 input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
2425                 break;
2426         case HID_DG_TIPSWITCH:
2427                 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
2428                 break;
2429         case HID_DG_CONTACTCOUNT:
2430                 wacom_wac->hid_data.cc_report = field->report->id;
2431                 wacom_wac->hid_data.cc_index = field->index;
2432                 wacom_wac->hid_data.cc_value_index = usage->usage_index;
2433                 break;
2434         case HID_DG_CONTACTID:
2435                 if ((field->logical_maximum - field->logical_minimum) < touch_max) {
2436                         /*
2437                          * The HID descriptor for G11 sensors leaves logical
2438                          * maximum set to '1' despite it being a multitouch
2439                          * device. Override to a sensible number.
2440                          */
2441                         field->logical_maximum = 255;
2442                 }
2443                 break;
2444         }
2445 }
2446
2447 static void wacom_wac_finger_slot(struct wacom_wac *wacom_wac,
2448                 struct input_dev *input)
2449 {
2450         struct hid_data *hid_data = &wacom_wac->hid_data;
2451         bool mt = wacom_wac->features.touch_max > 1;
2452         bool prox = hid_data->tipswitch &&
2453                     report_touch_events(wacom_wac);
2454
2455         if (wacom_wac->shared->has_mute_touch_switch &&
2456             !wacom_wac->shared->is_touch_on) {
2457                 if (!wacom_wac->shared->touch_down)
2458                         return;
2459                 prox = 0;
2460         }
2461
2462         wacom_wac->hid_data.num_received++;
2463         if (wacom_wac->hid_data.num_received > wacom_wac->hid_data.num_expected)
2464                 return;
2465
2466         if (mt) {
2467                 int slot;
2468
2469                 slot = input_mt_get_slot_by_key(input, hid_data->id);
2470                 input_mt_slot(input, slot);
2471                 input_mt_report_slot_state(input, MT_TOOL_FINGER, prox);
2472         }
2473         else {
2474                 input_report_key(input, BTN_TOUCH, prox);
2475         }
2476
2477         if (prox) {
2478                 input_report_abs(input, mt ? ABS_MT_POSITION_X : ABS_X,
2479                                  hid_data->x);
2480                 input_report_abs(input, mt ? ABS_MT_POSITION_Y : ABS_Y,
2481                                  hid_data->y);
2482
2483                 if (test_bit(ABS_MT_TOUCH_MAJOR, input->absbit)) {
2484                         input_report_abs(input, ABS_MT_TOUCH_MAJOR, max(hid_data->width, hid_data->height));
2485                         input_report_abs(input, ABS_MT_TOUCH_MINOR, min(hid_data->width, hid_data->height));
2486                         if (hid_data->width != hid_data->height)
2487                                 input_report_abs(input, ABS_MT_ORIENTATION, hid_data->width <= hid_data->height ? 0 : 1);
2488                 }
2489         }
2490 }
2491
2492 static void wacom_wac_finger_event(struct hid_device *hdev,
2493                 struct hid_field *field, struct hid_usage *usage, __s32 value)
2494 {
2495         struct wacom *wacom = hid_get_drvdata(hdev);
2496         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2497         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
2498
2499         switch (equivalent_usage) {
2500         case HID_GD_X:
2501                 wacom_wac->hid_data.x = value;
2502                 break;
2503         case HID_GD_Y:
2504                 wacom_wac->hid_data.y = value;
2505                 break;
2506         case HID_DG_WIDTH:
2507                 wacom_wac->hid_data.width = value;
2508                 break;
2509         case HID_DG_HEIGHT:
2510                 wacom_wac->hid_data.height = value;
2511                 break;
2512         case HID_DG_CONTACTID:
2513                 wacom_wac->hid_data.id = value;
2514                 break;
2515         case HID_DG_TIPSWITCH:
2516                 wacom_wac->hid_data.tipswitch = value;
2517                 break;
2518         }
2519
2520
2521         if (usage->usage_index + 1 == field->report_count) {
2522                 if (equivalent_usage == wacom_wac->hid_data.last_slot_field)
2523                         wacom_wac_finger_slot(wacom_wac, wacom_wac->touch_input);
2524         }
2525 }
2526
2527 static void wacom_wac_finger_pre_report(struct hid_device *hdev,
2528                 struct hid_report *report)
2529 {
2530         struct wacom *wacom = hid_get_drvdata(hdev);
2531         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2532         struct hid_data* hid_data = &wacom_wac->hid_data;
2533         int i;
2534
2535         for (i = 0; i < report->maxfield; i++) {
2536                 struct hid_field *field = report->field[i];
2537                 int j;
2538
2539                 for (j = 0; j < field->maxusage; j++) {
2540                         struct hid_usage *usage = &field->usage[j];
2541                         unsigned int equivalent_usage =
2542                                 wacom_equivalent_usage(usage->hid);
2543
2544                         switch (equivalent_usage) {
2545                         case HID_GD_X:
2546                         case HID_GD_Y:
2547                         case HID_DG_WIDTH:
2548                         case HID_DG_HEIGHT:
2549                         case HID_DG_CONTACTID:
2550                         case HID_DG_INRANGE:
2551                         case HID_DG_INVERT:
2552                         case HID_DG_TIPSWITCH:
2553                                 hid_data->last_slot_field = equivalent_usage;
2554                                 break;
2555                         case HID_DG_CONTACTCOUNT:
2556                                 hid_data->cc_report = report->id;
2557                                 hid_data->cc_index = i;
2558                                 hid_data->cc_value_index = j;
2559                                 break;
2560                         }
2561                 }
2562         }
2563
2564         if (hid_data->cc_report != 0 &&
2565             hid_data->cc_index >= 0) {
2566                 struct hid_field *field = report->field[hid_data->cc_index];
2567                 int value = field->value[hid_data->cc_value_index];
2568                 if (value)
2569                         hid_data->num_expected = value;
2570         }
2571         else {
2572                 hid_data->num_expected = wacom_wac->features.touch_max;
2573         }
2574 }
2575
2576 static void wacom_wac_finger_report(struct hid_device *hdev,
2577                 struct hid_report *report)
2578 {
2579         struct wacom *wacom = hid_get_drvdata(hdev);
2580         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2581         struct input_dev *input = wacom_wac->touch_input;
2582         unsigned touch_max = wacom_wac->features.touch_max;
2583
2584         /* If more packets of data are expected, give us a chance to
2585          * process them rather than immediately syncing a partial
2586          * update.
2587          */
2588         if (wacom_wac->hid_data.num_received < wacom_wac->hid_data.num_expected)
2589                 return;
2590
2591         if (touch_max > 1)
2592                 input_mt_sync_frame(input);
2593
2594         input_sync(input);
2595         wacom_wac->hid_data.num_received = 0;
2596
2597         /* keep touch state for pen event */
2598         wacom_wac->shared->touch_down = wacom_wac_finger_count_touches(wacom_wac);
2599 }
2600
2601 void wacom_wac_usage_mapping(struct hid_device *hdev,
2602                 struct hid_field *field, struct hid_usage *usage)
2603 {
2604         struct wacom *wacom = hid_get_drvdata(hdev);
2605         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2606         struct wacom_features *features = &wacom_wac->features;
2607
2608         if (WACOM_DIRECT_DEVICE(field))
2609                 features->device_type |= WACOM_DEVICETYPE_DIRECT;
2610
2611         /* usage tests must precede field tests */
2612         if (WACOM_BATTERY_USAGE(usage))
2613                 wacom_wac_battery_usage_mapping(hdev, field, usage);
2614         else if (WACOM_PAD_FIELD(field))
2615                 wacom_wac_pad_usage_mapping(hdev, field, usage);
2616         else if (WACOM_PEN_FIELD(field))
2617                 wacom_wac_pen_usage_mapping(hdev, field, usage);
2618         else if (WACOM_FINGER_FIELD(field))
2619                 wacom_wac_finger_usage_mapping(hdev, field, usage);
2620 }
2621
2622 void wacom_wac_event(struct hid_device *hdev, struct hid_field *field,
2623                 struct hid_usage *usage, __s32 value)
2624 {
2625         struct wacom *wacom = hid_get_drvdata(hdev);
2626
2627         if (wacom->wacom_wac.features.type != HID_GENERIC)
2628                 return;
2629
2630         if (value > field->logical_maximum || value < field->logical_minimum)
2631                 return;
2632
2633         /* usage tests must precede field tests */
2634         if (WACOM_BATTERY_USAGE(usage))
2635                 wacom_wac_battery_event(hdev, field, usage, value);
2636         else if (WACOM_PAD_FIELD(field) && wacom->wacom_wac.pad_input)
2637                 wacom_wac_pad_event(hdev, field, usage, value);
2638         else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2639                 wacom_wac_pen_event(hdev, field, usage, value);
2640         else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
2641                 wacom_wac_finger_event(hdev, field, usage, value);
2642 }
2643
2644 static void wacom_report_events(struct hid_device *hdev,
2645                                 struct hid_report *report, int collection_index,
2646                                 int field_index)
2647 {
2648         int r;
2649
2650         for (r = field_index; r < report->maxfield; r++) {
2651                 struct hid_field *field;
2652                 unsigned count, n;
2653
2654                 field = report->field[r];
2655                 count = field->report_count;
2656
2657                 if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
2658                         continue;
2659
2660                 for (n = 0 ; n < count; n++) {
2661                         if (field->usage[n].collection_index == collection_index)
2662                                 wacom_wac_event(hdev, field, &field->usage[n],
2663                                                 field->value[n]);
2664                         else
2665                                 return;
2666                 }
2667         }
2668 }
2669
2670 static int wacom_wac_collection(struct hid_device *hdev, struct hid_report *report,
2671                          int collection_index, struct hid_field *field,
2672                          int field_index)
2673 {
2674         struct wacom *wacom = hid_get_drvdata(hdev);
2675
2676         wacom_report_events(hdev, report, collection_index, field_index);
2677
2678         /*
2679          * Non-input reports may be sent prior to the device being
2680          * completely initialized. Since only their events need
2681          * to be processed, exit after 'wacom_report_events' has
2682          * been called to prevent potential crashes in the report-
2683          * processing functions.
2684          */
2685         if (report->type != HID_INPUT_REPORT)
2686                 return -1;
2687
2688         if (WACOM_PAD_FIELD(field) && wacom->wacom_wac.pad_input)
2689                 wacom_wac_pad_report(hdev, report, field);
2690         else if (WACOM_PEN_FIELD(field) && wacom->wacom_wac.pen_input)
2691                 wacom_wac_pen_report(hdev, report);
2692         else if (WACOM_FINGER_FIELD(field) && wacom->wacom_wac.touch_input)
2693                 wacom_wac_finger_report(hdev, report);
2694
2695         return 0;
2696 }
2697
2698 void wacom_wac_report(struct hid_device *hdev, struct hid_report *report)
2699 {
2700         struct wacom *wacom = hid_get_drvdata(hdev);
2701         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2702         struct hid_field *field;
2703         bool pad_in_hid_field = false, pen_in_hid_field = false,
2704                 finger_in_hid_field = false;
2705         int r;
2706         int prev_collection = -1;
2707
2708         if (wacom_wac->features.type != HID_GENERIC)
2709                 return;
2710
2711         for (r = 0; r < report->maxfield; r++) {
2712                 field = report->field[r];
2713
2714                 if (WACOM_PAD_FIELD(field))
2715                         pad_in_hid_field = true;
2716                 if (WACOM_PEN_FIELD(field))
2717                         pen_in_hid_field = true;
2718                 if (WACOM_FINGER_FIELD(field))
2719                         finger_in_hid_field = true;
2720         }
2721
2722         wacom_wac_battery_pre_report(hdev, report);
2723
2724         if (pad_in_hid_field && wacom->wacom_wac.pad_input)
2725                 wacom_wac_pad_pre_report(hdev, report);
2726         if (pen_in_hid_field && wacom->wacom_wac.pen_input)
2727                 wacom_wac_pen_pre_report(hdev, report);
2728         if (finger_in_hid_field && wacom->wacom_wac.touch_input)
2729                 wacom_wac_finger_pre_report(hdev, report);
2730
2731         for (r = 0; r < report->maxfield; r++) {
2732                 field = report->field[r];
2733
2734                 if (field->usage[0].collection_index != prev_collection) {
2735                         if (wacom_wac_collection(hdev, report,
2736                                 field->usage[0].collection_index, field, r) < 0)
2737                                 return;
2738                         prev_collection = field->usage[0].collection_index;
2739                 }
2740         }
2741
2742         wacom_wac_battery_report(hdev, report);
2743 }
2744
2745 static int wacom_bpt_touch(struct wacom_wac *wacom)
2746 {
2747         struct wacom_features *features = &wacom->features;
2748         struct input_dev *input = wacom->touch_input;
2749         struct input_dev *pad_input = wacom->pad_input;
2750         unsigned char *data = wacom->data;
2751         int i;
2752
2753         if (data[0] != 0x02)
2754             return 0;
2755
2756         for (i = 0; i < 2; i++) {
2757                 int offset = (data[1] & 0x80) ? (8 * i) : (9 * i);
2758                 bool touch = report_touch_events(wacom)
2759                            && (data[offset + 3] & 0x80);
2760
2761                 input_mt_slot(input, i);
2762                 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
2763                 if (touch) {
2764                         int x = get_unaligned_be16(&data[offset + 3]) & 0x7ff;
2765                         int y = get_unaligned_be16(&data[offset + 5]) & 0x7ff;
2766                         if (features->quirks & WACOM_QUIRK_BBTOUCH_LOWRES) {
2767                                 x <<= 5;
2768                                 y <<= 5;
2769                         }
2770                         input_report_abs(input, ABS_MT_POSITION_X, x);
2771                         input_report_abs(input, ABS_MT_POSITION_Y, y);
2772                 }
2773         }
2774
2775         input_mt_sync_frame(input);
2776
2777         input_report_key(pad_input, BTN_LEFT, (data[1] & 0x08) != 0);
2778         input_report_key(pad_input, BTN_FORWARD, (data[1] & 0x04) != 0);
2779         input_report_key(pad_input, BTN_BACK, (data[1] & 0x02) != 0);
2780         input_report_key(pad_input, BTN_RIGHT, (data[1] & 0x01) != 0);
2781         wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
2782
2783         return 1;
2784 }
2785
2786 static void wacom_bpt3_touch_msg(struct wacom_wac *wacom, unsigned char *data)
2787 {
2788         struct wacom_features *features = &wacom->features;
2789         struct input_dev *input = wacom->touch_input;
2790         bool touch = data[1] & 0x80;
2791         int slot = input_mt_get_slot_by_key(input, data[0]);
2792
2793         if (slot < 0)
2794                 return;
2795
2796         touch = touch && report_touch_events(wacom);
2797
2798         input_mt_slot(input, slot);
2799         input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
2800
2801         if (touch) {
2802                 int x = (data[2] << 4) | (data[4] >> 4);
2803                 int y = (data[3] << 4) | (data[4] & 0x0f);
2804                 int width, height;
2805
2806                 if (features->type >= INTUOSPS && features->type <= INTUOSHT2) {
2807                         width  = data[5] * 100;
2808                         height = data[6] * 100;
2809                 } else {
2810                         /*
2811                          * "a" is a scaled-down area which we assume is
2812                          * roughly circular and which can be described as:
2813                          * a=(pi*r^2)/C.
2814                          */
2815                         int a = data[5];
2816                         int x_res = input_abs_get_res(input, ABS_MT_POSITION_X);
2817                         int y_res = input_abs_get_res(input, ABS_MT_POSITION_Y);
2818                         width = 2 * int_sqrt(a * WACOM_CONTACT_AREA_SCALE);
2819                         height = width * y_res / x_res;
2820                 }
2821
2822                 input_report_abs(input, ABS_MT_POSITION_X, x);
2823                 input_report_abs(input, ABS_MT_POSITION_Y, y);
2824                 input_report_abs(input, ABS_MT_TOUCH_MAJOR, width);
2825                 input_report_abs(input, ABS_MT_TOUCH_MINOR, height);
2826         }
2827 }
2828
2829 static void wacom_bpt3_button_msg(struct wacom_wac *wacom, unsigned char *data)
2830 {
2831         struct input_dev *input = wacom->pad_input;
2832         struct wacom_features *features = &wacom->features;
2833
2834         if (features->type == INTUOSHT || features->type == INTUOSHT2) {
2835                 input_report_key(input, BTN_LEFT, (data[1] & 0x02) != 0);
2836                 input_report_key(input, BTN_BACK, (data[1] & 0x08) != 0);
2837         } else {
2838                 input_report_key(input, BTN_BACK, (data[1] & 0x02) != 0);
2839                 input_report_key(input, BTN_LEFT, (data[1] & 0x08) != 0);
2840         }
2841         input_report_key(input, BTN_FORWARD, (data[1] & 0x04) != 0);
2842         input_report_key(input, BTN_RIGHT, (data[1] & 0x01) != 0);
2843 }
2844
2845 static int wacom_bpt3_touch(struct wacom_wac *wacom)
2846 {
2847         unsigned char *data = wacom->data;
2848         int count = data[1] & 0x07;
2849         int  touch_changed = 0, i;
2850
2851         if (data[0] != 0x02)
2852             return 0;
2853
2854         /* data has up to 7 fixed sized 8-byte messages starting at data[2] */
2855         for (i = 0; i < count; i++) {
2856                 int offset = (8 * i) + 2;
2857                 int msg_id = data[offset];
2858
2859                 if (msg_id >= 2 && msg_id <= 17) {
2860                         wacom_bpt3_touch_msg(wacom, data + offset);
2861                         touch_changed++;
2862                 } else if (msg_id == 128)
2863                         wacom_bpt3_button_msg(wacom, data + offset);
2864
2865         }
2866
2867         /* only update touch if we actually have a touchpad and touch data changed */
2868         if (wacom->touch_input && touch_changed) {
2869                 input_mt_sync_frame(wacom->touch_input);
2870                 wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
2871         }
2872
2873         return 1;
2874 }
2875
2876 static int wacom_bpt_pen(struct wacom_wac *wacom)
2877 {
2878         struct wacom_features *features = &wacom->features;
2879         struct input_dev *input = wacom->pen_input;
2880         unsigned char *data = wacom->data;
2881         int prox = 0, x = 0, y = 0, p = 0, d = 0, pen = 0, btn1 = 0, btn2 = 0;
2882
2883         if (data[0] != WACOM_REPORT_PENABLED)
2884             return 0;
2885
2886         prox = (data[1] & 0x20) == 0x20;
2887
2888         /*
2889          * All reports shared between PEN and RUBBER tool must be
2890          * forced to a known starting value (zero) when transitioning to
2891          * out-of-prox.
2892          *
2893          * If not reset then, to userspace, it will look like lost events
2894          * if new tool comes in-prox with same values as previous tool sent.
2895          *
2896          * Hardware does report zero in most out-of-prox cases but not all.
2897          */
2898         if (!wacom->shared->stylus_in_proximity) {
2899                 if (data[1] & 0x08) {
2900                         wacom->tool[0] = BTN_TOOL_RUBBER;
2901                         wacom->id[0] = ERASER_DEVICE_ID;
2902                 } else {
2903                         wacom->tool[0] = BTN_TOOL_PEN;
2904                         wacom->id[0] = STYLUS_DEVICE_ID;
2905                 }
2906         }
2907
2908         wacom->shared->stylus_in_proximity = prox;
2909         if (delay_pen_events(wacom))
2910                 return 0;
2911
2912         if (prox) {
2913                 x = le16_to_cpup((__le16 *)&data[2]);
2914                 y = le16_to_cpup((__le16 *)&data[4]);
2915                 p = le16_to_cpup((__le16 *)&data[6]);
2916                 /*
2917                  * Convert distance from out prox to distance from tablet.
2918                  * distance will be greater than distance_max once
2919                  * touching and applying pressure; do not report negative
2920                  * distance.
2921                  */
2922                 if (data[8] <= features->distance_max)
2923                         d = features->distance_max - data[8];
2924
2925                 pen = data[1] & 0x01;
2926                 btn1 = data[1] & 0x02;
2927                 btn2 = data[1] & 0x04;
2928         } else {
2929                 wacom->id[0] = 0;
2930         }
2931
2932         input_report_key(input, BTN_TOUCH, pen);
2933         input_report_key(input, BTN_STYLUS, btn1);
2934         input_report_key(input, BTN_STYLUS2, btn2);
2935
2936         input_report_abs(input, ABS_X, x);
2937         input_report_abs(input, ABS_Y, y);
2938         input_report_abs(input, ABS_PRESSURE, p);
2939         input_report_abs(input, ABS_DISTANCE, d);
2940
2941         input_report_key(input, wacom->tool[0], prox); /* PEN or RUBBER */
2942         input_report_abs(input, ABS_MISC, wacom->id[0]); /* TOOL ID */
2943
2944         return 1;
2945 }
2946
2947 static int wacom_bpt_irq(struct wacom_wac *wacom, size_t len)
2948 {
2949         struct wacom_features *features = &wacom->features;
2950
2951         if ((features->type == INTUOSHT2) &&
2952             (features->device_type & WACOM_DEVICETYPE_PEN))
2953                 return wacom_intuos_irq(wacom);
2954         else if (len == WACOM_PKGLEN_BBTOUCH)
2955                 return wacom_bpt_touch(wacom);
2956         else if (len == WACOM_PKGLEN_BBTOUCH3)
2957                 return wacom_bpt3_touch(wacom);
2958         else if (len == WACOM_PKGLEN_BBFUN || len == WACOM_PKGLEN_BBPEN)
2959                 return wacom_bpt_pen(wacom);
2960
2961         return 0;
2962 }
2963
2964 static void wacom_bamboo_pad_pen_event(struct wacom_wac *wacom,
2965                 unsigned char *data)
2966 {
2967         unsigned char prefix;
2968
2969         /*
2970          * We need to reroute the event from the debug interface to the
2971          * pen interface.
2972          * We need to add the report ID to the actual pen report, so we
2973          * temporary overwrite the first byte to prevent having to kzalloc/kfree
2974          * and memcpy the report.
2975          */
2976         prefix = data[0];
2977         data[0] = WACOM_REPORT_BPAD_PEN;
2978
2979         /*
2980          * actually reroute the event.
2981          * No need to check if wacom->shared->pen is valid, hid_input_report()
2982          * will check for us.
2983          */
2984         hid_input_report(wacom->shared->pen, HID_INPUT_REPORT, data,
2985                          WACOM_PKGLEN_PENABLED, 1);
2986
2987         data[0] = prefix;
2988 }
2989
2990 static int wacom_bamboo_pad_touch_event(struct wacom_wac *wacom,
2991                 unsigned char *data)
2992 {
2993         struct input_dev *input = wacom->touch_input;
2994         unsigned char *finger_data, prefix;
2995         unsigned id;
2996         int x, y;
2997         bool valid;
2998
2999         prefix = data[0];
3000
3001         for (id = 0; id < wacom->features.touch_max; id++) {
3002                 valid = !!(prefix & BIT(id)) &&
3003                         report_touch_events(wacom);
3004
3005                 input_mt_slot(input, id);
3006                 input_mt_report_slot_state(input, MT_TOOL_FINGER, valid);
3007
3008                 if (!valid)
3009                         continue;
3010
3011                 finger_data = data + 1 + id * 3;
3012                 x = finger_data[0] | ((finger_data[1] & 0x0f) << 8);
3013                 y = (finger_data[2] << 4) | (finger_data[1] >> 4);
3014
3015                 input_report_abs(input, ABS_MT_POSITION_X, x);
3016                 input_report_abs(input, ABS_MT_POSITION_Y, y);
3017         }
3018
3019         input_mt_sync_frame(input);
3020
3021         input_report_key(input, BTN_LEFT, prefix & 0x40);
3022         input_report_key(input, BTN_RIGHT, prefix & 0x80);
3023
3024         /* keep touch state for pen event */
3025         wacom->shared->touch_down = !!prefix && report_touch_events(wacom);
3026
3027         return 1;
3028 }
3029
3030 static int wacom_bamboo_pad_irq(struct wacom_wac *wacom, size_t len)
3031 {
3032         unsigned char *data = wacom->data;
3033
3034         if (!((len == WACOM_PKGLEN_BPAD_TOUCH) ||
3035               (len == WACOM_PKGLEN_BPAD_TOUCH_USB)) ||
3036             (data[0] != WACOM_REPORT_BPAD_TOUCH))
3037                 return 0;
3038
3039         if (data[1] & 0x01)
3040                 wacom_bamboo_pad_pen_event(wacom, &data[1]);
3041
3042         if (data[1] & 0x02)
3043                 return wacom_bamboo_pad_touch_event(wacom, &data[9]);
3044
3045         return 0;
3046 }
3047
3048 static int wacom_wireless_irq(struct wacom_wac *wacom, size_t len)
3049 {
3050         unsigned char *data = wacom->data;
3051         int connected;
3052
3053         if (len != WACOM_PKGLEN_WIRELESS || data[0] != WACOM_REPORT_WL)
3054                 return 0;
3055
3056         connected = data[1] & 0x01;
3057         if (connected) {
3058                 int pid, battery, charging;
3059
3060                 if ((wacom->shared->type == INTUOSHT ||
3061                     wacom->shared->type == INTUOSHT2) &&
3062                     wacom->shared->touch_input &&
3063                     wacom->shared->touch_max) {
3064                         input_report_switch(wacom->shared->touch_input,
3065                                         SW_MUTE_DEVICE, data[5] & 0x40);
3066                         input_sync(wacom->shared->touch_input);
3067                 }
3068
3069                 pid = get_unaligned_be16(&data[6]);
3070                 battery = (data[5] & 0x3f) * 100 / 31;
3071                 charging = !!(data[5] & 0x80);
3072                 if (wacom->pid != pid) {
3073                         wacom->pid = pid;
3074                         wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
3075                 }
3076
3077                 wacom_notify_battery(wacom, WACOM_POWER_SUPPLY_STATUS_AUTO,
3078                                      battery, charging, 1, 0);
3079
3080         } else if (wacom->pid != 0) {
3081                 /* disconnected while previously connected */
3082                 wacom->pid = 0;
3083                 wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
3084                 wacom_notify_battery(wacom, POWER_SUPPLY_STATUS_UNKNOWN, 0, 0, 0, 0);
3085         }
3086
3087         return 0;
3088 }
3089
3090 static int wacom_status_irq(struct wacom_wac *wacom_wac, size_t len)
3091 {
3092         struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
3093         struct wacom_features *features = &wacom_wac->features;
3094         unsigned char *data = wacom_wac->data;
3095
3096         if (data[0] != WACOM_REPORT_USB)
3097                 return 0;
3098
3099         if ((features->type == INTUOSHT ||
3100             features->type == INTUOSHT2) &&
3101             wacom_wac->shared->touch_input &&
3102             features->touch_max) {
3103                 input_report_switch(wacom_wac->shared->touch_input,
3104                                     SW_MUTE_DEVICE, data[8] & 0x40);
3105                 input_sync(wacom_wac->shared->touch_input);
3106         }
3107
3108         if (data[9] & 0x02) { /* wireless module is attached */
3109                 int battery = (data[8] & 0x3f) * 100 / 31;
3110                 bool charging = !!(data[8] & 0x80);
3111
3112                 wacom_notify_battery(wacom_wac, WACOM_POWER_SUPPLY_STATUS_AUTO,
3113                                      battery, charging, battery || charging, 1);
3114
3115                 if (!wacom->battery.battery &&
3116                     !(features->quirks & WACOM_QUIRK_BATTERY)) {
3117                         features->quirks |= WACOM_QUIRK_BATTERY;
3118                         wacom_schedule_work(wacom_wac, WACOM_WORKER_BATTERY);
3119                 }
3120         }
3121         else if ((features->quirks & WACOM_QUIRK_BATTERY) &&
3122                  wacom->battery.battery) {
3123                 features->quirks &= ~WACOM_QUIRK_BATTERY;
3124                 wacom_schedule_work(wacom_wac, WACOM_WORKER_BATTERY);
3125                 wacom_notify_battery(wacom_wac, POWER_SUPPLY_STATUS_UNKNOWN, 0, 0, 0, 0);
3126         }
3127         return 0;
3128 }
3129
3130 void wacom_wac_irq(struct wacom_wac *wacom_wac, size_t len)
3131 {
3132         bool sync;
3133
3134         switch (wacom_wac->features.type) {
3135         case PENPARTNER:
3136                 sync = wacom_penpartner_irq(wacom_wac);
3137                 break;
3138
3139         case PL:
3140                 sync = wacom_pl_irq(wacom_wac);
3141                 break;
3142
3143         case WACOM_G4:
3144         case GRAPHIRE:
3145         case GRAPHIRE_BT:
3146         case WACOM_MO:
3147                 sync = wacom_graphire_irq(wacom_wac);
3148                 break;
3149
3150         case PTU:
3151                 sync = wacom_ptu_irq(wacom_wac);
3152                 break;
3153
3154         case DTU:
3155                 sync = wacom_dtu_irq(wacom_wac);
3156                 break;
3157
3158         case DTUS:
3159         case DTUSX:
3160                 sync = wacom_dtus_irq(wacom_wac);
3161                 break;
3162
3163         case INTUOS:
3164         case INTUOS3S:
3165         case INTUOS3:
3166         case INTUOS3L:
3167         case INTUOS4S:
3168         case INTUOS4:
3169         case INTUOS4L:
3170         case CINTIQ:
3171         case WACOM_BEE:
3172         case WACOM_13HD:
3173         case WACOM_21UX2:
3174         case WACOM_22HD:
3175         case WACOM_24HD:
3176         case WACOM_27QHD:
3177         case DTK:
3178         case CINTIQ_HYBRID:
3179         case CINTIQ_COMPANION_2:
3180                 sync = wacom_intuos_irq(wacom_wac);
3181                 break;
3182
3183         case INTUOS4WL:
3184                 sync = wacom_intuos_bt_irq(wacom_wac, len);
3185                 break;
3186
3187         case WACOM_24HDT:
3188         case WACOM_27QHDT:
3189                 sync = wacom_24hdt_irq(wacom_wac);
3190                 break;
3191
3192         case INTUOS5S:
3193         case INTUOS5:
3194         case INTUOS5L:
3195         case INTUOSPS:
3196         case INTUOSPM:
3197         case INTUOSPL:
3198                 if (len == WACOM_PKGLEN_BBTOUCH3)
3199                         sync = wacom_bpt3_touch(wacom_wac);
3200                 else if (wacom_wac->data[0] == WACOM_REPORT_USB)
3201                         sync = wacom_status_irq(wacom_wac, len);
3202                 else
3203                         sync = wacom_intuos_irq(wacom_wac);
3204                 break;