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