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