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