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