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