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