HID: wacom: leds: handle the switch of the LEDs directly in the kernel
[cascardo/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 void wacom_report_numbered_buttons(struct input_dev *input_dev,
38                                 int button_count, int mask);
39
40 /*
41  * Percent of battery capacity for Graphire.
42  * 8th value means AC online and show 100% capacity.
43  */
44 static unsigned short batcap_gr[8] = { 1, 15, 25, 35, 50, 70, 100, 100 };
45
46 /*
47  * Percent of battery capacity for Intuos4 WL, AC has a separate bit.
48  */
49 static unsigned short batcap_i4[8] = { 1, 15, 30, 45, 60, 70, 85, 100 };
50
51 static void __wacom_notify_battery(struct wacom_battery *battery,
52                                    int bat_capacity, bool bat_charging,
53                                    bool bat_connected, bool ps_connected)
54 {
55         bool changed = battery->battery_capacity != bat_capacity  ||
56                        battery->bat_charging     != bat_charging  ||
57                        battery->bat_connected    != bat_connected ||
58                        battery->ps_connected     != ps_connected;
59
60         if (changed) {
61                 battery->battery_capacity = bat_capacity;
62                 battery->bat_charging = bat_charging;
63                 battery->bat_connected = bat_connected;
64                 battery->ps_connected = ps_connected;
65
66                 if (battery->battery)
67                         power_supply_changed(battery->battery);
68         }
69 }
70
71 static void wacom_notify_battery(struct wacom_wac *wacom_wac,
72         int bat_capacity, bool bat_charging, bool bat_connected,
73         bool ps_connected)
74 {
75         struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
76
77         __wacom_notify_battery(&wacom->battery, bat_capacity, bat_charging,
78                                bat_connected, ps_connected);
79 }
80
81 static int wacom_penpartner_irq(struct wacom_wac *wacom)
82 {
83         unsigned char *data = wacom->data;
84         struct input_dev *input = wacom->pen_input;
85
86         switch (data[0]) {
87         case 1:
88                 if (data[5] & 0x80) {
89                         wacom->tool[0] = (data[5] & 0x20) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
90                         wacom->id[0] = (data[5] & 0x20) ? ERASER_DEVICE_ID : STYLUS_DEVICE_ID;
91                         input_report_key(input, wacom->tool[0], 1);
92                         input_report_abs(input, ABS_MISC, wacom->id[0]); /* report tool id */
93                         input_report_abs(input, ABS_X, get_unaligned_le16(&data[1]));
94                         input_report_abs(input, ABS_Y, get_unaligned_le16(&data[3]));
95                         input_report_abs(input, ABS_PRESSURE, (signed char)data[6] + 127);
96                         input_report_key(input, BTN_TOUCH, ((signed char)data[6] > -127));
97                         input_report_key(input, BTN_STYLUS, (data[5] & 0x40));
98                 } else {
99                         input_report_key(input, wacom->tool[0], 0);
100                         input_report_abs(input, ABS_MISC, 0); /* report tool id */
101                         input_report_abs(input, ABS_PRESSURE, -1);
102                         input_report_key(input, BTN_TOUCH, 0);
103                 }
104                 break;
105
106         case 2:
107                 input_report_key(input, BTN_TOOL_PEN, 1);
108                 input_report_abs(input, ABS_MISC, STYLUS_DEVICE_ID); /* report tool id */
109                 input_report_abs(input, ABS_X, get_unaligned_le16(&data[1]));
110                 input_report_abs(input, ABS_Y, get_unaligned_le16(&data[3]));
111                 input_report_abs(input, ABS_PRESSURE, (signed char)data[6] + 127);
112                 input_report_key(input, BTN_TOUCH, ((signed char)data[6] > -80) && !(data[5] & 0x20));
113                 input_report_key(input, BTN_STYLUS, (data[5] & 0x40));
114                 break;
115
116         default:
117                 dev_dbg(input->dev.parent,
118                         "%s: received unknown report #%d\n", __func__, data[0]);
119                 return 0;
120         }
121
122         return 1;
123 }
124
125 static int wacom_pl_irq(struct wacom_wac *wacom)
126 {
127         struct wacom_features *features = &wacom->features;
128         unsigned char *data = wacom->data;
129         struct input_dev *input = wacom->pen_input;
130         int prox, pressure;
131
132         if (data[0] != WACOM_REPORT_PENABLED) {
133                 dev_dbg(input->dev.parent,
134                         "%s: received unknown report #%d\n", __func__, data[0]);
135                 return 0;
136         }
137
138         prox = data[1] & 0x40;
139
140         if (!wacom->id[0]) {
141                 if ((data[0] & 0x10) || (data[4] & 0x20)) {
142                         wacom->tool[0] = BTN_TOOL_RUBBER;
143                         wacom->id[0] = ERASER_DEVICE_ID;
144                 }
145                 else {
146                         wacom->tool[0] = BTN_TOOL_PEN;
147                         wacom->id[0] = STYLUS_DEVICE_ID;
148                 }
149         }
150
151         /* If the eraser is in prox, STYLUS2 is always set. If we
152          * mis-detected the type and notice that STYLUS2 isn't set
153          * then force the eraser out of prox and let the pen in.
154          */
155         if (wacom->tool[0] == BTN_TOOL_RUBBER && !(data[4] & 0x20)) {
156                 input_report_key(input, BTN_TOOL_RUBBER, 0);
157                 input_report_abs(input, ABS_MISC, 0);
158                 input_sync(input);
159                 wacom->tool[0] = BTN_TOOL_PEN;
160                 wacom->id[0] = STYLUS_DEVICE_ID;
161         }
162
163         pressure = (signed char)((data[7] << 1) | ((data[4] >> 2) & 1));
164         if (features->pressure_max > 255)
165                 pressure = (pressure << 1) | ((data[4] >> 6) & 1);
166         pressure += (features->pressure_max + 1) / 2;
167
168         input_report_abs(input, ABS_X, data[3] | (data[2] << 7) | ((data[1] & 0x03) << 14));
169         input_report_abs(input, ABS_Y, data[6] | (data[5] << 7) | ((data[4] & 0x03) << 14));
170         input_report_abs(input, ABS_PRESSURE, pressure);
171
172         input_report_key(input, BTN_TOUCH, data[4] & 0x08);
173         input_report_key(input, BTN_STYLUS, data[4] & 0x10);
174         /* Only allow the stylus2 button to be reported for the pen tool. */
175         input_report_key(input, BTN_STYLUS2, (wacom->tool[0] == BTN_TOOL_PEN) && (data[4] & 0x20));
176
177         if (!prox)
178                 wacom->id[0] = 0;
179         input_report_key(input, wacom->tool[0], prox);
180         input_report_abs(input, ABS_MISC, wacom->id[0]);
181         return 1;
182 }
183
184 static int wacom_ptu_irq(struct wacom_wac *wacom)
185 {
186         unsigned char *data = wacom->data;
187         struct input_dev *input = wacom->pen_input;
188
189         if (data[0] != WACOM_REPORT_PENABLED) {
190                 dev_dbg(input->dev.parent,
191                         "%s: received unknown report #%d\n", __func__, data[0]);
192                 return 0;
193         }
194
195         if (data[1] & 0x04) {
196                 input_report_key(input, BTN_TOOL_RUBBER, data[1] & 0x20);
197                 input_report_key(input, BTN_TOUCH, data[1] & 0x08);
198                 wacom->id[0] = ERASER_DEVICE_ID;
199         } else {
200                 input_report_key(input, BTN_TOOL_PEN, data[1] & 0x20);
201                 input_report_key(input, BTN_TOUCH, data[1] & 0x01);
202                 wacom->id[0] = STYLUS_DEVICE_ID;
203         }
204         input_report_abs(input, ABS_MISC, wacom->id[0]); /* report tool id */
205         input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
206         input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
207         input_report_abs(input, ABS_PRESSURE, le16_to_cpup((__le16 *)&data[6]));
208         input_report_key(input, BTN_STYLUS, data[1] & 0x02);
209         input_report_key(input, BTN_STYLUS2, data[1] & 0x10);
210         return 1;
211 }
212
213 static int wacom_dtu_irq(struct wacom_wac *wacom)
214 {
215         unsigned char *data = wacom->data;
216         struct input_dev *input = wacom->pen_input;
217         int prox = data[1] & 0x20;
218
219         dev_dbg(input->dev.parent,
220                 "%s: received report #%d", __func__, data[0]);
221
222         if (prox) {
223                 /* Going into proximity select tool */
224                 wacom->tool[0] = (data[1] & 0x0c) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
225                 if (wacom->tool[0] == BTN_TOOL_PEN)
226                         wacom->id[0] = STYLUS_DEVICE_ID;
227                 else
228                         wacom->id[0] = ERASER_DEVICE_ID;
229         }
230         input_report_key(input, BTN_STYLUS, data[1] & 0x02);
231         input_report_key(input, BTN_STYLUS2, data[1] & 0x10);
232         input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
233         input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
234         input_report_abs(input, ABS_PRESSURE, ((data[7] & 0x01) << 8) | data[6]);
235         input_report_key(input, BTN_TOUCH, data[1] & 0x05);
236         if (!prox) /* out-prox */
237                 wacom->id[0] = 0;
238         input_report_key(input, wacom->tool[0], prox);
239         input_report_abs(input, ABS_MISC, wacom->id[0]);
240         return 1;
241 }
242
243 static int wacom_dtus_irq(struct wacom_wac *wacom)
244 {
245         char *data = wacom->data;
246         struct input_dev *input = wacom->pen_input;
247         unsigned short prox, pressure = 0;
248
249         if (data[0] != WACOM_REPORT_DTUS && data[0] != WACOM_REPORT_DTUSPAD) {
250                 dev_dbg(input->dev.parent,
251                         "%s: received unknown report #%d", __func__, data[0]);
252                 return 0;
253         } else if (data[0] == WACOM_REPORT_DTUSPAD) {
254                 input = wacom->pad_input;
255                 input_report_key(input, BTN_0, (data[1] & 0x01));
256                 input_report_key(input, BTN_1, (data[1] & 0x02));
257                 input_report_key(input, BTN_2, (data[1] & 0x04));
258                 input_report_key(input, BTN_3, (data[1] & 0x08));
259                 input_report_abs(input, ABS_MISC,
260                                  data[1] & 0x0f ? PAD_DEVICE_ID : 0);
261                 return 1;
262         } else {
263                 prox = data[1] & 0x80;
264                 if (prox) {
265                         switch ((data[1] >> 3) & 3) {
266                         case 1: /* Rubber */
267                                 wacom->tool[0] = BTN_TOOL_RUBBER;
268                                 wacom->id[0] = ERASER_DEVICE_ID;
269                                 break;
270
271                         case 2: /* Pen */
272                                 wacom->tool[0] = BTN_TOOL_PEN;
273                                 wacom->id[0] = STYLUS_DEVICE_ID;
274                                 break;
275                         }
276                 }
277
278                 input_report_key(input, BTN_STYLUS, data[1] & 0x20);
279                 input_report_key(input, BTN_STYLUS2, data[1] & 0x40);
280                 input_report_abs(input, ABS_X, get_unaligned_be16(&data[3]));
281                 input_report_abs(input, ABS_Y, get_unaligned_be16(&data[5]));
282                 pressure = ((data[1] & 0x03) << 8) | (data[2] & 0xff);
283                 input_report_abs(input, ABS_PRESSURE, pressure);
284                 input_report_key(input, BTN_TOUCH, pressure > 10);
285
286                 if (!prox) /* out-prox */
287                         wacom->id[0] = 0;
288                 input_report_key(input, wacom->tool[0], prox);
289                 input_report_abs(input, ABS_MISC, wacom->id[0]);
290                 return 1;
291         }
292 }
293
294 static int wacom_graphire_irq(struct wacom_wac *wacom)
295 {
296         struct wacom_features *features = &wacom->features;
297         unsigned char *data = wacom->data;
298         struct input_dev *input = wacom->pen_input;
299         struct input_dev *pad_input = wacom->pad_input;
300         int battery_capacity, ps_connected;
301         int prox;
302         int rw = 0;
303         int retval = 0;
304
305         if (features->type == GRAPHIRE_BT) {
306                 if (data[0] != WACOM_REPORT_PENABLED_BT) {
307                         dev_dbg(input->dev.parent,
308                                 "%s: received unknown report #%d\n", __func__,
309                                 data[0]);
310                         goto exit;
311                 }
312         } else if (data[0] != WACOM_REPORT_PENABLED) {
313                 dev_dbg(input->dev.parent,
314                         "%s: received unknown report #%d\n", __func__, data[0]);
315                 goto exit;
316         }
317
318         prox = data[1] & 0x80;
319         if (prox || wacom->id[0]) {
320                 if (prox) {
321                         switch ((data[1] >> 5) & 3) {
322
323                         case 0: /* Pen */
324                                 wacom->tool[0] = BTN_TOOL_PEN;
325                                 wacom->id[0] = STYLUS_DEVICE_ID;
326                                 break;
327
328                         case 1: /* Rubber */
329                                 wacom->tool[0] = BTN_TOOL_RUBBER;
330                                 wacom->id[0] = ERASER_DEVICE_ID;
331                                 break;
332
333                         case 2: /* Mouse with wheel */
334                                 input_report_key(input, BTN_MIDDLE, data[1] & 0x04);
335                                 /* fall through */
336
337                         case 3: /* Mouse without wheel */
338                                 wacom->tool[0] = BTN_TOOL_MOUSE;
339                                 wacom->id[0] = CURSOR_DEVICE_ID;
340                                 break;
341                         }
342                 }
343                 input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
344                 input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
345                 if (wacom->tool[0] != BTN_TOOL_MOUSE) {
346                         if (features->type == GRAPHIRE_BT)
347                                 input_report_abs(input, ABS_PRESSURE, data[6] |
348                                         (((__u16) (data[1] & 0x08)) << 5));
349                         else
350                                 input_report_abs(input, ABS_PRESSURE, data[6] |
351                                         ((data[7] & 0x03) << 8));
352                         input_report_key(input, BTN_TOUCH, data[1] & 0x01);
353                         input_report_key(input, BTN_STYLUS, data[1] & 0x02);
354                         input_report_key(input, BTN_STYLUS2, data[1] & 0x04);
355                 } else {
356                         input_report_key(input, BTN_LEFT, data[1] & 0x01);
357                         input_report_key(input, BTN_RIGHT, data[1] & 0x02);
358                         if (features->type == WACOM_G4 ||
359                                         features->type == WACOM_MO) {
360                                 input_report_abs(input, ABS_DISTANCE, data[6] & 0x3f);
361                                 rw = (data[7] & 0x04) - (data[7] & 0x03);
362                         } else if (features->type == GRAPHIRE_BT) {
363                                 /* Compute distance between mouse and tablet */
364                                 rw = 44 - (data[6] >> 2);
365                                 rw = clamp_val(rw, 0, 31);
366                                 input_report_abs(input, ABS_DISTANCE, rw);
367                                 if (((data[1] >> 5) & 3) == 2) {
368                                         /* Mouse with wheel */
369                                         input_report_key(input, BTN_MIDDLE,
370                                                         data[1] & 0x04);
371                                         rw = (data[6] & 0x01) ? -1 :
372                                                 (data[6] & 0x02) ? 1 : 0;
373                                 } else {
374                                         rw = 0;
375                                 }
376                         } else {
377                                 input_report_abs(input, ABS_DISTANCE, data[7] & 0x3f);
378                                 rw = -(signed char)data[6];
379                         }
380                         input_report_rel(input, REL_WHEEL, rw);
381                 }
382
383                 if (!prox)
384                         wacom->id[0] = 0;
385                 input_report_abs(input, ABS_MISC, wacom->id[0]); /* report tool id */
386                 input_report_key(input, wacom->tool[0], prox);
387                 input_sync(input); /* sync last event */
388         }
389
390         /* send pad data */
391         switch (features->type) {
392         case WACOM_G4:
393                 prox = data[7] & 0xf8;
394                 if (prox || wacom->id[1]) {
395                         wacom->id[1] = PAD_DEVICE_ID;
396                         input_report_key(pad_input, BTN_BACK, (data[7] & 0x40));
397                         input_report_key(pad_input, BTN_FORWARD, (data[7] & 0x80));
398                         rw = ((data[7] & 0x18) >> 3) - ((data[7] & 0x20) >> 3);
399                         input_report_rel(pad_input, REL_WHEEL, rw);
400                         if (!prox)
401                                 wacom->id[1] = 0;
402                         input_report_abs(pad_input, ABS_MISC, wacom->id[1]);
403                         retval = 1;
404                 }
405                 break;
406
407         case WACOM_MO:
408                 prox = (data[7] & 0xf8) || data[8];
409                 if (prox || wacom->id[1]) {
410                         wacom->id[1] = PAD_DEVICE_ID;
411                         input_report_key(pad_input, BTN_BACK, (data[7] & 0x08));
412                         input_report_key(pad_input, BTN_LEFT, (data[7] & 0x20));
413                         input_report_key(pad_input, BTN_FORWARD, (data[7] & 0x10));
414                         input_report_key(pad_input, BTN_RIGHT, (data[7] & 0x40));
415                         input_report_abs(pad_input, ABS_WHEEL, (data[8] & 0x7f));
416                         if (!prox)
417                                 wacom->id[1] = 0;
418                         input_report_abs(pad_input, ABS_MISC, wacom->id[1]);
419                         retval = 1;
420                 }
421                 break;
422         case GRAPHIRE_BT:
423                 prox = data[7] & 0x03;
424                 if (prox || wacom->id[1]) {
425                         wacom->id[1] = PAD_DEVICE_ID;
426                         input_report_key(pad_input, BTN_0, (data[7] & 0x02));
427                         input_report_key(pad_input, BTN_1, (data[7] & 0x01));
428                         if (!prox)
429                                 wacom->id[1] = 0;
430                         input_report_abs(pad_input, ABS_MISC, wacom->id[1]);
431                         retval = 1;
432                 }
433                 break;
434         }
435
436         /* Store current battery capacity and power supply state */
437         if (features->type == GRAPHIRE_BT) {
438                 rw = (data[7] >> 2 & 0x07);
439                 battery_capacity = batcap_gr[rw];
440                 ps_connected = rw == 7;
441                 wacom_notify_battery(wacom, battery_capacity, ps_connected,
442                                      1, ps_connected);
443         }
444 exit:
445         return retval;
446 }
447
448 static void wacom_intuos_schedule_prox_event(struct wacom_wac *wacom_wac)
449 {
450         struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
451         struct wacom_features *features = &wacom_wac->features;
452         struct hid_report *r;
453         struct hid_report_enum *re;
454
455         re = &(wacom->hdev->report_enum[HID_FEATURE_REPORT]);
456         if (features->type == INTUOSHT2)
457                 r = re->report_id_hash[WACOM_REPORT_INTUOSHT2_ID];
458         else
459                 r = re->report_id_hash[WACOM_REPORT_INTUOS_ID1];
460         if (r) {
461                 hid_hw_request(wacom->hdev, r, HID_REQ_GET_REPORT);
462         }
463 }
464
465 static int wacom_intuos_pad(struct wacom_wac *wacom)
466 {
467         struct wacom_features *features = &wacom->features;
468         unsigned char *data = wacom->data;
469         struct input_dev *input = wacom->pad_input;
470         int i;
471         int buttons = 0, nbuttons = features->numbered_buttons;
472         int keys = 0, nkeys = 0;
473         int ring1 = 0, ring2 = 0;
474         int strip1 = 0, strip2 = 0;
475         bool prox = false;
476
477         /* pad packets. Works as a second tool and is always in prox */
478         if (!(data[0] == WACOM_REPORT_INTUOSPAD || data[0] == WACOM_REPORT_INTUOS5PAD ||
479               data[0] == WACOM_REPORT_CINTIQPAD))
480                 return 0;
481
482         if (features->type >= INTUOS4S && features->type <= INTUOS4L) {
483                 buttons = (data[3] << 1) | (data[2] & 0x01);
484                 ring1 = data[1];
485         } else if (features->type == DTK) {
486                 buttons = data[6];
487         } else if (features->type == WACOM_13HD) {
488                 buttons = (data[4] << 1) | (data[3] & 0x01);
489         } else if (features->type == WACOM_24HD) {
490                 buttons = (data[8] << 8) | data[6];
491                 ring1 = data[1];
492                 ring2 = data[2];
493
494                 /*
495                  * Three "buttons" are available on the 24HD which are
496                  * physically implemented as a touchstrip. Each button
497                  * is approximately 3 bits wide with a 2 bit spacing.
498                  * The raw touchstrip bits are stored at:
499                  *    ((data[3] & 0x1f) << 8) | data[4])
500                  */
501                 nkeys = 3;
502                 keys = ((data[3] & 0x1C) ? 1<<2 : 0) |
503                        ((data[4] & 0xE0) ? 1<<1 : 0) |
504                        ((data[4] & 0x07) ? 1<<0 : 0);
505         } else if (features->type == WACOM_27QHD) {
506                 nkeys = 3;
507                 keys = data[2] & 0x07;
508
509                 input_report_abs(input, ABS_X, be16_to_cpup((__be16 *)&data[4]));
510                 input_report_abs(input, ABS_Y, be16_to_cpup((__be16 *)&data[6]));
511                 input_report_abs(input, ABS_Z, be16_to_cpup((__be16 *)&data[8]));
512         } else if (features->type == CINTIQ_HYBRID) {
513                 /*
514                  * Do not send hardware buttons under Android. They
515                  * are already sent to the system through GPIO (and
516                  * have different meaning).
517                  *
518                  * d-pad right  -> data[4] & 0x10
519                  * d-pad up     -> data[4] & 0x20
520                  * d-pad left   -> data[4] & 0x40
521                  * d-pad down   -> data[4] & 0x80
522                  * d-pad center -> data[3] & 0x01
523                  */
524                 buttons = (data[4] << 1) | (data[3] & 0x01);
525         } else if (features->type == CINTIQ_COMPANION_2) {
526                 /* d-pad right  -> data[4] & 0x10
527                  * d-pad up     -> data[4] & 0x20
528                  * d-pad left   -> data[4] & 0x40
529                  * d-pad down   -> data[4] & 0x80
530                  * d-pad center -> data[3] & 0x01
531                  */
532                 buttons = ((data[2] >> 4) << 7) |
533                           ((data[1] & 0x04) << 6) |
534                           ((data[2] & 0x0F) << 2) |
535                           (data[1] & 0x03);
536         } else if (features->type >= INTUOS5S && features->type <= INTUOSPL) {
537                 /*
538                  * ExpressKeys on Intuos5/Intuos Pro have a capacitive sensor in
539                  * addition to the mechanical switch. Switch data is
540                  * stored in data[4], capacitive data in data[5].
541                  *
542                  * Touch ring mode switch (data[3]) has no capacitive sensor
543                  */
544                 buttons = (data[4] << 1) | (data[3] & 0x01);
545                 ring1 = data[2];
546         } else {
547                 if (features->type == WACOM_21UX2 || features->type == WACOM_22HD) {
548                         buttons = (data[8] << 10) | ((data[7] & 0x01) << 9) |
549                                   (data[6] << 1) | (data[5] & 0x01);
550
551                         if (features->type == WACOM_22HD) {
552                                 nkeys = 3;
553                                 keys = data[9] & 0x07;
554                         }
555                 } else {
556                         buttons = ((data[6] & 0x10) << 10) |
557                                   ((data[5] & 0x10) << 9)  |
558                                   ((data[6] & 0x0F) << 4)  |
559                                   (data[5] & 0x0F);
560                 }
561                 strip1 = ((data[1] & 0x1f) << 8) | data[2];
562                 strip2 = ((data[3] & 0x1f) << 8) | data[4];
563         }
564
565         prox = (buttons & ~(~0 << nbuttons)) | (keys & ~(~0 << nkeys)) |
566                (ring1 & 0x80) | (ring2 & 0x80) | strip1 | strip2;
567
568         wacom_report_numbered_buttons(input, nbuttons, buttons);
569
570         for (i = 0; i < nkeys; i++)
571                 input_report_key(input, KEY_PROG1 + i, keys & (1 << i));
572
573         input_report_abs(input, ABS_RX, strip1);
574         input_report_abs(input, ABS_RY, strip2);
575
576         input_report_abs(input, ABS_WHEEL,    (ring1 & 0x80) ? (ring1 & 0x7f) : 0);
577         input_report_abs(input, ABS_THROTTLE, (ring2 & 0x80) ? (ring2 & 0x7f) : 0);
578
579         input_report_key(input, wacom->tool[1], prox ? 1 : 0);
580         input_report_abs(input, ABS_MISC, prox ? PAD_DEVICE_ID : 0);
581
582         input_event(input, EV_MSC, MSC_SERIAL, 0xffffffff);
583
584         return 1;
585 }
586
587 static int wacom_intuos_get_tool_type(int tool_id)
588 {
589         int tool_type;
590
591         switch (tool_id) {
592         case 0x812: /* Inking pen */
593         case 0x801: /* Intuos3 Inking pen */
594         case 0x120802: /* Intuos4/5 Inking Pen */
595         case 0x012:
596                 tool_type = BTN_TOOL_PENCIL;
597                 break;
598
599         case 0x822: /* Pen */
600         case 0x842:
601         case 0x852:
602         case 0x823: /* Intuos3 Grip Pen */
603         case 0x813: /* Intuos3 Classic Pen */
604         case 0x885: /* Intuos3 Marker Pen */
605         case 0x802: /* Intuos4/5 13HD/24HD General Pen */
606         case 0x804: /* Intuos4/5 13HD/24HD Marker Pen */
607         case 0x8e2: /* IntuosHT2 pen */
608         case 0x022:
609         case 0x100804: /* Intuos4/5 13HD/24HD Art Pen */
610         case 0x140802: /* Intuos4/5 13HD/24HD Classic Pen */
611         case 0x160802: /* Cintiq 13HD Pro Pen */
612         case 0x180802: /* DTH2242 Pen */
613         case 0x100802: /* Intuos4/5 13HD/24HD General Pen */
614                 tool_type = BTN_TOOL_PEN;
615                 break;
616
617         case 0x832: /* Stroke pen */
618         case 0x032:
619                 tool_type = BTN_TOOL_BRUSH;
620                 break;
621
622         case 0x007: /* Mouse 4D and 2D */
623         case 0x09c:
624         case 0x094:
625         case 0x017: /* Intuos3 2D Mouse */
626         case 0x806: /* Intuos4 Mouse */
627                 tool_type = BTN_TOOL_MOUSE;
628                 break;
629
630         case 0x096: /* Lens cursor */
631         case 0x097: /* Intuos3 Lens cursor */
632         case 0x006: /* Intuos4 Lens cursor */
633                 tool_type = BTN_TOOL_LENS;
634                 break;
635
636         case 0x82a: /* Eraser */
637         case 0x85a:
638         case 0x91a:
639         case 0xd1a:
640         case 0x0fa:
641         case 0x82b: /* Intuos3 Grip Pen Eraser */
642         case 0x81b: /* Intuos3 Classic Pen Eraser */
643         case 0x91b: /* Intuos3 Airbrush Eraser */
644         case 0x80c: /* Intuos4/5 13HD/24HD Marker Pen Eraser */
645         case 0x80a: /* Intuos4/5 13HD/24HD General Pen Eraser */
646         case 0x90a: /* Intuos4/5 13HD/24HD Airbrush Eraser */
647         case 0x14080a: /* Intuos4/5 13HD/24HD Classic Pen Eraser */
648         case 0x10090a: /* Intuos4/5 13HD/24HD Airbrush Eraser */
649         case 0x10080c: /* Intuos4/5 13HD/24HD Art Pen Eraser */
650         case 0x16080a: /* Cintiq 13HD Pro Pen Eraser */
651         case 0x18080a: /* DTH2242 Eraser */
652         case 0x10080a: /* Intuos4/5 13HD/24HD General Pen Eraser */
653                 tool_type = BTN_TOOL_RUBBER;
654                 break;
655
656         case 0xd12:
657         case 0x912:
658         case 0x112:
659         case 0x913: /* Intuos3 Airbrush */
660         case 0x902: /* Intuos4/5 13HD/24HD Airbrush */
661         case 0x100902: /* Intuos4/5 13HD/24HD Airbrush */
662                 tool_type = BTN_TOOL_AIRBRUSH;
663                 break;
664
665         default: /* Unknown tool */
666                 tool_type = BTN_TOOL_PEN;
667                 break;
668         }
669         return tool_type;
670 }
671
672 static int wacom_intuos_inout(struct wacom_wac *wacom)
673 {
674         struct wacom_features *features = &wacom->features;
675         unsigned char *data = wacom->data;
676         struct input_dev *input = wacom->pen_input;
677         int idx = (features->type == INTUOS) ? (data[1] & 0x01) : 0;
678
679         if (!(((data[1] & 0xfc) == 0xc0) ||  /* in prox */
680             ((data[1] & 0xfe) == 0x20) ||    /* in range */
681             ((data[1] & 0xfe) == 0x80)))     /* out prox */
682                 return 0;
683
684         /* Enter report */
685         if ((data[1] & 0xfc) == 0xc0) {
686                 /* serial number of the tool */
687                 wacom->serial[idx] = ((data[3] & 0x0f) << 28) +
688                         (data[4] << 20) + (data[5] << 12) +
689                         (data[6] << 4) + (data[7] >> 4);
690
691                 wacom->id[idx] = (data[2] << 4) | (data[3] >> 4) |
692                         ((data[7] & 0x0f) << 20) | ((data[8] & 0xf0) << 12);
693
694                 wacom->tool[idx] = wacom_intuos_get_tool_type(wacom->id[idx]);
695
696                 wacom->shared->stylus_in_proximity = true;
697                 return 1;
698         }
699
700         /* in Range */
701         if ((data[1] & 0xfe) == 0x20) {
702                 if (features->type != INTUOSHT2)
703                         wacom->shared->stylus_in_proximity = true;
704
705                 /* in Range while exiting */
706                 if (wacom->reporting_data) {
707                         input_report_key(input, BTN_TOUCH, 0);
708                         input_report_abs(input, ABS_PRESSURE, 0);
709                         input_report_abs(input, ABS_DISTANCE, wacom->features.distance_max);
710                         return 2;
711                 }
712                 return 1;
713         }
714
715         /* Exit report */
716         if ((data[1] & 0xfe) == 0x80) {
717                 wacom->shared->stylus_in_proximity = false;
718                 wacom->reporting_data = false;
719
720                 /* don't report exit if we don't know the ID */
721                 if (!wacom->id[idx])
722                         return 1;
723
724                 /*
725                  * Reset all states otherwise we lose the initial states
726                  * when in-prox next time
727                  */
728                 input_report_abs(input, ABS_X, 0);
729                 input_report_abs(input, ABS_Y, 0);
730                 input_report_abs(input, ABS_DISTANCE, 0);
731                 input_report_abs(input, ABS_TILT_X, 0);
732                 input_report_abs(input, ABS_TILT_Y, 0);
733                 if (wacom->tool[idx] >= BTN_TOOL_MOUSE) {
734                         input_report_key(input, BTN_LEFT, 0);
735                         input_report_key(input, BTN_MIDDLE, 0);
736                         input_report_key(input, BTN_RIGHT, 0);
737                         input_report_key(input, BTN_SIDE, 0);
738                         input_report_key(input, BTN_EXTRA, 0);
739                         input_report_abs(input, ABS_THROTTLE, 0);
740                         input_report_abs(input, ABS_RZ, 0);
741                 } else {
742                         input_report_abs(input, ABS_PRESSURE, 0);
743                         input_report_key(input, BTN_STYLUS, 0);
744                         input_report_key(input, BTN_STYLUS2, 0);
745                         input_report_key(input, BTN_TOUCH, 0);
746                         input_report_abs(input, ABS_WHEEL, 0);
747                         if (features->type >= INTUOS3S)
748                                 input_report_abs(input, ABS_Z, 0);
749                 }
750                 input_report_key(input, wacom->tool[idx], 0);
751                 input_report_abs(input, ABS_MISC, 0); /* reset tool id */
752                 input_event(input, EV_MSC, MSC_SERIAL, wacom->serial[idx]);
753                 wacom->id[idx] = 0;
754                 return 2;
755         }
756
757         return 0;
758 }
759
760 static int wacom_remote_irq(struct wacom_wac *wacom_wac, size_t len)
761 {
762         unsigned char *data = wacom_wac->data;
763         struct input_dev *input;
764         struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
765         struct wacom_remote *remote = wacom->remote;
766         int bat_charging, bat_percent, touch_ring_mode;
767         __u32 serial;
768         int i, index = -1;
769         unsigned long flags;
770
771         if (data[0] != WACOM_REPORT_REMOTE) {
772                 hid_dbg(wacom->hdev, "%s: received unknown report #%d",
773                         __func__, data[0]);
774                 return 0;
775         }
776
777         serial = data[3] + (data[4] << 8) + (data[5] << 16);
778         wacom_wac->id[0] = PAD_DEVICE_ID;
779
780         spin_lock_irqsave(&remote->remote_lock, flags);
781
782         for (i = 0; i < WACOM_MAX_REMOTES; i++) {
783                 if (remote->remotes[i].serial == serial) {
784                         index = i;
785                         break;
786                 }
787         }
788
789         if (index < 0 || !remote->remotes[index].registered)
790                 goto out;
791
792         input = remote->remotes[index].input;
793
794         input_report_key(input, BTN_0, (data[9] & 0x01));
795         input_report_key(input, BTN_1, (data[9] & 0x02));
796         input_report_key(input, BTN_2, (data[9] & 0x04));
797         input_report_key(input, BTN_3, (data[9] & 0x08));
798         input_report_key(input, BTN_4, (data[9] & 0x10));
799         input_report_key(input, BTN_5, (data[9] & 0x20));
800         input_report_key(input, BTN_6, (data[9] & 0x40));
801         input_report_key(input, BTN_7, (data[9] & 0x80));
802
803         input_report_key(input, BTN_8, (data[10] & 0x01));
804         input_report_key(input, BTN_9, (data[10] & 0x02));
805         input_report_key(input, BTN_A, (data[10] & 0x04));
806         input_report_key(input, BTN_B, (data[10] & 0x08));
807         input_report_key(input, BTN_C, (data[10] & 0x10));
808         input_report_key(input, BTN_X, (data[10] & 0x20));
809         input_report_key(input, BTN_Y, (data[10] & 0x40));
810         input_report_key(input, BTN_Z, (data[10] & 0x80));
811
812         input_report_key(input, BTN_BASE, (data[11] & 0x01));
813         input_report_key(input, BTN_BASE2, (data[11] & 0x02));
814
815         if (data[12] & 0x80)
816                 input_report_abs(input, ABS_WHEEL, (data[12] & 0x7f));
817         else
818                 input_report_abs(input, ABS_WHEEL, 0);
819
820         bat_percent = data[7] & 0x7f;
821         bat_charging = !!(data[7] & 0x80);
822
823         if (data[9] | data[10] | (data[11] & 0x03) | data[12])
824                 input_report_abs(input, ABS_MISC, PAD_DEVICE_ID);
825         else
826                 input_report_abs(input, ABS_MISC, 0);
827
828         input_event(input, EV_MSC, MSC_SERIAL, serial);
829
830         input_sync(input);
831
832         /*Which mode select (LED light) is currently on?*/
833         touch_ring_mode = (data[11] & 0xC0) >> 6;
834
835         for (i = 0; i < WACOM_MAX_REMOTES; i++) {
836                 if (remote->remotes[i].serial == serial)
837                         wacom->led.groups[i].select = touch_ring_mode;
838         }
839
840         __wacom_notify_battery(&remote->remotes[index].battery, bat_percent,
841                                 bat_charging, 1, bat_charging);
842
843 out:
844         spin_unlock_irqrestore(&remote->remote_lock, flags);
845         return 0;
846 }
847
848 static void wacom_remote_status_irq(struct wacom_wac *wacom_wac, size_t len)
849 {
850         struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
851         unsigned char *data = wacom_wac->data;
852         struct wacom_remote *remote = wacom->remote;
853         struct wacom_remote_data remote_data;
854         unsigned long flags;
855         int i, ret;
856
857         if (data[0] != WACOM_REPORT_DEVICE_LIST)
858                 return;
859
860         memset(&remote_data, 0, sizeof(struct wacom_remote_data));
861
862         for (i = 0; i < WACOM_MAX_REMOTES; i++) {
863                 int j = i * 6;
864                 int serial = (data[j+6] << 16) + (data[j+5] << 8) + data[j+4];
865                 bool connected = data[j+2];
866
867                 remote_data.remote[i].serial = serial;
868                 remote_data.remote[i].connected = connected;
869         }
870
871         spin_lock_irqsave(&remote->remote_lock, flags);
872
873         ret = kfifo_in(&remote->remote_fifo, &remote_data, sizeof(remote_data));
874         if (ret != sizeof(remote_data)) {
875                 spin_unlock_irqrestore(&remote->remote_lock, flags);
876                 hid_err(wacom->hdev, "Can't queue Remote status event.\n");
877                 return;
878         }
879
880         spin_unlock_irqrestore(&remote->remote_lock, flags);
881
882         wacom_schedule_work(wacom_wac, WACOM_WORKER_REMOTE);
883 }
884
885 static int wacom_intuos_general(struct wacom_wac *wacom)
886 {
887         struct wacom_features *features = &wacom->features;
888         unsigned char *data = wacom->data;
889         struct input_dev *input = wacom->pen_input;
890         int idx = (features->type == INTUOS) ? (data[1] & 0x01) : 0;
891         unsigned char type = (data[1] >> 1) & 0x0F;
892         unsigned int x, y, distance, t;
893
894         if (data[0] != WACOM_REPORT_PENABLED && data[0] != WACOM_REPORT_CINTIQ &&
895                 data[0] != WACOM_REPORT_INTUOS_PEN)
896                 return 0;
897
898         if (wacom->shared->touch_down)
899                 return 1;
900
901         /* don't report events if we don't know the tool ID */
902         if (!wacom->id[idx]) {
903                 /* but reschedule a read of the current tool */
904                 wacom_intuos_schedule_prox_event(wacom);
905                 return 1;
906         }
907
908         /*
909          * don't report events for invalid data
910          */
911         /* older I4 styli don't work with new Cintiqs */
912         if ((!((wacom->id[idx] >> 20) & 0x01) &&
913                         (features->type == WACOM_21UX2)) ||
914             /* Only large Intuos support Lense Cursor */
915             (wacom->tool[idx] == BTN_TOOL_LENS &&
916                 (features->type == INTUOS3 ||
917                  features->type == INTUOS3S ||
918                  features->type == INTUOS4 ||
919                  features->type == INTUOS4S ||
920                  features->type == INTUOS5 ||
921                  features->type == INTUOS5S ||
922                  features->type == INTUOSPM ||
923                  features->type == INTUOSPS)) ||
924            /* Cintiq doesn't send data when RDY bit isn't set */
925            (features->type == CINTIQ && !(data[1] & 0x40)))
926                 return 1;
927
928         x = (be16_to_cpup((__be16 *)&data[2]) << 1) | ((data[9] >> 1) & 1);
929         y = (be16_to_cpup((__be16 *)&data[4]) << 1) | (data[9] & 1);
930         distance = data[9] >> 2;
931         if (features->type < INTUOS3S) {
932                 x >>= 1;
933                 y >>= 1;
934                 distance >>= 1;
935         }
936         input_report_abs(input, ABS_X, x);
937         input_report_abs(input, ABS_Y, y);
938         input_report_abs(input, ABS_DISTANCE, distance);
939
940         switch (type) {
941         case 0x00:
942         case 0x01:
943         case 0x02:
944         case 0x03:
945                 /* general pen packet */
946                 t = (data[6] << 3) | ((data[7] & 0xC0) >> 5) | (data[1] & 1);
947                 if (features->pressure_max < 2047)
948                         t >>= 1;
949                 input_report_abs(input, ABS_PRESSURE, t);
950                 if (features->type != INTUOSHT2) {
951                     input_report_abs(input, ABS_TILT_X,
952                                  (((data[7] << 1) & 0x7e) | (data[8] >> 7)) - 64);
953                     input_report_abs(input, ABS_TILT_Y, (data[8] & 0x7f) - 64);
954                 }
955                 input_report_key(input, BTN_STYLUS, data[1] & 2);
956                 input_report_key(input, BTN_STYLUS2, data[1] & 4);
957                 input_report_key(input, BTN_TOUCH, t > 10);
958                 break;
959
960         case 0x0a:
961                 /* airbrush second packet */
962                 input_report_abs(input, ABS_WHEEL,
963                                 (data[6] << 2) | ((data[7] >> 6) & 3));
964                 input_report_abs(input, ABS_TILT_X,
965                                  (((data[7] << 1) & 0x7e) | (data[8] >> 7)) - 64);
966                 input_report_abs(input, ABS_TILT_Y, (data[8] & 0x7f) - 64);
967                 break;
968
969         case 0x05:
970                 /* Rotation packet */
971                 if (features->type >= INTUOS3S) {
972                         /* I3 marker pen rotation */
973                         t = (data[6] << 3) | ((data[7] >> 5) & 7);
974                         t = (data[7] & 0x20) ? ((t > 900) ? ((t-1) / 2 - 1350) :
975                                 ((t-1) / 2 + 450)) : (450 - t / 2) ;
976                         input_report_abs(input, ABS_Z, t);
977                 } else {
978                         /* 4D mouse 2nd packet */
979                         t = (data[6] << 3) | ((data[7] >> 5) & 7);
980                         input_report_abs(input, ABS_RZ, (data[7] & 0x20) ?
981                                 ((t - 1) / 2) : -t / 2);
982                 }
983                 break;
984
985         case 0x04:
986                 /* 4D mouse 1st packet */
987                 input_report_key(input, BTN_LEFT,   data[8] & 0x01);
988                 input_report_key(input, BTN_MIDDLE, data[8] & 0x02);
989                 input_report_key(input, BTN_RIGHT,  data[8] & 0x04);
990
991                 input_report_key(input, BTN_SIDE,   data[8] & 0x20);
992                 input_report_key(input, BTN_EXTRA,  data[8] & 0x10);
993                 t = (data[6] << 2) | ((data[7] >> 6) & 3);
994                 input_report_abs(input, ABS_THROTTLE, (data[8] & 0x08) ? -t : t);
995                 break;
996
997         case 0x06:
998                 /* I4 mouse */
999                 input_report_key(input, BTN_LEFT,   data[6] & 0x01);
1000                 input_report_key(input, BTN_MIDDLE, data[6] & 0x02);
1001                 input_report_key(input, BTN_RIGHT,  data[6] & 0x04);
1002                 input_report_rel(input, REL_WHEEL, ((data[7] & 0x80) >> 7)
1003                                  - ((data[7] & 0x40) >> 6));
1004                 input_report_key(input, BTN_SIDE,   data[6] & 0x08);
1005                 input_report_key(input, BTN_EXTRA,  data[6] & 0x10);
1006
1007                 input_report_abs(input, ABS_TILT_X,
1008                         (((data[7] << 1) & 0x7e) | (data[8] >> 7)) - 64);
1009                 input_report_abs(input, ABS_TILT_Y, (data[8] & 0x7f) - 64);
1010                 break;
1011
1012         case 0x08:
1013                 if (wacom->tool[idx] == BTN_TOOL_MOUSE) {
1014                         /* 2D mouse packet */
1015                         input_report_key(input, BTN_LEFT,   data[8] & 0x04);
1016                         input_report_key(input, BTN_MIDDLE, data[8] & 0x08);
1017                         input_report_key(input, BTN_RIGHT,  data[8] & 0x10);
1018                         input_report_rel(input, REL_WHEEL, (data[8] & 0x01)
1019                                          - ((data[8] & 0x02) >> 1));
1020
1021                         /* I3 2D mouse side buttons */
1022                         if (features->type >= INTUOS3S && features->type <= INTUOS3L) {
1023                                 input_report_key(input, BTN_SIDE,   data[8] & 0x40);
1024                                 input_report_key(input, BTN_EXTRA,  data[8] & 0x20);
1025                         }
1026                 }
1027                 else if (wacom->tool[idx] == BTN_TOOL_LENS) {
1028                         /* Lens cursor packets */
1029                         input_report_key(input, BTN_LEFT,   data[8] & 0x01);
1030                         input_report_key(input, BTN_MIDDLE, data[8] & 0x02);
1031                         input_report_key(input, BTN_RIGHT,  data[8] & 0x04);
1032                         input_report_key(input, BTN_SIDE,   data[8] & 0x10);
1033                         input_report_key(input, BTN_EXTRA,  data[8] & 0x08);
1034                 }
1035                 break;
1036
1037         case 0x07:
1038         case 0x09:
1039         case 0x0b:
1040         case 0x0c:
1041         case 0x0d:
1042         case 0x0e:
1043         case 0x0f:
1044                 /* unhandled */
1045                 break;
1046         }
1047
1048         input_report_abs(input, ABS_MISC, wacom->id[idx]); /* report tool id */
1049         input_report_key(input, wacom->tool[idx], 1);
1050         input_event(input, EV_MSC, MSC_SERIAL, wacom->serial[idx]);
1051         wacom->reporting_data = true;
1052         return 2;
1053 }
1054
1055 static int wacom_intuos_irq(struct wacom_wac *wacom)
1056 {
1057         unsigned char *data = wacom->data;
1058         struct input_dev *input = wacom->pen_input;
1059         int result;
1060
1061         if (data[0] != WACOM_REPORT_PENABLED &&
1062             data[0] != WACOM_REPORT_INTUOS_ID1 &&
1063             data[0] != WACOM_REPORT_INTUOS_ID2 &&
1064             data[0] != WACOM_REPORT_INTUOSPAD &&
1065             data[0] != WACOM_REPORT_INTUOS_PEN &&
1066             data[0] != WACOM_REPORT_CINTIQ &&
1067             data[0] != WACOM_REPORT_CINTIQPAD &&
1068             data[0] != WACOM_REPORT_INTUOS5PAD) {
1069                 dev_dbg(input->dev.parent,
1070                         "%s: received unknown report #%d\n", __func__, data[0]);
1071                 return 0;
1072         }
1073
1074         /* process pad events */
1075         result = wacom_intuos_pad(wacom);
1076         if (result)
1077                 return result;
1078
1079         /* process in/out prox events */
1080         result = wacom_intuos_inout(wacom);
1081         if (result)
1082                 return result - 1;
1083
1084         /* process general packets */
1085         result = wacom_intuos_general(wacom);
1086         if (result)
1087                 return result - 1;
1088
1089         return 0;
1090 }
1091
1092 static int int_dist(int x1, int y1, int x2, int y2)
1093 {
1094         int x = x2 - x1;
1095         int y = y2 - y1;
1096
1097         return int_sqrt(x*x + y*y);
1098 }
1099
1100 static void wacom_intuos_bt_process_data(struct wacom_wac *wacom,
1101                 unsigned char *data)
1102 {
1103         memcpy(wacom->data, data, 10);
1104         wacom_intuos_irq(wacom);
1105
1106         input_sync(wacom->pen_input);
1107         if (wacom->pad_input)
1108                 input_sync(wacom->pad_input);
1109 }
1110
1111 static int wacom_intuos_bt_irq(struct wacom_wac *wacom, size_t len)
1112 {
1113         unsigned char data[WACOM_PKGLEN_MAX];
1114         int i = 1;
1115         unsigned power_raw, battery_capacity, bat_charging, ps_connected;
1116
1117         memcpy(data, wacom->data, len);
1118
1119         switch (data[0]) {
1120         case 0x04:
1121                 wacom_intuos_bt_process_data(wacom, data + i);
1122                 i += 10;
1123                 /* fall through */
1124         case 0x03:
1125                 wacom_intuos_bt_process_data(wacom, data + i);
1126                 i += 10;
1127                 wacom_intuos_bt_process_data(wacom, data + i);
1128                 i += 10;
1129                 power_raw = data[i];
1130                 bat_charging = (power_raw & 0x08) ? 1 : 0;
1131                 ps_connected = (power_raw & 0x10) ? 1 : 0;
1132                 battery_capacity = batcap_i4[power_raw & 0x07];
1133                 wacom_notify_battery(wacom, battery_capacity, bat_charging,
1134                                      battery_capacity || bat_charging,
1135                                      ps_connected);
1136                 break;
1137         default:
1138                 dev_dbg(wacom->pen_input->dev.parent,
1139                                 "Unknown report: %d,%d size:%zu\n",
1140                                 data[0], data[1], len);
1141                 return 0;
1142         }
1143         return 0;
1144 }
1145
1146 static int wacom_wac_finger_count_touches(struct wacom_wac *wacom)
1147 {
1148         struct input_dev *input = wacom->touch_input;
1149         unsigned touch_max = wacom->features.touch_max;
1150         int count = 0;
1151         int i;
1152
1153         if (!touch_max)
1154                 return 0;
1155
1156         if (touch_max == 1)
1157                 return test_bit(BTN_TOUCH, input->key) &&
1158                        !wacom->shared->stylus_in_proximity;
1159
1160         for (i = 0; i < input->mt->num_slots; i++) {
1161                 struct input_mt_slot *ps = &input->mt->slots[i];
1162                 int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
1163                 if (id >= 0)
1164                         count++;
1165         }
1166
1167         return count;
1168 }
1169
1170 static int wacom_24hdt_irq(struct wacom_wac *wacom)
1171 {
1172         struct input_dev *input = wacom->touch_input;
1173         unsigned char *data = wacom->data;
1174         int i;
1175         int current_num_contacts = data[61];
1176         int contacts_to_send = 0;
1177         int num_contacts_left = 4; /* maximum contacts per packet */
1178         int byte_per_packet = WACOM_BYTES_PER_24HDT_PACKET;
1179         int y_offset = 2;
1180
1181         if (wacom->features.type == WACOM_27QHDT) {
1182                 current_num_contacts = data[63];
1183                 num_contacts_left = 10;
1184                 byte_per_packet = WACOM_BYTES_PER_QHDTHID_PACKET;
1185                 y_offset = 0;
1186         }
1187
1188         /*
1189          * First packet resets the counter since only the first
1190          * packet in series will have non-zero current_num_contacts.
1191          */
1192         if (current_num_contacts)
1193                 wacom->num_contacts_left = current_num_contacts;
1194
1195         contacts_to_send = min(num_contacts_left, wacom->num_contacts_left);
1196
1197         for (i = 0; i < contacts_to_send; i++) {
1198                 int offset = (byte_per_packet * i) + 1;
1199                 bool touch = (data[offset] & 0x1) && !wacom->shared->stylus_in_proximity;
1200                 int slot = input_mt_get_slot_by_key(input, data[offset + 1]);
1201
1202                 if (slot < 0)
1203                         continue;
1204                 input_mt_slot(input, slot);
1205                 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1206
1207                 if (touch) {
1208                         int t_x = get_unaligned_le16(&data[offset + 2]);
1209                         int t_y = get_unaligned_le16(&data[offset + 4 + y_offset]);
1210
1211                         input_report_abs(input, ABS_MT_POSITION_X, t_x);
1212                         input_report_abs(input, ABS_MT_POSITION_Y, t_y);
1213
1214                         if (wacom->features.type != WACOM_27QHDT) {
1215                                 int c_x = get_unaligned_le16(&data[offset + 4]);
1216                                 int c_y = get_unaligned_le16(&data[offset + 8]);
1217                                 int w = get_unaligned_le16(&data[offset + 10]);
1218                                 int h = get_unaligned_le16(&data[offset + 12]);
1219
1220                                 input_report_abs(input, ABS_MT_TOUCH_MAJOR, min(w,h));
1221                                 input_report_abs(input, ABS_MT_WIDTH_MAJOR,
1222                                                  min(w, h) + int_dist(t_x, t_y, c_x, c_y));
1223                                 input_report_abs(input, ABS_MT_WIDTH_MINOR, min(w, h));
1224                                 input_report_abs(input, ABS_MT_ORIENTATION, w > h);
1225                         }
1226                 }
1227         }
1228         input_mt_sync_frame(input);
1229
1230         wacom->num_contacts_left -= contacts_to_send;
1231         if (wacom->num_contacts_left <= 0) {
1232                 wacom->num_contacts_left = 0;
1233                 wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1234         }
1235         return 1;
1236 }
1237
1238 static int wacom_mt_touch(struct wacom_wac *wacom)
1239 {
1240         struct input_dev *input = wacom->touch_input;
1241         unsigned char *data = wacom->data;
1242         int i;
1243         int current_num_contacts = data[2];
1244         int contacts_to_send = 0;
1245         int x_offset = 0;
1246
1247         /* MTTPC does not support Height and Width */
1248         if (wacom->features.type == MTTPC || wacom->features.type == MTTPC_B)
1249                 x_offset = -4;
1250
1251         /*
1252          * First packet resets the counter since only the first
1253          * packet in series will have non-zero current_num_contacts.
1254          */
1255         if (current_num_contacts)
1256                 wacom->num_contacts_left = current_num_contacts;
1257
1258         /* There are at most 5 contacts per packet */
1259         contacts_to_send = min(5, wacom->num_contacts_left);
1260
1261         for (i = 0; i < contacts_to_send; i++) {
1262                 int offset = (WACOM_BYTES_PER_MT_PACKET + x_offset) * i + 3;
1263                 bool touch = (data[offset] & 0x1) && !wacom->shared->stylus_in_proximity;
1264                 int id = get_unaligned_le16(&data[offset + 1]);
1265                 int slot = input_mt_get_slot_by_key(input, id);
1266
1267                 if (slot < 0)
1268                         continue;
1269
1270                 input_mt_slot(input, slot);
1271                 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1272                 if (touch) {
1273                         int x = get_unaligned_le16(&data[offset + x_offset + 7]);
1274                         int y = get_unaligned_le16(&data[offset + x_offset + 9]);
1275                         input_report_abs(input, ABS_MT_POSITION_X, x);
1276                         input_report_abs(input, ABS_MT_POSITION_Y, y);
1277                 }
1278         }
1279         input_mt_sync_frame(input);
1280
1281         wacom->num_contacts_left -= contacts_to_send;
1282         if (wacom->num_contacts_left <= 0) {
1283                 wacom->num_contacts_left = 0;
1284                 wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1285         }
1286         return 1;
1287 }
1288
1289 static int wacom_tpc_mt_touch(struct wacom_wac *wacom)
1290 {
1291         struct input_dev *input = wacom->touch_input;
1292         unsigned char *data = wacom->data;
1293         int i;
1294
1295         for (i = 0; i < 2; i++) {
1296                 int p = data[1] & (1 << i);
1297                 bool touch = p && !wacom->shared->stylus_in_proximity;
1298
1299                 input_mt_slot(input, i);
1300                 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1301                 if (touch) {
1302                         int x = le16_to_cpup((__le16 *)&data[i * 2 + 2]) & 0x7fff;
1303                         int y = le16_to_cpup((__le16 *)&data[i * 2 + 6]) & 0x7fff;
1304
1305                         input_report_abs(input, ABS_MT_POSITION_X, x);
1306                         input_report_abs(input, ABS_MT_POSITION_Y, y);
1307                 }
1308         }
1309         input_mt_sync_frame(input);
1310
1311         /* keep touch state for pen event */
1312         wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1313
1314         return 1;
1315 }
1316
1317 static int wacom_tpc_single_touch(struct wacom_wac *wacom, size_t len)
1318 {
1319         unsigned char *data = wacom->data;
1320         struct input_dev *input = wacom->touch_input;
1321         bool prox = !wacom->shared->stylus_in_proximity;
1322         int x = 0, y = 0;
1323
1324         if (wacom->features.touch_max > 1 || len > WACOM_PKGLEN_TPC2FG)
1325                 return 0;
1326
1327         if (len == WACOM_PKGLEN_TPC1FG) {
1328                 prox = prox && (data[0] & 0x01);
1329                 x = get_unaligned_le16(&data[1]);
1330                 y = get_unaligned_le16(&data[3]);
1331         } else if (len == WACOM_PKGLEN_TPC1FG_B) {
1332                 prox = prox && (data[2] & 0x01);
1333                 x = get_unaligned_le16(&data[3]);
1334                 y = get_unaligned_le16(&data[5]);
1335         } else {
1336                 prox = prox && (data[1] & 0x01);
1337                 x = le16_to_cpup((__le16 *)&data[2]);
1338                 y = le16_to_cpup((__le16 *)&data[4]);
1339         }
1340
1341         if (prox) {
1342                 input_report_abs(input, ABS_X, x);
1343                 input_report_abs(input, ABS_Y, y);
1344         }
1345         input_report_key(input, BTN_TOUCH, prox);
1346
1347         /* keep touch state for pen events */
1348         wacom->shared->touch_down = prox;
1349
1350         return 1;
1351 }
1352
1353 static int wacom_tpc_pen(struct wacom_wac *wacom)
1354 {
1355         unsigned char *data = wacom->data;
1356         struct input_dev *input = wacom->pen_input;
1357         bool prox = data[1] & 0x20;
1358
1359         if (!wacom->shared->stylus_in_proximity) /* first in prox */
1360                 /* Going into proximity select tool */
1361                 wacom->tool[0] = (data[1] & 0x0c) ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
1362
1363         /* keep pen state for touch events */
1364         wacom->shared->stylus_in_proximity = prox;
1365
1366         /* send pen events only when touch is up or forced out */
1367         if (!wacom->shared->touch_down) {
1368                 input_report_key(input, BTN_STYLUS, data[1] & 0x02);
1369                 input_report_key(input, BTN_STYLUS2, data[1] & 0x10);
1370                 input_report_abs(input, ABS_X, le16_to_cpup((__le16 *)&data[2]));
1371                 input_report_abs(input, ABS_Y, le16_to_cpup((__le16 *)&data[4]));
1372                 input_report_abs(input, ABS_PRESSURE, ((data[7] & 0x07) << 8) | data[6]);
1373                 input_report_key(input, BTN_TOUCH, data[1] & 0x05);
1374                 input_report_key(input, wacom->tool[0], prox);
1375                 return 1;
1376         }
1377
1378         return 0;
1379 }
1380
1381 static int wacom_tpc_irq(struct wacom_wac *wacom, size_t len)
1382 {
1383         unsigned char *data = wacom->data;
1384
1385         if (wacom->pen_input)
1386                 dev_dbg(wacom->pen_input->dev.parent,
1387                         "%s: received report #%d\n", __func__, data[0]);
1388         else if (wacom->touch_input)
1389                 dev_dbg(wacom->touch_input->dev.parent,
1390                         "%s: received report #%d\n", __func__, data[0]);
1391
1392         switch (len) {
1393         case WACOM_PKGLEN_TPC1FG:
1394                 return wacom_tpc_single_touch(wacom, len);
1395
1396         case WACOM_PKGLEN_TPC2FG:
1397                 return wacom_tpc_mt_touch(wacom);
1398
1399         case WACOM_PKGLEN_PENABLED:
1400                 return wacom_tpc_pen(wacom);
1401
1402         default:
1403                 switch (data[0]) {
1404                 case WACOM_REPORT_TPC1FG:
1405                 case WACOM_REPORT_TPCHID:
1406                 case WACOM_REPORT_TPCST:
1407                 case WACOM_REPORT_TPC1FGE:
1408                         return wacom_tpc_single_touch(wacom, len);
1409
1410                 case WACOM_REPORT_TPCMT:
1411                 case WACOM_REPORT_TPCMT2:
1412                         return wacom_mt_touch(wacom);
1413
1414                 case WACOM_REPORT_PENABLED:
1415                         return wacom_tpc_pen(wacom);
1416                 }
1417         }
1418
1419         return 0;
1420 }
1421
1422 static void wacom_map_usage(struct input_dev *input, struct hid_usage *usage,
1423                 struct hid_field *field, __u8 type, __u16 code, int fuzz)
1424 {
1425         int fmin = field->logical_minimum;
1426         int fmax = field->logical_maximum;
1427
1428         usage->type = type;
1429         usage->code = code;
1430
1431         set_bit(type, input->evbit);
1432
1433         switch (type) {
1434         case EV_ABS:
1435                 input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
1436                 input_abs_set_res(input, code,
1437                                   hidinput_calc_abs_res(field, code));
1438                 break;
1439         case EV_KEY:
1440                 input_set_capability(input, EV_KEY, code);
1441                 break;
1442         case EV_MSC:
1443                 input_set_capability(input, EV_MSC, code);
1444                 break;
1445         }
1446 }
1447
1448 static void wacom_wac_pen_usage_mapping(struct hid_device *hdev,
1449                 struct hid_field *field, struct hid_usage *usage)
1450 {
1451         struct wacom *wacom = hid_get_drvdata(hdev);
1452         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1453         struct input_dev *input = wacom_wac->pen_input;
1454
1455         switch (usage->hid) {
1456         case HID_GD_X:
1457                 wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 4);
1458                 break;
1459         case HID_GD_Y:
1460                 wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 4);
1461                 break;
1462         case HID_DG_TIPPRESSURE:
1463                 wacom_map_usage(input, usage, field, EV_ABS, ABS_PRESSURE, 0);
1464                 break;
1465         case HID_DG_INRANGE:
1466                 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOOL_PEN, 0);
1467                 break;
1468         case HID_DG_INVERT:
1469                 wacom_map_usage(input, usage, field, EV_KEY,
1470                                 BTN_TOOL_RUBBER, 0);
1471                 break;
1472         case HID_DG_ERASER:
1473         case HID_DG_TIPSWITCH:
1474                 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
1475                 break;
1476         case HID_DG_BARRELSWITCH:
1477                 wacom_map_usage(input, usage, field, EV_KEY, BTN_STYLUS, 0);
1478                 break;
1479         case HID_DG_BARRELSWITCH2:
1480                 wacom_map_usage(input, usage, field, EV_KEY, BTN_STYLUS2, 0);
1481                 break;
1482         case HID_DG_TOOLSERIALNUMBER:
1483                 wacom_map_usage(input, usage, field, EV_MSC, MSC_SERIAL, 0);
1484                 break;
1485         }
1486 }
1487
1488 static int wacom_wac_pen_event(struct hid_device *hdev, struct hid_field *field,
1489                 struct hid_usage *usage, __s32 value)
1490 {
1491         struct wacom *wacom = hid_get_drvdata(hdev);
1492         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1493         struct input_dev *input = wacom_wac->pen_input;
1494
1495         /* checking which Tool / tip switch to send */
1496         switch (usage->hid) {
1497         case HID_DG_INRANGE:
1498                 wacom_wac->hid_data.inrange_state = value;
1499                 return 0;
1500         case HID_DG_INVERT:
1501                 wacom_wac->hid_data.invert_state = value;
1502                 return 0;
1503         case HID_DG_ERASER:
1504         case HID_DG_TIPSWITCH:
1505                 wacom_wac->hid_data.tipswitch |= value;
1506                 return 0;
1507         }
1508
1509         /* send pen events only when touch is up or forced out */
1510         if (!usage->type || wacom_wac->shared->touch_down)
1511                 return 0;
1512
1513         input_event(input, usage->type, usage->code, value);
1514
1515         return 0;
1516 }
1517
1518 static void wacom_wac_pen_pre_report(struct hid_device *hdev,
1519                 struct hid_report *report)
1520 {
1521         return;
1522 }
1523
1524 static void wacom_wac_pen_report(struct hid_device *hdev,
1525                 struct hid_report *report)
1526 {
1527         struct wacom *wacom = hid_get_drvdata(hdev);
1528         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1529         struct input_dev *input = wacom_wac->pen_input;
1530         bool prox = wacom_wac->hid_data.inrange_state;
1531
1532         if (!wacom_wac->shared->stylus_in_proximity) /* first in prox */
1533                 /* Going into proximity select tool */
1534                 wacom_wac->tool[0] = wacom_wac->hid_data.invert_state ?
1535                                                 BTN_TOOL_RUBBER : BTN_TOOL_PEN;
1536
1537         /* keep pen state for touch events */
1538         wacom_wac->shared->stylus_in_proximity = prox;
1539
1540         /* send pen events only when touch is up or forced out */
1541         if (!wacom_wac->shared->touch_down) {
1542                 input_report_key(input, BTN_TOUCH,
1543                                 wacom_wac->hid_data.tipswitch);
1544                 input_report_key(input, wacom_wac->tool[0], prox);
1545
1546                 wacom_wac->hid_data.tipswitch = false;
1547
1548                 input_sync(input);
1549         }
1550 }
1551
1552 static void wacom_wac_finger_usage_mapping(struct hid_device *hdev,
1553                 struct hid_field *field, struct hid_usage *usage)
1554 {
1555         struct wacom *wacom = hid_get_drvdata(hdev);
1556         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1557         struct wacom_features *features = &wacom_wac->features;
1558         struct input_dev *input = wacom_wac->touch_input;
1559         unsigned touch_max = wacom_wac->features.touch_max;
1560
1561         switch (usage->hid) {
1562         case HID_GD_X:
1563                 features->last_slot_field = usage->hid;
1564                 if (touch_max == 1)
1565                         wacom_map_usage(input, usage, field, EV_ABS, ABS_X, 4);
1566                 else
1567                         wacom_map_usage(input, usage, field, EV_ABS,
1568                                         ABS_MT_POSITION_X, 4);
1569                 break;
1570         case HID_GD_Y:
1571                 features->last_slot_field = usage->hid;
1572                 if (touch_max == 1)
1573                         wacom_map_usage(input, usage, field, EV_ABS, ABS_Y, 4);
1574                 else
1575                         wacom_map_usage(input, usage, field, EV_ABS,
1576                                         ABS_MT_POSITION_Y, 4);
1577                 break;
1578         case HID_DG_WIDTH:
1579         case HID_DG_HEIGHT:
1580                 features->last_slot_field = usage->hid;
1581                 wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MAJOR, 0);
1582                 wacom_map_usage(input, usage, field, EV_ABS, ABS_MT_TOUCH_MINOR, 0);
1583                 input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
1584                 break;
1585         case HID_DG_CONTACTID:
1586                 features->last_slot_field = usage->hid;
1587                 break;
1588         case HID_DG_INRANGE:
1589                 features->last_slot_field = usage->hid;
1590                 break;
1591         case HID_DG_INVERT:
1592                 features->last_slot_field = usage->hid;
1593                 break;
1594         case HID_DG_TIPSWITCH:
1595                 features->last_slot_field = usage->hid;
1596                 wacom_map_usage(input, usage, field, EV_KEY, BTN_TOUCH, 0);
1597                 break;
1598         case HID_DG_CONTACTCOUNT:
1599                 wacom_wac->hid_data.cc_report = field->report->id;
1600                 wacom_wac->hid_data.cc_index = field->index;
1601                 wacom_wac->hid_data.cc_value_index = usage->usage_index;
1602                 break;
1603         }
1604 }
1605
1606 static void wacom_wac_finger_slot(struct wacom_wac *wacom_wac,
1607                 struct input_dev *input)
1608 {
1609         struct hid_data *hid_data = &wacom_wac->hid_data;
1610         bool mt = wacom_wac->features.touch_max > 1;
1611         bool prox = hid_data->tipswitch &&
1612                     !wacom_wac->shared->stylus_in_proximity;
1613
1614         wacom_wac->hid_data.num_received++;
1615         if (wacom_wac->hid_data.num_received > wacom_wac->hid_data.num_expected)
1616                 return;
1617
1618         if (mt) {
1619                 int slot;
1620
1621                 slot = input_mt_get_slot_by_key(input, hid_data->id);
1622                 input_mt_slot(input, slot);
1623                 input_mt_report_slot_state(input, MT_TOOL_FINGER, prox);
1624         }
1625         else {
1626                 input_report_key(input, BTN_TOUCH, prox);
1627         }
1628
1629         if (prox) {
1630                 input_report_abs(input, mt ? ABS_MT_POSITION_X : ABS_X,
1631                                  hid_data->x);
1632                 input_report_abs(input, mt ? ABS_MT_POSITION_Y : ABS_Y,
1633                                  hid_data->y);
1634
1635                 if (test_bit(ABS_MT_TOUCH_MAJOR, input->absbit)) {
1636                         input_report_abs(input, ABS_MT_TOUCH_MAJOR, max(hid_data->width, hid_data->height));
1637                         input_report_abs(input, ABS_MT_TOUCH_MINOR, min(hid_data->width, hid_data->height));
1638                         if (hid_data->width != hid_data->height)
1639                                 input_report_abs(input, ABS_MT_ORIENTATION, hid_data->width <= hid_data->height ? 0 : 1);
1640                 }
1641         }
1642 }
1643
1644 static int wacom_wac_finger_event(struct hid_device *hdev,
1645                 struct hid_field *field, struct hid_usage *usage, __s32 value)
1646 {
1647         struct wacom *wacom = hid_get_drvdata(hdev);
1648         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1649
1650         switch (usage->hid) {
1651         case HID_GD_X:
1652                 wacom_wac->hid_data.x = value;
1653                 break;
1654         case HID_GD_Y:
1655                 wacom_wac->hid_data.y = value;
1656                 break;
1657         case HID_DG_WIDTH:
1658                 wacom_wac->hid_data.width = value;
1659                 break;
1660         case HID_DG_HEIGHT:
1661                 wacom_wac->hid_data.height = value;
1662                 break;
1663         case HID_DG_CONTACTID:
1664                 wacom_wac->hid_data.id = value;
1665                 break;
1666         case HID_DG_TIPSWITCH:
1667                 wacom_wac->hid_data.tipswitch = value;
1668                 break;
1669         }
1670
1671
1672         if (usage->usage_index + 1 == field->report_count) {
1673                 if (usage->hid == wacom_wac->features.last_slot_field)
1674                         wacom_wac_finger_slot(wacom_wac, wacom_wac->touch_input);
1675         }
1676
1677         return 0;
1678 }
1679
1680 static void wacom_wac_finger_pre_report(struct hid_device *hdev,
1681                 struct hid_report *report)
1682 {
1683         struct wacom *wacom = hid_get_drvdata(hdev);
1684         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1685         struct hid_data* hid_data = &wacom_wac->hid_data;
1686
1687         if (hid_data->cc_report != 0 &&
1688             hid_data->cc_report != report->id) {
1689                 int i;
1690
1691                 hid_data->cc_report = report->id;
1692                 hid_data->cc_index = -1;
1693                 hid_data->cc_value_index = -1;
1694
1695                 for (i = 0; i < report->maxfield; i++) {
1696                         struct hid_field *field = report->field[i];
1697                         int j;
1698
1699                         for (j = 0; j < field->maxusage; j++) {
1700                                 if (field->usage[j].hid == HID_DG_CONTACTCOUNT) {
1701                                         hid_data->cc_index = i;
1702                                         hid_data->cc_value_index = j;
1703
1704                                         /* break */
1705                                         i = report->maxfield;
1706                                         j = field->maxusage;
1707                                 }
1708                         }
1709                 }
1710         }
1711         if (hid_data->cc_report != 0 &&
1712             hid_data->cc_index >= 0) {
1713                 struct hid_field *field = report->field[hid_data->cc_index];
1714                 int value = field->value[hid_data->cc_value_index];
1715                 if (value)
1716                         hid_data->num_expected = value;
1717         }
1718         else {
1719                 hid_data->num_expected = wacom_wac->features.touch_max;
1720         }
1721 }
1722
1723 static void wacom_wac_finger_report(struct hid_device *hdev,
1724                 struct hid_report *report)
1725 {
1726         struct wacom *wacom = hid_get_drvdata(hdev);
1727         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1728         struct input_dev *input = wacom_wac->touch_input;
1729         unsigned touch_max = wacom_wac->features.touch_max;
1730
1731         /* If more packets of data are expected, give us a chance to
1732          * process them rather than immediately syncing a partial
1733          * update.
1734          */
1735         if (wacom_wac->hid_data.num_received < wacom_wac->hid_data.num_expected)
1736                 return;
1737
1738         if (touch_max > 1)
1739                 input_mt_sync_frame(input);
1740
1741         input_sync(input);
1742         wacom_wac->hid_data.num_received = 0;
1743
1744         /* keep touch state for pen event */
1745         wacom_wac->shared->touch_down = wacom_wac_finger_count_touches(wacom_wac);
1746 }
1747
1748 void wacom_wac_usage_mapping(struct hid_device *hdev,
1749                 struct hid_field *field, struct hid_usage *usage)
1750 {
1751         struct wacom *wacom = hid_get_drvdata(hdev);
1752         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1753
1754         /* currently, only direct devices have proper hid report descriptors */
1755         __set_bit(INPUT_PROP_DIRECT, wacom_wac->pen_input->propbit);
1756         __set_bit(INPUT_PROP_DIRECT, wacom_wac->touch_input->propbit);
1757
1758         if (WACOM_PEN_FIELD(field))
1759                 return wacom_wac_pen_usage_mapping(hdev, field, usage);
1760
1761         if (WACOM_FINGER_FIELD(field))
1762                 return wacom_wac_finger_usage_mapping(hdev, field, usage);
1763 }
1764
1765 int wacom_wac_event(struct hid_device *hdev, struct hid_field *field,
1766                 struct hid_usage *usage, __s32 value)
1767 {
1768         struct wacom *wacom = hid_get_drvdata(hdev);
1769
1770         if (wacom->wacom_wac.features.type != HID_GENERIC)
1771                 return 0;
1772
1773         if (WACOM_PEN_FIELD(field))
1774                 return wacom_wac_pen_event(hdev, field, usage, value);
1775
1776         if (WACOM_FINGER_FIELD(field))
1777                 return wacom_wac_finger_event(hdev, field, usage, value);
1778
1779         return 0;
1780 }
1781
1782 static void wacom_report_events(struct hid_device *hdev, struct hid_report *report)
1783 {
1784         int r;
1785
1786         for (r = 0; r < report->maxfield; r++) {
1787                 struct hid_field *field;
1788                 unsigned count, n;
1789
1790                 field = report->field[r];
1791                 count = field->report_count;
1792
1793                 if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
1794                         continue;
1795
1796                 for (n = 0; n < count; n++)
1797                         wacom_wac_event(hdev, field, &field->usage[n], field->value[n]);
1798         }
1799 }
1800
1801 void wacom_wac_report(struct hid_device *hdev, struct hid_report *report)
1802 {
1803         struct wacom *wacom = hid_get_drvdata(hdev);
1804         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1805         struct hid_field *field = report->field[0];
1806
1807         if (wacom_wac->features.type != HID_GENERIC)
1808                 return;
1809
1810         if (WACOM_PEN_FIELD(field))
1811                 wacom_wac_pen_pre_report(hdev, report);
1812
1813         if (WACOM_FINGER_FIELD(field))
1814                 wacom_wac_finger_pre_report(hdev, report);
1815
1816         wacom_report_events(hdev, report);
1817
1818         if (WACOM_PEN_FIELD(field))
1819                 return wacom_wac_pen_report(hdev, report);
1820
1821         if (WACOM_FINGER_FIELD(field))
1822                 return wacom_wac_finger_report(hdev, report);
1823 }
1824
1825 static int wacom_bpt_touch(struct wacom_wac *wacom)
1826 {
1827         struct wacom_features *features = &wacom->features;
1828         struct input_dev *input = wacom->touch_input;
1829         struct input_dev *pad_input = wacom->pad_input;
1830         unsigned char *data = wacom->data;
1831         int i;
1832
1833         if (data[0] != 0x02)
1834             return 0;
1835
1836         for (i = 0; i < 2; i++) {
1837                 int offset = (data[1] & 0x80) ? (8 * i) : (9 * i);
1838                 bool touch = data[offset + 3] & 0x80;
1839
1840                 /*
1841                  * Touch events need to be disabled while stylus is
1842                  * in proximity because user's hand is resting on touchpad
1843                  * and sending unwanted events.  User expects tablet buttons
1844                  * to continue working though.
1845                  */
1846                 touch = touch && !wacom->shared->stylus_in_proximity;
1847
1848                 input_mt_slot(input, i);
1849                 input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1850                 if (touch) {
1851                         int x = get_unaligned_be16(&data[offset + 3]) & 0x7ff;
1852                         int y = get_unaligned_be16(&data[offset + 5]) & 0x7ff;
1853                         if (features->quirks & WACOM_QUIRK_BBTOUCH_LOWRES) {
1854                                 x <<= 5;
1855                                 y <<= 5;
1856                         }
1857                         input_report_abs(input, ABS_MT_POSITION_X, x);
1858                         input_report_abs(input, ABS_MT_POSITION_Y, y);
1859                 }
1860         }
1861
1862         input_mt_sync_frame(input);
1863
1864         input_report_key(pad_input, BTN_LEFT, (data[1] & 0x08) != 0);
1865         input_report_key(pad_input, BTN_FORWARD, (data[1] & 0x04) != 0);
1866         input_report_key(pad_input, BTN_BACK, (data[1] & 0x02) != 0);
1867         input_report_key(pad_input, BTN_RIGHT, (data[1] & 0x01) != 0);
1868         wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1869
1870         return 1;
1871 }
1872
1873 static void wacom_bpt3_touch_msg(struct wacom_wac *wacom, unsigned char *data)
1874 {
1875         struct wacom_features *features = &wacom->features;
1876         struct input_dev *input = wacom->touch_input;
1877         bool touch = data[1] & 0x80;
1878         int slot = input_mt_get_slot_by_key(input, data[0]);
1879
1880         if (slot < 0)
1881                 return;
1882
1883         touch = touch && !wacom->shared->stylus_in_proximity;
1884
1885         input_mt_slot(input, slot);
1886         input_mt_report_slot_state(input, MT_TOOL_FINGER, touch);
1887
1888         if (touch) {
1889                 int x = (data[2] << 4) | (data[4] >> 4);
1890                 int y = (data[3] << 4) | (data[4] & 0x0f);
1891                 int width, height;
1892
1893                 if (features->type >= INTUOSPS && features->type <= INTUOSHT2) {
1894                         width  = data[5] * 100;
1895                         height = data[6] * 100;
1896                 } else {
1897                         /*
1898                          * "a" is a scaled-down area which we assume is
1899                          * roughly circular and which can be described as:
1900                          * a=(pi*r^2)/C.
1901                          */
1902                         int a = data[5];
1903                         int x_res = input_abs_get_res(input, ABS_MT_POSITION_X);
1904                         int y_res = input_abs_get_res(input, ABS_MT_POSITION_Y);
1905                         width = 2 * int_sqrt(a * WACOM_CONTACT_AREA_SCALE);
1906                         height = width * y_res / x_res;
1907                 }
1908
1909                 input_report_abs(input, ABS_MT_POSITION_X, x);
1910                 input_report_abs(input, ABS_MT_POSITION_Y, y);
1911                 input_report_abs(input, ABS_MT_TOUCH_MAJOR, width);
1912                 input_report_abs(input, ABS_MT_TOUCH_MINOR, height);
1913         }
1914 }
1915
1916 static void wacom_bpt3_button_msg(struct wacom_wac *wacom, unsigned char *data)
1917 {
1918         struct input_dev *input = wacom->pad_input;
1919         struct wacom_features *features = &wacom->features;
1920
1921         if (features->type == INTUOSHT || features->type == INTUOSHT2) {
1922                 input_report_key(input, BTN_LEFT, (data[1] & 0x02) != 0);
1923                 input_report_key(input, BTN_BACK, (data[1] & 0x08) != 0);
1924         } else {
1925                 input_report_key(input, BTN_BACK, (data[1] & 0x02) != 0);
1926                 input_report_key(input, BTN_LEFT, (data[1] & 0x08) != 0);
1927         }
1928         input_report_key(input, BTN_FORWARD, (data[1] & 0x04) != 0);
1929         input_report_key(input, BTN_RIGHT, (data[1] & 0x01) != 0);
1930 }
1931
1932 static int wacom_bpt3_touch(struct wacom_wac *wacom)
1933 {
1934         unsigned char *data = wacom->data;
1935         int count = data[1] & 0x07;
1936         int  touch_changed = 0, i;
1937
1938         if (data[0] != 0x02)
1939             return 0;
1940
1941         /* data has up to 7 fixed sized 8-byte messages starting at data[2] */
1942         for (i = 0; i < count; i++) {
1943                 int offset = (8 * i) + 2;
1944                 int msg_id = data[offset];
1945
1946                 if (msg_id >= 2 && msg_id <= 17) {
1947                         wacom_bpt3_touch_msg(wacom, data + offset);
1948                         touch_changed++;
1949                 } else if (msg_id == 128)
1950                         wacom_bpt3_button_msg(wacom, data + offset);
1951
1952         }
1953
1954         /* only update touch if we actually have a touchpad and touch data changed */
1955         if (wacom->touch_input && touch_changed) {
1956                 input_mt_sync_frame(wacom->touch_input);
1957                 wacom->shared->touch_down = wacom_wac_finger_count_touches(wacom);
1958         }
1959
1960         return 1;
1961 }
1962
1963 static int wacom_bpt_pen(struct wacom_wac *wacom)
1964 {
1965         struct wacom_features *features = &wacom->features;
1966         struct input_dev *input = wacom->pen_input;
1967         unsigned char *data = wacom->data;
1968         int prox = 0, x = 0, y = 0, p = 0, d = 0, pen = 0, btn1 = 0, btn2 = 0;
1969
1970         if (data[0] != WACOM_REPORT_PENABLED)
1971             return 0;
1972
1973         prox = (data[1] & 0x20) == 0x20;
1974
1975         /*
1976          * All reports shared between PEN and RUBBER tool must be
1977          * forced to a known starting value (zero) when transitioning to
1978          * out-of-prox.
1979          *
1980          * If not reset then, to userspace, it will look like lost events
1981          * if new tool comes in-prox with same values as previous tool sent.
1982          *
1983          * Hardware does report zero in most out-of-prox cases but not all.
1984          */
1985         if (!wacom->shared->stylus_in_proximity) {
1986                 if (data[1] & 0x08) {
1987                         wacom->tool[0] = BTN_TOOL_RUBBER;
1988                         wacom->id[0] = ERASER_DEVICE_ID;
1989                 } else {
1990                         wacom->tool[0] = BTN_TOOL_PEN;
1991                         wacom->id[0] = STYLUS_DEVICE_ID;
1992                 }
1993         }
1994
1995         wacom->shared->stylus_in_proximity = prox;
1996         if (wacom->shared->touch_down)
1997                 return 0;
1998
1999         if (prox) {
2000                 x = le16_to_cpup((__le16 *)&data[2]);
2001                 y = le16_to_cpup((__le16 *)&data[4]);
2002                 p = le16_to_cpup((__le16 *)&data[6]);
2003                 /*
2004                  * Convert distance from out prox to distance from tablet.
2005                  * distance will be greater than distance_max once
2006                  * touching and applying pressure; do not report negative
2007                  * distance.
2008                  */
2009                 if (data[8] <= features->distance_max)
2010                         d = features->distance_max - data[8];
2011
2012                 pen = data[1] & 0x01;
2013                 btn1 = data[1] & 0x02;
2014                 btn2 = data[1] & 0x04;
2015         } else {
2016                 wacom->id[0] = 0;
2017         }
2018
2019         input_report_key(input, BTN_TOUCH, pen);
2020         input_report_key(input, BTN_STYLUS, btn1);
2021         input_report_key(input, BTN_STYLUS2, btn2);
2022
2023         input_report_abs(input, ABS_X, x);
2024         input_report_abs(input, ABS_Y, y);
2025         input_report_abs(input, ABS_PRESSURE, p);
2026         input_report_abs(input, ABS_DISTANCE, d);
2027
2028         input_report_key(input, wacom->tool[0], prox); /* PEN or RUBBER */
2029         input_report_abs(input, ABS_MISC, wacom->id[0]); /* TOOL ID */
2030
2031         return 1;
2032 }
2033
2034 static int wacom_bpt_irq(struct wacom_wac *wacom, size_t len)
2035 {
2036         struct wacom_features *features = &wacom->features;
2037
2038         if ((features->type == INTUOSHT2) &&
2039             (features->device_type & WACOM_DEVICETYPE_PEN))
2040                 return wacom_intuos_irq(wacom);
2041         else if (len == WACOM_PKGLEN_BBTOUCH)
2042                 return wacom_bpt_touch(wacom);
2043         else if (len == WACOM_PKGLEN_BBTOUCH3)
2044                 return wacom_bpt3_touch(wacom);
2045         else if (len == WACOM_PKGLEN_BBFUN || len == WACOM_PKGLEN_BBPEN)
2046                 return wacom_bpt_pen(wacom);
2047
2048         return 0;
2049 }
2050
2051 static void wacom_bamboo_pad_pen_event(struct wacom_wac *wacom,
2052                 unsigned char *data)
2053 {
2054         unsigned char prefix;
2055
2056         /*
2057          * We need to reroute the event from the debug interface to the
2058          * pen interface.
2059          * We need to add the report ID to the actual pen report, so we
2060          * temporary overwrite the first byte to prevent having to kzalloc/kfree
2061          * and memcpy the report.
2062          */
2063         prefix = data[0];
2064         data[0] = WACOM_REPORT_BPAD_PEN;
2065
2066         /*
2067          * actually reroute the event.
2068          * No need to check if wacom->shared->pen is valid, hid_input_report()
2069          * will check for us.
2070          */
2071         hid_input_report(wacom->shared->pen, HID_INPUT_REPORT, data,
2072                          WACOM_PKGLEN_PENABLED, 1);
2073
2074         data[0] = prefix;
2075 }
2076
2077 static int wacom_bamboo_pad_touch_event(struct wacom_wac *wacom,
2078                 unsigned char *data)
2079 {
2080         struct input_dev *input = wacom->touch_input;
2081         unsigned char *finger_data, prefix;
2082         unsigned id;
2083         int x, y;
2084         bool valid;
2085
2086         prefix = data[0];
2087
2088         for (id = 0; id < wacom->features.touch_max; id++) {
2089                 valid = !!(prefix & BIT(id)) &&
2090                         !wacom->shared->stylus_in_proximity;
2091
2092                 input_mt_slot(input, id);
2093                 input_mt_report_slot_state(input, MT_TOOL_FINGER, valid);
2094
2095                 if (!valid)
2096                         continue;
2097
2098                 finger_data = data + 1 + id * 3;
2099                 x = finger_data[0] | ((finger_data[1] & 0x0f) << 8);
2100                 y = (finger_data[2] << 4) | (finger_data[1] >> 4);
2101
2102                 input_report_abs(input, ABS_MT_POSITION_X, x);
2103                 input_report_abs(input, ABS_MT_POSITION_Y, y);
2104         }
2105
2106         input_mt_sync_frame(input);
2107
2108         input_report_key(input, BTN_LEFT, prefix & 0x40);
2109         input_report_key(input, BTN_RIGHT, prefix & 0x80);
2110
2111         /* keep touch state for pen event */
2112         wacom->shared->touch_down = !!prefix &&
2113                                     !wacom->shared->stylus_in_proximity;
2114
2115         return 1;
2116 }
2117
2118 static int wacom_bamboo_pad_irq(struct wacom_wac *wacom, size_t len)
2119 {
2120         unsigned char *data = wacom->data;
2121
2122         if (!((len == WACOM_PKGLEN_BPAD_TOUCH) ||
2123               (len == WACOM_PKGLEN_BPAD_TOUCH_USB)) ||
2124             (data[0] != WACOM_REPORT_BPAD_TOUCH))
2125                 return 0;
2126
2127         if (data[1] & 0x01)
2128                 wacom_bamboo_pad_pen_event(wacom, &data[1]);
2129
2130         if (data[1] & 0x02)
2131                 return wacom_bamboo_pad_touch_event(wacom, &data[9]);
2132
2133         return 0;
2134 }
2135
2136 static int wacom_wireless_irq(struct wacom_wac *wacom, size_t len)
2137 {
2138         unsigned char *data = wacom->data;
2139         int connected;
2140
2141         if (len != WACOM_PKGLEN_WIRELESS || data[0] != WACOM_REPORT_WL)
2142                 return 0;
2143
2144         connected = data[1] & 0x01;
2145         if (connected) {
2146                 int pid, battery, charging;
2147
2148                 if ((wacom->shared->type == INTUOSHT ||
2149                     wacom->shared->type == INTUOSHT2) &&
2150                     wacom->shared->touch_input &&
2151                     wacom->shared->touch_max) {
2152                         input_report_switch(wacom->shared->touch_input,
2153                                         SW_MUTE_DEVICE, data[5] & 0x40);
2154                         input_sync(wacom->shared->touch_input);
2155                 }
2156
2157                 pid = get_unaligned_be16(&data[6]);
2158                 battery = (data[5] & 0x3f) * 100 / 31;
2159                 charging = !!(data[5] & 0x80);
2160                 if (wacom->pid != pid) {
2161                         wacom->pid = pid;
2162                         wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
2163                 }
2164
2165                 wacom_notify_battery(wacom, battery, charging, 1, 0);
2166
2167         } else if (wacom->pid != 0) {
2168                 /* disconnected while previously connected */
2169                 wacom->pid = 0;
2170                 wacom_schedule_work(wacom, WACOM_WORKER_WIRELESS);
2171                 wacom_notify_battery(wacom, 0, 0, 0, 0);
2172         }
2173
2174         return 0;
2175 }
2176
2177 static int wacom_status_irq(struct wacom_wac *wacom_wac, size_t len)
2178 {
2179         struct wacom *wacom = container_of(wacom_wac, struct wacom, wacom_wac);
2180         struct wacom_features *features = &wacom_wac->features;
2181         unsigned char *data = wacom_wac->data;
2182
2183         if (data[0] != WACOM_REPORT_USB)
2184                 return 0;
2185
2186         if ((features->type == INTUOSHT ||
2187             features->type == INTUOSHT2) &&
2188             wacom_wac->shared->touch_input &&
2189             features->touch_max) {
2190                 input_report_switch(wacom_wac->shared->touch_input,
2191                                     SW_MUTE_DEVICE, data[8] & 0x40);
2192                 input_sync(wacom_wac->shared->touch_input);
2193         }
2194
2195         if (data[9] & 0x02) { /* wireless module is attached */
2196                 int battery = (data[8] & 0x3f) * 100 / 31;
2197                 bool charging = !!(data[8] & 0x80);
2198
2199                 wacom_notify_battery(wacom_wac, battery, charging,
2200                                      battery || charging, 1);
2201
2202                 if (!wacom->battery.battery &&
2203                     !(features->quirks & WACOM_QUIRK_BATTERY)) {
2204                         features->quirks |= WACOM_QUIRK_BATTERY;
2205                         wacom_schedule_work(wacom_wac, WACOM_WORKER_BATTERY);
2206                 }
2207         }
2208         else if ((features->quirks & WACOM_QUIRK_BATTERY) &&
2209                  wacom->battery.battery) {
2210                 features->quirks &= ~WACOM_QUIRK_BATTERY;
2211                 wacom_schedule_work(wacom_wac, WACOM_WORKER_BATTERY);
2212                 wacom_notify_battery(wacom_wac, 0, 0, 0, 0);
2213         }
2214         return 0;
2215 }
2216
2217 void wacom_wac_irq(struct wacom_wac *wacom_wac, size_t len)
2218 {
2219         bool sync;
2220
2221         switch (wacom_wac->features.type) {
2222         case PENPARTNER:
2223                 sync = wacom_penpartner_irq(wacom_wac);
2224                 break;
2225
2226         case PL:
2227                 sync = wacom_pl_irq(wacom_wac);
2228                 break;
2229
2230         case WACOM_G4:
2231         case GRAPHIRE:
2232         case GRAPHIRE_BT:
2233         case WACOM_MO:
2234                 sync = wacom_graphire_irq(wacom_wac);
2235                 break;
2236
2237         case PTU:
2238                 sync = wacom_ptu_irq(wacom_wac);
2239                 break;
2240
2241         case DTU:
2242                 sync = wacom_dtu_irq(wacom_wac);
2243                 break;
2244
2245         case DTUS:
2246         case DTUSX:
2247                 sync = wacom_dtus_irq(wacom_wac);
2248                 break;
2249
2250         case INTUOS:
2251         case INTUOS3S:
2252         case INTUOS3:
2253         case INTUOS3L:
2254         case INTUOS4S:
2255         case INTUOS4:
2256         case INTUOS4L:
2257         case CINTIQ:
2258         case WACOM_BEE:
2259         case WACOM_13HD:
2260         case WACOM_21UX2:
2261         case WACOM_22HD:
2262         case WACOM_24HD:
2263         case WACOM_27QHD:
2264         case DTK:
2265         case CINTIQ_HYBRID:
2266         case CINTIQ_COMPANION_2:
2267                 sync = wacom_intuos_irq(wacom_wac);
2268                 break;
2269
2270         case INTUOS4WL:
2271                 sync = wacom_intuos_bt_irq(wacom_wac, len);
2272                 break;
2273
2274         case WACOM_24HDT:
2275         case WACOM_27QHDT:
2276                 sync = wacom_24hdt_irq(wacom_wac);
2277                 break;
2278
2279         case INTUOS5S:
2280         case INTUOS5:
2281         case INTUOS5L:
2282         case INTUOSPS:
2283         case INTUOSPM:
2284         case INTUOSPL:
2285                 if (len == WACOM_PKGLEN_BBTOUCH3)
2286                         sync = wacom_bpt3_touch(wacom_wac);
2287                 else if (wacom_wac->data[0] == WACOM_REPORT_USB)
2288                         sync = wacom_status_irq(wacom_wac, len);
2289                 else
2290                         sync = wacom_intuos_irq(wacom_wac);
2291                 break;
2292
2293         case TABLETPC:
2294         case TABLETPCE:
2295         case TABLETPC2FG:
2296         case MTSCREEN:
2297         case MTTPC:
2298         case MTTPC_B:
2299                 sync = wacom_tpc_irq(wacom_wac, len);
2300                 break;
2301
2302         case BAMBOO_PT:
2303         case BAMBOO_PEN:
2304         case BAMBOO_TOUCH:
2305         case INTUOSHT:
2306         case INTUOSHT2:
2307                 if (wacom_wac->data[0] == WACOM_REPORT_USB)
2308                         sync = wacom_status_irq(wacom_wac, len);
2309                 else
2310                         sync = wacom_bpt_irq(wacom_wac, len);
2311                 break;
2312
2313         case BAMBOO_PAD:
2314                 sync = wacom_bamboo_pad_irq(wacom_wac, len);
2315                 break;
2316
2317         case WIRELESS:
2318                 sync = wacom_wireless_irq(wacom_wac, len);
2319                 break;
2320
2321         case REMOTE:
2322                 sync = false;
2323                 if (wacom_wac->data[0] == WACOM_REPORT_DEVICE_LIST)
2324                         wacom_remote_status_irq(wacom_wac, len);
2325                 else
2326                         sync = wacom_remote_irq(wacom_wac, len);
2327                 break;
2328
2329         default:
2330                 sync = false;
2331                 break;
2332         }
2333
2334         if (sync) {
2335                 if (wacom_wac->pen_input)
2336                         input_sync(wacom_wac->pen_input);
2337                 if (wacom_wac->touch_input)
2338                         input_sync(wacom_wac->touch_input);
2339                 if (wacom_wac->pad_input)
2340                         input_sync(wacom_wac->pad_input);
2341         }
2342 }
2343
2344 static void wacom_setup_basic_pro_pen(struct wacom_wac *wacom_wac)
2345 {
2346         struct input_dev *input_dev = wacom_wac->pen_input;
2347
2348         input_set_capability(input_dev, EV_MSC, MSC_SERIAL);
2349
2350         __set_bit(BTN_TOOL_PEN, input_dev->keybit);
2351         __set_bit(BTN_STYLUS, input_dev->keybit);
2352         __set_bit(BTN_STYLUS2, input_dev->keybit);
2353
2354         input_set_abs_params(input_dev, ABS_DISTANCE,
2355                              0, wacom_wac->features.distance_max, wacom_wac->features.distance_fuzz, 0);
2356 }
2357
2358 static void wacom_setup_cintiq(struct wacom_wac *wacom_wac)
2359 {
2360         struct input_dev *input_dev = wacom_wac->pen_input;
2361         struct wacom_features *features = &wacom_wac->features;
2362
2363         wacom_setup_basic_pro_pen(wacom_wac);
2364
2365         __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
2366         __set_bit(BTN_TOOL_BRUSH, input_dev->keybit);
2367         __set_bit(BTN_TOOL_PENCIL, input_dev->keybit);
2368         __set_bit(BTN_TOOL_AIRBRUSH, input_dev->keybit);
2369
2370         input_set_abs_params(input_dev, ABS_WHEEL, 0, 1023, 0, 0);
2371         input_set_abs_params(input_dev, ABS_TILT_X, -64, 63, features->tilt_fuzz, 0);
2372         input_abs_set_res(input_dev, ABS_TILT_X, 57);
2373         input_set_abs_params(input_dev, ABS_TILT_Y, -64, 63, features->tilt_fuzz, 0);
2374         input_abs_set_res(input_dev, ABS_TILT_Y, 57);
2375 }
2376
2377 static void wacom_setup_intuos(struct wacom_wac *wacom_wac)
2378 {
2379         struct input_dev *input_dev = wacom_wac->pen_input;
2380
2381         input_set_capability(input_dev, EV_REL, REL_WHEEL);
2382
2383         wacom_setup_cintiq(wacom_wac);
2384
2385         __set_bit(BTN_LEFT, input_dev->keybit);
2386         __set_bit(BTN_RIGHT, input_dev->keybit);
2387         __set_bit(BTN_MIDDLE, input_dev->keybit);
2388         __set_bit(BTN_SIDE, input_dev->keybit);
2389         __set_bit(BTN_EXTRA, input_dev->keybit);
2390         __set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
2391         __set_bit(BTN_TOOL_LENS, input_dev->keybit);
2392
2393         input_set_abs_params(input_dev, ABS_RZ, -900, 899, 0, 0);
2394         input_abs_set_res(input_dev, ABS_RZ, 287);
2395         input_set_abs_params(input_dev, ABS_THROTTLE, -1023, 1023, 0, 0);
2396 }
2397
2398 void wacom_setup_device_quirks(struct wacom *wacom)
2399 {
2400         struct wacom_features *features = &wacom->wacom_wac.features;
2401
2402         /* The pen and pad share the same interface on most devices */
2403         if (features->type == GRAPHIRE_BT || features->type == WACOM_G4 ||
2404             features->type == DTUS ||
2405             (features->type >= INTUOS3S && features->type <= WACOM_MO)) {
2406                 if (features->device_type & WACOM_DEVICETYPE_PEN)
2407                         features->device_type |= WACOM_DEVICETYPE_PAD;
2408         }
2409
2410         /* touch device found but size is not defined. use default */
2411         if (features->device_type & WACOM_DEVICETYPE_TOUCH && !features->x_max) {
2412                 features->x_max = 1023;
2413                 features->y_max = 1023;
2414         }
2415
2416         /*
2417          * Intuos5/Pro and Bamboo 3rd gen have no useful data about its
2418          * touch interface in its HID descriptor. If this is the touch
2419          * interface (PacketSize of WACOM_PKGLEN_BBTOUCH3), override the
2420          * tablet values.
2421          */
2422         if ((features->type >= INTUOS5S && features->type <= INTUOSPL) ||
2423                 (features->type >= INTUOSHT && features->type <= BAMBOO_PT)) {
2424                 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
2425                         if (features->touch_max)
2426                                 features->device_type |= WACOM_DEVICETYPE_TOUCH;
2427                         if (features->type >= INTUOSHT && features->type <= BAMBOO_PT)
2428                                 features->device_type |= WACOM_DEVICETYPE_PAD;
2429
2430                         features->x_max = 4096;
2431                         features->y_max = 4096;
2432                 }
2433                 else if (features->pktlen == WACOM_PKGLEN_BBTOUCH) {
2434                         features->device_type |= WACOM_DEVICETYPE_PAD;
2435                 }
2436         }
2437
2438         /*
2439          * Hack for the Bamboo One:
2440          * the device presents a PAD/Touch interface as most Bamboos and even
2441          * sends ghosts PAD data on it. However, later, we must disable this
2442          * ghost interface, and we can not detect it unless we set it here
2443          * to WACOM_DEVICETYPE_PAD or WACOM_DEVICETYPE_TOUCH.
2444          */
2445         if (features->type == BAMBOO_PEN &&
2446             features->pktlen == WACOM_PKGLEN_BBTOUCH3)
2447                 features->device_type |= WACOM_DEVICETYPE_PAD;
2448
2449         /*
2450          * Raw Wacom-mode pen and touch events both come from interface
2451          * 0, whose HID descriptor has an application usage of 0xFF0D
2452          * (i.e., WACOM_VENDORDEFINED_PEN). We route pen packets back
2453          * out through the HID_GENERIC device created for interface 1,
2454          * so rewrite this one to be of type WACOM_DEVICETYPE_TOUCH.
2455          */
2456         if (features->type == BAMBOO_PAD)
2457                 features->device_type = WACOM_DEVICETYPE_TOUCH;
2458
2459         if (features->type == REMOTE)
2460                 features->device_type = WACOM_DEVICETYPE_PAD;
2461
2462         if (wacom->hdev->bus == BUS_BLUETOOTH)
2463                 features->quirks |= WACOM_QUIRK_BATTERY;
2464
2465         /* quirk for bamboo touch with 2 low res touches */
2466         if ((features->type == BAMBOO_PT || features->type == BAMBOO_TOUCH) &&
2467             features->pktlen == WACOM_PKGLEN_BBTOUCH) {
2468                 features->x_max <<= 5;
2469                 features->y_max <<= 5;
2470                 features->x_fuzz <<= 5;
2471                 features->y_fuzz <<= 5;
2472                 features->quirks |= WACOM_QUIRK_BBTOUCH_LOWRES;
2473         }
2474
2475         if (features->type == WIRELESS) {
2476                 if (features->device_type == WACOM_DEVICETYPE_WL_MONITOR) {
2477                         features->quirks |= WACOM_QUIRK_BATTERY;
2478                 }
2479         }
2480
2481         if (features->type == REMOTE)
2482                 features->device_type |= WACOM_DEVICETYPE_WL_MONITOR;
2483 }
2484
2485 int wacom_setup_pen_input_capabilities(struct input_dev *input_dev,
2486                                    struct wacom_wac *wacom_wac)
2487 {
2488         struct wacom_features *features = &wacom_wac->features;
2489
2490         input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
2491
2492         if (!(features->device_type & WACOM_DEVICETYPE_PEN))
2493                 return -ENODEV;
2494
2495         if (features->type == HID_GENERIC)
2496                 /* setup has already been done */
2497                 return 0;
2498
2499         __set_bit(BTN_TOUCH, input_dev->keybit);
2500         __set_bit(ABS_MISC, input_dev->absbit);
2501
2502         input_set_abs_params(input_dev, ABS_X, features->x_min,
2503                              features->x_max, features->x_fuzz, 0);
2504         input_set_abs_params(input_dev, ABS_Y, features->y_min,
2505                              features->y_max, features->y_fuzz, 0);
2506         input_set_abs_params(input_dev, ABS_PRESSURE, 0,
2507                 features->pressure_max, features->pressure_fuzz, 0);
2508
2509         /* penabled devices have fixed resolution for each model */
2510         input_abs_set_res(input_dev, ABS_X, features->x_resolution);
2511         input_abs_set_res(input_dev, ABS_Y, features->y_resolution);
2512
2513
2514         switch (features->type) {
2515         case GRAPHIRE_BT:
2516                 __clear_bit(ABS_MISC, input_dev->absbit);
2517
2518         case WACOM_MO:
2519         case WACOM_G4:
2520                 input_set_abs_params(input_dev, ABS_DISTANCE, 0,
2521                                               features->distance_max,
2522                                               features->distance_fuzz, 0);
2523                 /* fall through */
2524
2525         case GRAPHIRE:
2526                 input_set_capability(input_dev, EV_REL, REL_WHEEL);
2527
2528                 __set_bit(BTN_LEFT, input_dev->keybit);
2529                 __set_bit(BTN_RIGHT, input_dev->keybit);
2530                 __set_bit(BTN_MIDDLE, input_dev->keybit);
2531
2532                 __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
2533                 __set_bit(BTN_TOOL_PEN, input_dev->keybit);
2534                 __set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
2535                 __set_bit(BTN_STYLUS, input_dev->keybit);
2536                 __set_bit(BTN_STYLUS2, input_dev->keybit);
2537
2538                 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
2539                 break;
2540
2541         case WACOM_27QHD:
2542         case WACOM_24HD:
2543         case DTK:
2544         case WACOM_22HD:
2545         case WACOM_21UX2:
2546         case WACOM_BEE:
2547         case CINTIQ:
2548         case WACOM_13HD:
2549         case CINTIQ_HYBRID:
2550         case CINTIQ_COMPANION_2:
2551                 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
2552                 input_abs_set_res(input_dev, ABS_Z, 287);
2553                 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
2554                 wacom_setup_cintiq(wacom_wac);
2555                 break;
2556
2557         case INTUOS3:
2558         case INTUOS3L:
2559         case INTUOS3S:
2560         case INTUOS4:
2561         case INTUOS4WL:
2562         case INTUOS4L:
2563         case INTUOS4S:
2564                 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
2565                 input_abs_set_res(input_dev, ABS_Z, 287);
2566                 /* fall through */
2567
2568         case INTUOS:
2569                 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
2570
2571                 wacom_setup_intuos(wacom_wac);
2572                 break;
2573
2574         case INTUOS5:
2575         case INTUOS5L:
2576         case INTUOSPM:
2577         case INTUOSPL:
2578         case INTUOS5S:
2579         case INTUOSPS:
2580                 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
2581
2582                 input_set_abs_params(input_dev, ABS_DISTANCE, 0,
2583                                       features->distance_max,
2584                                       features->distance_fuzz, 0);
2585
2586                 input_set_abs_params(input_dev, ABS_Z, -900, 899, 0, 0);
2587                 input_abs_set_res(input_dev, ABS_Z, 287);
2588
2589                 wacom_setup_intuos(wacom_wac);
2590                 break;
2591
2592         case WACOM_24HDT:
2593         case WACOM_27QHDT:
2594         case MTSCREEN:
2595         case MTTPC:
2596         case MTTPC_B:
2597         case TABLETPC2FG:
2598         case TABLETPC:
2599         case TABLETPCE:
2600                 __clear_bit(ABS_MISC, input_dev->absbit);
2601                 /* fall through */
2602
2603         case DTUS:
2604         case DTUSX:
2605         case PL:
2606         case DTU:
2607                 __set_bit(BTN_TOOL_PEN, input_dev->keybit);
2608                 __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
2609                 __set_bit(BTN_STYLUS, input_dev->keybit);
2610                 __set_bit(BTN_STYLUS2, input_dev->keybit);
2611
2612                 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
2613                 break;
2614
2615         case PTU:
2616                 __set_bit(BTN_STYLUS2, input_dev->keybit);
2617                 /* fall through */
2618
2619         case PENPARTNER:
2620                 __set_bit(BTN_TOOL_PEN, input_dev->keybit);
2621                 __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
2622                 __set_bit(BTN_STYLUS, input_dev->keybit);
2623
2624                 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
2625                 break;
2626
2627         case INTUOSHT:
2628         case BAMBOO_PT:
2629         case BAMBOO_PEN:
2630         case INTUOSHT2:
2631                 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
2632
2633                 if (features->type == INTUOSHT2) {
2634                         wacom_setup_basic_pro_pen(wacom_wac);
2635                 } else {
2636                         __clear_bit(ABS_MISC, input_dev->absbit);
2637                         __set_bit(BTN_TOOL_PEN, input_dev->keybit);
2638                         __set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
2639                         __set_bit(BTN_STYLUS, input_dev->keybit);
2640                         __set_bit(BTN_STYLUS2, input_dev->keybit);
2641                         input_set_abs_params(input_dev, ABS_DISTANCE, 0,
2642                                       features->distance_max,
2643                                       features->distance_fuzz, 0);
2644                 }
2645                 break;
2646         case BAMBOO_PAD:
2647                 __clear_bit(ABS_MISC, input_dev->absbit);
2648                 break;
2649         }
2650         return 0;
2651 }
2652
2653 int wacom_setup_touch_input_capabilities(struct input_dev *input_dev,
2654                                          struct wacom_wac *wacom_wac)
2655 {
2656         struct wacom_features *features = &wacom_wac->features;
2657
2658         input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
2659
2660         if (!(features->device_type & WACOM_DEVICETYPE_TOUCH))
2661                 return -ENODEV;
2662
2663         if (features->type == HID_GENERIC)
2664                 /* setup has already been done */
2665                 return 0;
2666
2667         __set_bit(BTN_TOUCH, input_dev->keybit);
2668
2669         if (features->touch_max == 1) {
2670                 input_set_abs_params(input_dev, ABS_X, 0,
2671                         features->x_max, features->x_fuzz, 0);
2672                 input_set_abs_params(input_dev, ABS_Y, 0,
2673                         features->y_max, features->y_fuzz, 0);
2674                 input_abs_set_res(input_dev, ABS_X,
2675                                   features->x_resolution);
2676                 input_abs_set_res(input_dev, ABS_Y,
2677                                   features->y_resolution);
2678         }
2679         else if (features->touch_max > 1) {
2680                 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
2681                         features->x_max, features->x_fuzz, 0);
2682                 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
2683                         features->y_max, features->y_fuzz, 0);
2684                 input_abs_set_res(input_dev, ABS_MT_POSITION_X,
2685                                   features->x_resolution);
2686                 input_abs_set_res(input_dev, ABS_MT_POSITION_Y,
2687                                   features->y_resolution);
2688         }
2689
2690         switch (features->type) {
2691         case INTUOS5:
2692         case INTUOS5L:
2693         case INTUOSPM:
2694         case INTUOSPL:
2695         case INTUOS5S:
2696         case INTUOSPS:
2697                 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
2698
2699                 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, features->x_max, 0, 0);
2700                 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0, features->y_max, 0, 0);
2701                 input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_POINTER);
2702                 break;
2703
2704         case WACOM_24HDT:
2705                 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, features->x_max, 0, 0);
2706                 input_set_abs_params(input_dev, ABS_MT_WIDTH_MAJOR, 0, features->x_max, 0, 0);
2707                 input_set_abs_params(input_dev, ABS_MT_WIDTH_MINOR, 0, features->y_max, 0, 0);
2708                 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
2709                 /* fall through */
2710
2711         case WACOM_27QHDT:
2712         case MTSCREEN:
2713         case MTTPC:
2714         case MTTPC_B:
2715         case TABLETPC2FG:
2716                 input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_DIRECT);
2717                 /*fall through */
2718
2719         case TABLETPC:
2720         case TABLETPCE:
2721                 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
2722                 break;
2723
2724         case INTUOSHT:
2725         case INTUOSHT2:
2726                 input_dev->evbit[0] |= BIT_MASK(EV_SW);
2727                 __set_bit(SW_MUTE_DEVICE, input_dev->swbit);
2728                 /* fall through */
2729
2730         case BAMBOO_PT:
2731         case BAMBOO_TOUCH:
2732                 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
2733                         input_set_abs_params(input_dev,
2734                                      ABS_MT_TOUCH_MAJOR,
2735                                      0, features->x_max, 0, 0);
2736                         input_set_abs_params(input_dev,
2737                                      ABS_MT_TOUCH_MINOR,
2738                                      0, features->y_max, 0, 0);
2739                 }
2740                 input_mt_init_slots(input_dev, features->touch_max, INPUT_MT_POINTER);
2741                 break;
2742
2743         case BAMBOO_PAD:
2744                 input_mt_init_slots(input_dev, features->touch_max,
2745                                     INPUT_MT_POINTER);
2746                 __set_bit(BTN_LEFT, input_dev->keybit);
2747                 __set_bit(BTN_RIGHT, input_dev->keybit);
2748                 break;
2749         }
2750         return 0;
2751 }
2752
2753 static void wacom_setup_numbered_buttons(struct input_dev *input_dev,
2754                                 int button_count)
2755 {
2756         int i;
2757
2758         for (i = 0; i < button_count && i < 10; i++)
2759                 __set_bit(BTN_0 + i, input_dev->keybit);
2760         for (i = 10; i < button_count && i < 16; i++)
2761                 __set_bit(BTN_A + (i-10), input_dev->keybit);
2762         for (i = 16; i < button_count && i < 18; i++)
2763                 __set_bit(BTN_BASE + (i-16), input_dev->keybit);
2764 }
2765
2766 static bool wacom_is_led_toggled(struct wacom *wacom, int button_count,
2767                                  int mask, int group)
2768 {
2769         int button_per_group;
2770
2771         button_per_group = button_count/wacom->led.count;
2772
2773         return mask & (1 << (group * button_per_group));
2774 }
2775
2776 static void wacom_update_led(struct wacom *wacom, int button_count, int mask,
2777                              int group)
2778 {
2779         struct wacom_led *led, *next_led;
2780         int cur;
2781         bool pressed;
2782
2783         pressed = wacom_is_led_toggled(wacom, button_count, mask, group);
2784         cur = wacom->led.groups[group].select;
2785
2786         led = wacom_led_find(wacom, group, cur);
2787         if (!led) {
2788                 hid_err(wacom->hdev, "can't find current LED %d in group %d\n",
2789                         cur, group);
2790                 return;
2791         }
2792
2793         if (!pressed) {
2794                 led->held = false;
2795                 return;
2796         }
2797
2798         if (led->held && pressed)
2799                 return;
2800
2801         next_led = wacom_led_next(wacom, led);
2802         if (!next_led) {
2803                 hid_err(wacom->hdev, "can't find next LED in group %d\n",
2804                         group);
2805                 return;
2806         }
2807         if (next_led == led)
2808                 return;
2809
2810         next_led->held = true;
2811         led_trigger_event(&next_led->trigger,
2812                           wacom_leds_brightness_get(next_led));
2813 }
2814
2815 static void wacom_report_numbered_buttons(struct input_dev *input_dev,
2816                                 int button_count, int mask)
2817 {
2818         struct wacom *wacom = input_get_drvdata(input_dev);
2819         int i;
2820
2821         for (i = 0; i < wacom->led.count; i++)
2822                 wacom_update_led(wacom,  button_count, mask, i);
2823
2824         for (i = 0; i < button_count && i < 10; i++)
2825                 input_report_key(input_dev, BTN_0 + i, mask & (1 << i));
2826         for (i = 10; i < button_count && i < 16; i++)
2827                 input_report_key(input_dev, BTN_A + (i-10), mask & (1 << i));
2828         for (i = 16; i < button_count && i < 18; i++)
2829                 input_report_key(input_dev, BTN_BASE + (i-16), mask & (1 << i));
2830 }
2831
2832 int wacom_setup_pad_input_capabilities(struct input_dev *input_dev,
2833                                    struct wacom_wac *wacom_wac)
2834 {
2835         struct wacom_features *features = &wacom_wac->features;
2836
2837         if (!(features->device_type & WACOM_DEVICETYPE_PAD))
2838                 return -ENODEV;
2839
2840         if (features->type == REMOTE && input_dev == wacom_wac->pad_input)
2841                 return -ENODEV;
2842
2843         input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
2844
2845         /* kept for making legacy xf86-input-wacom working with the wheels */
2846         __set_bit(ABS_MISC, input_dev->absbit);
2847
2848         /* kept for making legacy xf86-input-wacom accepting the pad */
2849         input_set_abs_params(input_dev, ABS_X, 0, 1, 0, 0);
2850         input_set_abs_params(input_dev, ABS_Y, 0, 1, 0, 0);
2851
2852         /* kept for making udev and libwacom accepting the pad */
2853         __set_bit(BTN_STYLUS, input_dev->keybit);
2854
2855         wacom_setup_numbered_buttons(input_dev, features->numbered_buttons);
2856
2857         switch (features->type) {
2858
2859         case CINTIQ_HYBRID:
2860         case CINTIQ_COMPANION_2:
2861         case DTK:
2862         case DTUS:
2863         case GRAPHIRE_BT:
2864                 break;
2865
2866         case WACOM_MO:
2867                 __set_bit(BTN_BACK, input_dev->keybit);
2868                 __set_bit(BTN_LEFT, input_dev->keybit);
2869                 __set_bit(BTN_FORWARD, input_dev->keybit);
2870                 __set_bit(BTN_RIGHT, input_dev->keybit);
2871                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
2872                 break;
2873
2874         case WACOM_G4:
2875                 __set_bit(BTN_BACK, input_dev->keybit);
2876                 __set_bit(BTN_FORWARD, input_dev->keybit);
2877                 input_set_capability(input_dev, EV_REL, REL_WHEEL);
2878                 break;
2879
2880         case WACOM_24HD:
2881                 __set_bit(KEY_PROG1, input_dev->keybit);
2882                 __set_bit(KEY_PROG2, input_dev->keybit);
2883                 __set_bit(KEY_PROG3, input_dev->keybit);
2884
2885                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
2886                 input_set_abs_params(input_dev, ABS_THROTTLE, 0, 71, 0, 0);
2887                 break;
2888
2889         case WACOM_27QHD:
2890                 __set_bit(KEY_PROG1, input_dev->keybit);
2891                 __set_bit(KEY_PROG2, input_dev->keybit);
2892                 __set_bit(KEY_PROG3, input_dev->keybit);
2893                 input_set_abs_params(input_dev, ABS_X, -2048, 2048, 0, 0);
2894                 input_abs_set_res(input_dev, ABS_X, 1024); /* points/g */
2895                 input_set_abs_params(input_dev, ABS_Y, -2048, 2048, 0, 0);
2896                 input_abs_set_res(input_dev, ABS_Y, 1024);
2897                 input_set_abs_params(input_dev, ABS_Z, -2048, 2048, 0, 0);
2898                 input_abs_set_res(input_dev, ABS_Z, 1024);
2899                 __set_bit(INPUT_PROP_ACCELEROMETER, input_dev->propbit);
2900                 break;
2901
2902         case WACOM_22HD:
2903                 __set_bit(KEY_PROG1, input_dev->keybit);
2904                 __set_bit(KEY_PROG2, input_dev->keybit);
2905                 __set_bit(KEY_PROG3, input_dev->keybit);
2906                 /* fall through */
2907
2908         case WACOM_21UX2:
2909         case WACOM_BEE:
2910         case CINTIQ:
2911                 input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
2912                 input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
2913                 break;
2914
2915         case WACOM_13HD:
2916                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
2917                 break;
2918
2919         case INTUOS3:
2920         case INTUOS3L:
2921                 input_set_abs_params(input_dev, ABS_RY, 0, 4096, 0, 0);
2922                 /* fall through */
2923
2924         case INTUOS3S:
2925                 input_set_abs_params(input_dev, ABS_RX, 0, 4096, 0, 0);
2926                 break;
2927
2928         case INTUOS5:
2929         case INTUOS5L:
2930         case INTUOSPM:
2931         case INTUOSPL:
2932         case INTUOS5S:
2933         case INTUOSPS:
2934                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
2935                 break;
2936
2937         case INTUOS4WL:
2938                 /*
2939                  * For Bluetooth devices, the udev rule does not work correctly
2940                  * for pads unless we add a stylus capability, which forces
2941                  * ID_INPUT_TABLET to be set.
2942                  */
2943                 __set_bit(BTN_STYLUS, input_dev->keybit);
2944                 /* fall through */
2945
2946         case INTUOS4:
2947         case INTUOS4L:
2948         case INTUOS4S:
2949                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
2950                 break;
2951
2952         case INTUOSHT:
2953         case BAMBOO_PT:
2954         case BAMBOO_TOUCH:
2955         case INTUOSHT2:
2956                 __clear_bit(ABS_MISC, input_dev->absbit);
2957
2958                 __set_bit(BTN_LEFT, input_dev->keybit);
2959                 __set_bit(BTN_FORWARD, input_dev->keybit);
2960                 __set_bit(BTN_BACK, input_dev->keybit);
2961                 __set_bit(BTN_RIGHT, input_dev->keybit);
2962
2963                 break;
2964
2965         case REMOTE:
2966                 input_set_capability(input_dev, EV_MSC, MSC_SERIAL);
2967                 input_set_abs_params(input_dev, ABS_WHEEL, 0, 71, 0, 0);
2968                 break;
2969
2970         default:
2971                 /* no pad supported */
2972                 return -ENODEV;
2973         }
2974         return 0;
2975 }
2976
2977 static const struct wacom_features wacom_features_0x00 =
2978         { "Wacom Penpartner", 5040, 3780, 255, 0,
2979           PENPARTNER, WACOM_PENPRTN_RES, WACOM_PENPRTN_RES };
2980 static const struct wacom_features wacom_features_0x10 =
2981         { "Wacom Graphire", 10206, 7422, 511, 63,
2982           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
2983 static const struct wacom_features wacom_features_0x81 =
2984         { "Wacom Graphire BT", 16704, 12064, 511, 32,
2985           GRAPHIRE_BT, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES, 2 };
2986 static const struct wacom_features wacom_features_0x11 =
2987         { "Wacom Graphire2 4x5", 10206, 7422, 511, 63,
2988           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
2989 static const struct wacom_features wacom_features_0x12 =
2990         { "Wacom Graphire2 5x7", 13918, 10206, 511, 63,
2991           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
2992 static const struct wacom_features wacom_features_0x13 =
2993         { "Wacom Graphire3", 10208, 7424, 511, 63,
2994           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
2995 static const struct wacom_features wacom_features_0x14 =
2996         { "Wacom Graphire3 6x8", 16704, 12064, 511, 63,
2997           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
2998 static const struct wacom_features wacom_features_0x15 =
2999         { "Wacom Graphire4 4x5", 10208, 7424, 511, 63,
3000           WACOM_G4, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3001 static const struct wacom_features wacom_features_0x16 =
3002         { "Wacom Graphire4 6x8", 16704, 12064, 511, 63,
3003           WACOM_G4, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3004 static const struct wacom_features wacom_features_0x17 =
3005         { "Wacom BambooFun 4x5", 14760, 9225, 511, 63,
3006           WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3007 static const struct wacom_features wacom_features_0x18 =
3008         { "Wacom BambooFun 6x8", 21648, 13530, 511, 63,
3009           WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3010 static const struct wacom_features wacom_features_0x19 =
3011         { "Wacom Bamboo1 Medium", 16704, 12064, 511, 63,
3012           GRAPHIRE, WACOM_GRAPHIRE_RES, WACOM_GRAPHIRE_RES };
3013 static const struct wacom_features wacom_features_0x60 =
3014         { "Wacom Volito", 5104, 3712, 511, 63,
3015           GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
3016 static const struct wacom_features wacom_features_0x61 =
3017         { "Wacom PenStation2", 3250, 2320, 255, 63,
3018           GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
3019 static const struct wacom_features wacom_features_0x62 =
3020         { "Wacom Volito2 4x5", 5104, 3712, 511, 63,
3021           GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
3022 static const struct wacom_features wacom_features_0x63 =
3023         { "Wacom Volito2 2x3", 3248, 2320, 511, 63,
3024           GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
3025 static const struct wacom_features wacom_features_0x64 =
3026         { "Wacom PenPartner2", 3250, 2320, 511, 63,
3027           GRAPHIRE, WACOM_VOLITO_RES, WACOM_VOLITO_RES };
3028 static const struct wacom_features wacom_features_0x65 =
3029         { "Wacom Bamboo", 14760, 9225, 511, 63,
3030           WACOM_MO, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3031 static const struct wacom_features wacom_features_0x69 =
3032         { "Wacom Bamboo1", 5104, 3712, 511, 63,
3033           GRAPHIRE, WACOM_PENPRTN_RES, WACOM_PENPRTN_RES };
3034 static const struct wacom_features wacom_features_0x6A =
3035         { "Wacom Bamboo1 4x6", 14760, 9225, 1023, 63,
3036           GRAPHIRE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3037 static const struct wacom_features wacom_features_0x6B =
3038         { "Wacom Bamboo1 5x8", 21648, 13530, 1023, 63,
3039           GRAPHIRE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3040 static const struct wacom_features wacom_features_0x20 =
3041         { "Wacom Intuos 4x5", 12700, 10600, 1023, 31,
3042           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3043 static const struct wacom_features wacom_features_0x21 =
3044         { "Wacom Intuos 6x8", 20320, 16240, 1023, 31,
3045           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3046 static const struct wacom_features wacom_features_0x22 =
3047         { "Wacom Intuos 9x12", 30480, 24060, 1023, 31,
3048           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3049 static const struct wacom_features wacom_features_0x23 =
3050         { "Wacom Intuos 12x12", 30480, 31680, 1023, 31,
3051           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3052 static const struct wacom_features wacom_features_0x24 =
3053         { "Wacom Intuos 12x18", 45720, 31680, 1023, 31,
3054           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3055 static const struct wacom_features wacom_features_0x30 =
3056         { "Wacom PL400", 5408, 4056, 255, 0,
3057           PL, WACOM_PL_RES, WACOM_PL_RES };
3058 static const struct wacom_features wacom_features_0x31 =
3059         { "Wacom PL500", 6144, 4608, 255, 0,
3060           PL, WACOM_PL_RES, WACOM_PL_RES };
3061 static const struct wacom_features wacom_features_0x32 =
3062         { "Wacom PL600", 6126, 4604, 255, 0,
3063           PL, WACOM_PL_RES, WACOM_PL_RES };
3064 static const struct wacom_features wacom_features_0x33 =
3065         { "Wacom PL600SX", 6260, 5016, 255, 0,
3066           PL, WACOM_PL_RES, WACOM_PL_RES };
3067 static const struct wacom_features wacom_features_0x34 =
3068         { "Wacom PL550", 6144, 4608, 511, 0,
3069           PL, WACOM_PL_RES, WACOM_PL_RES };
3070 static const struct wacom_features wacom_features_0x35 =
3071         { "Wacom PL800", 7220, 5780, 511, 0,
3072           PL, WACOM_PL_RES, WACOM_PL_RES };
3073 static const struct wacom_features wacom_features_0x37 =
3074         { "Wacom PL700", 6758, 5406, 511, 0,
3075           PL, WACOM_PL_RES, WACOM_PL_RES };
3076 static const struct wacom_features wacom_features_0x38 =
3077         { "Wacom PL510", 6282, 4762, 511, 0,
3078           PL, WACOM_PL_RES, WACOM_PL_RES };
3079 static const struct wacom_features wacom_features_0x39 =
3080         { "Wacom DTU710", 34080, 27660, 511, 0,
3081           PL, WACOM_PL_RES, WACOM_PL_RES };
3082 static const struct wacom_features wacom_features_0xC4 =
3083         { "Wacom DTF521", 6282, 4762, 511, 0,
3084           PL, WACOM_PL_RES, WACOM_PL_RES };
3085 static const struct wacom_features wacom_features_0xC0 =
3086         { "Wacom DTF720", 6858, 5506, 511, 0,
3087           PL, WACOM_PL_RES, WACOM_PL_RES };
3088 static const struct wacom_features wacom_features_0xC2 =
3089         { "Wacom DTF720a", 6858, 5506, 511, 0,
3090           PL, WACOM_PL_RES, WACOM_PL_RES };
3091 static const struct wacom_features wacom_features_0x03 =
3092         { "Wacom Cintiq Partner", 20480, 15360, 511, 0,
3093           PTU, WACOM_PL_RES, WACOM_PL_RES };
3094 static const struct wacom_features wacom_features_0x41 =
3095         { "Wacom Intuos2 4x5", 12700, 10600, 1023, 31,
3096           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3097 static const struct wacom_features wacom_features_0x42 =
3098         { "Wacom Intuos2 6x8", 20320, 16240, 1023, 31,
3099           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3100 static const struct wacom_features wacom_features_0x43 =
3101         { "Wacom Intuos2 9x12", 30480, 24060, 1023, 31,
3102           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3103 static const struct wacom_features wacom_features_0x44 =
3104         { "Wacom Intuos2 12x12", 30480, 31680, 1023, 31,
3105           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3106 static const struct wacom_features wacom_features_0x45 =
3107         { "Wacom Intuos2 12x18", 45720, 31680, 1023, 31,
3108           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3109 static const struct wacom_features wacom_features_0xB0 =
3110         { "Wacom Intuos3 4x5", 25400, 20320, 1023, 63,
3111           INTUOS3S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 4 };
3112 static const struct wacom_features wacom_features_0xB1 =
3113         { "Wacom Intuos3 6x8", 40640, 30480, 1023, 63,
3114           INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3115 static const struct wacom_features wacom_features_0xB2 =
3116         { "Wacom Intuos3 9x12", 60960, 45720, 1023, 63,
3117           INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3118 static const struct wacom_features wacom_features_0xB3 =
3119         { "Wacom Intuos3 12x12", 60960, 60960, 1023, 63,
3120           INTUOS3L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3121 static const struct wacom_features wacom_features_0xB4 =
3122         { "Wacom Intuos3 12x19", 97536, 60960, 1023, 63,
3123           INTUOS3L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3124 static const struct wacom_features wacom_features_0xB5 =
3125         { "Wacom Intuos3 6x11", 54204, 31750, 1023, 63,
3126           INTUOS3, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3127 static const struct wacom_features wacom_features_0xB7 =
3128         { "Wacom Intuos3 4x6", 31496, 19685, 1023, 63,
3129           INTUOS3S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 4 };
3130 static const struct wacom_features wacom_features_0xB8 =
3131         { "Wacom Intuos4 4x6", 31496, 19685, 2047, 63,
3132           INTUOS4S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7 };
3133 static const struct wacom_features wacom_features_0xB9 =
3134         { "Wacom Intuos4 6x9", 44704, 27940, 2047, 63,
3135           INTUOS4, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3136 static const struct wacom_features wacom_features_0xBA =
3137         { "Wacom Intuos4 8x13", 65024, 40640, 2047, 63,
3138           INTUOS4L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3139 static const struct wacom_features wacom_features_0xBB =
3140         { "Wacom Intuos4 12x19", 97536, 60960, 2047, 63,
3141           INTUOS4L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3142 static const struct wacom_features wacom_features_0xBC =
3143         { "Wacom Intuos4 WL", 40640, 25400, 2047, 63,
3144           INTUOS4, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3145 static const struct wacom_features wacom_features_0xBD =
3146         { "Wacom Intuos4 WL", 40640, 25400, 2047, 63,
3147           INTUOS4WL, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3148 static const struct wacom_features wacom_features_0x26 =
3149         { "Wacom Intuos5 touch S", 31496, 19685, 2047, 63,
3150           INTUOS5S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7, .touch_max = 16 };
3151 static const struct wacom_features wacom_features_0x27 =
3152         { "Wacom Intuos5 touch M", 44704, 27940, 2047, 63,
3153           INTUOS5, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16 };
3154 static const struct wacom_features wacom_features_0x28 =
3155         { "Wacom Intuos5 touch L", 65024, 40640, 2047, 63,
3156           INTUOS5L, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16 };
3157 static const struct wacom_features wacom_features_0x29 =
3158         { "Wacom Intuos5 S", 31496, 19685, 2047, 63,
3159           INTUOS5S, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7 };
3160 static const struct wacom_features wacom_features_0x2A =
3161         { "Wacom Intuos5 M", 44704, 27940, 2047, 63,
3162           INTUOS5, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9 };
3163 static const struct wacom_features wacom_features_0x314 =
3164         { "Wacom Intuos Pro S", 31496, 19685, 2047, 63,
3165           INTUOSPS, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 7, .touch_max = 16,
3166           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3167 static const struct wacom_features wacom_features_0x315 =
3168         { "Wacom Intuos Pro M", 44704, 27940, 2047, 63,
3169           INTUOSPM, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16,
3170           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3171 static const struct wacom_features wacom_features_0x317 =
3172         { "Wacom Intuos Pro L", 65024, 40640, 2047, 63,
3173           INTUOSPL, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9, .touch_max = 16,
3174           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3175 static const struct wacom_features wacom_features_0xF4 =
3176         { "Wacom Cintiq 24HD", 104080, 65200, 2047, 63,
3177           WACOM_24HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 16,
3178           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
3179 static const struct wacom_features wacom_features_0xF8 =
3180         { "Wacom Cintiq 24HD touch", 104080, 65200, 2047, 63, /* Pen */
3181           WACOM_24HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 16,
3182           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3183           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0xf6 };
3184 static const struct wacom_features wacom_features_0xF6 =
3185         { "Wacom Cintiq 24HD touch", .type = WACOM_24HDT, /* Touch */
3186           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0xf8, .touch_max = 10,
3187           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3188 static const struct wacom_features wacom_features_0x32A =
3189         { "Wacom Cintiq 27QHD", 119740, 67520, 2047, 63,
3190           WACOM_27QHD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 0,
3191           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
3192 static const struct wacom_features wacom_features_0x32B =
3193         { "Wacom Cintiq 27QHD touch", 119740, 67520, 2047, 63,
3194           WACOM_27QHD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 0,
3195           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3196           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x32C };
3197 static const struct wacom_features wacom_features_0x32C =
3198         { "Wacom Cintiq 27QHD touch", .type = WACOM_27QHDT,
3199           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x32B, .touch_max = 10 };
3200 static const struct wacom_features wacom_features_0x3F =
3201         { "Wacom Cintiq 21UX", 87200, 65600, 1023, 63,
3202           CINTIQ, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 8 };
3203 static const struct wacom_features wacom_features_0xC5 =
3204         { "Wacom Cintiq 20WSX", 86680, 54180, 1023, 63,
3205           WACOM_BEE, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 10 };
3206 static const struct wacom_features wacom_features_0xC6 =
3207         { "Wacom Cintiq 12WX", 53020, 33440, 1023, 63,
3208           WACOM_BEE, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 10 };
3209 static const struct wacom_features wacom_features_0x304 =
3210         { "Wacom Cintiq 13HD", 59152, 33448, 1023, 63,
3211           WACOM_13HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
3212           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
3213 static const struct wacom_features wacom_features_0x333 =
3214         { "Wacom Cintiq 13HD touch", 59152, 33448, 2047, 63,
3215           WACOM_13HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
3216           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3217           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x335 };
3218 static const struct wacom_features wacom_features_0x335 =
3219         { "Wacom Cintiq 13HD touch", .type = WACOM_24HDT, /* Touch */
3220           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x333, .touch_max = 10,
3221           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3222 static const struct wacom_features wacom_features_0xC7 =
3223         { "Wacom DTU1931", 37832, 30305, 511, 0,
3224           PL, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3225 static const struct wacom_features wacom_features_0xCE =
3226         { "Wacom DTU2231", 47864, 27011, 511, 0,
3227           DTU, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
3228           .check_for_hid_type = true, .hid_type = HID_TYPE_USBMOUSE };
3229 static const struct wacom_features wacom_features_0xF0 =
3230         { "Wacom DTU1631", 34623, 19553, 511, 0,
3231           DTU, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3232 static const struct wacom_features wacom_features_0xFB =
3233         { "Wacom DTU1031", 21896, 13760, 511, 0,
3234           DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
3235           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
3236 static const struct wacom_features wacom_features_0x32F =
3237         { "Wacom DTU1031X", 22472, 12728, 511, 0,
3238           DTUSX, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 0,
3239           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
3240 static const struct wacom_features wacom_features_0x336 =
3241         { "Wacom DTU1141", 23472, 13203, 1023, 0,
3242           DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
3243           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
3244 static const struct wacom_features wacom_features_0x57 =
3245         { "Wacom DTK2241", 95640, 54060, 2047, 63,
3246           DTK, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 6,
3247           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
3248 static const struct wacom_features wacom_features_0x59 = /* Pen */
3249         { "Wacom DTH2242", 95640, 54060, 2047, 63,
3250           DTK, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 6,
3251           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3252           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5D };
3253 static const struct wacom_features wacom_features_0x5D = /* Touch */
3254         { "Wacom DTH2242",       .type = WACOM_24HDT,
3255           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x59, .touch_max = 10,
3256           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3257 static const struct wacom_features wacom_features_0xCC =
3258         { "Wacom Cintiq 21UX2", 86800, 65200, 2047, 63,
3259           WACOM_21UX2, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
3260           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
3261 static const struct wacom_features wacom_features_0xFA =
3262         { "Wacom Cintiq 22HD", 95440, 53860, 2047, 63,
3263           WACOM_22HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
3264           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET };
3265 static const struct wacom_features wacom_features_0x5B =
3266         { "Wacom Cintiq 22HDT", 95440, 53860, 2047, 63,
3267           WACOM_22HD, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 18,
3268           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3269           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5e };
3270 static const struct wacom_features wacom_features_0x5E =
3271         { "Wacom Cintiq 22HDT", .type = WACOM_24HDT,
3272           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x5b, .touch_max = 10,
3273           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3274 static const struct wacom_features wacom_features_0x90 =
3275         { "Wacom ISDv4 90", 26202, 16325, 255, 0,
3276           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3277 static const struct wacom_features wacom_features_0x93 =
3278         { "Wacom ISDv4 93", 26202, 16325, 255, 0,
3279           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3280 static const struct wacom_features wacom_features_0x97 =
3281         { "Wacom ISDv4 97", 26202, 16325, 511, 0,
3282           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3283 static const struct wacom_features wacom_features_0x9A =
3284         { "Wacom ISDv4 9A", 26202, 16325, 255, 0,
3285           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3286 static const struct wacom_features wacom_features_0x9F =
3287         { "Wacom ISDv4 9F", 26202, 16325, 255, 0,
3288           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3289 static const struct wacom_features wacom_features_0xE2 =
3290         { "Wacom ISDv4 E2", 26202, 16325, 255, 0,
3291           TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
3292 static const struct wacom_features wacom_features_0xE3 =
3293         { "Wacom ISDv4 E3", 26202, 16325, 255, 0,
3294           TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
3295 static const struct wacom_features wacom_features_0xE5 =
3296         { "Wacom ISDv4 E5", 26202, 16325, 255, 0,
3297           MTSCREEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3298 static const struct wacom_features wacom_features_0xE6 =
3299         { "Wacom ISDv4 E6", 27760, 15694, 255, 0,
3300           TABLETPC2FG, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
3301 static const struct wacom_features wacom_features_0xEC =
3302         { "Wacom ISDv4 EC", 25710, 14500, 255, 0,
3303           TABLETPC,    WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3304 static const struct wacom_features wacom_features_0xED =
3305         { "Wacom ISDv4 ED", 26202, 16325, 255, 0,
3306           TABLETPCE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3307 static const struct wacom_features wacom_features_0xEF =
3308         { "Wacom ISDv4 EF", 26202, 16325, 255, 0,
3309           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3310 static const struct wacom_features wacom_features_0x100 =
3311         { "Wacom ISDv4 100", 26202, 16325, 255, 0,
3312           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3313 static const struct wacom_features wacom_features_0x101 =
3314         { "Wacom ISDv4 101", 26202, 16325, 255, 0,
3315           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3316 static const struct wacom_features wacom_features_0x10D =
3317         { "Wacom ISDv4 10D", 26202, 16325, 255, 0,
3318           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3319 static const struct wacom_features wacom_features_0x10E =
3320         { "Wacom ISDv4 10E", 27760, 15694, 255, 0,
3321           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3322 static const struct wacom_features wacom_features_0x10F =
3323         { "Wacom ISDv4 10F", 27760, 15694, 255, 0,
3324           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3325 static const struct wacom_features wacom_features_0x116 =
3326         { "Wacom ISDv4 116", 26202, 16325, 255, 0,
3327           TABLETPCE, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3328 static const struct wacom_features wacom_features_0x12C =
3329         { "Wacom ISDv4 12C", 27848, 15752, 2047, 0,
3330           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3331 static const struct wacom_features wacom_features_0x4001 =
3332         { "Wacom ISDv4 4001", 26202, 16325, 255, 0,
3333           MTTPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3334 static const struct wacom_features wacom_features_0x4004 =
3335         { "Wacom ISDv4 4004", 11060, 6220, 255, 0,
3336           MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3337 static const struct wacom_features wacom_features_0x5000 =
3338         { "Wacom ISDv4 5000", 27848, 15752, 1023, 0,
3339           MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3340 static const struct wacom_features wacom_features_0x5002 =
3341         { "Wacom ISDv4 5002", 29576, 16724, 1023, 0,
3342           MTTPC_B, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3343 static const struct wacom_features wacom_features_0x47 =
3344         { "Wacom Intuos2 6x8", 20320, 16240, 1023, 31,
3345           INTUOS, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3346 static const struct wacom_features wacom_features_0x84 =
3347         { "Wacom Wireless Receiver", .type = WIRELESS, .touch_max = 16 };
3348 static const struct wacom_features wacom_features_0xD0 =
3349         { "Wacom Bamboo 2FG", 14720, 9200, 1023, 31,
3350           BAMBOO_TOUCH, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
3351 static const struct wacom_features wacom_features_0xD1 =
3352         { "Wacom Bamboo 2FG 4x5", 14720, 9200, 1023, 31,
3353           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
3354 static const struct wacom_features wacom_features_0xD2 =
3355         { "Wacom Bamboo Craft", 14720, 9200, 1023, 31,
3356           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
3357 static const struct wacom_features wacom_features_0xD3 =
3358         { "Wacom Bamboo 2FG 6x8", 21648, 13700, 1023, 31,
3359           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
3360 static const struct wacom_features wacom_features_0xD4 =
3361         { "Wacom Bamboo Pen", 14720, 9200, 1023, 31,
3362           BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3363 static const struct wacom_features wacom_features_0xD5 =
3364         { "Wacom Bamboo Pen 6x8", 21648, 13700, 1023, 31,
3365           BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3366 static const struct wacom_features wacom_features_0xD6 =
3367         { "Wacom BambooPT 2FG 4x5", 14720, 9200, 1023, 31,
3368           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
3369 static const struct wacom_features wacom_features_0xD7 =
3370         { "Wacom BambooPT 2FG Small", 14720, 9200, 1023, 31,
3371           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
3372 static const struct wacom_features wacom_features_0xD8 =
3373         { "Wacom Bamboo Comic 2FG", 21648, 13700, 1023, 31,
3374           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
3375 static const struct wacom_features wacom_features_0xDA =
3376         { "Wacom Bamboo 2FG 4x5 SE", 14720, 9200, 1023, 31,
3377           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
3378 static const struct wacom_features wacom_features_0xDB =
3379         { "Wacom Bamboo 2FG 6x8 SE", 21648, 13700, 1023, 31,
3380           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 2 };
3381 static const struct wacom_features wacom_features_0xDD =
3382         { "Wacom Bamboo Connect", 14720, 9200, 1023, 31,
3383           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3384 static const struct wacom_features wacom_features_0xDE =
3385         { "Wacom Bamboo 16FG 4x5", 14720, 9200, 1023, 31,
3386           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16 };
3387 static const struct wacom_features wacom_features_0xDF =
3388         { "Wacom Bamboo 16FG 6x8", 21648, 13700, 1023, 31,
3389           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16 };
3390 static const struct wacom_features wacom_features_0x300 =
3391         { "Wacom Bamboo One S", 14720, 9225, 1023, 31,
3392           BAMBOO_PEN, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3393 static const struct wacom_features wacom_features_0x301 =
3394         { "Wacom Bamboo One M", 21648, 13530, 1023, 31,
3395           BAMBOO_PT, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3396 static const struct wacom_features wacom_features_0x302 =
3397         { "Wacom Intuos PT S", 15200, 9500, 1023, 31,
3398           INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
3399           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3400 static const struct wacom_features wacom_features_0x303 =
3401         { "Wacom Intuos PT M", 21600, 13500, 1023, 31,
3402           INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
3403           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3404 static const struct wacom_features wacom_features_0x30E =
3405         { "Wacom Intuos S", 15200, 9500, 1023, 31,
3406           INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
3407           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3408 static const struct wacom_features wacom_features_0x6004 =
3409         { "ISD-V4", 12800, 8000, 255, 0,
3410           TABLETPC, WACOM_INTUOS_RES, WACOM_INTUOS_RES };
3411 static const struct wacom_features wacom_features_0x307 =
3412         { "Wacom ISDv5 307", 59152, 33448, 2047, 63,
3413           CINTIQ_HYBRID, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
3414           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3415           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x309 };
3416 static const struct wacom_features wacom_features_0x309 =
3417         { "Wacom ISDv5 309", .type = WACOM_24HDT, /* Touch */
3418           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x0307, .touch_max = 10,
3419           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3420 static const struct wacom_features wacom_features_0x30A =
3421         { "Wacom ISDv5 30A", 59152, 33448, 2047, 63,
3422           CINTIQ_HYBRID, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 9,
3423           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3424           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x30C };
3425 static const struct wacom_features wacom_features_0x30C =
3426         { "Wacom ISDv5 30C", .type = WACOM_24HDT, /* Touch */
3427           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x30A, .touch_max = 10,
3428           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3429 static const struct wacom_features wacom_features_0x318 =
3430         { "Wacom USB Bamboo PAD", 4095, 4095, /* Touch */
3431           .type = BAMBOO_PAD, 35, 48, .touch_max = 4 };
3432 static const struct wacom_features wacom_features_0x319 =
3433         { "Wacom Wireless Bamboo PAD", 4095, 4095, /* Touch */
3434           .type = BAMBOO_PAD, 35, 48, .touch_max = 4 };
3435 static const struct wacom_features wacom_features_0x325 =
3436         { "Wacom ISDv5 325", 59552, 33848, 2047, 63,
3437           CINTIQ_COMPANION_2, WACOM_INTUOS3_RES, WACOM_INTUOS3_RES, 11,
3438           WACOM_CINTIQ_OFFSET, WACOM_CINTIQ_OFFSET,
3439           .oVid = USB_VENDOR_ID_WACOM, .oPid = 0x326 };
3440 static const struct wacom_features wacom_features_0x326 = /* Touch */
3441         { "Wacom ISDv5 326", .type = HID_GENERIC, .oVid = USB_VENDOR_ID_WACOM,
3442           .oPid = 0x325 };
3443 static const struct wacom_features wacom_features_0x323 =
3444         { "Wacom Intuos P M", 21600, 13500, 1023, 31,
3445           INTUOSHT, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
3446           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3447 static const struct wacom_features wacom_features_0x331 =
3448         { "Wacom Express Key Remote", .type = REMOTE,
3449           .numbered_buttons = 18, .check_for_hid_type = true,
3450           .hid_type = HID_TYPE_USBNONE };
3451 static const struct wacom_features wacom_features_0x33B =
3452         { "Wacom Intuos S 2", 15200, 9500, 2047, 63,
3453           INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
3454           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3455 static const struct wacom_features wacom_features_0x33C =
3456         { "Wacom Intuos PT S 2", 15200, 9500, 2047, 63,
3457           INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
3458           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3459 static const struct wacom_features wacom_features_0x33D =
3460         { "Wacom Intuos P M 2", 21600, 13500, 2047, 63,
3461           INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES,
3462           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3463 static const struct wacom_features wacom_features_0x33E =
3464         { "Wacom Intuos PT M 2", 21600, 13500, 2047, 63,
3465           INTUOSHT2, WACOM_INTUOS_RES, WACOM_INTUOS_RES, .touch_max = 16,
3466           .check_for_hid_type = true, .hid_type = HID_TYPE_USBNONE };
3467 static const struct wacom_features wacom_features_0x343 =
3468         { "Wacom DTK1651", 34616, 19559, 1023, 0,
3469           DTUS, WACOM_INTUOS_RES, WACOM_INTUOS_RES, 4,
3470           WACOM_DTU_OFFSET, WACOM_DTU_OFFSET };
3471
3472 static const struct wacom_features wacom_features_HID_ANY_ID =
3473         { "Wacom HID", .type = HID_GENERIC };
3474
3475 #define USB_DEVICE_WACOM(prod)                                          \
3476         HID_DEVICE(BUS_USB, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
3477         .driver_data = (kernel_ulong_t)&wacom_features_##prod
3478
3479 #define BT_DEVICE_WACOM(prod)                                           \
3480         HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
3481         .driver_data = (kernel_ulong_t)&wacom_features_##prod
3482
3483 #define I2C_DEVICE_WACOM(prod)                                          \
3484         HID_DEVICE(BUS_I2C, HID_GROUP_WACOM, USB_VENDOR_ID_WACOM, prod),\
3485         .driver_data = (kernel_ulong_t)&wacom_features_##prod
3486
3487 #define USB_DEVICE_LENOVO(prod)                                 \
3488         HID_USB_DEVICE(USB_VENDOR_ID_LENOVO, prod),                     \
3489         .driver_data = (kernel_ulong_t)&wacom_features_##prod
3490
3491 const struct hid_device_id wacom_ids[] = {
3492         { USB_DEVICE_WACOM(0x00) },
3493         { USB_DEVICE_WACOM(0x03) },
3494         { USB_DEVICE_WACOM(0x10) },
3495         { USB_DEVICE_WACOM(0x11) },
3496         { USB_DEVICE_WACOM(0x12) },
3497         { USB_DEVICE_WACOM(0x13) },
3498         { USB_DEVICE_WACOM(0x14) },
3499         { USB_DEVICE_WACOM(0x15) },
3500         { USB_DEVICE_WACOM(0x16) },
3501         { USB_DEVICE_WACOM(0x17) },
3502         { USB_DEVICE_WACOM(0x18) },
3503         { USB_DEVICE_WACOM(0x19) },
3504         { USB_DEVICE_WACOM(0x20) },
3505         { USB_DEVICE_WACOM(0x21) },
3506         { USB_DEVICE_WACOM(0x22) },
3507         { USB_DEVICE_WACOM(0x23) },
3508         { USB_DEVICE_WACOM(0x24) },
3509         { USB_DEVICE_WACOM(0x26) },
3510         { USB_DEVICE_WACOM(0x27) },
3511         { USB_DEVICE_WACOM(0x28) },
3512         { USB_DEVICE_WACOM(0x29) },
3513         { USB_DEVICE_WACOM(0x2A) },
3514         { USB_DEVICE_WACOM(0x30) },
3515         { USB_DEVICE_WACOM(0x31) },
3516         { USB_DEVICE_WACOM(0x32) },
3517         { USB_DEVICE_WACOM(0x33) },
3518         { USB_DEVICE_WACOM(0x34) },
3519         { USB_DEVICE_WACOM(0x35) },
3520         { USB_DEVICE_WACOM(0x37) },
3521         { USB_DEVICE_WACOM(0x38) },
3522         { USB_DEVICE_WACOM(0x39) },
3523         { USB_DEVICE_WACOM(0x3F) },
3524         { USB_DEVICE_WACOM(0x41) },
3525         { USB_DEVICE_WACOM(0x42) },
3526         { USB_DEVICE_WACOM(0x43) },
3527         { USB_DEVICE_WACOM(0x44) },
3528         { USB_DEVICE_WACOM(0x45) },
3529         { USB_DEVICE_WACOM(0x47) },
3530         { USB_DEVICE_WACOM(0x57) },
3531         { USB_DEVICE_WACOM(0x59) },
3532         { USB_DEVICE_WACOM(0x5B) },
3533         { USB_DEVICE_WACOM(0x5D) },
3534         { USB_DEVICE_WACOM(0x5E) },
3535         { USB_DEVICE_WACOM(0x60) },
3536         { USB_DEVICE_WACOM(0x61) },
3537         { USB_DEVICE_WACOM(0x62) },
3538         { USB_DEVICE_WACOM(0x63) },
3539         { USB_DEVICE_WACOM(0x64) },
3540         { USB_DEVICE_WACOM(0x65) },
3541         { USB_DEVICE_WACOM(0x69) },
3542         { USB_DEVICE_WACOM(0x6A) },
3543         { USB_DEVICE_WACOM(0x6B) },
3544         { BT_DEVICE_WACOM(0x81) },
3545         { USB_DEVICE_WACOM(0x84) },
3546         { USB_DEVICE_WACOM(0x90) },
3547         { USB_DEVICE_WACOM(0x93) },
3548         { USB_DEVICE_WACOM(0x97) },
3549         { USB_DEVICE_WACOM(0x9A) },
3550         { USB_DEVICE_WACOM(0x9F) },
3551         { USB_DEVICE_WACOM(0xB0) },
3552         { USB_DEVICE_WACOM(0xB1) },
3553         { USB_DEVICE_WACOM(0xB2) },
3554         { USB_DEVICE_WACOM(0xB3) },
3555         { USB_DEVICE_WACOM(0xB4) },
3556         { USB_DEVICE_WACOM(0xB5) },
3557         { USB_DEVICE_WACOM(0xB7) },
3558         { USB_DEVICE_WACOM(0xB8) },
3559         { USB_DEVICE_WACOM(0xB9) },
3560         { USB_DEVICE_WACOM(0xBA) },
3561         { USB_DEVICE_WACOM(0xBB) },
3562         { USB_DEVICE_WACOM(0xBC) },
3563         { BT_DEVICE_WACOM(0xBD) },
3564         { USB_DEVICE_WACOM(0xC0) },
3565         { USB_DEVICE_WACOM(0xC2) },
3566         { USB_DEVICE_WACOM(0xC4) },
3567         { USB_DEVICE_WACOM(0xC5) },
3568         { USB_DEVICE_WACOM(0xC6) },
3569         { USB_DEVICE_WACOM(0xC7) },
3570         { USB_DEVICE_WACOM(0xCC) },
3571         { USB_DEVICE_WACOM(0xCE) },
3572         { USB_DEVICE_WACOM(0xD0) },
3573         { USB_DEVICE_WACOM(0xD1) },
3574         { USB_DEVICE_WACOM(0xD2) },
3575         { USB_DEVICE_WACOM(0xD3) },
3576         { USB_DEVICE_WACOM(0xD4) },
3577         { USB_DEVICE_WACOM(0xD5) },
3578         { USB_DEVICE_WACOM(0xD6) },
3579         { USB_DEVICE_WACOM(0xD7) },
3580         { USB_DEVICE_WACOM(0xD8) },
3581         { USB_DEVICE_WACOM(0xDA) },
3582         { USB_DEVICE_WACOM(0xDB) },
3583         { USB_DEVICE_WACOM(0xDD) },
3584         { USB_DEVICE_WACOM(0xDE) },
3585         { USB_DEVICE_WACOM(0xDF) },
3586         { USB_DEVICE_WACOM(0xE2) },
3587         { USB_DEVICE_WACOM(0xE3) },
3588         { USB_DEVICE_WACOM(0xE5) },
3589         { USB_DEVICE_WACOM(0xE6) },
3590         { USB_DEVICE_WACOM(0xEC) },
3591         { USB_DEVICE_WACOM(0xED) },
3592         { USB_DEVICE_WACOM(0xEF) },
3593         { USB_DEVICE_WACOM(0xF0) },
3594         { USB_DEVICE_WACOM(0xF4) },
3595         { USB_DEVICE_WACOM(0xF6) },
3596         { USB_DEVICE_WACOM(0xF8) },
3597         { USB_DEVICE_WACOM(0xFA) },
3598         { USB_DEVICE_WACOM(0xFB) },
3599         { USB_DEVICE_WACOM(0x100) },
3600         { USB_DEVICE_WACOM(0x101) },
3601         { USB_DEVICE_WACOM(0x10D) },
3602         { USB_DEVICE_WACOM(0x10E) },
3603         { USB_DEVICE_WACOM(0x10F) },
3604         { USB_DEVICE_WACOM(0x116) },
3605         { USB_DEVICE_WACOM(0x12C) },
3606         { USB_DEVICE_WACOM(0x300) },
3607         { USB_DEVICE_WACOM(0x301) },
3608         { USB_DEVICE_WACOM(0x302) },
3609         { USB_DEVICE_WACOM(0x303) },
3610         { USB_DEVICE_WACOM(0x304) },
3611         { USB_DEVICE_WACOM(0x307) },
3612         { USB_DEVICE_WACOM(0x309) },
3613         { USB_DEVICE_WACOM(0x30A) },
3614         { USB_DEVICE_WACOM(0x30C) },
3615         { USB_DEVICE_WACOM(0x30E) },
3616         { USB_DEVICE_WACOM(0x314) },
3617         { USB_DEVICE_WACOM(0x315) },
3618         { USB_DEVICE_WACOM(0x317) },
3619         { USB_DEVICE_WACOM(0x318) },
3620         { USB_DEVICE_WACOM(0x319) },
3621         { USB_DEVICE_WACOM(0x323) },
3622         { USB_DEVICE_WACOM(0x325) },
3623         { USB_DEVICE_WACOM(0x326) },
3624         { USB_DEVICE_WACOM(0x32A) },
3625         { USB_DEVICE_WACOM(0x32B) },
3626         { USB_DEVICE_WACOM(0x32C) },
3627         { USB_DEVICE_WACOM(0x32F) },
3628         { USB_DEVICE_WACOM(0x331) },
3629         { USB_DEVICE_WACOM(0x333) },
3630         { USB_DEVICE_WACOM(0x335) },
3631         { USB_DEVICE_WACOM(0x336) },
3632         { USB_DEVICE_WACOM(0x33B) },
3633         { USB_DEVICE_WACOM(0x33C) },
3634         { USB_DEVICE_WACOM(0x33D) },
3635         { USB_DEVICE_WACOM(0x33E) },
3636         { USB_DEVICE_WACOM(0x343) },
3637         { USB_DEVICE_WACOM(0x4001) },
3638         { USB_DEVICE_WACOM(0x4004) },
3639         { USB_DEVICE_WACOM(0x5000) },
3640         { USB_DEVICE_WACOM(0x5002) },
3641         { USB_DEVICE_LENOVO(0x6004) },
3642
3643         { USB_DEVICE_WACOM(HID_ANY_ID) },
3644         { I2C_DEVICE_WACOM(HID_ANY_ID) },
3645         { }
3646 };
3647 MODULE_DEVICE_TABLE(hid, wacom_ids);