HID: wacom - enable LED support for Wireless Intuos5/Pro
[firefly-linux-kernel-4.4.55.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/hid.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
27 #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
28 #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
29
30 static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
31                             size_t size, unsigned int retries)
32 {
33         int retval;
34
35         do {
36                 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
37                                 HID_REQ_GET_REPORT);
38         } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
39
40         return retval;
41 }
42
43 static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
44                             size_t size, unsigned int retries)
45 {
46         int retval;
47
48         do {
49                 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
50                                 HID_REQ_SET_REPORT);
51         } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
52
53         return retval;
54 }
55
56 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
57                 u8 *raw_data, int size)
58 {
59         struct wacom *wacom = hid_get_drvdata(hdev);
60
61         if (size > WACOM_PKGLEN_MAX)
62                 return 1;
63
64         memcpy(wacom->wacom_wac.data, raw_data, size);
65
66         wacom_wac_irq(&wacom->wacom_wac, size);
67
68         return 0;
69 }
70
71 static int wacom_open(struct input_dev *dev)
72 {
73         struct wacom *wacom = input_get_drvdata(dev);
74         int retval;
75
76         mutex_lock(&wacom->lock);
77         retval = hid_hw_open(wacom->hdev);
78         mutex_unlock(&wacom->lock);
79
80         return retval;
81 }
82
83 static void wacom_close(struct input_dev *dev)
84 {
85         struct wacom *wacom = input_get_drvdata(dev);
86
87         mutex_lock(&wacom->lock);
88         hid_hw_close(wacom->hdev);
89         mutex_unlock(&wacom->lock);
90 }
91
92 /*
93  * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
94  */
95 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
96                                unsigned unit, int exponent)
97 {
98         struct hid_field field = {
99                 .logical_maximum = logical_extents,
100                 .physical_maximum = physical_extents,
101                 .unit = unit,
102                 .unit_exponent = exponent,
103         };
104
105         return hidinput_calc_abs_res(&field, ABS_X);
106 }
107
108 static void wacom_feature_mapping(struct hid_device *hdev,
109                 struct hid_field *field, struct hid_usage *usage)
110 {
111         struct wacom *wacom = hid_get_drvdata(hdev);
112         struct wacom_features *features = &wacom->wacom_wac.features;
113
114         switch (usage->hid) {
115         case HID_DG_CONTACTMAX:
116                 /* leave touch_max as is if predefined */
117                 if (!features->touch_max)
118                         features->touch_max = field->value[0];
119                 break;
120         }
121 }
122
123 /*
124  * Interface Descriptor of wacom devices can be incomplete and
125  * inconsistent so wacom_features table is used to store stylus
126  * device's packet lengths, various maximum values, and tablet
127  * resolution based on product ID's.
128  *
129  * For devices that contain 2 interfaces, wacom_features table is
130  * inaccurate for the touch interface.  Since the Interface Descriptor
131  * for touch interfaces has pretty complete data, this function exists
132  * to query tablet for this missing information instead of hard coding in
133  * an additional table.
134  *
135  * A typical Interface Descriptor for a stylus will contain a
136  * boot mouse application collection that is not of interest and this
137  * function will ignore it.
138  *
139  * It also contains a digitizer application collection that also is not
140  * of interest since any information it contains would be duplicate
141  * of what is in wacom_features. Usually it defines a report of an array
142  * of bytes that could be used as max length of the stylus packet returned.
143  * If it happens to define a Digitizer-Stylus Physical Collection then
144  * the X and Y logical values contain valid data but it is ignored.
145  *
146  * A typical Interface Descriptor for a touch interface will contain a
147  * Digitizer-Finger Physical Collection which will define both logical
148  * X/Y maximum as well as the physical size of tablet. Since touch
149  * interfaces haven't supported pressure or distance, this is enough
150  * information to override invalid values in the wacom_features table.
151  *
152  * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
153  * data. We deal with them after returning from this function.
154  */
155 static void wacom_usage_mapping(struct hid_device *hdev,
156                 struct hid_field *field, struct hid_usage *usage)
157 {
158         struct wacom *wacom = hid_get_drvdata(hdev);
159         struct wacom_features *features = &wacom->wacom_wac.features;
160         bool finger = (field->logical == HID_DG_FINGER) ||
161                       (field->physical == HID_DG_FINGER);
162         bool pen = (field->logical == HID_DG_STYLUS) ||
163                    (field->physical == HID_DG_STYLUS);
164
165         /*
166         * Requiring Stylus Usage will ignore boot mouse
167         * X/Y values and some cases of invalid Digitizer X/Y
168         * values commonly reported.
169         */
170         if (!pen && !finger)
171                 return;
172
173         if (finger && !features->touch_max)
174                 /* touch device at least supports one touch point */
175                 features->touch_max = 1;
176
177         switch (usage->hid) {
178         case HID_GD_X:
179                 features->x_max = field->logical_maximum;
180                 if (finger) {
181                         features->device_type = BTN_TOOL_FINGER;
182                         features->x_phy = field->physical_maximum;
183                         if (features->type != BAMBOO_PT) {
184                                 features->unit = field->unit;
185                                 features->unitExpo = field->unit_exponent;
186                         }
187                 } else {
188                         features->device_type = BTN_TOOL_PEN;
189                 }
190                 break;
191         case HID_GD_Y:
192                 features->y_max = field->logical_maximum;
193                 if (finger) {
194                         features->y_phy = field->physical_maximum;
195                         if (features->type != BAMBOO_PT) {
196                                 features->unit = field->unit;
197                                 features->unitExpo = field->unit_exponent;
198                         }
199                 }
200                 break;
201         case HID_DG_TIPPRESSURE:
202                 if (pen)
203                         features->pressure_max = field->logical_maximum;
204                 break;
205         }
206 }
207
208 static void wacom_parse_hid(struct hid_device *hdev,
209                            struct wacom_features *features)
210 {
211         struct hid_report_enum *rep_enum;
212         struct hid_report *hreport;
213         int i, j;
214
215         /* check features first */
216         rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
217         list_for_each_entry(hreport, &rep_enum->report_list, list) {
218                 for (i = 0; i < hreport->maxfield; i++) {
219                         /* Ignore if report count is out of bounds. */
220                         if (hreport->field[i]->report_count < 1)
221                                 continue;
222
223                         for (j = 0; j < hreport->field[i]->maxusage; j++) {
224                                 wacom_feature_mapping(hdev, hreport->field[i],
225                                                 hreport->field[i]->usage + j);
226                         }
227                 }
228         }
229
230         /* now check the input usages */
231         rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
232         list_for_each_entry(hreport, &rep_enum->report_list, list) {
233
234                 if (!hreport->maxfield)
235                         continue;
236
237                 for (i = 0; i < hreport->maxfield; i++)
238                         for (j = 0; j < hreport->field[i]->maxusage; j++)
239                                 wacom_usage_mapping(hdev, hreport->field[i],
240                                                 hreport->field[i]->usage + j);
241         }
242 }
243
244 static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
245                 int length, int mode)
246 {
247         unsigned char *rep_data;
248         int error = -ENOMEM, limit = 0;
249
250         rep_data = kzalloc(length, GFP_KERNEL);
251         if (!rep_data)
252                 return error;
253
254         do {
255                 rep_data[0] = report_id;
256                 rep_data[1] = mode;
257
258                 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
259                                          length, 1);
260                 if (error >= 0)
261                         error = wacom_get_report(hdev, HID_FEATURE_REPORT,
262                                                  rep_data, length, 1);
263         } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
264
265         kfree(rep_data);
266
267         return error < 0 ? error : 0;
268 }
269
270 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
271                 struct wacom_features *features)
272 {
273         struct wacom *wacom = hid_get_drvdata(hdev);
274         int ret;
275         u8 rep_data[2];
276
277         switch (features->type) {
278         case GRAPHIRE_BT:
279                 rep_data[0] = 0x03;
280                 rep_data[1] = 0x00;
281                 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
282                                         3);
283
284                 if (ret >= 0) {
285                         rep_data[0] = speed == 0 ? 0x05 : 0x06;
286                         rep_data[1] = 0x00;
287
288                         ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
289                                                 rep_data, 2, 3);
290
291                         if (ret >= 0) {
292                                 wacom->wacom_wac.bt_high_speed = speed;
293                                 return 0;
294                         }
295                 }
296
297                 /*
298                  * Note that if the raw queries fail, it's not a hard failure
299                  * and it is safe to continue
300                  */
301                 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
302                          rep_data[0], ret);
303                 break;
304         case INTUOS4WL:
305                 if (speed == 1)
306                         wacom->wacom_wac.bt_features &= ~0x20;
307                 else
308                         wacom->wacom_wac.bt_features |= 0x20;
309
310                 rep_data[0] = 0x03;
311                 rep_data[1] = wacom->wacom_wac.bt_features;
312
313                 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
314                                         1);
315                 if (ret >= 0)
316                         wacom->wacom_wac.bt_high_speed = speed;
317                 break;
318         }
319
320         return 0;
321 }
322
323 /*
324  * Switch the tablet into its most-capable mode. Wacom tablets are
325  * typically configured to power-up in a mode which sends mouse-like
326  * reports to the OS. To get absolute position, pressure data, etc.
327  * from the tablet, it is necessary to switch the tablet out of this
328  * mode and into one which sends the full range of tablet data.
329  */
330 static int wacom_query_tablet_data(struct hid_device *hdev,
331                 struct wacom_features *features)
332 {
333         if (hdev->bus == BUS_BLUETOOTH)
334                 return wacom_bt_query_tablet_data(hdev, 1, features);
335
336         if (features->device_type == BTN_TOOL_FINGER) {
337                 if (features->type > TABLETPC) {
338                         /* MT Tablet PC touch */
339                         return wacom_set_device_mode(hdev, 3, 4, 4);
340                 }
341                 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
342                         return wacom_set_device_mode(hdev, 18, 3, 2);
343                 }
344         } else if (features->device_type == BTN_TOOL_PEN) {
345                 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
346                         return wacom_set_device_mode(hdev, 2, 2, 2);
347                 }
348         }
349
350         return 0;
351 }
352
353 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
354                                          struct wacom_features *features)
355 {
356         struct wacom *wacom = hid_get_drvdata(hdev);
357         struct usb_interface *intf = wacom->intf;
358
359         /* default features */
360         features->device_type = BTN_TOOL_PEN;
361         features->x_fuzz = 4;
362         features->y_fuzz = 4;
363         features->pressure_fuzz = 0;
364         features->distance_fuzz = 0;
365
366         /*
367          * The wireless device HID is basic and layout conflicts with
368          * other tablets (monitor and touch interface can look like pen).
369          * Skip the query for this type and modify defaults based on
370          * interface number.
371          */
372         if (features->type == WIRELESS) {
373                 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
374                         features->device_type = 0;
375                 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
376                         features->device_type = BTN_TOOL_FINGER;
377                         features->pktlen = WACOM_PKGLEN_BBTOUCH3;
378                 }
379         }
380
381         /* only devices that support touch need to retrieve the info */
382         if (features->type < BAMBOO_PT)
383                 return;
384
385         wacom_parse_hid(hdev, features);
386 }
387
388 struct wacom_hdev_data {
389         struct list_head list;
390         struct kref kref;
391         struct hid_device *dev;
392         struct wacom_shared shared;
393 };
394
395 static LIST_HEAD(wacom_udev_list);
396 static DEFINE_MUTEX(wacom_udev_list_lock);
397
398 static bool wacom_are_sibling(struct hid_device *hdev,
399                 struct hid_device *sibling)
400 {
401         struct wacom *wacom = hid_get_drvdata(hdev);
402         struct wacom_features *features = &wacom->wacom_wac.features;
403         int vid = features->oVid;
404         int pid = features->oPid;
405         int n1,n2;
406
407         if (vid == 0 && pid == 0) {
408                 vid = hdev->vendor;
409                 pid = hdev->product;
410         }
411
412         if (vid != sibling->vendor || pid != sibling->product)
413                 return false;
414
415         /* Compare the physical path. */
416         n1 = strrchr(hdev->phys, '.') - hdev->phys;
417         n2 = strrchr(sibling->phys, '.') - sibling->phys;
418         if (n1 != n2 || n1 <= 0 || n2 <= 0)
419                 return false;
420
421         return !strncmp(hdev->phys, sibling->phys, n1);
422 }
423
424 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
425 {
426         struct wacom_hdev_data *data;
427
428         list_for_each_entry(data, &wacom_udev_list, list) {
429                 if (wacom_are_sibling(hdev, data->dev)) {
430                         kref_get(&data->kref);
431                         return data;
432                 }
433         }
434
435         return NULL;
436 }
437
438 static int wacom_add_shared_data(struct hid_device *hdev)
439 {
440         struct wacom *wacom = hid_get_drvdata(hdev);
441         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
442         struct wacom_hdev_data *data;
443         int retval = 0;
444
445         mutex_lock(&wacom_udev_list_lock);
446
447         data = wacom_get_hdev_data(hdev);
448         if (!data) {
449                 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
450                 if (!data) {
451                         retval = -ENOMEM;
452                         goto out;
453                 }
454
455                 kref_init(&data->kref);
456                 data->dev = hdev;
457                 list_add_tail(&data->list, &wacom_udev_list);
458         }
459
460         wacom_wac->shared = &data->shared;
461
462 out:
463         mutex_unlock(&wacom_udev_list_lock);
464         return retval;
465 }
466
467 static void wacom_release_shared_data(struct kref *kref)
468 {
469         struct wacom_hdev_data *data =
470                 container_of(kref, struct wacom_hdev_data, kref);
471
472         mutex_lock(&wacom_udev_list_lock);
473         list_del(&data->list);
474         mutex_unlock(&wacom_udev_list_lock);
475
476         kfree(data);
477 }
478
479 static void wacom_remove_shared_data(struct wacom_wac *wacom)
480 {
481         struct wacom_hdev_data *data;
482
483         if (wacom->shared) {
484                 data = container_of(wacom->shared, struct wacom_hdev_data, shared);
485                 kref_put(&data->kref, wacom_release_shared_data);
486                 wacom->shared = NULL;
487         }
488 }
489
490 static int wacom_led_control(struct wacom *wacom)
491 {
492         unsigned char *buf;
493         int retval;
494         unsigned char report_id = WAC_CMD_LED_CONTROL;
495         int buf_size = 9;
496
497         if (wacom->wacom_wac.pid) { /* wireless connected */
498                 report_id = WAC_CMD_WL_LED_CONTROL;
499                 buf_size = 13;
500         }
501         buf = kzalloc(buf_size, GFP_KERNEL);
502         if (!buf)
503                 return -ENOMEM;
504
505         if (wacom->wacom_wac.features.type >= INTUOS5S &&
506             wacom->wacom_wac.features.type <= INTUOSPL) {
507                 /*
508                  * Touch Ring and crop mark LED luminance may take on
509                  * one of four values:
510                  *    0 = Low; 1 = Medium; 2 = High; 3 = Off
511                  */
512                 int ring_led = wacom->led.select[0] & 0x03;
513                 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
514                 int crop_lum = 0;
515                 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
516
517                 buf[0] = report_id;
518                 if (wacom->wacom_wac.pid) {
519                         wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
520                                          buf, buf_size, WAC_CMD_RETRIES);
521                         buf[0] = report_id;
522                         buf[4] = led_bits;
523                 } else
524                         buf[1] = led_bits;
525         }
526         else {
527                 int led = wacom->led.select[0] | 0x4;
528
529                 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
530                     wacom->wacom_wac.features.type == WACOM_24HD)
531                         led |= (wacom->led.select[1] << 4) | 0x40;
532
533                 buf[0] = report_id;
534                 buf[1] = led;
535                 buf[2] = wacom->led.llv;
536                 buf[3] = wacom->led.hlv;
537                 buf[4] = wacom->led.img_lum;
538         }
539
540         retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
541                                   WAC_CMD_RETRIES);
542         kfree(buf);
543
544         return retval;
545 }
546
547 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
548                 const unsigned len, const void *img)
549 {
550         unsigned char *buf;
551         int i, retval;
552         const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
553
554         buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
555         if (!buf)
556                 return -ENOMEM;
557
558         /* Send 'start' command */
559         buf[0] = WAC_CMD_ICON_START;
560         buf[1] = 1;
561         retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
562                                   WAC_CMD_RETRIES);
563         if (retval < 0)
564                 goto out;
565
566         buf[0] = xfer_id;
567         buf[1] = button_id & 0x07;
568         for (i = 0; i < 4; i++) {
569                 buf[2] = i;
570                 memcpy(buf + 3, img + i * chunk_len, chunk_len);
571
572                 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
573                                           buf, chunk_len + 3, WAC_CMD_RETRIES);
574                 if (retval < 0)
575                         break;
576         }
577
578         /* Send 'stop' */
579         buf[0] = WAC_CMD_ICON_START;
580         buf[1] = 0;
581         wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
582                          WAC_CMD_RETRIES);
583
584 out:
585         kfree(buf);
586         return retval;
587 }
588
589 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
590                                       const char *buf, size_t count)
591 {
592         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
593         struct wacom *wacom = hid_get_drvdata(hdev);
594         unsigned int id;
595         int err;
596
597         err = kstrtouint(buf, 10, &id);
598         if (err)
599                 return err;
600
601         mutex_lock(&wacom->lock);
602
603         wacom->led.select[set_id] = id & 0x3;
604         err = wacom_led_control(wacom);
605
606         mutex_unlock(&wacom->lock);
607
608         return err < 0 ? err : count;
609 }
610
611 #define DEVICE_LED_SELECT_ATTR(SET_ID)                                  \
612 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,     \
613         struct device_attribute *attr, const char *buf, size_t count)   \
614 {                                                                       \
615         return wacom_led_select_store(dev, SET_ID, buf, count);         \
616 }                                                                       \
617 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,      \
618         struct device_attribute *attr, char *buf)                       \
619 {                                                                       \
620         struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
621         struct wacom *wacom = hid_get_drvdata(hdev);                    \
622         return scnprintf(buf, PAGE_SIZE, "%d\n",                        \
623                          wacom->led.select[SET_ID]);                    \
624 }                                                                       \
625 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,       \
626                     wacom_led##SET_ID##_select_show,                    \
627                     wacom_led##SET_ID##_select_store)
628
629 DEVICE_LED_SELECT_ATTR(0);
630 DEVICE_LED_SELECT_ATTR(1);
631
632 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
633                                      const char *buf, size_t count)
634 {
635         unsigned int value;
636         int err;
637
638         err = kstrtouint(buf, 10, &value);
639         if (err)
640                 return err;
641
642         mutex_lock(&wacom->lock);
643
644         *dest = value & 0x7f;
645         err = wacom_led_control(wacom);
646
647         mutex_unlock(&wacom->lock);
648
649         return err < 0 ? err : count;
650 }
651
652 #define DEVICE_LUMINANCE_ATTR(name, field)                              \
653 static ssize_t wacom_##name##_luminance_store(struct device *dev,       \
654         struct device_attribute *attr, const char *buf, size_t count)   \
655 {                                                                       \
656         struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
657         struct wacom *wacom = hid_get_drvdata(hdev);                    \
658                                                                         \
659         return wacom_luminance_store(wacom, &wacom->led.field,          \
660                                      buf, count);                       \
661 }                                                                       \
662 static ssize_t wacom_##name##_luminance_show(struct device *dev,        \
663         struct device_attribute *attr, char *buf)                       \
664 {                                                                       \
665         struct wacom *wacom = dev_get_drvdata(dev);                     \
666         return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);     \
667 }                                                                       \
668 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,                  \
669                    wacom_##name##_luminance_show,                       \
670                    wacom_##name##_luminance_store)
671
672 DEVICE_LUMINANCE_ATTR(status0, llv);
673 DEVICE_LUMINANCE_ATTR(status1, hlv);
674 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
675
676 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
677                                         const char *buf, size_t count)
678 {
679         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
680         struct wacom *wacom = hid_get_drvdata(hdev);
681         int err;
682         unsigned len;
683         u8 xfer_id;
684
685         if (hdev->bus == BUS_BLUETOOTH) {
686                 len = 256;
687                 xfer_id = WAC_CMD_ICON_BT_XFER;
688         } else {
689                 len = 1024;
690                 xfer_id = WAC_CMD_ICON_XFER;
691         }
692
693         if (count != len)
694                 return -EINVAL;
695
696         mutex_lock(&wacom->lock);
697
698         err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
699
700         mutex_unlock(&wacom->lock);
701
702         return err < 0 ? err : count;
703 }
704
705 #define DEVICE_BTNIMG_ATTR(BUTTON_ID)                                   \
706 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,      \
707         struct device_attribute *attr, const char *buf, size_t count)   \
708 {                                                                       \
709         return wacom_button_image_store(dev, BUTTON_ID, buf, count);    \
710 }                                                                       \
711 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,        \
712                    NULL, wacom_btnimg##BUTTON_ID##_store)
713
714 DEVICE_BTNIMG_ATTR(0);
715 DEVICE_BTNIMG_ATTR(1);
716 DEVICE_BTNIMG_ATTR(2);
717 DEVICE_BTNIMG_ATTR(3);
718 DEVICE_BTNIMG_ATTR(4);
719 DEVICE_BTNIMG_ATTR(5);
720 DEVICE_BTNIMG_ATTR(6);
721 DEVICE_BTNIMG_ATTR(7);
722
723 static struct attribute *cintiq_led_attrs[] = {
724         &dev_attr_status_led0_select.attr,
725         &dev_attr_status_led1_select.attr,
726         NULL
727 };
728
729 static struct attribute_group cintiq_led_attr_group = {
730         .name = "wacom_led",
731         .attrs = cintiq_led_attrs,
732 };
733
734 static struct attribute *intuos4_led_attrs[] = {
735         &dev_attr_status0_luminance.attr,
736         &dev_attr_status1_luminance.attr,
737         &dev_attr_status_led0_select.attr,
738         &dev_attr_buttons_luminance.attr,
739         &dev_attr_button0_rawimg.attr,
740         &dev_attr_button1_rawimg.attr,
741         &dev_attr_button2_rawimg.attr,
742         &dev_attr_button3_rawimg.attr,
743         &dev_attr_button4_rawimg.attr,
744         &dev_attr_button5_rawimg.attr,
745         &dev_attr_button6_rawimg.attr,
746         &dev_attr_button7_rawimg.attr,
747         NULL
748 };
749
750 static struct attribute_group intuos4_led_attr_group = {
751         .name = "wacom_led",
752         .attrs = intuos4_led_attrs,
753 };
754
755 static struct attribute *intuos5_led_attrs[] = {
756         &dev_attr_status0_luminance.attr,
757         &dev_attr_status_led0_select.attr,
758         NULL
759 };
760
761 static struct attribute_group intuos5_led_attr_group = {
762         .name = "wacom_led",
763         .attrs = intuos5_led_attrs,
764 };
765
766 static int wacom_initialize_leds(struct wacom *wacom)
767 {
768         int error;
769
770         /* Initialize default values */
771         switch (wacom->wacom_wac.features.type) {
772         case INTUOS4S:
773         case INTUOS4:
774         case INTUOS4WL:
775         case INTUOS4L:
776                 wacom->led.select[0] = 0;
777                 wacom->led.select[1] = 0;
778                 wacom->led.llv = 10;
779                 wacom->led.hlv = 20;
780                 wacom->led.img_lum = 10;
781                 error = sysfs_create_group(&wacom->hdev->dev.kobj,
782                                            &intuos4_led_attr_group);
783                 break;
784
785         case WACOM_24HD:
786         case WACOM_21UX2:
787                 wacom->led.select[0] = 0;
788                 wacom->led.select[1] = 0;
789                 wacom->led.llv = 0;
790                 wacom->led.hlv = 0;
791                 wacom->led.img_lum = 0;
792
793                 error = sysfs_create_group(&wacom->hdev->dev.kobj,
794                                            &cintiq_led_attr_group);
795                 break;
796
797         case INTUOS5S:
798         case INTUOS5:
799         case INTUOS5L:
800         case INTUOSPS:
801         case INTUOSPM:
802         case INTUOSPL:
803                 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
804                         wacom->led.select[0] = 0;
805                         wacom->led.select[1] = 0;
806                         wacom->led.llv = 32;
807                         wacom->led.hlv = 0;
808                         wacom->led.img_lum = 0;
809
810                         error = sysfs_create_group(&wacom->hdev->dev.kobj,
811                                                   &intuos5_led_attr_group);
812                 } else
813                         return 0;
814                 break;
815
816         default:
817                 return 0;
818         }
819
820         if (error) {
821                 hid_err(wacom->hdev,
822                         "cannot create sysfs group err: %d\n", error);
823                 return error;
824         }
825         wacom_led_control(wacom);
826         wacom->led_initialized = true;
827
828         return 0;
829 }
830
831 static void wacom_destroy_leds(struct wacom *wacom)
832 {
833         if (!wacom->led_initialized)
834                 return;
835
836         wacom->led_initialized = false;
837
838         switch (wacom->wacom_wac.features.type) {
839         case INTUOS4S:
840         case INTUOS4:
841         case INTUOS4WL:
842         case INTUOS4L:
843                 sysfs_remove_group(&wacom->hdev->dev.kobj,
844                                    &intuos4_led_attr_group);
845                 break;
846
847         case WACOM_24HD:
848         case WACOM_21UX2:
849                 sysfs_remove_group(&wacom->hdev->dev.kobj,
850                                    &cintiq_led_attr_group);
851                 break;
852
853         case INTUOS5S:
854         case INTUOS5:
855         case INTUOS5L:
856         case INTUOSPS:
857         case INTUOSPM:
858         case INTUOSPL:
859                 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
860                         sysfs_remove_group(&wacom->hdev->dev.kobj,
861                                            &intuos5_led_attr_group);
862                 break;
863         }
864 }
865
866 static enum power_supply_property wacom_battery_props[] = {
867         POWER_SUPPLY_PROP_STATUS,
868         POWER_SUPPLY_PROP_SCOPE,
869         POWER_SUPPLY_PROP_CAPACITY
870 };
871
872 static enum power_supply_property wacom_ac_props[] = {
873         POWER_SUPPLY_PROP_PRESENT,
874         POWER_SUPPLY_PROP_ONLINE,
875         POWER_SUPPLY_PROP_SCOPE,
876 };
877
878 static int wacom_battery_get_property(struct power_supply *psy,
879                                       enum power_supply_property psp,
880                                       union power_supply_propval *val)
881 {
882         struct wacom *wacom = container_of(psy, struct wacom, battery);
883         int ret = 0;
884
885         switch (psp) {
886                 case POWER_SUPPLY_PROP_SCOPE:
887                         val->intval = POWER_SUPPLY_SCOPE_DEVICE;
888                         break;
889                 case POWER_SUPPLY_PROP_CAPACITY:
890                         val->intval =
891                                 wacom->wacom_wac.battery_capacity;
892                         break;
893                 case POWER_SUPPLY_PROP_STATUS:
894                         if (wacom->wacom_wac.bat_charging)
895                                 val->intval = POWER_SUPPLY_STATUS_CHARGING;
896                         else if (wacom->wacom_wac.battery_capacity == 100 &&
897                                     wacom->wacom_wac.ps_connected)
898                                 val->intval = POWER_SUPPLY_STATUS_FULL;
899                         else
900                                 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
901                         break;
902                 default:
903                         ret = -EINVAL;
904                         break;
905         }
906
907         return ret;
908 }
909
910 static int wacom_ac_get_property(struct power_supply *psy,
911                                 enum power_supply_property psp,
912                                 union power_supply_propval *val)
913 {
914         struct wacom *wacom = container_of(psy, struct wacom, ac);
915         int ret = 0;
916
917         switch (psp) {
918         case POWER_SUPPLY_PROP_PRESENT:
919                 /* fall through */
920         case POWER_SUPPLY_PROP_ONLINE:
921                 val->intval = wacom->wacom_wac.ps_connected;
922                 break;
923         case POWER_SUPPLY_PROP_SCOPE:
924                 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
925                 break;
926         default:
927                 ret = -EINVAL;
928                 break;
929         }
930         return ret;
931 }
932
933 static int wacom_initialize_battery(struct wacom *wacom)
934 {
935         static atomic_t battery_no = ATOMIC_INIT(0);
936         int error;
937         unsigned long n;
938
939         if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
940                 n = atomic_inc_return(&battery_no) - 1;
941
942                 wacom->battery.properties = wacom_battery_props;
943                 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
944                 wacom->battery.get_property = wacom_battery_get_property;
945                 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
946                 wacom->battery.name = wacom->wacom_wac.bat_name;
947                 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
948                 wacom->battery.use_for_apm = 0;
949
950                 wacom->ac.properties = wacom_ac_props;
951                 wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
952                 wacom->ac.get_property = wacom_ac_get_property;
953                 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
954                 wacom->ac.name = wacom->wacom_wac.ac_name;
955                 wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
956                 wacom->ac.use_for_apm = 0;
957
958                 error = power_supply_register(&wacom->hdev->dev,
959                                               &wacom->battery);
960                 if (error)
961                         return error;
962
963                 power_supply_powers(&wacom->battery, &wacom->hdev->dev);
964
965                 error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
966                 if (error) {
967                         power_supply_unregister(&wacom->battery);
968                         return error;
969                 }
970
971                 power_supply_powers(&wacom->ac, &wacom->hdev->dev);
972         }
973
974         return 0;
975 }
976
977 static void wacom_destroy_battery(struct wacom *wacom)
978 {
979         if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
980              wacom->battery.dev) {
981                 power_supply_unregister(&wacom->battery);
982                 wacom->battery.dev = NULL;
983                 power_supply_unregister(&wacom->ac);
984                 wacom->ac.dev = NULL;
985         }
986 }
987
988 static ssize_t wacom_show_speed(struct device *dev,
989                                 struct device_attribute
990                                 *attr, char *buf)
991 {
992         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
993         struct wacom *wacom = hid_get_drvdata(hdev);
994
995         return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
996 }
997
998 static ssize_t wacom_store_speed(struct device *dev,
999                                 struct device_attribute *attr,
1000                                 const char *buf, size_t count)
1001 {
1002         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1003         struct wacom *wacom = hid_get_drvdata(hdev);
1004         u8 new_speed;
1005
1006         if (kstrtou8(buf, 0, &new_speed))
1007                 return -EINVAL;
1008
1009         if (new_speed != 0 && new_speed != 1)
1010                 return -EINVAL;
1011
1012         wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1013
1014         return count;
1015 }
1016
1017 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1018                 wacom_show_speed, wacom_store_speed);
1019
1020 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1021 {
1022         struct input_dev *input_dev;
1023         struct hid_device *hdev = wacom->hdev;
1024         struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1025
1026         input_dev = input_allocate_device();
1027         if (!input_dev)
1028                 return NULL;
1029
1030         input_dev->name = wacom_wac->name;
1031         input_dev->phys = hdev->phys;
1032         input_dev->dev.parent = &hdev->dev;
1033         input_dev->open = wacom_open;
1034         input_dev->close = wacom_close;
1035         input_dev->uniq = hdev->uniq;
1036         input_dev->id.bustype = hdev->bus;
1037         input_dev->id.vendor  = hdev->vendor;
1038         input_dev->id.product = hdev->product;
1039         input_dev->id.version = hdev->version;
1040         input_set_drvdata(input_dev, wacom);
1041
1042         return input_dev;
1043 }
1044
1045 static void wacom_unregister_inputs(struct wacom *wacom)
1046 {
1047         if (wacom->wacom_wac.input)
1048                 input_unregister_device(wacom->wacom_wac.input);
1049         if (wacom->wacom_wac.pad_input)
1050                 input_unregister_device(wacom->wacom_wac.pad_input);
1051         wacom->wacom_wac.input = NULL;
1052         wacom->wacom_wac.pad_input = NULL;
1053         wacom_destroy_leds(wacom);
1054 }
1055
1056 static int wacom_register_inputs(struct wacom *wacom)
1057 {
1058         struct input_dev *input_dev, *pad_input_dev;
1059         struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1060         int error;
1061
1062         input_dev = wacom_allocate_input(wacom);
1063         pad_input_dev = wacom_allocate_input(wacom);
1064         if (!input_dev || !pad_input_dev) {
1065                 error = -ENOMEM;
1066                 goto fail1;
1067         }
1068
1069         wacom_wac->input = input_dev;
1070         wacom_wac->pad_input = pad_input_dev;
1071         wacom_wac->pad_input->name = wacom_wac->pad_name;
1072
1073         error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1074         if (error)
1075                 goto fail2;
1076
1077         error = input_register_device(input_dev);
1078         if (error)
1079                 goto fail2;
1080
1081         error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1082         if (error) {
1083                 /* no pad in use on this interface */
1084                 input_free_device(pad_input_dev);
1085                 wacom_wac->pad_input = NULL;
1086                 pad_input_dev = NULL;
1087         } else {
1088                 error = input_register_device(pad_input_dev);
1089                 if (error)
1090                         goto fail3;
1091
1092                 error = wacom_initialize_leds(wacom);
1093                 if (error)
1094                         goto fail4;
1095         }
1096
1097         return 0;
1098
1099 fail4:
1100         input_unregister_device(pad_input_dev);
1101         pad_input_dev = NULL;
1102 fail3:
1103         input_unregister_device(input_dev);
1104         input_dev = NULL;
1105 fail2:
1106         wacom_wac->input = NULL;
1107         wacom_wac->pad_input = NULL;
1108 fail1:
1109         if (input_dev)
1110                 input_free_device(input_dev);
1111         if (pad_input_dev)
1112                 input_free_device(pad_input_dev);
1113         return error;
1114 }
1115
1116 static void wacom_wireless_work(struct work_struct *work)
1117 {
1118         struct wacom *wacom = container_of(work, struct wacom, work);
1119         struct usb_device *usbdev = wacom->usbdev;
1120         struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1121         struct hid_device *hdev1, *hdev2;
1122         struct wacom *wacom1, *wacom2;
1123         struct wacom_wac *wacom_wac1, *wacom_wac2;
1124         int error;
1125
1126         /*
1127          * Regardless if this is a disconnect or a new tablet,
1128          * remove any existing input and battery devices.
1129          */
1130
1131         wacom_destroy_battery(wacom);
1132
1133         /* Stylus interface */
1134         hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1135         wacom1 = hid_get_drvdata(hdev1);
1136         wacom_wac1 = &(wacom1->wacom_wac);
1137         wacom_unregister_inputs(wacom1);
1138
1139         /* Touch interface */
1140         hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1141         wacom2 = hid_get_drvdata(hdev2);
1142         wacom_wac2 = &(wacom2->wacom_wac);
1143         wacom_unregister_inputs(wacom2);
1144
1145         if (wacom_wac->pid == 0) {
1146                 hid_info(wacom->hdev, "wireless tablet disconnected\n");
1147                 wacom_wac1->shared->type = 0;
1148         } else {
1149                 const struct hid_device_id *id = wacom_ids;
1150
1151                 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1152                          wacom_wac->pid);
1153
1154                 while (id->bus) {
1155                         if (id->vendor == USB_VENDOR_ID_WACOM &&
1156                             id->product == wacom_wac->pid)
1157                                 break;
1158                         id++;
1159                 }
1160
1161                 if (!id->bus) {
1162                         hid_info(wacom->hdev, "ignoring unknown PID.\n");
1163                         return;
1164                 }
1165
1166                 /* Stylus interface */
1167                 wacom_wac1->features =
1168                         *((struct wacom_features *)id->driver_data);
1169                 wacom_wac1->features.device_type = BTN_TOOL_PEN;
1170                 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1171                          wacom_wac1->features.name);
1172                 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1173                          wacom_wac1->features.name);
1174                 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1175                 wacom_wac1->shared->type = wacom_wac1->features.type;
1176                 wacom_wac1->pid = wacom_wac->pid;
1177                 error = wacom_register_inputs(wacom1);
1178                 if (error)
1179                         goto fail;
1180
1181                 /* Touch interface */
1182                 if (wacom_wac1->features.touch_max ||
1183                     wacom_wac1->features.type == INTUOSHT) {
1184                         wacom_wac2->features =
1185                                 *((struct wacom_features *)id->driver_data);
1186                         wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1187                         wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1188                         wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1189                         if (wacom_wac2->features.touch_max)
1190                                 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1191                                          "%s (WL) Finger",wacom_wac2->features.name);
1192                         else
1193                                 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1194                                          "%s (WL) Pad",wacom_wac2->features.name);
1195                         snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1196                                  "%s (WL) Pad", wacom_wac2->features.name);
1197                         wacom_wac2->pid = wacom_wac->pid;
1198                         error = wacom_register_inputs(wacom2);
1199                         if (error)
1200                                 goto fail;
1201
1202                         if (wacom_wac1->features.type == INTUOSHT &&
1203                             wacom_wac1->features.touch_max)
1204                                 wacom_wac->shared->touch_input = wacom_wac2->input;
1205                 }
1206
1207                 error = wacom_initialize_battery(wacom);
1208                 if (error)
1209                         goto fail;
1210         }
1211
1212         return;
1213
1214 fail:
1215         wacom_unregister_inputs(wacom1);
1216         wacom_unregister_inputs(wacom2);
1217         return;
1218 }
1219
1220 /*
1221  * Not all devices report physical dimensions from HID.
1222  * Compute the default from hardcoded logical dimension
1223  * and resolution before driver overwrites them.
1224  */
1225 static void wacom_set_default_phy(struct wacom_features *features)
1226 {
1227         if (features->x_resolution) {
1228                 features->x_phy = (features->x_max * 100) /
1229                                         features->x_resolution;
1230                 features->y_phy = (features->y_max * 100) /
1231                                         features->y_resolution;
1232         }
1233 }
1234
1235 static void wacom_calculate_res(struct wacom_features *features)
1236 {
1237         features->x_resolution = wacom_calc_hid_res(features->x_max,
1238                                                     features->x_phy,
1239                                                     features->unit,
1240                                                     features->unitExpo);
1241         features->y_resolution = wacom_calc_hid_res(features->y_max,
1242                                                     features->y_phy,
1243                                                     features->unit,
1244                                                     features->unitExpo);
1245 }
1246
1247 static int wacom_hid_report_len(struct hid_report *report)
1248 {
1249         /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1250         return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1251 }
1252
1253 static size_t wacom_compute_pktlen(struct hid_device *hdev)
1254 {
1255         struct hid_report_enum *report_enum;
1256         struct hid_report *report;
1257         size_t size = 0;
1258
1259         report_enum = hdev->report_enum + HID_INPUT_REPORT;
1260
1261         list_for_each_entry(report, &report_enum->report_list, list) {
1262                 size_t report_size = wacom_hid_report_len(report);
1263                 if (report_size > size)
1264                         size = report_size;
1265         }
1266
1267         return size;
1268 }
1269
1270 static int wacom_probe(struct hid_device *hdev,
1271                 const struct hid_device_id *id)
1272 {
1273         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1274         struct usb_device *dev = interface_to_usbdev(intf);
1275         struct wacom *wacom;
1276         struct wacom_wac *wacom_wac;
1277         struct wacom_features *features;
1278         int error;
1279
1280         if (!id->driver_data)
1281                 return -EINVAL;
1282
1283         wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1284         if (!wacom)
1285                 return -ENOMEM;
1286
1287         hid_set_drvdata(hdev, wacom);
1288         wacom->hdev = hdev;
1289
1290         /* ask for the report descriptor to be loaded by HID */
1291         error = hid_parse(hdev);
1292         if (error) {
1293                 hid_err(hdev, "parse failed\n");
1294                 goto fail1;
1295         }
1296
1297         wacom_wac = &wacom->wacom_wac;
1298         wacom_wac->features = *((struct wacom_features *)id->driver_data);
1299         features = &wacom_wac->features;
1300         features->pktlen = wacom_compute_pktlen(hdev);
1301         if (features->pktlen > WACOM_PKGLEN_MAX) {
1302                 error = -EINVAL;
1303                 goto fail1;
1304         }
1305
1306         if (features->check_for_hid_type && features->hid_type != hdev->type) {
1307                 error = -ENODEV;
1308                 goto fail1;
1309         }
1310
1311         wacom->usbdev = dev;
1312         wacom->intf = intf;
1313         mutex_init(&wacom->lock);
1314         INIT_WORK(&wacom->work, wacom_wireless_work);
1315
1316         /* set the default size in case we do not get them from hid */
1317         wacom_set_default_phy(features);
1318
1319         /* Retrieve the physical and logical size for touch devices */
1320         wacom_retrieve_hid_descriptor(hdev, features);
1321
1322         /*
1323          * Intuos5 has no useful data about its touch interface in its
1324          * HID descriptor. If this is the touch interface (PacketSize
1325          * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1326          */
1327         if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
1328                 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
1329                         features->device_type = BTN_TOOL_FINGER;
1330
1331                         features->x_max = 4096;
1332                         features->y_max = 4096;
1333                 } else {
1334                         features->device_type = BTN_TOOL_PEN;
1335                 }
1336         }
1337
1338         /*
1339          * Same thing for Bamboo 3rd gen.
1340          */
1341         if ((features->type == BAMBOO_PT) &&
1342             (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1343             (features->device_type == BTN_TOOL_PEN)) {
1344                 features->device_type = BTN_TOOL_FINGER;
1345
1346                 features->x_max = 4096;
1347                 features->y_max = 4096;
1348         }
1349
1350         if (hdev->bus == BUS_BLUETOOTH)
1351                 features->quirks |= WACOM_QUIRK_BATTERY;
1352
1353         wacom_setup_device_quirks(features);
1354
1355         /* set unit to "100th of a mm" for devices not reported by HID */
1356         if (!features->unit) {
1357                 features->unit = 0x11;
1358                 features->unitExpo = -3;
1359         }
1360         wacom_calculate_res(features);
1361
1362         strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1363         snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1364                 "%s Pad", features->name);
1365
1366         if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
1367                 /* Append the device type to the name */
1368                 if (features->device_type != BTN_TOOL_FINGER)
1369                         strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1370                 else if (features->touch_max)
1371                         strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1372                 else
1373                         strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
1374
1375                 error = wacom_add_shared_data(hdev);
1376                 if (error)
1377                         goto fail1;
1378         }
1379
1380         if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1381              (features->quirks & WACOM_QUIRK_BATTERY)) {
1382                 error = wacom_initialize_battery(wacom);
1383                 if (error)
1384                         goto fail2;
1385         }
1386
1387         if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1388                 error = wacom_register_inputs(wacom);
1389                 if (error)
1390                         goto fail3;
1391         }
1392
1393         if (hdev->bus == BUS_BLUETOOTH) {
1394                 error = device_create_file(&hdev->dev, &dev_attr_speed);
1395                 if (error)
1396                         hid_warn(hdev,
1397                                  "can't create sysfs speed attribute err: %d\n",
1398                                  error);
1399         }
1400
1401         /* Note that if query fails it is not a hard failure */
1402         wacom_query_tablet_data(hdev, features);
1403
1404         /* Regular HID work starts now */
1405         error = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1406         if (error) {
1407                 hid_err(hdev, "hw start failed\n");
1408                 goto fail4;
1409         }
1410
1411         if (features->quirks & WACOM_QUIRK_MONITOR)
1412                 error = hid_hw_open(hdev);
1413
1414         if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1415                 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1416                         wacom_wac->shared->touch_input = wacom_wac->input;
1417         }
1418
1419         return 0;
1420
1421  fail4: if (hdev->bus == BUS_BLUETOOTH)
1422                 device_remove_file(&hdev->dev, &dev_attr_speed);
1423         wacom_unregister_inputs(wacom);
1424  fail3: wacom_destroy_battery(wacom);
1425  fail2: wacom_remove_shared_data(wacom_wac);
1426  fail1: kfree(wacom);
1427         hid_set_drvdata(hdev, NULL);
1428         return error;
1429 }
1430
1431 static void wacom_remove(struct hid_device *hdev)
1432 {
1433         struct wacom *wacom = hid_get_drvdata(hdev);
1434
1435         hid_hw_stop(hdev);
1436
1437         cancel_work_sync(&wacom->work);
1438         wacom_unregister_inputs(wacom);
1439         if (hdev->bus == BUS_BLUETOOTH)
1440                 device_remove_file(&hdev->dev, &dev_attr_speed);
1441         wacom_destroy_battery(wacom);
1442         wacom_remove_shared_data(&wacom->wacom_wac);
1443
1444         hid_set_drvdata(hdev, NULL);
1445         kfree(wacom);
1446 }
1447
1448 #ifdef CONFIG_PM
1449 static int wacom_resume(struct hid_device *hdev)
1450 {
1451         struct wacom *wacom = hid_get_drvdata(hdev);
1452         struct wacom_features *features = &wacom->wacom_wac.features;
1453
1454         mutex_lock(&wacom->lock);
1455
1456         /* switch to wacom mode first */
1457         wacom_query_tablet_data(hdev, features);
1458         wacom_led_control(wacom);
1459
1460         mutex_unlock(&wacom->lock);
1461
1462         return 0;
1463 }
1464
1465 static int wacom_reset_resume(struct hid_device *hdev)
1466 {
1467         return wacom_resume(hdev);
1468 }
1469 #endif /* CONFIG_PM */
1470
1471 static struct hid_driver wacom_driver = {
1472         .name =         "wacom",
1473         .id_table =     wacom_ids,
1474         .probe =        wacom_probe,
1475         .remove =       wacom_remove,
1476 #ifdef CONFIG_PM
1477         .resume =       wacom_resume,
1478         .reset_resume = wacom_reset_resume,
1479 #endif
1480         .raw_event =    wacom_raw_event,
1481 };
1482 module_hid_driver(wacom_driver);
1483
1484 MODULE_VERSION(DRIVER_VERSION);
1485 MODULE_AUTHOR(DRIVER_AUTHOR);
1486 MODULE_DESCRIPTION(DRIVER_DESC);
1487 MODULE_LICENSE(DRIVER_LICENSE);