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