GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / hid / wacom_sys.c
1 /*
2  * drivers/input/tablet/wacom_sys.c
3  *
4  *  USB Wacom tablet support - system specific code
5  */
6
7 /*
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include "wacom_wac.h"
15 #include "wacom.h"
16 #include <linux/input/mt.h>
17
18 #define WAC_MSG_RETRIES         5
19 #define WAC_CMD_RETRIES         10
20
21 #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
22 #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
23 #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
24
25 static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
26                             size_t size, unsigned int retries)
27 {
28         int retval;
29
30         do {
31                 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
32                                 HID_REQ_GET_REPORT);
33         } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
34
35         if (retval < 0)
36                 hid_err(hdev, "wacom_get_report: ran out of retries "
37                         "(last error = %d)\n", retval);
38
39         return retval;
40 }
41
42 static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
43                             size_t size, unsigned int retries)
44 {
45         int retval;
46
47         do {
48                 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
49                                 HID_REQ_SET_REPORT);
50         } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
51
52         if (retval < 0)
53                 hid_err(hdev, "wacom_set_report: ran out of retries "
54                         "(last error = %d)\n", retval);
55
56         return retval;
57 }
58
59 static void wacom_wac_queue_insert(struct hid_device *hdev,
60                                    struct kfifo_rec_ptr_2 *fifo,
61                                    u8 *raw_data, int size)
62 {
63         bool warned = false;
64
65         while (kfifo_avail(fifo) < size) {
66                 if (!warned)
67                         hid_warn(hdev, "%s: kfifo has filled, starting to drop events\n", __func__);
68                 warned = true;
69
70                 kfifo_skip(fifo);
71         }
72
73         kfifo_in(fifo, raw_data, size);
74 }
75
76 static void wacom_wac_queue_flush(struct hid_device *hdev,
77                                   struct kfifo_rec_ptr_2 *fifo)
78 {
79         while (!kfifo_is_empty(fifo)) {
80                 u8 buf[WACOM_PKGLEN_MAX];
81                 int size;
82                 int err;
83
84                 size = kfifo_out(fifo, buf, sizeof(buf));
85                 err = hid_report_raw_event(hdev, HID_INPUT_REPORT, buf, size, false);
86                 if (err) {
87                         hid_warn(hdev, "%s: unable to flush event due to error %d\n",
88                                  __func__, err);
89                 }
90         }
91 }
92
93 static int wacom_wac_pen_serial_enforce(struct hid_device *hdev,
94                 struct hid_report *report, u8 *raw_data, int report_size)
95 {
96         struct wacom *wacom = hid_get_drvdata(hdev);
97         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
98         struct wacom_features *features = &wacom_wac->features;
99         bool flush = false;
100         bool insert = false;
101         int i, j;
102
103         if (wacom_wac->serial[0] || !(features->quirks & WACOM_QUIRK_TOOLSERIAL))
104                 return 0;
105
106         /* Queue events which have invalid tool type or serial number */
107         for (i = 0; i < report->maxfield; i++) {
108                 for (j = 0; j < report->field[i]->maxusage; j++) {
109                         struct hid_field *field = report->field[i];
110                         struct hid_usage *usage = &field->usage[j];
111                         unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
112                         unsigned int offset;
113                         unsigned int size;
114                         unsigned int value;
115
116                         if (equivalent_usage != HID_DG_INRANGE &&
117                             equivalent_usage != HID_DG_TOOLSERIALNUMBER &&
118                             equivalent_usage != WACOM_HID_WD_SERIALHI &&
119                             equivalent_usage != WACOM_HID_WD_TOOLTYPE)
120                                 continue;
121
122                         offset = field->report_offset;
123                         size = field->report_size;
124                         value = hid_field_extract(hdev, raw_data+1, offset + j * size, size);
125
126                         /* If we go out of range, we need to flush the queue ASAP */
127                         if (equivalent_usage == HID_DG_INRANGE)
128                                 value = !value;
129
130                         if (value) {
131                                 flush = true;
132                                 switch (equivalent_usage) {
133                                 case HID_DG_TOOLSERIALNUMBER:
134                                         wacom_wac->serial[0] = value;
135                                         break;
136
137                                 case WACOM_HID_WD_SERIALHI:
138                                         wacom_wac->serial[0] |= ((__u64)value) << 32;
139                                         break;
140
141                                 case WACOM_HID_WD_TOOLTYPE:
142                                         wacom_wac->id[0] = value;
143                                         break;
144                                 }
145                         }
146                         else {
147                                 insert = true;
148                         }
149                 }
150         }
151
152         if (flush)
153                 wacom_wac_queue_flush(hdev, wacom_wac->pen_fifo);
154         else if (insert)
155                 wacom_wac_queue_insert(hdev, wacom_wac->pen_fifo,
156                                        raw_data, report_size);
157
158         return insert && !flush;
159 }
160
161 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
162                 u8 *raw_data, int size)
163 {
164         struct wacom *wacom = hid_get_drvdata(hdev);
165
166         if (size > WACOM_PKGLEN_MAX)
167                 return 1;
168
169         if (wacom_wac_pen_serial_enforce(hdev, report, raw_data, size))
170                 return -1;
171
172         memcpy(wacom->wacom_wac.data, raw_data, size);
173
174         wacom_wac_irq(&wacom->wacom_wac, size);
175
176         return 0;
177 }
178
179 static int wacom_open(struct input_dev *dev)
180 {
181         struct wacom *wacom = input_get_drvdata(dev);
182
183         return hid_hw_open(wacom->hdev);
184 }
185
186 static void wacom_close(struct input_dev *dev)
187 {
188         struct wacom *wacom = input_get_drvdata(dev);
189
190         /*
191          * wacom->hdev should never be null, but surprisingly, I had the case
192          * once while unplugging the Wacom Wireless Receiver.
193          */
194         if (wacom->hdev)
195                 hid_hw_close(wacom->hdev);
196 }
197
198 /*
199  * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
200  */
201 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
202                                unsigned unit, int exponent)
203 {
204         struct hid_field field = {
205                 .logical_maximum = logical_extents,
206                 .physical_maximum = physical_extents,
207                 .unit = unit,
208                 .unit_exponent = exponent,
209         };
210
211         return hidinput_calc_abs_res(&field, ABS_X);
212 }
213
214 static void wacom_hid_usage_quirk(struct hid_device *hdev,
215                 struct hid_field *field, struct hid_usage *usage)
216 {
217         struct wacom *wacom = hid_get_drvdata(hdev);
218         struct wacom_features *features = &wacom->wacom_wac.features;
219         unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
220
221         /*
222          * The Dell Canvas 27 needs to be switched to its vendor-defined
223          * report to provide the best resolution.
224          */
225         if (hdev->vendor == USB_VENDOR_ID_WACOM &&
226             hdev->product == 0x4200 &&
227             field->application == HID_UP_MSVENDOR) {
228                 wacom->wacom_wac.mode_report = field->report->id;
229                 wacom->wacom_wac.mode_value = 2;
230         }
231
232         /*
233          * ISDv4 devices which predate HID's adoption of the
234          * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
235          * position instead. We can accurately detect if a
236          * usage with that value should be HID_DG_BARRELSWITCH2
237          * based on the surrounding usages, which have remained
238          * constant across generations.
239          */
240         if (features->type == HID_GENERIC &&
241             usage->hid == 0x000D0000 &&
242             field->application == HID_DG_PEN &&
243             field->physical == HID_DG_STYLUS) {
244                 int i = usage->usage_index;
245
246                 if (i-4 >= 0 && i+1 < field->maxusage &&
247                     field->usage[i-4].hid == HID_DG_TIPSWITCH &&
248                     field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
249                     field->usage[i-2].hid == HID_DG_ERASER &&
250                     field->usage[i-1].hid == HID_DG_INVERT &&
251                     field->usage[i+1].hid == HID_DG_INRANGE) {
252                         usage->hid = HID_DG_BARRELSWITCH2;
253                 }
254         }
255
256         /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
257         if (hdev->vendor == USB_VENDOR_ID_WACOM &&
258             hdev->product == 0x0358 &&
259             WACOM_PEN_FIELD(field) &&
260             equivalent_usage == HID_GD_Y) {
261                 field->logical_maximum = 43200;
262         }
263 }
264
265 static void wacom_feature_mapping(struct hid_device *hdev,
266                 struct hid_field *field, struct hid_usage *usage)
267 {
268         struct wacom *wacom = hid_get_drvdata(hdev);
269         struct wacom_features *features = &wacom->wacom_wac.features;
270         struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
271         unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
272         u8 *data;
273         int ret;
274         u32 n;
275
276         wacom_hid_usage_quirk(hdev, field, usage);
277
278         switch (equivalent_usage) {
279         case WACOM_HID_WD_TOUCH_RING_SETTING:
280                 wacom->generic_has_leds = true;
281                 break;
282         case HID_DG_CONTACTMAX:
283                 /* leave touch_max as is if predefined */
284                 if (!features->touch_max) {
285                         /* read manually */
286                         n = hid_report_len(field->report);
287                         data = hid_alloc_report_buf(field->report, GFP_KERNEL);
288                         if (!data)
289                                 break;
290                         data[0] = field->report->id;
291                         ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
292                                                data, n, WAC_CMD_RETRIES);
293                         if (ret == n && features->type == HID_GENERIC) {
294                                 ret = hid_report_raw_event(hdev,
295                                         HID_FEATURE_REPORT, data, n, 0);
296                         } else if (ret == 2 && features->type != HID_GENERIC) {
297                                 features->touch_max = data[1];
298                         } else {
299                                 features->touch_max = 16;
300                                 hid_warn(hdev, "wacom_feature_mapping: "
301                                          "could not get HID_DG_CONTACTMAX, "
302                                          "defaulting to %d\n",
303                                           features->touch_max);
304                         }
305                         kfree(data);
306                 }
307                 break;
308         case HID_DG_INPUTMODE:
309                 /* Ignore if value index is out of bounds. */
310                 if (usage->usage_index >= field->report_count) {
311                         dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
312                         break;
313                 }
314
315                 hid_data->inputmode = field->report->id;
316                 hid_data->inputmode_index = usage->usage_index;
317                 break;
318
319         case HID_UP_DIGITIZER:
320                 if (field->report->id == 0x0B &&
321                     (field->application == WACOM_HID_G9_PEN ||
322                      field->application == WACOM_HID_G11_PEN)) {
323                         wacom->wacom_wac.mode_report = field->report->id;
324                         wacom->wacom_wac.mode_value = 0;
325                 }
326                 break;
327
328         case WACOM_HID_WD_DATAMODE:
329                 wacom->wacom_wac.mode_report = field->report->id;
330                 wacom->wacom_wac.mode_value = 2;
331                 break;
332
333         case WACOM_HID_UP_G9:
334         case WACOM_HID_UP_G11:
335                 if (field->report->id == 0x03 &&
336                     (field->application == WACOM_HID_G9_TOUCHSCREEN ||
337                      field->application == WACOM_HID_G11_TOUCHSCREEN)) {
338                         wacom->wacom_wac.mode_report = field->report->id;
339                         wacom->wacom_wac.mode_value = 0;
340                 }
341                 break;
342         case WACOM_HID_WD_OFFSETLEFT:
343         case WACOM_HID_WD_OFFSETTOP:
344         case WACOM_HID_WD_OFFSETRIGHT:
345         case WACOM_HID_WD_OFFSETBOTTOM:
346                 /* read manually */
347                 n = hid_report_len(field->report);
348                 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
349                 if (!data)
350                         break;
351                 data[0] = field->report->id;
352                 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
353                                         data, n, WAC_CMD_RETRIES);
354                 if (ret == n) {
355                         ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
356                                                    data, n, 0);
357                 } else {
358                         hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
359                                  __func__);
360                 }
361                 kfree(data);
362                 break;
363         }
364 }
365
366 /*
367  * Interface Descriptor of wacom devices can be incomplete and
368  * inconsistent so wacom_features table is used to store stylus
369  * device's packet lengths, various maximum values, and tablet
370  * resolution based on product ID's.
371  *
372  * For devices that contain 2 interfaces, wacom_features table is
373  * inaccurate for the touch interface.  Since the Interface Descriptor
374  * for touch interfaces has pretty complete data, this function exists
375  * to query tablet for this missing information instead of hard coding in
376  * an additional table.
377  *
378  * A typical Interface Descriptor for a stylus will contain a
379  * boot mouse application collection that is not of interest and this
380  * function will ignore it.
381  *
382  * It also contains a digitizer application collection that also is not
383  * of interest since any information it contains would be duplicate
384  * of what is in wacom_features. Usually it defines a report of an array
385  * of bytes that could be used as max length of the stylus packet returned.
386  * If it happens to define a Digitizer-Stylus Physical Collection then
387  * the X and Y logical values contain valid data but it is ignored.
388  *
389  * A typical Interface Descriptor for a touch interface will contain a
390  * Digitizer-Finger Physical Collection which will define both logical
391  * X/Y maximum as well as the physical size of tablet. Since touch
392  * interfaces haven't supported pressure or distance, this is enough
393  * information to override invalid values in the wacom_features table.
394  *
395  * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
396  * data. We deal with them after returning from this function.
397  */
398 static void wacom_usage_mapping(struct hid_device *hdev,
399                 struct hid_field *field, struct hid_usage *usage)
400 {
401         struct wacom *wacom = hid_get_drvdata(hdev);
402         struct wacom_features *features = &wacom->wacom_wac.features;
403         bool finger = WACOM_FINGER_FIELD(field);
404         bool pen = WACOM_PEN_FIELD(field);
405         unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
406
407         /*
408         * Requiring Stylus Usage will ignore boot mouse
409         * X/Y values and some cases of invalid Digitizer X/Y
410         * values commonly reported.
411         */
412         if (pen)
413                 features->device_type |= WACOM_DEVICETYPE_PEN;
414         else if (finger)
415                 features->device_type |= WACOM_DEVICETYPE_TOUCH;
416         else
417                 return;
418
419         wacom_hid_usage_quirk(hdev, field, usage);
420
421         switch (equivalent_usage) {
422         case HID_GD_X:
423                 features->x_max = field->logical_maximum;
424                 if (finger) {
425                         features->x_phy = field->physical_maximum;
426                         if ((features->type != BAMBOO_PT) &&
427                             (features->type != BAMBOO_TOUCH)) {
428                                 features->unit = field->unit;
429                                 features->unitExpo = field->unit_exponent;
430                         }
431                 }
432                 break;
433         case HID_GD_Y:
434                 features->y_max = field->logical_maximum;
435                 if (finger) {
436                         features->y_phy = field->physical_maximum;
437                         if ((features->type != BAMBOO_PT) &&
438                             (features->type != BAMBOO_TOUCH)) {
439                                 features->unit = field->unit;
440                                 features->unitExpo = field->unit_exponent;
441                         }
442                 }
443                 break;
444         case HID_DG_TIPPRESSURE:
445                 if (pen)
446                         features->pressure_max = field->logical_maximum;
447                 break;
448         }
449
450         if (features->type == HID_GENERIC)
451                 wacom_wac_usage_mapping(hdev, field, usage);
452 }
453
454 static void wacom_post_parse_hid(struct hid_device *hdev,
455                                  struct wacom_features *features)
456 {
457         struct wacom *wacom = hid_get_drvdata(hdev);
458         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
459
460         if (features->type == HID_GENERIC) {
461                 /* Any last-minute generic device setup */
462                 if (wacom_wac->has_mode_change) {
463                         if (wacom_wac->is_direct_mode)
464                                 features->device_type |= WACOM_DEVICETYPE_DIRECT;
465                         else
466                                 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
467                 }
468
469                 if (features->touch_max > 1) {
470                         if (features->device_type & WACOM_DEVICETYPE_DIRECT)
471                                 input_mt_init_slots(wacom_wac->touch_input,
472                                                     wacom_wac->features.touch_max,
473                                                     INPUT_MT_DIRECT);
474                         else
475                                 input_mt_init_slots(wacom_wac->touch_input,
476                                                     wacom_wac->features.touch_max,
477                                                     INPUT_MT_POINTER);
478                 }
479         }
480 }
481
482 static void wacom_parse_hid(struct hid_device *hdev,
483                            struct wacom_features *features)
484 {
485         struct hid_report_enum *rep_enum;
486         struct hid_report *hreport;
487         int i, j;
488
489         /* check features first */
490         rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
491         list_for_each_entry(hreport, &rep_enum->report_list, list) {
492                 for (i = 0; i < hreport->maxfield; i++) {
493                         /* Ignore if report count is out of bounds. */
494                         if (hreport->field[i]->report_count < 1)
495                                 continue;
496
497                         for (j = 0; j < hreport->field[i]->maxusage; j++) {
498                                 wacom_feature_mapping(hdev, hreport->field[i],
499                                                 hreport->field[i]->usage + j);
500                         }
501                 }
502         }
503
504         /* now check the input usages */
505         rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
506         list_for_each_entry(hreport, &rep_enum->report_list, list) {
507
508                 if (!hreport->maxfield)
509                         continue;
510
511                 for (i = 0; i < hreport->maxfield; i++)
512                         for (j = 0; j < hreport->field[i]->maxusage; j++)
513                                 wacom_usage_mapping(hdev, hreport->field[i],
514                                                 hreport->field[i]->usage + j);
515         }
516
517         wacom_post_parse_hid(hdev, features);
518 }
519
520 static int wacom_hid_set_device_mode(struct hid_device *hdev)
521 {
522         struct wacom *wacom = hid_get_drvdata(hdev);
523         struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
524         struct hid_report *r;
525         struct hid_report_enum *re;
526
527         if (hid_data->inputmode < 0)
528                 return 0;
529
530         re = &(hdev->report_enum[HID_FEATURE_REPORT]);
531         r = re->report_id_hash[hid_data->inputmode];
532         if (r) {
533                 r->field[0]->value[hid_data->inputmode_index] = 2;
534                 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
535         }
536         return 0;
537 }
538
539 static int wacom_set_device_mode(struct hid_device *hdev,
540                                  struct wacom_wac *wacom_wac)
541 {
542         u8 *rep_data;
543         struct hid_report *r;
544         struct hid_report_enum *re;
545         u32 length;
546         int error = -ENOMEM, limit = 0;
547
548         if (wacom_wac->mode_report < 0)
549                 return 0;
550
551         re = &(hdev->report_enum[HID_FEATURE_REPORT]);
552         r = re->report_id_hash[wacom_wac->mode_report];
553         if (!r)
554                 return -EINVAL;
555
556         rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
557         if (!rep_data)
558                 return -ENOMEM;
559
560         length = hid_report_len(r);
561
562         do {
563                 rep_data[0] = wacom_wac->mode_report;
564                 rep_data[1] = wacom_wac->mode_value;
565
566                 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
567                                          length, 1);
568                 if (error >= 0)
569                         error = wacom_get_report(hdev, HID_FEATURE_REPORT,
570                                                  rep_data, length, 1);
571         } while (error >= 0 &&
572                  rep_data[1] != wacom_wac->mode_report &&
573                  limit++ < WAC_MSG_RETRIES);
574
575         kfree(rep_data);
576
577         return error < 0 ? error : 0;
578 }
579
580 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
581                 struct wacom_features *features)
582 {
583         struct wacom *wacom = hid_get_drvdata(hdev);
584         int ret;
585         u8 rep_data[2];
586
587         switch (features->type) {
588         case GRAPHIRE_BT:
589                 rep_data[0] = 0x03;
590                 rep_data[1] = 0x00;
591                 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
592                                         3);
593
594                 if (ret >= 0) {
595                         rep_data[0] = speed == 0 ? 0x05 : 0x06;
596                         rep_data[1] = 0x00;
597
598                         ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
599                                                 rep_data, 2, 3);
600
601                         if (ret >= 0) {
602                                 wacom->wacom_wac.bt_high_speed = speed;
603                                 return 0;
604                         }
605                 }
606
607                 /*
608                  * Note that if the raw queries fail, it's not a hard failure
609                  * and it is safe to continue
610                  */
611                 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
612                          rep_data[0], ret);
613                 break;
614         case INTUOS4WL:
615                 if (speed == 1)
616                         wacom->wacom_wac.bt_features &= ~0x20;
617                 else
618                         wacom->wacom_wac.bt_features |= 0x20;
619
620                 rep_data[0] = 0x03;
621                 rep_data[1] = wacom->wacom_wac.bt_features;
622
623                 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
624                                         1);
625                 if (ret >= 0)
626                         wacom->wacom_wac.bt_high_speed = speed;
627                 break;
628         }
629
630         return 0;
631 }
632
633 /*
634  * Switch the tablet into its most-capable mode. Wacom tablets are
635  * typically configured to power-up in a mode which sends mouse-like
636  * reports to the OS. To get absolute position, pressure data, etc.
637  * from the tablet, it is necessary to switch the tablet out of this
638  * mode and into one which sends the full range of tablet data.
639  */
640 static int _wacom_query_tablet_data(struct wacom *wacom)
641 {
642         struct hid_device *hdev = wacom->hdev;
643         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
644         struct wacom_features *features = &wacom_wac->features;
645
646         if (hdev->bus == BUS_BLUETOOTH)
647                 return wacom_bt_query_tablet_data(hdev, 1, features);
648
649         if (features->type != HID_GENERIC) {
650                 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
651                         if (features->type > TABLETPC) {
652                                 /* MT Tablet PC touch */
653                                 wacom_wac->mode_report = 3;
654                                 wacom_wac->mode_value = 4;
655                         } else if (features->type == WACOM_24HDT) {
656                                 wacom_wac->mode_report = 18;
657                                 wacom_wac->mode_value = 2;
658                         } else if (features->type == WACOM_27QHDT) {
659                                 wacom_wac->mode_report = 131;
660                                 wacom_wac->mode_value = 2;
661                         } else if (features->type == BAMBOO_PAD) {
662                                 wacom_wac->mode_report = 2;
663                                 wacom_wac->mode_value = 2;
664                         }
665                 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
666                         if (features->type <= BAMBOO_PT) {
667                                 wacom_wac->mode_report = 2;
668                                 wacom_wac->mode_value = 2;
669                         }
670                 }
671         }
672
673         wacom_set_device_mode(hdev, wacom_wac);
674
675         if (features->type == HID_GENERIC)
676                 return wacom_hid_set_device_mode(hdev);
677
678         return 0;
679 }
680
681 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
682                                          struct wacom_features *features)
683 {
684         struct wacom *wacom = hid_get_drvdata(hdev);
685         struct usb_interface *intf = wacom->intf;
686
687         /* default features */
688         features->x_fuzz = 4;
689         features->y_fuzz = 4;
690         features->pressure_fuzz = 0;
691         features->distance_fuzz = 1;
692         features->tilt_fuzz = 1;
693
694         /*
695          * The wireless device HID is basic and layout conflicts with
696          * other tablets (monitor and touch interface can look like pen).
697          * Skip the query for this type and modify defaults based on
698          * interface number.
699          */
700         if (features->type == WIRELESS && intf) {
701                 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
702                         features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
703                 else
704                         features->device_type = WACOM_DEVICETYPE_NONE;
705                 return;
706         }
707
708         wacom_parse_hid(hdev, features);
709 }
710
711 struct wacom_hdev_data {
712         struct list_head list;
713         struct kref kref;
714         struct hid_device *dev;
715         struct wacom_shared shared;
716 };
717
718 static LIST_HEAD(wacom_udev_list);
719 static DEFINE_MUTEX(wacom_udev_list_lock);
720
721 static bool wacom_are_sibling(struct hid_device *hdev,
722                 struct hid_device *sibling)
723 {
724         struct wacom *wacom = hid_get_drvdata(hdev);
725         struct wacom_features *features = &wacom->wacom_wac.features;
726         struct wacom *sibling_wacom = hid_get_drvdata(sibling);
727         struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
728         __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
729         __u32 oPid = features->oPid ? features->oPid : hdev->product;
730
731         /* The defined oVid/oPid must match that of the sibling */
732         if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
733                 return false;
734         if (features->oPid != HID_ANY_ID && sibling->product != oPid)
735                 return false;
736
737         /*
738          * Devices with the same VID/PID must share the same physical
739          * device path, while those with different VID/PID must share
740          * the same physical parent device path.
741          */
742         if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
743                 if (!hid_compare_device_paths(hdev, sibling, '/'))
744                         return false;
745         } else {
746                 if (!hid_compare_device_paths(hdev, sibling, '.'))
747                         return false;
748         }
749
750         /* Skip the remaining heuristics unless you are a HID_GENERIC device */
751         if (features->type != HID_GENERIC)
752                 return true;
753
754         /*
755          * Direct-input devices may not be siblings of indirect-input
756          * devices.
757          */
758         if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
759             !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
760                 return false;
761
762         /*
763          * Indirect-input devices may not be siblings of direct-input
764          * devices.
765          */
766         if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
767             (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
768                 return false;
769
770         /* Pen devices may only be siblings of touch devices */
771         if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
772             !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
773                 return false;
774
775         /* Touch devices may only be siblings of pen devices */
776         if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
777             !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
778                 return false;
779
780         /*
781          * No reason could be found for these two devices to NOT be
782          * siblings, so there's a good chance they ARE siblings
783          */
784         return true;
785 }
786
787 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
788 {
789         struct wacom_hdev_data *data;
790
791         /* Try to find an already-probed interface from the same device */
792         list_for_each_entry(data, &wacom_udev_list, list) {
793                 if (hid_compare_device_paths(hdev, data->dev, '/')) {
794                         kref_get(&data->kref);
795                         return data;
796                 }
797         }
798
799         /* Fallback to finding devices that appear to be "siblings" */
800         list_for_each_entry(data, &wacom_udev_list, list) {
801                 if (wacom_are_sibling(hdev, data->dev)) {
802                         kref_get(&data->kref);
803                         return data;
804                 }
805         }
806
807         return NULL;
808 }
809
810 static void wacom_release_shared_data(struct kref *kref)
811 {
812         struct wacom_hdev_data *data =
813                 container_of(kref, struct wacom_hdev_data, kref);
814
815         mutex_lock(&wacom_udev_list_lock);
816         list_del(&data->list);
817         mutex_unlock(&wacom_udev_list_lock);
818
819         kfree(data);
820 }
821
822 static void wacom_remove_shared_data(void *res)
823 {
824         struct wacom *wacom = res;
825         struct wacom_hdev_data *data;
826         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
827
828         if (wacom_wac->shared) {
829                 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
830                                     shared);
831
832                 if (wacom_wac->shared->touch == wacom->hdev)
833                         wacom_wac->shared->touch = NULL;
834                 else if (wacom_wac->shared->pen == wacom->hdev)
835                         wacom_wac->shared->pen = NULL;
836
837                 kref_put(&data->kref, wacom_release_shared_data);
838                 wacom_wac->shared = NULL;
839         }
840 }
841
842 static int wacom_add_shared_data(struct hid_device *hdev)
843 {
844         struct wacom *wacom = hid_get_drvdata(hdev);
845         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
846         struct wacom_hdev_data *data;
847         int retval = 0;
848
849         mutex_lock(&wacom_udev_list_lock);
850
851         data = wacom_get_hdev_data(hdev);
852         if (!data) {
853                 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
854                 if (!data) {
855                         retval = -ENOMEM;
856                         goto out;
857                 }
858
859                 kref_init(&data->kref);
860                 data->dev = hdev;
861                 list_add_tail(&data->list, &wacom_udev_list);
862         }
863
864         wacom_wac->shared = &data->shared;
865
866         retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
867         if (retval) {
868                 mutex_unlock(&wacom_udev_list_lock);
869                 wacom_remove_shared_data(wacom);
870                 return retval;
871         }
872
873         if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
874                 wacom_wac->shared->touch = hdev;
875         else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
876                 wacom_wac->shared->pen = hdev;
877
878 out:
879         mutex_unlock(&wacom_udev_list_lock);
880         return retval;
881 }
882
883 static int wacom_led_control(struct wacom *wacom)
884 {
885         unsigned char *buf;
886         int retval;
887         unsigned char report_id = WAC_CMD_LED_CONTROL;
888         int buf_size = 9;
889
890         if (!wacom->led.groups)
891                 return -ENOTSUPP;
892
893         if (wacom->wacom_wac.features.type == REMOTE)
894                 return -ENOTSUPP;
895
896         if (wacom->wacom_wac.pid) { /* wireless connected */
897                 report_id = WAC_CMD_WL_LED_CONTROL;
898                 buf_size = 13;
899         }
900         else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
901                 report_id = WAC_CMD_WL_INTUOSP2;
902                 buf_size = 51;
903         }
904         buf = kzalloc(buf_size, GFP_KERNEL);
905         if (!buf)
906                 return -ENOMEM;
907
908         if (wacom->wacom_wac.features.type == HID_GENERIC) {
909                 buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
910                 buf[1] = wacom->led.llv;
911                 buf[2] = wacom->led.groups[0].select & 0x03;
912
913         } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
914             wacom->wacom_wac.features.type <= INTUOSPL)) {
915                 /*
916                  * Touch Ring and crop mark LED luminance may take on
917                  * one of four values:
918                  *    0 = Low; 1 = Medium; 2 = High; 3 = Off
919                  */
920                 int ring_led = wacom->led.groups[0].select & 0x03;
921                 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
922                 int crop_lum = 0;
923                 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
924
925                 buf[0] = report_id;
926                 if (wacom->wacom_wac.pid) {
927                         wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
928                                          buf, buf_size, WAC_CMD_RETRIES);
929                         buf[0] = report_id;
930                         buf[4] = led_bits;
931                 } else
932                         buf[1] = led_bits;
933         }
934         else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
935                 buf[0] = report_id;
936                 buf[4] = 100; // Power Connection LED (ORANGE)
937                 buf[5] = 100; // BT Connection LED (BLUE)
938                 buf[6] = 100; // Paper Mode (RED?)
939                 buf[7] = 100; // Paper Mode (GREEN?)
940                 buf[8] = 100; // Paper Mode (BLUE?)
941                 buf[9] = wacom->led.llv;
942                 buf[10] = wacom->led.groups[0].select & 0x03;
943         }
944         else {
945                 int led = wacom->led.groups[0].select | 0x4;
946
947                 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
948                     wacom->wacom_wac.features.type == WACOM_24HD)
949                         led |= (wacom->led.groups[1].select << 4) | 0x40;
950
951                 buf[0] = report_id;
952                 buf[1] = led;
953                 buf[2] = wacom->led.llv;
954                 buf[3] = wacom->led.hlv;
955                 buf[4] = wacom->led.img_lum;
956         }
957
958         retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
959                                   WAC_CMD_RETRIES);
960         kfree(buf);
961
962         return retval;
963 }
964
965 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
966                 const unsigned len, const void *img)
967 {
968         unsigned char *buf;
969         int i, retval;
970         const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
971
972         buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
973         if (!buf)
974                 return -ENOMEM;
975
976         /* Send 'start' command */
977         buf[0] = WAC_CMD_ICON_START;
978         buf[1] = 1;
979         retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
980                                   WAC_CMD_RETRIES);
981         if (retval < 0)
982                 goto out;
983
984         buf[0] = xfer_id;
985         buf[1] = button_id & 0x07;
986         for (i = 0; i < 4; i++) {
987                 buf[2] = i;
988                 memcpy(buf + 3, img + i * chunk_len, chunk_len);
989
990                 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
991                                           buf, chunk_len + 3, WAC_CMD_RETRIES);
992                 if (retval < 0)
993                         break;
994         }
995
996         /* Send 'stop' */
997         buf[0] = WAC_CMD_ICON_START;
998         buf[1] = 0;
999         wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1000                          WAC_CMD_RETRIES);
1001
1002 out:
1003         kfree(buf);
1004         return retval;
1005 }
1006
1007 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
1008                                       const char *buf, size_t count)
1009 {
1010         struct hid_device *hdev = to_hid_device(dev);
1011         struct wacom *wacom = hid_get_drvdata(hdev);
1012         unsigned int id;
1013         int err;
1014
1015         err = kstrtouint(buf, 10, &id);
1016         if (err)
1017                 return err;
1018
1019         mutex_lock(&wacom->lock);
1020
1021         wacom->led.groups[set_id].select = id & 0x3;
1022         err = wacom_led_control(wacom);
1023
1024         mutex_unlock(&wacom->lock);
1025
1026         return err < 0 ? err : count;
1027 }
1028
1029 #define DEVICE_LED_SELECT_ATTR(SET_ID)                                  \
1030 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,     \
1031         struct device_attribute *attr, const char *buf, size_t count)   \
1032 {                                                                       \
1033         return wacom_led_select_store(dev, SET_ID, buf, count);         \
1034 }                                                                       \
1035 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,      \
1036         struct device_attribute *attr, char *buf)                       \
1037 {                                                                       \
1038         struct hid_device *hdev = to_hid_device(dev);\
1039         struct wacom *wacom = hid_get_drvdata(hdev);                    \
1040         return scnprintf(buf, PAGE_SIZE, "%d\n",                        \
1041                          wacom->led.groups[SET_ID].select);             \
1042 }                                                                       \
1043 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,       \
1044                     wacom_led##SET_ID##_select_show,                    \
1045                     wacom_led##SET_ID##_select_store)
1046
1047 DEVICE_LED_SELECT_ATTR(0);
1048 DEVICE_LED_SELECT_ATTR(1);
1049
1050 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
1051                                      const char *buf, size_t count)
1052 {
1053         unsigned int value;
1054         int err;
1055
1056         err = kstrtouint(buf, 10, &value);
1057         if (err)
1058                 return err;
1059
1060         mutex_lock(&wacom->lock);
1061
1062         *dest = value & 0x7f;
1063         err = wacom_led_control(wacom);
1064
1065         mutex_unlock(&wacom->lock);
1066
1067         return err < 0 ? err : count;
1068 }
1069
1070 #define DEVICE_LUMINANCE_ATTR(name, field)                              \
1071 static ssize_t wacom_##name##_luminance_store(struct device *dev,       \
1072         struct device_attribute *attr, const char *buf, size_t count)   \
1073 {                                                                       \
1074         struct hid_device *hdev = to_hid_device(dev);\
1075         struct wacom *wacom = hid_get_drvdata(hdev);                    \
1076                                                                         \
1077         return wacom_luminance_store(wacom, &wacom->led.field,          \
1078                                      buf, count);                       \
1079 }                                                                       \
1080 static ssize_t wacom_##name##_luminance_show(struct device *dev,        \
1081         struct device_attribute *attr, char *buf)                       \
1082 {                                                                       \
1083         struct wacom *wacom = dev_get_drvdata(dev);                     \
1084         return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);     \
1085 }                                                                       \
1086 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,                  \
1087                    wacom_##name##_luminance_show,                       \
1088                    wacom_##name##_luminance_store)
1089
1090 DEVICE_LUMINANCE_ATTR(status0, llv);
1091 DEVICE_LUMINANCE_ATTR(status1, hlv);
1092 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
1093
1094 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
1095                                         const char *buf, size_t count)
1096 {
1097         struct hid_device *hdev = to_hid_device(dev);
1098         struct wacom *wacom = hid_get_drvdata(hdev);
1099         int err;
1100         unsigned len;
1101         u8 xfer_id;
1102
1103         if (hdev->bus == BUS_BLUETOOTH) {
1104                 len = 256;
1105                 xfer_id = WAC_CMD_ICON_BT_XFER;
1106         } else {
1107                 len = 1024;
1108                 xfer_id = WAC_CMD_ICON_XFER;
1109         }
1110
1111         if (count != len)
1112                 return -EINVAL;
1113
1114         mutex_lock(&wacom->lock);
1115
1116         err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
1117
1118         mutex_unlock(&wacom->lock);
1119
1120         return err < 0 ? err : count;
1121 }
1122
1123 #define DEVICE_BTNIMG_ATTR(BUTTON_ID)                                   \
1124 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,      \
1125         struct device_attribute *attr, const char *buf, size_t count)   \
1126 {                                                                       \
1127         return wacom_button_image_store(dev, BUTTON_ID, buf, count);    \
1128 }                                                                       \
1129 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,        \
1130                    NULL, wacom_btnimg##BUTTON_ID##_store)
1131
1132 DEVICE_BTNIMG_ATTR(0);
1133 DEVICE_BTNIMG_ATTR(1);
1134 DEVICE_BTNIMG_ATTR(2);
1135 DEVICE_BTNIMG_ATTR(3);
1136 DEVICE_BTNIMG_ATTR(4);
1137 DEVICE_BTNIMG_ATTR(5);
1138 DEVICE_BTNIMG_ATTR(6);
1139 DEVICE_BTNIMG_ATTR(7);
1140
1141 static struct attribute *cintiq_led_attrs[] = {
1142         &dev_attr_status_led0_select.attr,
1143         &dev_attr_status_led1_select.attr,
1144         NULL
1145 };
1146
1147 static struct attribute_group cintiq_led_attr_group = {
1148         .name = "wacom_led",
1149         .attrs = cintiq_led_attrs,
1150 };
1151
1152 static struct attribute *intuos4_led_attrs[] = {
1153         &dev_attr_status0_luminance.attr,
1154         &dev_attr_status1_luminance.attr,
1155         &dev_attr_status_led0_select.attr,
1156         &dev_attr_buttons_luminance.attr,
1157         &dev_attr_button0_rawimg.attr,
1158         &dev_attr_button1_rawimg.attr,
1159         &dev_attr_button2_rawimg.attr,
1160         &dev_attr_button3_rawimg.attr,
1161         &dev_attr_button4_rawimg.attr,
1162         &dev_attr_button5_rawimg.attr,
1163         &dev_attr_button6_rawimg.attr,
1164         &dev_attr_button7_rawimg.attr,
1165         NULL
1166 };
1167
1168 static struct attribute_group intuos4_led_attr_group = {
1169         .name = "wacom_led",
1170         .attrs = intuos4_led_attrs,
1171 };
1172
1173 static struct attribute *intuos5_led_attrs[] = {
1174         &dev_attr_status0_luminance.attr,
1175         &dev_attr_status_led0_select.attr,
1176         NULL
1177 };
1178
1179 static struct attribute_group intuos5_led_attr_group = {
1180         .name = "wacom_led",
1181         .attrs = intuos5_led_attrs,
1182 };
1183
1184 static struct attribute *generic_led_attrs[] = {
1185         &dev_attr_status0_luminance.attr,
1186         &dev_attr_status_led0_select.attr,
1187         NULL
1188 };
1189
1190 static struct attribute_group generic_led_attr_group = {
1191         .name = "wacom_led",
1192         .attrs = generic_led_attrs,
1193 };
1194
1195 struct wacom_sysfs_group_devres {
1196         struct attribute_group *group;
1197         struct kobject *root;
1198 };
1199
1200 static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1201 {
1202         struct wacom_sysfs_group_devres *devres = res;
1203         struct kobject *kobj = devres->root;
1204
1205         dev_dbg(dev, "%s: dropping reference to %s\n",
1206                 __func__, devres->group->name);
1207         sysfs_remove_group(kobj, devres->group);
1208 }
1209
1210 static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1211                                            struct kobject *root,
1212                                            struct attribute_group *group)
1213 {
1214         struct wacom_sysfs_group_devres *devres;
1215         int error;
1216
1217         devres = devres_alloc(wacom_devm_sysfs_group_release,
1218                               sizeof(struct wacom_sysfs_group_devres),
1219                               GFP_KERNEL);
1220         if (!devres)
1221                 return -ENOMEM;
1222
1223         devres->group = group;
1224         devres->root = root;
1225
1226         error = sysfs_create_group(devres->root, group);
1227         if (error) {
1228                 devres_free(devres);
1229                 return error;
1230         }
1231
1232         devres_add(&wacom->hdev->dev, devres);
1233
1234         return 0;
1235 }
1236
1237 static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1238                                          struct attribute_group *group)
1239 {
1240         return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1241                                                group);
1242 }
1243
1244 static void wacom_devm_kfifo_release(struct device *dev, void *res)
1245 {
1246         struct kfifo_rec_ptr_2 *devres = res;
1247
1248         kfifo_free(devres);
1249 }
1250
1251 static int wacom_devm_kfifo_alloc(struct wacom *wacom)
1252 {
1253         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1254         struct kfifo_rec_ptr_2 *pen_fifo;
1255         int error;
1256
1257         pen_fifo = devres_alloc(wacom_devm_kfifo_release,
1258                               sizeof(struct kfifo_rec_ptr_2),
1259                               GFP_KERNEL);
1260
1261         if (!pen_fifo)
1262                 return -ENOMEM;
1263
1264         error = kfifo_alloc(pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
1265         if (error) {
1266                 devres_free(pen_fifo);
1267                 return error;
1268         }
1269
1270         devres_add(&wacom->hdev->dev, pen_fifo);
1271         wacom_wac->pen_fifo = pen_fifo;
1272
1273         return 0;
1274 }
1275
1276 enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
1277 {
1278         struct wacom *wacom = led->wacom;
1279
1280         if (wacom->led.max_hlv)
1281                 return led->hlv * LED_FULL / wacom->led.max_hlv;
1282
1283         if (wacom->led.max_llv)
1284                 return led->llv * LED_FULL / wacom->led.max_llv;
1285
1286         /* device doesn't support brightness tuning */
1287         return LED_FULL;
1288 }
1289
1290 static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1291 {
1292         struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1293         struct wacom *wacom = led->wacom;
1294
1295         if (wacom->led.groups[led->group].select != led->id)
1296                 return LED_OFF;
1297
1298         return wacom_leds_brightness_get(led);
1299 }
1300
1301 static int wacom_led_brightness_set(struct led_classdev *cdev,
1302                                     enum led_brightness brightness)
1303 {
1304         struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1305         struct wacom *wacom = led->wacom;
1306         int error;
1307
1308         mutex_lock(&wacom->lock);
1309
1310         if (!wacom->led.groups || (brightness == LED_OFF &&
1311             wacom->led.groups[led->group].select != led->id)) {
1312                 error = 0;
1313                 goto out;
1314         }
1315
1316         led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1317         led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1318
1319         wacom->led.groups[led->group].select = led->id;
1320
1321         error = wacom_led_control(wacom);
1322
1323 out:
1324         mutex_unlock(&wacom->lock);
1325
1326         return error;
1327 }
1328
1329 static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1330                                                enum led_brightness brightness)
1331 {
1332 }
1333
1334 static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1335                                   struct wacom_led *led, unsigned int group,
1336                                   unsigned int id, bool read_only)
1337 {
1338         int error;
1339         char *name;
1340
1341         name = devm_kasprintf(dev, GFP_KERNEL,
1342                               "%s::wacom-%d.%d",
1343                               dev_name(dev),
1344                               group,
1345                               id);
1346         if (!name)
1347                 return -ENOMEM;
1348
1349         if (!read_only) {
1350                 led->trigger.name = name;
1351                 error = devm_led_trigger_register(dev, &led->trigger);
1352                 if (error) {
1353                         hid_err(wacom->hdev,
1354                                 "failed to register LED trigger %s: %d\n",
1355                                 led->cdev.name, error);
1356                         return error;
1357                 }
1358         }
1359
1360         led->group = group;
1361         led->id = id;
1362         led->wacom = wacom;
1363         led->llv = wacom->led.llv;
1364         led->hlv = wacom->led.hlv;
1365         led->cdev.name = name;
1366         led->cdev.max_brightness = LED_FULL;
1367         led->cdev.flags = LED_HW_PLUGGABLE;
1368         led->cdev.brightness_get = __wacom_led_brightness_get;
1369         if (!read_only) {
1370                 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
1371                 led->cdev.default_trigger = led->cdev.name;
1372         } else {
1373                 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
1374         }
1375
1376         error = devm_led_classdev_register(dev, &led->cdev);
1377         if (error) {
1378                 hid_err(wacom->hdev,
1379                         "failed to register LED %s: %d\n",
1380                         led->cdev.name, error);
1381                 led->cdev.name = NULL;
1382                 return error;
1383         }
1384
1385         return 0;
1386 }
1387
1388 static void wacom_led_groups_release_one(void *data)
1389 {
1390         struct wacom_group_leds *group = data;
1391
1392         devres_release_group(group->dev, group);
1393 }
1394
1395 static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1396                                                    struct wacom *wacom,
1397                                                    int group_id, int count,
1398                                                    bool read_only)
1399 {
1400         struct wacom_led *leds;
1401         int i, error;
1402
1403         if (group_id >= wacom->led.count || count <= 0)
1404                 return -EINVAL;
1405
1406         if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1407                 return -ENOMEM;
1408
1409         leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL);
1410         if (!leds) {
1411                 error = -ENOMEM;
1412                 goto err;
1413         }
1414
1415         wacom->led.groups[group_id].leds = leds;
1416         wacom->led.groups[group_id].count = count;
1417
1418         for (i = 0; i < count; i++) {
1419                 error = wacom_led_register_one(dev, wacom, &leds[i],
1420                                                group_id, i, read_only);
1421                 if (error)
1422                         goto err;
1423         }
1424
1425         wacom->led.groups[group_id].dev = dev;
1426
1427         devres_close_group(dev, &wacom->led.groups[group_id]);
1428
1429         /*
1430          * There is a bug (?) in devm_led_classdev_register() in which its
1431          * increments the refcount of the parent. If the parent is an input
1432          * device, that means the ref count never reaches 0 when
1433          * devm_input_device_release() gets called.
1434          * This means that the LEDs are still there after disconnect.
1435          * Manually force the release of the group so that the leds are released
1436          * once we are done using them.
1437          */
1438         error = devm_add_action_or_reset(&wacom->hdev->dev,
1439                                          wacom_led_groups_release_one,
1440                                          &wacom->led.groups[group_id]);
1441         if (error)
1442                 return error;
1443
1444         return 0;
1445
1446 err:
1447         devres_release_group(dev, &wacom->led.groups[group_id]);
1448         return error;
1449 }
1450
1451 struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1452                                  unsigned int id)
1453 {
1454         struct wacom_group_leds *group;
1455
1456         if (group_id >= wacom->led.count)
1457                 return NULL;
1458
1459         group = &wacom->led.groups[group_id];
1460
1461         if (!group->leds)
1462                 return NULL;
1463
1464         id %= group->count;
1465
1466         return &group->leds[id];
1467 }
1468
1469 /**
1470  * wacom_led_next: gives the next available led with a wacom trigger.
1471  *
1472  * returns the next available struct wacom_led which has its default trigger
1473  * or the current one if none is available.
1474  */
1475 struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1476 {
1477         struct wacom_led *next_led;
1478         int group, next;
1479
1480         if (!wacom || !cur)
1481                 return NULL;
1482
1483         group = cur->group;
1484         next = cur->id;
1485
1486         do {
1487                 next_led = wacom_led_find(wacom, group, ++next);
1488                 if (!next_led || next_led == cur)
1489                         return next_led;
1490         } while (next_led->cdev.trigger != &next_led->trigger);
1491
1492         return next_led;
1493 }
1494
1495 static void wacom_led_groups_release(void *data)
1496 {
1497         struct wacom *wacom = data;
1498
1499         wacom->led.groups = NULL;
1500         wacom->led.count = 0;
1501 }
1502
1503 static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1504 {
1505         struct device *dev = &wacom->hdev->dev;
1506         struct wacom_group_leds *groups;
1507         int error;
1508
1509         groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds),
1510                               GFP_KERNEL);
1511         if (!groups)
1512                 return -ENOMEM;
1513
1514         error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1515         if (error)
1516                 return error;
1517
1518         wacom->led.groups = groups;
1519         wacom->led.count = count;
1520
1521         return 0;
1522 }
1523
1524 static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1525                                          int led_per_group, bool read_only)
1526 {
1527         struct device *dev;
1528         int i, error;
1529
1530         if (!wacom->wacom_wac.pad_input)
1531                 return -EINVAL;
1532
1533         dev = &wacom->wacom_wac.pad_input->dev;
1534
1535         error = wacom_led_groups_allocate(wacom, group_count);
1536         if (error)
1537                 return error;
1538
1539         for (i = 0; i < group_count; i++) {
1540                 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1541                                                                 led_per_group,
1542                                                                 read_only);
1543                 if (error)
1544                         return error;
1545         }
1546
1547         return 0;
1548 }
1549
1550 int wacom_initialize_leds(struct wacom *wacom)
1551 {
1552         int error;
1553
1554         if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1555                 return 0;
1556
1557         /* Initialize default values */
1558         switch (wacom->wacom_wac.features.type) {
1559         case HID_GENERIC:
1560                 if (!wacom->generic_has_leds)
1561                         return 0;
1562                 wacom->led.llv = 100;
1563                 wacom->led.max_llv = 100;
1564
1565                 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1566                 if (error) {
1567                         hid_err(wacom->hdev,
1568                                 "cannot create leds err: %d\n", error);
1569                         return error;
1570                 }
1571
1572                 error = wacom_devm_sysfs_create_group(wacom,
1573                                                       &generic_led_attr_group);
1574                 break;
1575
1576         case INTUOS4S:
1577         case INTUOS4:
1578         case INTUOS4WL:
1579         case INTUOS4L:
1580                 wacom->led.llv = 10;
1581                 wacom->led.hlv = 20;
1582                 wacom->led.max_llv = 127;
1583                 wacom->led.max_hlv = 127;
1584                 wacom->led.img_lum = 10;
1585
1586                 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1587                 if (error) {
1588                         hid_err(wacom->hdev,
1589                                 "cannot create leds err: %d\n", error);
1590                         return error;
1591                 }
1592
1593                 error = wacom_devm_sysfs_create_group(wacom,
1594                                                       &intuos4_led_attr_group);
1595                 break;
1596
1597         case WACOM_24HD:
1598         case WACOM_21UX2:
1599                 wacom->led.llv = 0;
1600                 wacom->led.hlv = 0;
1601                 wacom->led.img_lum = 0;
1602
1603                 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1604                 if (error) {
1605                         hid_err(wacom->hdev,
1606                                 "cannot create leds err: %d\n", error);
1607                         return error;
1608                 }
1609
1610                 error = wacom_devm_sysfs_create_group(wacom,
1611                                                       &cintiq_led_attr_group);
1612                 break;
1613
1614         case INTUOS5S:
1615         case INTUOS5:
1616         case INTUOS5L:
1617         case INTUOSPS:
1618         case INTUOSPM:
1619         case INTUOSPL:
1620                 wacom->led.llv = 32;
1621                 wacom->led.max_llv = 96;
1622
1623                 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1624                 if (error) {
1625                         hid_err(wacom->hdev,
1626                                 "cannot create leds err: %d\n", error);
1627                         return error;
1628                 }
1629
1630                 error = wacom_devm_sysfs_create_group(wacom,
1631                                                       &intuos5_led_attr_group);
1632                 break;
1633
1634         case INTUOSP2_BT:
1635                 wacom->led.llv = 50;
1636                 wacom->led.max_llv = 100;
1637                 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1638                 if (error) {
1639                         hid_err(wacom->hdev,
1640                                 "cannot create leds err: %d\n", error);
1641                         return error;
1642                 }
1643                 return 0;
1644
1645         case REMOTE:
1646                 wacom->led.llv = 255;
1647                 wacom->led.max_llv = 255;
1648                 error = wacom_led_groups_allocate(wacom, 5);
1649                 if (error) {
1650                         hid_err(wacom->hdev,
1651                                 "cannot create leds err: %d\n", error);
1652                         return error;
1653                 }
1654                 return 0;
1655
1656         default:
1657                 return 0;
1658         }
1659
1660         if (error) {
1661                 hid_err(wacom->hdev,
1662                         "cannot create sysfs group err: %d\n", error);
1663                 return error;
1664         }
1665
1666         return 0;
1667 }
1668
1669 static void wacom_init_work(struct work_struct *work)
1670 {
1671         struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1672
1673         _wacom_query_tablet_data(wacom);
1674         wacom_led_control(wacom);
1675 }
1676
1677 static void wacom_query_tablet_data(struct wacom *wacom)
1678 {
1679         schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1680 }
1681
1682 static enum power_supply_property wacom_battery_props[] = {
1683         POWER_SUPPLY_PROP_MODEL_NAME,
1684         POWER_SUPPLY_PROP_PRESENT,
1685         POWER_SUPPLY_PROP_STATUS,
1686         POWER_SUPPLY_PROP_SCOPE,
1687         POWER_SUPPLY_PROP_CAPACITY
1688 };
1689
1690 static int wacom_battery_get_property(struct power_supply *psy,
1691                                       enum power_supply_property psp,
1692                                       union power_supply_propval *val)
1693 {
1694         struct wacom_battery *battery = power_supply_get_drvdata(psy);
1695         int ret = 0;
1696
1697         switch (psp) {
1698                 case POWER_SUPPLY_PROP_MODEL_NAME:
1699                         val->strval = battery->wacom->wacom_wac.name;
1700                         break;
1701                 case POWER_SUPPLY_PROP_PRESENT:
1702                         val->intval = battery->bat_connected;
1703                         break;
1704                 case POWER_SUPPLY_PROP_SCOPE:
1705                         val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1706                         break;
1707                 case POWER_SUPPLY_PROP_CAPACITY:
1708                         val->intval = battery->battery_capacity;
1709                         break;
1710                 case POWER_SUPPLY_PROP_STATUS:
1711                         if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1712                                 val->intval = battery->bat_status;
1713                         else if (battery->bat_charging)
1714                                 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1715                         else if (battery->battery_capacity == 100 &&
1716                                     battery->ps_connected)
1717                                 val->intval = POWER_SUPPLY_STATUS_FULL;
1718                         else if (battery->ps_connected)
1719                                 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1720                         else
1721                                 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1722                         break;
1723                 default:
1724                         ret = -EINVAL;
1725                         break;
1726         }
1727
1728         return ret;
1729 }
1730
1731 static int __wacom_initialize_battery(struct wacom *wacom,
1732                                       struct wacom_battery *battery)
1733 {
1734         static atomic_t battery_no = ATOMIC_INIT(0);
1735         struct device *dev = &wacom->hdev->dev;
1736         struct power_supply_config psy_cfg = { .drv_data = battery, };
1737         struct power_supply *ps_bat;
1738         struct power_supply_desc *bat_desc = &battery->bat_desc;
1739         unsigned long n;
1740         int error;
1741
1742         if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1743                 return -ENOMEM;
1744
1745         battery->wacom = wacom;
1746
1747         n = atomic_inc_return(&battery_no) - 1;
1748
1749         bat_desc->properties = wacom_battery_props;
1750         bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1751         bat_desc->get_property = wacom_battery_get_property;
1752         sprintf(battery->bat_name, "wacom_battery_%ld", n);
1753         bat_desc->name = battery->bat_name;
1754         bat_desc->type = POWER_SUPPLY_TYPE_USB;
1755         bat_desc->use_for_apm = 0;
1756
1757         ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1758         if (IS_ERR(ps_bat)) {
1759                 error = PTR_ERR(ps_bat);
1760                 goto err;
1761         }
1762
1763         power_supply_powers(ps_bat, &wacom->hdev->dev);
1764
1765         battery->battery = ps_bat;
1766
1767         devres_close_group(dev, bat_desc);
1768         return 0;
1769
1770 err:
1771         devres_release_group(dev, bat_desc);
1772         return error;
1773 }
1774
1775 static int wacom_initialize_battery(struct wacom *wacom)
1776 {
1777         if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1778                 return __wacom_initialize_battery(wacom, &wacom->battery);
1779
1780         return 0;
1781 }
1782
1783 static void wacom_destroy_battery(struct wacom *wacom)
1784 {
1785         if (wacom->battery.battery) {
1786                 devres_release_group(&wacom->hdev->dev,
1787                                      &wacom->battery.bat_desc);
1788                 wacom->battery.battery = NULL;
1789         }
1790 }
1791
1792 static ssize_t wacom_show_speed(struct device *dev,
1793                                 struct device_attribute
1794                                 *attr, char *buf)
1795 {
1796         struct hid_device *hdev = to_hid_device(dev);
1797         struct wacom *wacom = hid_get_drvdata(hdev);
1798
1799         return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1800 }
1801
1802 static ssize_t wacom_store_speed(struct device *dev,
1803                                 struct device_attribute *attr,
1804                                 const char *buf, size_t count)
1805 {
1806         struct hid_device *hdev = to_hid_device(dev);
1807         struct wacom *wacom = hid_get_drvdata(hdev);
1808         u8 new_speed;
1809
1810         if (kstrtou8(buf, 0, &new_speed))
1811                 return -EINVAL;
1812
1813         if (new_speed != 0 && new_speed != 1)
1814                 return -EINVAL;
1815
1816         wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1817
1818         return count;
1819 }
1820
1821 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1822                 wacom_show_speed, wacom_store_speed);
1823
1824
1825 static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1826                                       struct kobj_attribute *kattr,
1827                                       char *buf, int index)
1828 {
1829         struct device *dev = kobj_to_dev(kobj->parent);
1830         struct hid_device *hdev = to_hid_device(dev);
1831         struct wacom *wacom = hid_get_drvdata(hdev);
1832         u8 mode;
1833
1834         mode = wacom->led.groups[index].select;
1835         return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
1836 }
1837
1838 #define DEVICE_EKR_ATTR_GROUP(SET_ID)                                   \
1839 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj,   \
1840                                struct kobj_attribute *kattr, char *buf) \
1841 {                                                                       \
1842         return wacom_show_remote_mode(kobj, kattr, buf, SET_ID);        \
1843 }                                                                       \
1844 static struct kobj_attribute remote##SET_ID##_mode_attr = {             \
1845         .attr = {.name = "remote_mode",                                 \
1846                 .mode = DEV_ATTR_RO_PERM},                              \
1847         .show = wacom_show_remote##SET_ID##_mode,                       \
1848 };                                                                      \
1849 static struct attribute *remote##SET_ID##_serial_attrs[] = {            \
1850         &remote##SET_ID##_mode_attr.attr,                               \
1851         NULL                                                            \
1852 };                                                                      \
1853 static struct attribute_group remote##SET_ID##_serial_group = {         \
1854         .name = NULL,                                                   \
1855         .attrs = remote##SET_ID##_serial_attrs,                         \
1856 }
1857
1858 DEVICE_EKR_ATTR_GROUP(0);
1859 DEVICE_EKR_ATTR_GROUP(1);
1860 DEVICE_EKR_ATTR_GROUP(2);
1861 DEVICE_EKR_ATTR_GROUP(3);
1862 DEVICE_EKR_ATTR_GROUP(4);
1863
1864 static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1865                                           int index)
1866 {
1867         int error = 0;
1868         struct wacom_remote *remote = wacom->remote;
1869
1870         remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1871                                                           GFP_KERNEL,
1872                                                           "%d", serial);
1873         if (!remote->remotes[index].group.name)
1874                 return -ENOMEM;
1875
1876         error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1877                                                 &remote->remotes[index].group);
1878         if (error) {
1879                 remote->remotes[index].group.name = NULL;
1880                 hid_err(wacom->hdev,
1881                         "cannot create sysfs group err: %d\n", error);
1882                 return error;
1883         }
1884
1885         return 0;
1886 }
1887
1888 static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1889 {
1890         const size_t buf_size = 2;
1891         unsigned char *buf;
1892         int retval;
1893
1894         buf = kzalloc(buf_size, GFP_KERNEL);
1895         if (!buf)
1896                 return -ENOMEM;
1897
1898         buf[0] = WAC_CMD_DELETE_PAIRING;
1899         buf[1] = selector;
1900
1901         retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1902                                   buf_size, WAC_CMD_RETRIES);
1903         kfree(buf);
1904
1905         return retval;
1906 }
1907
1908 static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1909                                          struct kobj_attribute *attr,
1910                                          const char *buf, size_t count)
1911 {
1912         unsigned char selector = 0;
1913         struct device *dev = kobj_to_dev(kobj->parent);
1914         struct hid_device *hdev = to_hid_device(dev);
1915         struct wacom *wacom = hid_get_drvdata(hdev);
1916         int err;
1917
1918         if (!strncmp(buf, "*\n", 2)) {
1919                 selector = WAC_CMD_UNPAIR_ALL;
1920         } else {
1921                 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1922                          buf);
1923                 return -1;
1924         }
1925
1926         mutex_lock(&wacom->lock);
1927
1928         err = wacom_cmd_unpair_remote(wacom, selector);
1929         mutex_unlock(&wacom->lock);
1930
1931         return err < 0 ? err : count;
1932 }
1933
1934 static struct kobj_attribute unpair_remote_attr = {
1935         .attr = {.name = "unpair_remote", .mode = 0200},
1936         .store = wacom_store_unpair_remote,
1937 };
1938
1939 static const struct attribute *remote_unpair_attrs[] = {
1940         &unpair_remote_attr.attr,
1941         NULL
1942 };
1943
1944 static void wacom_remotes_destroy(void *data)
1945 {
1946         struct wacom *wacom = data;
1947         struct wacom_remote *remote = wacom->remote;
1948
1949         if (!remote)
1950                 return;
1951
1952         kobject_put(remote->remote_dir);
1953         kfifo_free(&remote->remote_fifo);
1954         wacom->remote = NULL;
1955 }
1956
1957 static int wacom_initialize_remotes(struct wacom *wacom)
1958 {
1959         int error = 0;
1960         struct wacom_remote *remote;
1961         int i;
1962
1963         if (wacom->wacom_wac.features.type != REMOTE)
1964                 return 0;
1965
1966         remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1967                               GFP_KERNEL);
1968         if (!remote)
1969                 return -ENOMEM;
1970
1971         wacom->remote = remote;
1972
1973         spin_lock_init(&remote->remote_lock);
1974
1975         error = kfifo_alloc(&remote->remote_fifo,
1976                         5 * sizeof(struct wacom_remote_data),
1977                         GFP_KERNEL);
1978         if (error) {
1979                 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1980                 return -ENOMEM;
1981         }
1982
1983         remote->remotes[0].group = remote0_serial_group;
1984         remote->remotes[1].group = remote1_serial_group;
1985         remote->remotes[2].group = remote2_serial_group;
1986         remote->remotes[3].group = remote3_serial_group;
1987         remote->remotes[4].group = remote4_serial_group;
1988
1989         remote->remote_dir = kobject_create_and_add("wacom_remote",
1990                                                     &wacom->hdev->dev.kobj);
1991         if (!remote->remote_dir)
1992                 return -ENOMEM;
1993
1994         error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
1995
1996         if (error) {
1997                 hid_err(wacom->hdev,
1998                         "cannot create sysfs group err: %d\n", error);
1999                 return error;
2000         }
2001
2002         for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2003                 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2004                 remote->remotes[i].serial = 0;
2005         }
2006
2007         error = devm_add_action_or_reset(&wacom->hdev->dev,
2008                                          wacom_remotes_destroy, wacom);
2009         if (error)
2010                 return error;
2011
2012         return 0;
2013 }
2014
2015 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
2016 {
2017         struct input_dev *input_dev;
2018         struct hid_device *hdev = wacom->hdev;
2019         struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2020
2021         input_dev = devm_input_allocate_device(&hdev->dev);
2022         if (!input_dev)
2023                 return NULL;
2024
2025         input_dev->name = wacom_wac->features.name;
2026         input_dev->phys = hdev->phys;
2027         input_dev->dev.parent = &hdev->dev;
2028         input_dev->open = wacom_open;
2029         input_dev->close = wacom_close;
2030         input_dev->uniq = hdev->uniq;
2031         input_dev->id.bustype = hdev->bus;
2032         input_dev->id.vendor  = hdev->vendor;
2033         input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
2034         input_dev->id.version = hdev->version;
2035         input_set_drvdata(input_dev, wacom);
2036
2037         return input_dev;
2038 }
2039
2040 static int wacom_allocate_inputs(struct wacom *wacom)
2041 {
2042         struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2043
2044         wacom_wac->pen_input = wacom_allocate_input(wacom);
2045         wacom_wac->touch_input = wacom_allocate_input(wacom);
2046         wacom_wac->pad_input = wacom_allocate_input(wacom);
2047         if (!wacom_wac->pen_input ||
2048             !wacom_wac->touch_input ||
2049             !wacom_wac->pad_input)
2050                 return -ENOMEM;
2051
2052         wacom_wac->pen_input->name = wacom_wac->pen_name;
2053         wacom_wac->touch_input->name = wacom_wac->touch_name;
2054         wacom_wac->pad_input->name = wacom_wac->pad_name;
2055
2056         return 0;
2057 }
2058
2059 static int wacom_register_inputs(struct wacom *wacom)
2060 {
2061         struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2062         struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2063         int error = 0;
2064
2065         pen_input_dev = wacom_wac->pen_input;
2066         touch_input_dev = wacom_wac->touch_input;
2067         pad_input_dev = wacom_wac->pad_input;
2068
2069         if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
2070                 return -EINVAL;
2071
2072         error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
2073         if (error) {
2074                 /* no pen in use on this interface */
2075                 input_free_device(pen_input_dev);
2076                 wacom_wac->pen_input = NULL;
2077                 pen_input_dev = NULL;
2078         } else {
2079                 error = input_register_device(pen_input_dev);
2080                 if (error)
2081                         goto fail;
2082         }
2083
2084         error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
2085         if (error) {
2086                 /* no touch in use on this interface */
2087                 input_free_device(touch_input_dev);
2088                 wacom_wac->touch_input = NULL;
2089                 touch_input_dev = NULL;
2090         } else {
2091                 error = input_register_device(touch_input_dev);
2092                 if (error)
2093                         goto fail;
2094         }
2095
2096         error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2097         if (error) {
2098                 /* no pad in use on this interface */
2099                 input_free_device(pad_input_dev);
2100                 wacom_wac->pad_input = NULL;
2101                 pad_input_dev = NULL;
2102         } else {
2103                 error = input_register_device(pad_input_dev);
2104                 if (error)
2105                         goto fail;
2106         }
2107
2108         return 0;
2109
2110 fail:
2111         wacom_wac->pad_input = NULL;
2112         wacom_wac->touch_input = NULL;
2113         wacom_wac->pen_input = NULL;
2114         return error;
2115 }
2116
2117 /*
2118  * Not all devices report physical dimensions from HID.
2119  * Compute the default from hardcoded logical dimension
2120  * and resolution before driver overwrites them.
2121  */
2122 static void wacom_set_default_phy(struct wacom_features *features)
2123 {
2124         if (features->x_resolution) {
2125                 features->x_phy = (features->x_max * 100) /
2126                                         features->x_resolution;
2127                 features->y_phy = (features->y_max * 100) /
2128                                         features->y_resolution;
2129         }
2130 }
2131
2132 static void wacom_calculate_res(struct wacom_features *features)
2133 {
2134         /* set unit to "100th of a mm" for devices not reported by HID */
2135         if (!features->unit) {
2136                 features->unit = 0x11;
2137                 features->unitExpo = -3;
2138         }
2139
2140         features->x_resolution = wacom_calc_hid_res(features->x_max,
2141                                                     features->x_phy,
2142                                                     features->unit,
2143                                                     features->unitExpo);
2144         features->y_resolution = wacom_calc_hid_res(features->y_max,
2145                                                     features->y_phy,
2146                                                     features->unit,
2147                                                     features->unitExpo);
2148 }
2149
2150 void wacom_battery_work(struct work_struct *work)
2151 {
2152         struct wacom *wacom = container_of(work, struct wacom, battery_work);
2153
2154         if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2155              !wacom->battery.battery) {
2156                 wacom_initialize_battery(wacom);
2157         }
2158         else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2159                  wacom->battery.battery) {
2160                 wacom_destroy_battery(wacom);
2161         }
2162 }
2163
2164 static size_t wacom_compute_pktlen(struct hid_device *hdev)
2165 {
2166         struct hid_report_enum *report_enum;
2167         struct hid_report *report;
2168         size_t size = 0;
2169
2170         report_enum = hdev->report_enum + HID_INPUT_REPORT;
2171
2172         list_for_each_entry(report, &report_enum->report_list, list) {
2173                 size_t report_size = hid_report_len(report);
2174                 if (report_size > size)
2175                         size = report_size;
2176         }
2177
2178         return size;
2179 }
2180
2181 static void wacom_update_name(struct wacom *wacom, const char *suffix)
2182 {
2183         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2184         struct wacom_features *features = &wacom_wac->features;
2185         char name[WACOM_NAME_MAX - 20]; /* Leave some room for suffixes */
2186
2187         /* Generic devices name unspecified */
2188         if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
2189                 char *product_name = wacom->hdev->name;
2190
2191                 if (hid_is_usb(wacom->hdev)) {
2192                         struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2193                         struct usb_device *dev = interface_to_usbdev(intf);
2194                         product_name = dev->product;
2195                 }
2196
2197                 if (wacom->hdev->bus == BUS_I2C) {
2198                         snprintf(name, sizeof(name), "%s %X",
2199                                  features->name, wacom->hdev->product);
2200                 } else if (strstr(product_name, "Wacom") ||
2201                            strstr(product_name, "wacom") ||
2202                            strstr(product_name, "WACOM")) {
2203                         strlcpy(name, product_name, sizeof(name));
2204                 } else {
2205                         snprintf(name, sizeof(name), "Wacom %s", product_name);
2206                 }
2207
2208                 /* strip out excess whitespaces */
2209                 while (1) {
2210                         char *gap = strstr(name, "  ");
2211                         if (gap == NULL)
2212                                 break;
2213                         /* shift everything including the terminator */
2214                         memmove(gap, gap+1, strlen(gap));
2215                 }
2216
2217                 /* get rid of trailing whitespace */
2218                 if (name[strlen(name)-1] == ' ')
2219                         name[strlen(name)-1] = '\0';
2220         } else {
2221                 strlcpy(name, features->name, sizeof(name));
2222         }
2223
2224         snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2225                  name, suffix);
2226
2227         /* Append the device type to the name */
2228         snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
2229                 "%s%s Pen", name, suffix);
2230         snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
2231                 "%s%s Finger", name, suffix);
2232         snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
2233                 "%s%s Pad", name, suffix);
2234 }
2235
2236 static void wacom_release_resources(struct wacom *wacom)
2237 {
2238         struct hid_device *hdev = wacom->hdev;
2239
2240         if (!wacom->resources)
2241                 return;
2242
2243         devres_release_group(&hdev->dev, wacom);
2244
2245         wacom->resources = false;
2246
2247         wacom->wacom_wac.pen_input = NULL;
2248         wacom->wacom_wac.touch_input = NULL;
2249         wacom->wacom_wac.pad_input = NULL;
2250 }
2251
2252 static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2253 {
2254         if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2255                 wacom_wac->shared->type = wacom_wac->features.type;
2256                 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2257         }
2258
2259         if (wacom_wac->has_mute_touch_switch) {
2260                 wacom_wac->shared->has_mute_touch_switch = true;
2261                 wacom_wac->shared->is_touch_on = true;
2262         }
2263
2264         if (wacom_wac->shared->has_mute_touch_switch &&
2265             wacom_wac->shared->touch_input) {
2266                 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2267                 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2268                                      SW_MUTE_DEVICE);
2269         }
2270 }
2271
2272 static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
2273 {
2274         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2275         struct wacom_features *features = &wacom_wac->features;
2276         struct hid_device *hdev = wacom->hdev;
2277         int error;
2278         unsigned int connect_mask = HID_CONNECT_HIDRAW;
2279
2280         features->pktlen = wacom_compute_pktlen(hdev);
2281         if (features->pktlen > WACOM_PKGLEN_MAX)
2282                 return -EINVAL;
2283
2284         if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2285                 return -ENOMEM;
2286
2287         wacom->resources = true;
2288
2289         error = wacom_allocate_inputs(wacom);
2290         if (error)
2291                 goto fail;
2292
2293         /*
2294          * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2295          * into debug mode for the touch part.
2296          * We ignore the other interfaces.
2297          */
2298         if (features->type == BAMBOO_PAD) {
2299                 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2300                         features->type = HID_GENERIC;
2301                 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2302                            (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2303                         error = -ENODEV;
2304                         goto fail;
2305                 }
2306         }
2307
2308         /* set the default size in case we do not get them from hid */
2309         wacom_set_default_phy(features);
2310
2311         /* Retrieve the physical and logical size for touch devices */
2312         wacom_retrieve_hid_descriptor(hdev, features);
2313         wacom_setup_device_quirks(wacom);
2314
2315         if (features->device_type == WACOM_DEVICETYPE_NONE &&
2316             features->type != WIRELESS) {
2317                 error = features->type == HID_GENERIC ? -ENODEV : 0;
2318
2319                 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
2320                          hdev->name,
2321                          error ? "Ignoring" : "Assuming pen");
2322
2323                 if (error)
2324                         goto fail;
2325
2326                 features->device_type |= WACOM_DEVICETYPE_PEN;
2327         }
2328
2329         wacom_calculate_res(features);
2330
2331         wacom_update_name(wacom, wireless ? " (WL)" : "");
2332
2333         /* pen only Bamboo neither support touch nor pad */
2334         if ((features->type == BAMBOO_PEN) &&
2335             ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2336             (features->device_type & WACOM_DEVICETYPE_PAD))) {
2337                 error = -ENODEV;
2338                 goto fail;
2339         }
2340
2341         error = wacom_add_shared_data(hdev);
2342         if (error)
2343                 goto fail;
2344
2345         if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2346              (features->quirks & WACOM_QUIRK_BATTERY)) {
2347                 error = wacom_initialize_battery(wacom);
2348                 if (error)
2349                         goto fail;
2350         }
2351
2352         error = wacom_register_inputs(wacom);
2353         if (error)
2354                 goto fail;
2355
2356         if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
2357                 error = wacom_initialize_leds(wacom);
2358                 if (error)
2359                         goto fail;
2360
2361                 error = wacom_initialize_remotes(wacom);
2362                 if (error)
2363                         goto fail;
2364         }
2365
2366         if (features->type == HID_GENERIC)
2367                 connect_mask |= HID_CONNECT_DRIVER;
2368
2369         /* Regular HID work starts now */
2370         error = hid_hw_start(hdev, connect_mask);
2371         if (error) {
2372                 hid_err(hdev, "hw start failed\n");
2373                 goto fail;
2374         }
2375
2376         if (!wireless) {
2377                 /* Note that if query fails it is not a hard failure */
2378                 wacom_query_tablet_data(wacom);
2379         }
2380
2381         /* touch only Bamboo doesn't support pen */
2382         if ((features->type == BAMBOO_TOUCH) &&
2383             (features->device_type & WACOM_DEVICETYPE_PEN)) {
2384                 cancel_delayed_work_sync(&wacom->init_work);
2385                 _wacom_query_tablet_data(wacom);
2386                 error = -ENODEV;
2387                 goto fail_quirks;
2388         }
2389
2390         if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2391                 error = hid_hw_open(hdev);
2392
2393         wacom_set_shared_values(wacom_wac);
2394         devres_close_group(&hdev->dev, wacom);
2395
2396         return 0;
2397
2398 fail_quirks:
2399         hid_hw_stop(hdev);
2400 fail:
2401         wacom_release_resources(wacom);
2402         return error;
2403 }
2404
2405 static void wacom_wireless_work(struct work_struct *work)
2406 {
2407         struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2408         struct usb_device *usbdev = wacom->usbdev;
2409         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2410         struct hid_device *hdev1, *hdev2;
2411         struct wacom *wacom1, *wacom2;
2412         struct wacom_wac *wacom_wac1, *wacom_wac2;
2413         int error;
2414
2415         /*
2416          * Regardless if this is a disconnect or a new tablet,
2417          * remove any existing input and battery devices.
2418          */
2419
2420         wacom_destroy_battery(wacom);
2421
2422         if (!usbdev)
2423                 return;
2424
2425         /* Stylus interface */
2426         hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2427         wacom1 = hid_get_drvdata(hdev1);
2428         wacom_wac1 = &(wacom1->wacom_wac);
2429         wacom_release_resources(wacom1);
2430
2431         /* Touch interface */
2432         hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2433         wacom2 = hid_get_drvdata(hdev2);
2434         wacom_wac2 = &(wacom2->wacom_wac);
2435         wacom_release_resources(wacom2);
2436
2437         if (wacom_wac->pid == 0) {
2438                 hid_info(wacom->hdev, "wireless tablet disconnected\n");
2439         } else {
2440                 const struct hid_device_id *id = wacom_ids;
2441
2442                 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2443                          wacom_wac->pid);
2444
2445                 while (id->bus) {
2446                         if (id->vendor == USB_VENDOR_ID_WACOM &&
2447                             id->product == wacom_wac->pid)
2448                                 break;
2449                         id++;
2450                 }
2451
2452                 if (!id->bus) {
2453                         hid_info(wacom->hdev, "ignoring unknown PID.\n");
2454                         return;
2455                 }
2456
2457                 /* Stylus interface */
2458                 wacom_wac1->features =
2459                         *((struct wacom_features *)id->driver_data);
2460
2461                 wacom_wac1->pid = wacom_wac->pid;
2462                 hid_hw_stop(hdev1);
2463                 error = wacom_parse_and_register(wacom1, true);
2464                 if (error)
2465                         goto fail;
2466
2467                 /* Touch interface */
2468                 if (wacom_wac1->features.touch_max ||
2469                     (wacom_wac1->features.type >= INTUOSHT &&
2470                     wacom_wac1->features.type <= BAMBOO_PT)) {
2471                         wacom_wac2->features =
2472                                 *((struct wacom_features *)id->driver_data);
2473                         wacom_wac2->pid = wacom_wac->pid;
2474                         hid_hw_stop(hdev2);
2475                         error = wacom_parse_and_register(wacom2, true);
2476                         if (error)
2477                                 goto fail;
2478                 }
2479
2480                 strlcpy(wacom_wac->name, wacom_wac1->name,
2481                         sizeof(wacom_wac->name));
2482                 error = wacom_initialize_battery(wacom);
2483                 if (error)
2484                         goto fail;
2485         }
2486
2487         return;
2488
2489 fail:
2490         wacom_release_resources(wacom1);
2491         wacom_release_resources(wacom2);
2492         return;
2493 }
2494
2495 static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2496 {
2497         struct wacom_remote *remote = wacom->remote;
2498         u32 serial = remote->remotes[index].serial;
2499         int i;
2500         unsigned long flags;
2501
2502         for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2503                 if (remote->remotes[i].serial == serial) {
2504
2505                         spin_lock_irqsave(&remote->remote_lock, flags);
2506                         remote->remotes[i].registered = false;
2507                         spin_unlock_irqrestore(&remote->remote_lock, flags);
2508
2509                         if (remote->remotes[i].battery.battery)
2510                                 devres_release_group(&wacom->hdev->dev,
2511                                                      &remote->remotes[i].battery.bat_desc);
2512
2513                         if (remote->remotes[i].group.name)
2514                                 devres_release_group(&wacom->hdev->dev,
2515                                                      &remote->remotes[i]);
2516
2517                         remote->remotes[i].serial = 0;
2518                         remote->remotes[i].group.name = NULL;
2519                         remote->remotes[i].battery.battery = NULL;
2520                         wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2521                 }
2522         }
2523 }
2524
2525 static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2526                                    unsigned int index)
2527 {
2528         struct wacom_remote *remote = wacom->remote;
2529         struct device *dev = &wacom->hdev->dev;
2530         int error, k;
2531
2532         /* A remote can pair more than once with an EKR,
2533          * check to make sure this serial isn't already paired.
2534          */
2535         for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2536                 if (remote->remotes[k].serial == serial)
2537                         break;
2538         }
2539
2540         if (k < WACOM_MAX_REMOTES) {
2541                 remote->remotes[index].serial = serial;
2542                 return 0;
2543         }
2544
2545         if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2546                 return -ENOMEM;
2547
2548         error = wacom_remote_create_attr_group(wacom, serial, index);
2549         if (error)
2550                 goto fail;
2551
2552         remote->remotes[index].input = wacom_allocate_input(wacom);
2553         if (!remote->remotes[index].input) {
2554                 error = -ENOMEM;
2555                 goto fail;
2556         }
2557         remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2558         remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2559
2560         if (!remote->remotes[index].input->name) {
2561                 error = -EINVAL;
2562                 goto fail;
2563         }
2564
2565         error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2566                                                    &wacom->wacom_wac);
2567         if (error)
2568                 goto fail;
2569
2570         remote->remotes[index].serial = serial;
2571
2572         error = input_register_device(remote->remotes[index].input);
2573         if (error)
2574                 goto fail;
2575
2576         error = wacom_led_groups_alloc_and_register_one(
2577                                         &remote->remotes[index].input->dev,
2578                                         wacom, index, 3, true);
2579         if (error)
2580                 goto fail;
2581
2582         remote->remotes[index].registered = true;
2583
2584         devres_close_group(dev, &remote->remotes[index]);
2585         return 0;
2586
2587 fail:
2588         devres_release_group(dev, &remote->remotes[index]);
2589         remote->remotes[index].serial = 0;
2590         return error;
2591 }
2592
2593 static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2594 {
2595         struct wacom_remote *remote = wacom->remote;
2596         int error;
2597
2598         if (!remote->remotes[index].registered)
2599                 return 0;
2600
2601         if (remote->remotes[index].battery.battery)
2602                 return 0;
2603
2604         if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2605                 return 0;
2606
2607         error = __wacom_initialize_battery(wacom,
2608                                         &wacom->remote->remotes[index].battery);
2609         if (error)
2610                 return error;
2611
2612         return 0;
2613 }
2614
2615 static void wacom_remote_work(struct work_struct *work)
2616 {
2617         struct wacom *wacom = container_of(work, struct wacom, remote_work);
2618         struct wacom_remote *remote = wacom->remote;
2619         struct wacom_remote_data data;
2620         unsigned long flags;
2621         unsigned int count;
2622         u32 serial;
2623         int i;
2624
2625         spin_lock_irqsave(&remote->remote_lock, flags);
2626
2627         count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2628
2629         if (count != sizeof(data)) {
2630                 hid_err(wacom->hdev,
2631                         "workitem triggered without status available\n");
2632                 spin_unlock_irqrestore(&remote->remote_lock, flags);
2633                 return;
2634         }
2635
2636         if (!kfifo_is_empty(&remote->remote_fifo))
2637                 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2638
2639         spin_unlock_irqrestore(&remote->remote_lock, flags);
2640
2641         for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2642                 serial = data.remote[i].serial;
2643                 if (data.remote[i].connected) {
2644
2645                         if (remote->remotes[i].serial == serial) {
2646                                 wacom_remote_attach_battery(wacom, i);
2647                                 continue;
2648                         }
2649
2650                         if (remote->remotes[i].serial)
2651                                 wacom_remote_destroy_one(wacom, i);
2652
2653                         wacom_remote_create_one(wacom, serial, i);
2654
2655                 } else if (remote->remotes[i].serial) {
2656                         wacom_remote_destroy_one(wacom, i);
2657                 }
2658         }
2659 }
2660
2661 static void wacom_mode_change_work(struct work_struct *work)
2662 {
2663         struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2664         struct wacom_shared *shared = wacom->wacom_wac.shared;
2665         struct wacom *wacom1 = NULL;
2666         struct wacom *wacom2 = NULL;
2667         bool is_direct = wacom->wacom_wac.is_direct_mode;
2668         int error = 0;
2669
2670         if (shared->pen) {
2671                 wacom1 = hid_get_drvdata(shared->pen);
2672                 wacom_release_resources(wacom1);
2673                 hid_hw_stop(wacom1->hdev);
2674                 wacom1->wacom_wac.has_mode_change = true;
2675                 wacom1->wacom_wac.is_direct_mode = is_direct;
2676         }
2677
2678         if (shared->touch) {
2679                 wacom2 = hid_get_drvdata(shared->touch);
2680                 wacom_release_resources(wacom2);
2681                 hid_hw_stop(wacom2->hdev);
2682                 wacom2->wacom_wac.has_mode_change = true;
2683                 wacom2->wacom_wac.is_direct_mode = is_direct;
2684         }
2685
2686         if (wacom1) {
2687                 error = wacom_parse_and_register(wacom1, false);
2688                 if (error)
2689                         return;
2690         }
2691
2692         if (wacom2) {
2693                 error = wacom_parse_and_register(wacom2, false);
2694                 if (error)
2695                         return;
2696         }
2697
2698         return;
2699 }
2700
2701 static int wacom_probe(struct hid_device *hdev,
2702                 const struct hid_device_id *id)
2703 {
2704         struct wacom *wacom;
2705         struct wacom_wac *wacom_wac;
2706         struct wacom_features *features;
2707         int error;
2708
2709         if (!id->driver_data)
2710                 return -EINVAL;
2711
2712         hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2713
2714         /* hid-core sets this quirk for the boot interface */
2715         hdev->quirks &= ~HID_QUIRK_NOGET;
2716
2717         wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2718         if (!wacom)
2719                 return -ENOMEM;
2720
2721         hid_set_drvdata(hdev, wacom);
2722         wacom->hdev = hdev;
2723
2724         wacom_wac = &wacom->wacom_wac;
2725         wacom_wac->features = *((struct wacom_features *)id->driver_data);
2726         features = &wacom_wac->features;
2727
2728         if (features->check_for_hid_type && features->hid_type != hdev->type) {
2729                 error = -ENODEV;
2730                 goto fail;
2731         }
2732
2733         error = wacom_devm_kfifo_alloc(wacom);
2734         if (error)
2735                 goto fail;
2736
2737         wacom_wac->hid_data.inputmode = -1;
2738         wacom_wac->mode_report = -1;
2739
2740         if (hid_is_usb(hdev)) {
2741                 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2742                 struct usb_device *dev = interface_to_usbdev(intf);
2743
2744                 wacom->usbdev = dev;
2745                 wacom->intf = intf;
2746         }
2747
2748         mutex_init(&wacom->lock);
2749         INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
2750         INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2751         INIT_WORK(&wacom->battery_work, wacom_battery_work);
2752         INIT_WORK(&wacom->remote_work, wacom_remote_work);
2753         INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
2754
2755         /* ask for the report descriptor to be loaded by HID */
2756         error = hid_parse(hdev);
2757         if (error) {
2758                 hid_err(hdev, "parse failed\n");
2759                 goto fail;
2760         }
2761
2762         error = wacom_parse_and_register(wacom, false);
2763         if (error)
2764                 goto fail;
2765
2766         if (hdev->bus == BUS_BLUETOOTH) {
2767                 error = device_create_file(&hdev->dev, &dev_attr_speed);
2768                 if (error)
2769                         hid_warn(hdev,
2770                                  "can't create sysfs speed attribute err: %d\n",
2771                                  error);
2772         }
2773
2774         return 0;
2775
2776 fail:
2777         hid_set_drvdata(hdev, NULL);
2778         return error;
2779 }
2780
2781 static void wacom_remove(struct hid_device *hdev)
2782 {
2783         struct wacom *wacom = hid_get_drvdata(hdev);
2784         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2785         struct wacom_features *features = &wacom_wac->features;
2786
2787         if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2788                 hid_hw_close(hdev);
2789
2790         hid_hw_stop(hdev);
2791
2792         cancel_delayed_work_sync(&wacom->init_work);
2793         cancel_work_sync(&wacom->wireless_work);
2794         cancel_work_sync(&wacom->battery_work);
2795         cancel_work_sync(&wacom->remote_work);
2796         cancel_work_sync(&wacom->mode_change_work);
2797         if (hdev->bus == BUS_BLUETOOTH)
2798                 device_remove_file(&hdev->dev, &dev_attr_speed);
2799
2800         /* make sure we don't trigger the LEDs */
2801         wacom_led_groups_release(wacom);
2802
2803         if (wacom->wacom_wac.features.type != REMOTE)
2804                 wacom_release_resources(wacom);
2805
2806         hid_set_drvdata(hdev, NULL);
2807 }
2808
2809 #ifdef CONFIG_PM
2810 static int wacom_resume(struct hid_device *hdev)
2811 {
2812         struct wacom *wacom = hid_get_drvdata(hdev);
2813
2814         mutex_lock(&wacom->lock);
2815
2816         /* switch to wacom mode first */
2817         _wacom_query_tablet_data(wacom);
2818         wacom_led_control(wacom);
2819
2820         mutex_unlock(&wacom->lock);
2821
2822         return 0;
2823 }
2824
2825 static int wacom_reset_resume(struct hid_device *hdev)
2826 {
2827         return wacom_resume(hdev);
2828 }
2829 #endif /* CONFIG_PM */
2830
2831 static struct hid_driver wacom_driver = {
2832         .name =         "wacom",
2833         .id_table =     wacom_ids,
2834         .probe =        wacom_probe,
2835         .remove =       wacom_remove,
2836         .report =       wacom_wac_report,
2837 #ifdef CONFIG_PM
2838         .resume =       wacom_resume,
2839         .reset_resume = wacom_reset_resume,
2840 #endif
2841         .raw_event =    wacom_raw_event,
2842 };
2843 module_hid_driver(wacom_driver);
2844
2845 MODULE_VERSION(DRIVER_VERSION);
2846 MODULE_AUTHOR(DRIVER_AUTHOR);
2847 MODULE_DESCRIPTION(DRIVER_DESC);
2848 MODULE_LICENSE("GPL");