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