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