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