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