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