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