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