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