]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/hid/wacom_sys.c
drm/i915/gvt: hold reference of VFIO group during opening of vgpu
[mirror_ubuntu-hirsute-kernel.git] / drivers / hid / wacom_sys.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
3bea733a 2/*
4104d13f 3 * drivers/input/tablet/wacom_sys.c
3bea733a 4 *
232f5693 5 * USB Wacom tablet support - system specific code
3bea733a
PC
6 */
7
8/*
3bea733a
PC
9 */
10
3bea733a 11#include "wacom_wac.h"
51269fe8 12#include "wacom.h"
b58ba1ba 13#include <linux/input/mt.h>
3bea733a 14
a417ea44 15#define WAC_MSG_RETRIES 5
5d7e7d47
EH
16#define WAC_CMD_RETRIES 10
17
e0984bc3
PC
18#define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
19#define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
72b236d6 20#define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
e0984bc3 21
c64d8834
PC
22static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
23 size_t size, unsigned int retries)
3bea733a 24{
5d7e7d47
EH
25 int retval;
26
27 do {
c64d8834 28 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
27b20a9d 29 HID_REQ_GET_REPORT);
aef3156d
JG
30 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
31
32 if (retval < 0)
33 hid_err(hdev, "wacom_get_report: ran out of retries "
34 "(last error = %d)\n", retval);
5d7e7d47
EH
35
36 return retval;
3bea733a
PC
37}
38
296b7378
PF
39static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
40 size_t size, unsigned int retries)
3bea733a 41{
5d7e7d47
EH
42 int retval;
43
44 do {
296b7378 45 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
27b20a9d 46 HID_REQ_SET_REPORT);
aef3156d
JG
47 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
48
49 if (retval < 0)
50 hid_err(hdev, "wacom_set_report: ran out of retries "
51 "(last error = %d)\n", retval);
5d7e7d47
EH
52
53 return retval;
3bea733a
PC
54}
55
83417206
JG
56static void wacom_wac_queue_insert(struct hid_device *hdev,
57 struct kfifo_rec_ptr_2 *fifo,
58 u8 *raw_data, int size)
59{
60 bool warned = false;
61
62 while (kfifo_avail(fifo) < size) {
63 if (!warned)
64 hid_warn(hdev, "%s: kfifo has filled, starting to drop events\n", __func__);
65 warned = true;
66
67 kfifo_skip(fifo);
68 }
69
70 kfifo_in(fifo, raw_data, size);
71}
72
73static void wacom_wac_queue_flush(struct hid_device *hdev,
74 struct kfifo_rec_ptr_2 *fifo)
75{
76 while (!kfifo_is_empty(fifo)) {
77 u8 buf[WACOM_PKGLEN_MAX];
78 int size;
79 int err;
80
81 size = kfifo_out(fifo, buf, sizeof(buf));
82 err = hid_report_raw_event(hdev, HID_INPUT_REPORT, buf, size, false);
83 if (err) {
84 hid_warn(hdev, "%s: unable to flush event due to error %d\n",
85 __func__, err);
86 }
87 }
88}
89
90static int wacom_wac_pen_serial_enforce(struct hid_device *hdev,
073b50bc 91 struct hid_report *report, u8 *raw_data, int report_size)
83417206
JG
92{
93 struct wacom *wacom = hid_get_drvdata(hdev);
94 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
95 struct wacom_features *features = &wacom_wac->features;
96 bool flush = false;
97 bool insert = false;
98 int i, j;
99
100 if (wacom_wac->serial[0] || !(features->quirks & WACOM_QUIRK_TOOLSERIAL))
101 return 0;
102
103 /* Queue events which have invalid tool type or serial number */
104 for (i = 0; i < report->maxfield; i++) {
105 for (j = 0; j < report->field[i]->maxusage; j++) {
106 struct hid_field *field = report->field[i];
107 struct hid_usage *usage = &field->usage[j];
108 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
109 unsigned int offset;
110 unsigned int size;
111 unsigned int value;
112
113 if (equivalent_usage != HID_DG_INRANGE &&
114 equivalent_usage != HID_DG_TOOLSERIALNUMBER &&
115 equivalent_usage != WACOM_HID_WD_SERIALHI &&
116 equivalent_usage != WACOM_HID_WD_TOOLTYPE)
117 continue;
118
119 offset = field->report_offset;
120 size = field->report_size;
121 value = hid_field_extract(hdev, raw_data+1, offset + j * size, size);
122
123 /* If we go out of range, we need to flush the queue ASAP */
124 if (equivalent_usage == HID_DG_INRANGE)
125 value = !value;
126
127 if (value) {
128 flush = true;
129 switch (equivalent_usage) {
130 case HID_DG_TOOLSERIALNUMBER:
131 wacom_wac->serial[0] = value;
132 break;
133
134 case WACOM_HID_WD_SERIALHI:
135 wacom_wac->serial[0] |= ((__u64)value) << 32;
136 break;
137
138 case WACOM_HID_WD_TOOLTYPE:
139 wacom_wac->id[0] = value;
140 break;
141 }
142 }
143 else {
144 insert = true;
145 }
146 }
147 }
148
149 if (flush)
150 wacom_wac_queue_flush(hdev, &wacom_wac->pen_fifo);
151 else if (insert)
073b50bc
JG
152 wacom_wac_queue_insert(hdev, &wacom_wac->pen_fifo,
153 raw_data, report_size);
83417206
JG
154
155 return insert && !flush;
156}
157
29b47391
BT
158static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
159 u8 *raw_data, int size)
3bea733a 160{
29b47391 161 struct wacom *wacom = hid_get_drvdata(hdev);
3bea733a 162
29b47391
BT
163 if (size > WACOM_PKGLEN_MAX)
164 return 1;
3bea733a 165
83417206
JG
166 if (wacom_wac_pen_serial_enforce(hdev, report, raw_data, size))
167 return -1;
168
29b47391 169 memcpy(wacom->wacom_wac.data, raw_data, size);
3bea733a 170
29b47391
BT
171 wacom_wac_irq(&wacom->wacom_wac, size);
172
173 return 0;
3bea733a
PC
174}
175
3bea733a
PC
176static int wacom_open(struct input_dev *dev)
177{
7791bdae 178 struct wacom *wacom = input_get_drvdata(dev);
29b47391 179
dff67416 180 return hid_hw_open(wacom->hdev);
3bea733a
PC
181}
182
183static void wacom_close(struct input_dev *dev)
184{
7791bdae 185 struct wacom *wacom = input_get_drvdata(dev);
3bea733a 186
3dad188e
BT
187 /*
188 * wacom->hdev should never be null, but surprisingly, I had the case
189 * once while unplugging the Wacom Wireless Receiver.
190 */
191 if (wacom->hdev)
192 hid_hw_close(wacom->hdev);
3bea733a
PC
193}
194
115d5e12 195/*
198fdee2 196 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
115d5e12
JG
197 */
198static int wacom_calc_hid_res(int logical_extents, int physical_extents,
c669fb2b 199 unsigned unit, int exponent)
115d5e12 200{
198fdee2
BT
201 struct hid_field field = {
202 .logical_maximum = logical_extents,
203 .physical_maximum = physical_extents,
204 .unit = unit,
205 .unit_exponent = exponent,
206 };
207
208 return hidinput_calc_abs_res(&field, ABS_X);
115d5e12
JG
209}
210
57832512
JG
211static void wacom_hid_usage_quirk(struct hid_device *hdev,
212 struct hid_field *field, struct hid_usage *usage)
213{
214 struct wacom *wacom = hid_get_drvdata(hdev);
215 struct wacom_features *features = &wacom->wacom_wac.features;
216 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
217
218 /*
219 * The Dell Canvas 27 needs to be switched to its vendor-defined
220 * report to provide the best resolution.
221 */
222 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
223 hdev->product == 0x4200 &&
224 field->application == HID_UP_MSVENDOR) {
225 wacom->wacom_wac.mode_report = field->report->id;
226 wacom->wacom_wac.mode_value = 2;
227 }
228
229 /*
230 * ISDv4 devices which predate HID's adoption of the
231 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
232 * position instead. We can accurately detect if a
233 * usage with that value should be HID_DG_BARRELSWITCH2
234 * based on the surrounding usages, which have remained
235 * constant across generations.
236 */
237 if (features->type == HID_GENERIC &&
238 usage->hid == 0x000D0000 &&
239 field->application == HID_DG_PEN &&
240 field->physical == HID_DG_STYLUS) {
241 int i = usage->usage_index;
242
243 if (i-4 >= 0 && i+1 < field->maxusage &&
244 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
245 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
246 field->usage[i-2].hid == HID_DG_ERASER &&
247 field->usage[i-1].hid == HID_DG_INVERT &&
248 field->usage[i+1].hid == HID_DG_INRANGE) {
249 usage->hid = HID_DG_BARRELSWITCH2;
250 }
251 }
252
e9fe0d49
JG
253 /*
254 * Wacom's AES devices use different vendor-defined usages to
255 * report serial number information compared to their branded
256 * hardware. The usages are also sometimes ill-defined and do
257 * not have the correct logical min/max values set. Lets patch
258 * the descriptor to use the branded usage convention and fix
259 * the errors.
260 */
261 if (usage->hid == WACOM_HID_WT_SERIALNUMBER &&
262 field->report_size == 16 &&
263 field->index + 2 < field->report->maxfield) {
264 struct hid_field *a = field->report->field[field->index + 1];
265 struct hid_field *b = field->report->field[field->index + 2];
266
267 if (a->maxusage > 0 &&
268 a->usage[0].hid == HID_DG_TOOLSERIALNUMBER &&
269 a->report_size == 32 &&
270 b->maxusage > 0 &&
271 b->usage[0].hid == 0xFF000000 &&
272 b->report_size == 8) {
273 features->quirks |= WACOM_QUIRK_AESPEN;
274 usage->hid = WACOM_HID_WD_TOOLTYPE;
275 field->logical_minimum = S16_MIN;
276 field->logical_maximum = S16_MAX;
277 a->logical_minimum = S32_MIN;
278 a->logical_maximum = S32_MAX;
279 b->usage[0].hid = WACOM_HID_WD_SERIALHI;
280 b->logical_minimum = 0;
281 b->logical_maximum = U8_MAX;
282 }
283 }
284
57832512
JG
285 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
286 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
287 hdev->product == 0x0358 &&
288 WACOM_PEN_FIELD(field) &&
289 equivalent_usage == HID_GD_Y) {
290 field->logical_maximum = 43200;
291 }
292}
293
c669fb2b
BT
294static void wacom_feature_mapping(struct hid_device *hdev,
295 struct hid_field *field, struct hid_usage *usage)
f393ee2b 296{
c669fb2b
BT
297 struct wacom *wacom = hid_get_drvdata(hdev);
298 struct wacom_features *features = &wacom->wacom_wac.features;
5ae6e89f 299 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
ac2423c9 300 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
8ffffd52
BT
301 u8 *data;
302 int ret;
3064a03b 303 u32 n;
f393ee2b 304
57832512
JG
305 wacom_hid_usage_quirk(hdev, field, usage);
306
ac2423c9 307 switch (equivalent_usage) {
d8e98060
AAS
308 case WACOM_HID_WD_TOUCH_RING_SETTING:
309 wacom->generic_has_leds = true;
310 break;
c669fb2b
BT
311 case HID_DG_CONTACTMAX:
312 /* leave touch_max as is if predefined */
8ffffd52
BT
313 if (!features->touch_max) {
314 /* read manually */
184eccd4
AAS
315 n = hid_report_len(field->report);
316 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
8ffffd52
BT
317 if (!data)
318 break;
319 data[0] = field->report->id;
320 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
184eccd4
AAS
321 data, n, WAC_CMD_RETRIES);
322 if (ret == n) {
323 ret = hid_report_raw_event(hdev,
324 HID_FEATURE_REPORT, data, n, 0);
05e8fd92
JG
325 } else {
326 features->touch_max = 16;
327 hid_warn(hdev, "wacom_feature_mapping: "
328 "could not get HID_DG_CONTACTMAX, "
329 "defaulting to %d\n",
330 features->touch_max);
331 }
8ffffd52
BT
332 kfree(data);
333 }
c669fb2b 334 break;
5ae6e89f
BT
335 case HID_DG_INPUTMODE:
336 /* Ignore if value index is out of bounds. */
337 if (usage->usage_index >= field->report_count) {
338 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
339 break;
340 }
341
342 hid_data->inputmode = field->report->id;
343 hid_data->inputmode_index = usage->usage_index;
344 break;
326ea2a9
JG
345
346 case HID_UP_DIGITIZER:
347 if (field->report->id == 0x0B &&
8de82280
JG
348 (field->application == WACOM_HID_G9_PEN ||
349 field->application == WACOM_HID_G11_PEN)) {
326ea2a9
JG
350 wacom->wacom_wac.mode_report = field->report->id;
351 wacom->wacom_wac.mode_value = 0;
352 }
353 break;
354
c9c09587
JG
355 case WACOM_HID_WD_DATAMODE:
356 wacom->wacom_wac.mode_report = field->report->id;
357 wacom->wacom_wac.mode_value = 2;
358 break;
359
8de82280
JG
360 case WACOM_HID_UP_G9:
361 case WACOM_HID_UP_G11:
326ea2a9 362 if (field->report->id == 0x03 &&
8de82280
JG
363 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
364 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
326ea2a9
JG
365 wacom->wacom_wac.mode_report = field->report->id;
366 wacom->wacom_wac.mode_value = 0;
367 }
368 break;
345857bb
JG
369 case WACOM_HID_WD_OFFSETLEFT:
370 case WACOM_HID_WD_OFFSETTOP:
371 case WACOM_HID_WD_OFFSETRIGHT:
372 case WACOM_HID_WD_OFFSETBOTTOM:
373 /* read manually */
374 n = hid_report_len(field->report);
375 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
376 if (!data)
377 break;
378 data[0] = field->report->id;
379 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
380 data, n, WAC_CMD_RETRIES);
381 if (ret == n) {
382 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
383 data, n, 0);
384 } else {
385 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
386 __func__);
387 }
388 kfree(data);
389 break;
f393ee2b
PC
390 }
391}
392
428f8588
CB
393/*
394 * Interface Descriptor of wacom devices can be incomplete and
395 * inconsistent so wacom_features table is used to store stylus
396 * device's packet lengths, various maximum values, and tablet
397 * resolution based on product ID's.
398 *
399 * For devices that contain 2 interfaces, wacom_features table is
400 * inaccurate for the touch interface. Since the Interface Descriptor
401 * for touch interfaces has pretty complete data, this function exists
402 * to query tablet for this missing information instead of hard coding in
403 * an additional table.
404 *
405 * A typical Interface Descriptor for a stylus will contain a
406 * boot mouse application collection that is not of interest and this
407 * function will ignore it.
408 *
409 * It also contains a digitizer application collection that also is not
410 * of interest since any information it contains would be duplicate
411 * of what is in wacom_features. Usually it defines a report of an array
412 * of bytes that could be used as max length of the stylus packet returned.
413 * If it happens to define a Digitizer-Stylus Physical Collection then
414 * the X and Y logical values contain valid data but it is ignored.
415 *
416 * A typical Interface Descriptor for a touch interface will contain a
417 * Digitizer-Finger Physical Collection which will define both logical
418 * X/Y maximum as well as the physical size of tablet. Since touch
419 * interfaces haven't supported pressure or distance, this is enough
420 * information to override invalid values in the wacom_features table.
4134361a 421 *
c669fb2b
BT
422 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
423 * data. We deal with them after returning from this function.
428f8588 424 */
c669fb2b
BT
425static void wacom_usage_mapping(struct hid_device *hdev,
426 struct hid_field *field, struct hid_usage *usage)
545f4e99 427{
c669fb2b
BT
428 struct wacom *wacom = hid_get_drvdata(hdev);
429 struct wacom_features *features = &wacom->wacom_wac.features;
d97a5522
BT
430 bool finger = WACOM_FINGER_FIELD(field);
431 bool pen = WACOM_PEN_FIELD(field);
418b573b 432 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
e9fc413f 433
c669fb2b
BT
434 /*
435 * Requiring Stylus Usage will ignore boot mouse
436 * X/Y values and some cases of invalid Digitizer X/Y
437 * values commonly reported.
438 */
042628ab 439 if (pen)
aa86b18c 440 features->device_type |= WACOM_DEVICETYPE_PEN;
042628ab 441 else if (finger)
aa86b18c 442 features->device_type |= WACOM_DEVICETYPE_TOUCH;
042628ab 443 else
c669fb2b
BT
444 return;
445
57832512 446 wacom_hid_usage_quirk(hdev, field, usage);
d471b6b2 447
418b573b 448 switch (equivalent_usage) {
c669fb2b
BT
449 case HID_GD_X:
450 features->x_max = field->logical_maximum;
451 if (finger) {
c669fb2b 452 features->x_phy = field->physical_maximum;
3b164a00
PC
453 if ((features->type != BAMBOO_PT) &&
454 (features->type != BAMBOO_TOUCH)) {
c669fb2b
BT
455 features->unit = field->unit;
456 features->unitExpo = field->unit_exponent;
545f4e99 457 }
c669fb2b
BT
458 }
459 break;
460 case HID_GD_Y:
461 features->y_max = field->logical_maximum;
462 if (finger) {
463 features->y_phy = field->physical_maximum;
3b164a00
PC
464 if ((features->type != BAMBOO_PT) &&
465 (features->type != BAMBOO_TOUCH)) {
c669fb2b
BT
466 features->unit = field->unit;
467 features->unitExpo = field->unit_exponent;
468 }
469 }
470 break;
471 case HID_DG_TIPPRESSURE:
472 if (pen)
473 features->pressure_max = field->logical_maximum;
474 break;
475 }
7704ac93
BT
476
477 if (features->type == HID_GENERIC)
478 wacom_wac_usage_mapping(hdev, field, usage);
c669fb2b 479}
4134361a 480
b58ba1ba
JG
481static void wacom_post_parse_hid(struct hid_device *hdev,
482 struct wacom_features *features)
483{
484 struct wacom *wacom = hid_get_drvdata(hdev);
485 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
486
487 if (features->type == HID_GENERIC) {
488 /* Any last-minute generic device setup */
4082da80
BT
489 if (wacom_wac->has_mode_change) {
490 if (wacom_wac->is_direct_mode)
491 features->device_type |= WACOM_DEVICETYPE_DIRECT;
492 else
493 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
494 }
495
b58ba1ba 496 if (features->touch_max > 1) {
ac2423c9
AAS
497 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
498 input_mt_init_slots(wacom_wac->touch_input,
499 wacom_wac->features.touch_max,
500 INPUT_MT_DIRECT);
501 else
502 input_mt_init_slots(wacom_wac->touch_input,
503 wacom_wac->features.touch_max,
504 INPUT_MT_POINTER);
b58ba1ba
JG
505 }
506 }
507}
508
c669fb2b
BT
509static void wacom_parse_hid(struct hid_device *hdev,
510 struct wacom_features *features)
511{
512 struct hid_report_enum *rep_enum;
513 struct hid_report *hreport;
514 int i, j;
515
516 /* check features first */
517 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
518 list_for_each_entry(hreport, &rep_enum->report_list, list) {
519 for (i = 0; i < hreport->maxfield; i++) {
520 /* Ignore if report count is out of bounds. */
521 if (hreport->field[i]->report_count < 1)
522 continue;
523
524 for (j = 0; j < hreport->field[i]->maxusage; j++) {
525 wacom_feature_mapping(hdev, hreport->field[i],
526 hreport->field[i]->usage + j);
4134361a 527 }
545f4e99
PC
528 }
529 }
530
c669fb2b
BT
531 /* now check the input usages */
532 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
533 list_for_each_entry(hreport, &rep_enum->report_list, list) {
534
535 if (!hreport->maxfield)
536 continue;
537
538 for (i = 0; i < hreport->maxfield; i++)
539 for (j = 0; j < hreport->field[i]->maxusage; j++)
540 wacom_usage_mapping(hdev, hreport->field[i],
541 hreport->field[i]->usage + j);
542 }
b58ba1ba
JG
543
544 wacom_post_parse_hid(hdev, features);
545f4e99
PC
545}
546
5ae6e89f
BT
547static int wacom_hid_set_device_mode(struct hid_device *hdev)
548{
549 struct wacom *wacom = hid_get_drvdata(hdev);
550 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
551 struct hid_report *r;
552 struct hid_report_enum *re;
553
554 if (hid_data->inputmode < 0)
555 return 0;
556
557 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
558 r = re->report_id_hash[hid_data->inputmode];
559 if (r) {
560 r->field[0]->value[hid_data->inputmode_index] = 2;
561 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
562 }
563 return 0;
564}
565
326ea2a9
JG
566static int wacom_set_device_mode(struct hid_device *hdev,
567 struct wacom_wac *wacom_wac)
3b7307c2 568{
326ea2a9
JG
569 u8 *rep_data;
570 struct hid_report *r;
571 struct hid_report_enum *re;
3064a03b 572 u32 length;
fe494bc2 573 int error = -ENOMEM, limit = 0;
3b7307c2 574
326ea2a9
JG
575 if (wacom_wac->mode_report < 0)
576 return 0;
577
578 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
579 r = re->report_id_hash[wacom_wac->mode_report];
580 if (!r)
581 return -EINVAL;
582
583 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
3b7307c2 584 if (!rep_data)
326ea2a9
JG
585 return -ENOMEM;
586
587 length = hid_report_len(r);
ec67bbed 588
fe494bc2 589 do {
326ea2a9
JG
590 rep_data[0] = wacom_wac->mode_report;
591 rep_data[1] = wacom_wac->mode_value;
9937c026 592
296b7378
PF
593 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
594 length, 1);
3cb83157 595 if (error >= 0)
27b20a9d 596 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
c64d8834 597 rep_data, length, 1);
326ea2a9
JG
598 } while (error >= 0 &&
599 rep_data[1] != wacom_wac->mode_report &&
600 limit++ < WAC_MSG_RETRIES);
fe494bc2
JG
601
602 kfree(rep_data);
603
604 return error < 0 ? error : 0;
605}
606
f81a1295
BT
607static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
608 struct wacom_features *features)
609{
387142bb
BT
610 struct wacom *wacom = hid_get_drvdata(hdev);
611 int ret;
612 u8 rep_data[2];
613
614 switch (features->type) {
615 case GRAPHIRE_BT:
616 rep_data[0] = 0x03;
617 rep_data[1] = 0x00;
296b7378
PF
618 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
619 3);
387142bb
BT
620
621 if (ret >= 0) {
622 rep_data[0] = speed == 0 ? 0x05 : 0x06;
623 rep_data[1] = 0x00;
624
625 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
296b7378 626 rep_data, 2, 3);
387142bb
BT
627
628 if (ret >= 0) {
629 wacom->wacom_wac.bt_high_speed = speed;
630 return 0;
631 }
632 }
633
634 /*
635 * Note that if the raw queries fail, it's not a hard failure
636 * and it is safe to continue
637 */
638 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
639 rep_data[0], ret);
640 break;
81af7e61
BT
641 case INTUOS4WL:
642 if (speed == 1)
643 wacom->wacom_wac.bt_features &= ~0x20;
644 else
645 wacom->wacom_wac.bt_features |= 0x20;
646
647 rep_data[0] = 0x03;
648 rep_data[1] = wacom->wacom_wac.bt_features;
649
296b7378
PF
650 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
651 1);
81af7e61
BT
652 if (ret >= 0)
653 wacom->wacom_wac.bt_high_speed = speed;
654 break;
387142bb
BT
655 }
656
f81a1295
BT
657 return 0;
658}
659
fe494bc2
JG
660/*
661 * Switch the tablet into its most-capable mode. Wacom tablets are
662 * typically configured to power-up in a mode which sends mouse-like
663 * reports to the OS. To get absolute position, pressure data, etc.
664 * from the tablet, it is necessary to switch the tablet out of this
665 * mode and into one which sends the full range of tablet data.
666 */
a544c619 667static int _wacom_query_tablet_data(struct wacom *wacom)
fe494bc2 668{
a544c619 669 struct hid_device *hdev = wacom->hdev;
326ea2a9 670 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
a544c619 671 struct wacom_features *features = &wacom_wac->features;
326ea2a9 672
f81a1295
BT
673 if (hdev->bus == BUS_BLUETOOTH)
674 return wacom_bt_query_tablet_data(hdev, 1, features);
675
326ea2a9
JG
676 if (features->type != HID_GENERIC) {
677 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
678 if (features->type > TABLETPC) {
679 /* MT Tablet PC touch */
680 wacom_wac->mode_report = 3;
681 wacom_wac->mode_value = 4;
682 } else if (features->type == WACOM_24HDT) {
683 wacom_wac->mode_report = 18;
684 wacom_wac->mode_value = 2;
685 } else if (features->type == WACOM_27QHDT) {
686 wacom_wac->mode_report = 131;
687 wacom_wac->mode_value = 2;
688 } else if (features->type == BAMBOO_PAD) {
689 wacom_wac->mode_report = 2;
690 wacom_wac->mode_value = 2;
691 }
692 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
693 if (features->type <= BAMBOO_PT) {
694 wacom_wac->mode_report = 2;
695 wacom_wac->mode_value = 2;
696 }
1963518b 697 }
ec67bbed 698 }
3b7307c2 699
326ea2a9
JG
700 wacom_set_device_mode(hdev, wacom_wac);
701
702 if (features->type == HID_GENERIC)
703 return wacom_hid_set_device_mode(hdev);
704
fe494bc2 705 return 0;
3b7307c2
DT
706}
707
c669fb2b 708static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
1963518b 709 struct wacom_features *features)
ec67bbed 710{
27b20a9d
BT
711 struct wacom *wacom = hid_get_drvdata(hdev);
712 struct usb_interface *intf = wacom->intf;
ec67bbed 713
fed87e65 714 /* default features */
fed87e65
HR
715 features->x_fuzz = 4;
716 features->y_fuzz = 4;
717 features->pressure_fuzz = 0;
bef7e200
JG
718 features->distance_fuzz = 1;
719 features->tilt_fuzz = 1;
ec67bbed 720
d3825d51
CB
721 /*
722 * The wireless device HID is basic and layout conflicts with
723 * other tablets (monitor and touch interface can look like pen).
724 * Skip the query for this type and modify defaults based on
725 * interface number.
726 */
727 if (features->type == WIRELESS) {
3f14a63a 728 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
ccad85cc 729 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
3f14a63a 730 else
aa86b18c 731 features->device_type = WACOM_DEVICETYPE_NONE;
3f14a63a 732 return;
d3825d51
CB
733 }
734
c669fb2b 735 wacom_parse_hid(hdev, features);
ec67bbed
PC
736}
737
4451e088 738struct wacom_hdev_data {
4492efff
PC
739 struct list_head list;
740 struct kref kref;
4451e088 741 struct hid_device *dev;
4492efff
PC
742 struct wacom_shared shared;
743};
744
745static LIST_HEAD(wacom_udev_list);
746static DEFINE_MUTEX(wacom_udev_list_lock);
747
4451e088
BT
748static bool wacom_are_sibling(struct hid_device *hdev,
749 struct hid_device *sibling)
aea2bf6a 750{
4451e088
BT
751 struct wacom *wacom = hid_get_drvdata(hdev);
752 struct wacom_features *features = &wacom->wacom_wac.features;
41372d5d
JG
753 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
754 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
755 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
756 __u32 oPid = features->oPid ? features->oPid : hdev->product;
4451e088 757
41372d5d
JG
758 /* The defined oVid/oPid must match that of the sibling */
759 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
760 return false;
761 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
762 return false;
763
764 /*
765 * Devices with the same VID/PID must share the same physical
766 * device path, while those with different VID/PID must share
767 * the same physical parent device path.
768 */
769 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
1a8861f1 770 if (!hid_compare_device_paths(hdev, sibling, '/'))
41372d5d
JG
771 return false;
772 } else {
1a8861f1 773 if (!hid_compare_device_paths(hdev, sibling, '.'))
41372d5d 774 return false;
4451e088 775 }
aea2bf6a 776
41372d5d
JG
777 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
778 if (features->type != HID_GENERIC)
779 return true;
780
781 /*
782 * Direct-input devices may not be siblings of indirect-input
783 * devices.
784 */
785 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
786 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
4451e088 787 return false;
aea2bf6a 788
41372d5d
JG
789 /*
790 * Indirect-input devices may not be siblings of direct-input
791 * devices.
792 */
793 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
794 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
4451e088 795 return false;
aea2bf6a 796
41372d5d
JG
797 /* Pen devices may only be siblings of touch devices */
798 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
799 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
800 return false;
801
802 /* Touch devices may only be siblings of pen devices */
803 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
804 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
805 return false;
806
807 /*
808 * No reason could be found for these two devices to NOT be
809 * siblings, so there's a good chance they ARE siblings
810 */
811 return true;
aea2bf6a
JG
812}
813
4451e088 814static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
4492efff 815{
4451e088 816 struct wacom_hdev_data *data;
4492efff 817
41372d5d
JG
818 /* Try to find an already-probed interface from the same device */
819 list_for_each_entry(data, &wacom_udev_list, list) {
1a8861f1 820 if (hid_compare_device_paths(hdev, data->dev, '/')) {
2a5e597c 821 kref_get(&data->kref);
41372d5d 822 return data;
2a5e597c 823 }
41372d5d
JG
824 }
825
826 /* Fallback to finding devices that appear to be "siblings" */
4492efff 827 list_for_each_entry(data, &wacom_udev_list, list) {
4451e088 828 if (wacom_are_sibling(hdev, data->dev)) {
4492efff
PC
829 kref_get(&data->kref);
830 return data;
831 }
832 }
833
834 return NULL;
835}
836
1c817c83
BT
837static void wacom_release_shared_data(struct kref *kref)
838{
839 struct wacom_hdev_data *data =
840 container_of(kref, struct wacom_hdev_data, kref);
841
842 mutex_lock(&wacom_udev_list_lock);
843 list_del(&data->list);
844 mutex_unlock(&wacom_udev_list_lock);
845
846 kfree(data);
847}
848
849static void wacom_remove_shared_data(void *res)
850{
851 struct wacom *wacom = res;
852 struct wacom_hdev_data *data;
853 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
854
855 if (wacom_wac->shared) {
856 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
857 shared);
858
859 if (wacom_wac->shared->touch == wacom->hdev)
860 wacom_wac->shared->touch = NULL;
861 else if (wacom_wac->shared->pen == wacom->hdev)
862 wacom_wac->shared->pen = NULL;
863
864 kref_put(&data->kref, wacom_release_shared_data);
865 wacom_wac->shared = NULL;
866 }
867}
868
4451e088 869static int wacom_add_shared_data(struct hid_device *hdev)
4492efff 870{
4451e088
BT
871 struct wacom *wacom = hid_get_drvdata(hdev);
872 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
873 struct wacom_hdev_data *data;
4492efff
PC
874 int retval = 0;
875
876 mutex_lock(&wacom_udev_list_lock);
877
4451e088 878 data = wacom_get_hdev_data(hdev);
4492efff 879 if (!data) {
4451e088 880 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
4492efff
PC
881 if (!data) {
882 retval = -ENOMEM;
883 goto out;
884 }
885
886 kref_init(&data->kref);
4451e088 887 data->dev = hdev;
4492efff
PC
888 list_add_tail(&data->list, &wacom_udev_list);
889 }
890
4451e088 891 wacom_wac->shared = &data->shared;
4492efff 892
1c817c83
BT
893 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
894 if (retval) {
895 mutex_unlock(&wacom_udev_list_lock);
896 wacom_remove_shared_data(wacom);
897 return retval;
898 }
899
a9ce7856
JG
900 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
901 wacom_wac->shared->touch = hdev;
902 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
903 wacom_wac->shared->pen = hdev;
904
4492efff
PC
905out:
906 mutex_unlock(&wacom_udev_list_lock);
907 return retval;
908}
909
5d7e7d47
EH
910static int wacom_led_control(struct wacom *wacom)
911{
912 unsigned char *buf;
9b5b95dd 913 int retval;
912ca216
PC
914 unsigned char report_id = WAC_CMD_LED_CONTROL;
915 int buf_size = 9;
5d7e7d47 916
a50aac71
BT
917 if (!wacom->led.groups)
918 return -ENOTSUPP;
919
74aebed6
AAS
920 if (wacom->wacom_wac.features.type == REMOTE)
921 return -ENOTSUPP;
922
912ca216
PC
923 if (wacom->wacom_wac.pid) { /* wireless connected */
924 report_id = WAC_CMD_WL_LED_CONTROL;
925 buf_size = 13;
926 }
4922cd26
JG
927 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
928 report_id = WAC_CMD_WL_INTUOSP2;
929 buf_size = 51;
930 }
912ca216 931 buf = kzalloc(buf_size, GFP_KERNEL);
5d7e7d47
EH
932 if (!buf)
933 return -ENOMEM;
934
10c55cac
AAS
935 if (wacom->wacom_wac.features.type == HID_GENERIC) {
936 buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
937 buf[1] = wacom->led.llv;
938 buf[2] = wacom->led.groups[0].select & 0x03;
939
940 } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
941 wacom->wacom_wac.features.type <= INTUOSPL)) {
9b5b95dd
JG
942 /*
943 * Touch Ring and crop mark LED luminance may take on
944 * one of four values:
945 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
946 */
a50aac71 947 int ring_led = wacom->led.groups[0].select & 0x03;
9b5b95dd
JG
948 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
949 int crop_lum = 0;
912ca216
PC
950 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
951
952 buf[0] = report_id;
953 if (wacom->wacom_wac.pid) {
954 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
955 buf, buf_size, WAC_CMD_RETRIES);
956 buf[0] = report_id;
957 buf[4] = led_bits;
958 } else
959 buf[1] = led_bits;
9b5b95dd 960 }
4922cd26
JG
961 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
962 buf[0] = report_id;
963 buf[4] = 100; // Power Connection LED (ORANGE)
964 buf[5] = 100; // BT Connection LED (BLUE)
965 buf[6] = 100; // Paper Mode (RED?)
966 buf[7] = 100; // Paper Mode (GREEN?)
967 buf[8] = 100; // Paper Mode (BLUE?)
968 buf[9] = wacom->led.llv;
969 buf[10] = wacom->led.groups[0].select & 0x03;
970 }
9b5b95dd 971 else {
a50aac71 972 int led = wacom->led.groups[0].select | 0x4;
9b5b95dd
JG
973
974 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
975 wacom->wacom_wac.features.type == WACOM_24HD)
a50aac71 976 led |= (wacom->led.groups[1].select << 4) | 0x40;
9b5b95dd 977
912ca216 978 buf[0] = report_id;
9b5b95dd
JG
979 buf[1] = led;
980 buf[2] = wacom->led.llv;
981 buf[3] = wacom->led.hlv;
982 buf[4] = wacom->led.img_lum;
983 }
5d7e7d47 984
912ca216 985 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
296b7378 986 WAC_CMD_RETRIES);
5d7e7d47
EH
987 kfree(buf);
988
989 return retval;
990}
991
849e2f06
BT
992static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
993 const unsigned len, const void *img)
5d7e7d47
EH
994{
995 unsigned char *buf;
996 int i, retval;
849e2f06 997 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
5d7e7d47 998
849e2f06 999 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
5d7e7d47
EH
1000 if (!buf)
1001 return -ENOMEM;
1002
1003 /* Send 'start' command */
1004 buf[0] = WAC_CMD_ICON_START;
1005 buf[1] = 1;
296b7378
PF
1006 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1007 WAC_CMD_RETRIES);
5d7e7d47
EH
1008 if (retval < 0)
1009 goto out;
1010
849e2f06 1011 buf[0] = xfer_id;
5d7e7d47
EH
1012 buf[1] = button_id & 0x07;
1013 for (i = 0; i < 4; i++) {
1014 buf[2] = i;
849e2f06 1015 memcpy(buf + 3, img + i * chunk_len, chunk_len);
5d7e7d47 1016
27b20a9d 1017 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
296b7378 1018 buf, chunk_len + 3, WAC_CMD_RETRIES);
5d7e7d47
EH
1019 if (retval < 0)
1020 break;
1021 }
1022
1023 /* Send 'stop' */
1024 buf[0] = WAC_CMD_ICON_START;
1025 buf[1] = 0;
296b7378
PF
1026 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1027 WAC_CMD_RETRIES);
5d7e7d47
EH
1028
1029out:
1030 kfree(buf);
1031 return retval;
1032}
1033
09e7d941 1034static ssize_t wacom_led_select_store(struct device *dev, int set_id,
5d7e7d47
EH
1035 const char *buf, size_t count)
1036{
ee79a8f8 1037 struct hid_device *hdev = to_hid_device(dev);
29b47391 1038 struct wacom *wacom = hid_get_drvdata(hdev);
5d7e7d47
EH
1039 unsigned int id;
1040 int err;
1041
1042 err = kstrtouint(buf, 10, &id);
1043 if (err)
1044 return err;
1045
1046 mutex_lock(&wacom->lock);
1047
a50aac71 1048 wacom->led.groups[set_id].select = id & 0x3;
5d7e7d47
EH
1049 err = wacom_led_control(wacom);
1050
1051 mutex_unlock(&wacom->lock);
1052
1053 return err < 0 ? err : count;
1054}
1055
09e7d941
PC
1056#define DEVICE_LED_SELECT_ATTR(SET_ID) \
1057static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
1058 struct device_attribute *attr, const char *buf, size_t count) \
1059{ \
1060 return wacom_led_select_store(dev, SET_ID, buf, count); \
1061} \
04c59abd
PC
1062static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
1063 struct device_attribute *attr, char *buf) \
1064{ \
ee79a8f8 1065 struct hid_device *hdev = to_hid_device(dev);\
29b47391 1066 struct wacom *wacom = hid_get_drvdata(hdev); \
37449adc 1067 return scnprintf(buf, PAGE_SIZE, "%d\n", \
a50aac71 1068 wacom->led.groups[SET_ID].select); \
04c59abd 1069} \
e0984bc3 1070static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
04c59abd 1071 wacom_led##SET_ID##_select_show, \
09e7d941
PC
1072 wacom_led##SET_ID##_select_store)
1073
1074DEVICE_LED_SELECT_ATTR(0);
1075DEVICE_LED_SELECT_ATTR(1);
5d7e7d47
EH
1076
1077static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
1078 const char *buf, size_t count)
1079{
1080 unsigned int value;
1081 int err;
1082
1083 err = kstrtouint(buf, 10, &value);
1084 if (err)
1085 return err;
1086
1087 mutex_lock(&wacom->lock);
1088
1089 *dest = value & 0x7f;
1090 err = wacom_led_control(wacom);
1091
1092 mutex_unlock(&wacom->lock);
1093
1094 return err < 0 ? err : count;
1095}
1096
1097#define DEVICE_LUMINANCE_ATTR(name, field) \
1098static ssize_t wacom_##name##_luminance_store(struct device *dev, \
1099 struct device_attribute *attr, const char *buf, size_t count) \
1100{ \
ee79a8f8 1101 struct hid_device *hdev = to_hid_device(dev);\
29b47391 1102 struct wacom *wacom = hid_get_drvdata(hdev); \
5d7e7d47
EH
1103 \
1104 return wacom_luminance_store(wacom, &wacom->led.field, \
1105 buf, count); \
1106} \
37449adc
PC
1107static ssize_t wacom_##name##_luminance_show(struct device *dev, \
1108 struct device_attribute *attr, char *buf) \
1109{ \
1110 struct wacom *wacom = dev_get_drvdata(dev); \
1111 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
1112} \
e0984bc3 1113static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
37449adc
PC
1114 wacom_##name##_luminance_show, \
1115 wacom_##name##_luminance_store)
5d7e7d47
EH
1116
1117DEVICE_LUMINANCE_ATTR(status0, llv);
1118DEVICE_LUMINANCE_ATTR(status1, hlv);
1119DEVICE_LUMINANCE_ATTR(buttons, img_lum);
1120
1121static ssize_t wacom_button_image_store(struct device *dev, int button_id,
1122 const char *buf, size_t count)
1123{
ee79a8f8 1124 struct hid_device *hdev = to_hid_device(dev);
29b47391 1125 struct wacom *wacom = hid_get_drvdata(hdev);
5d7e7d47 1126 int err;
849e2f06
BT
1127 unsigned len;
1128 u8 xfer_id;
1129
1130 if (hdev->bus == BUS_BLUETOOTH) {
1131 len = 256;
1132 xfer_id = WAC_CMD_ICON_BT_XFER;
1133 } else {
1134 len = 1024;
1135 xfer_id = WAC_CMD_ICON_XFER;
1136 }
5d7e7d47 1137
849e2f06 1138 if (count != len)
5d7e7d47
EH
1139 return -EINVAL;
1140
1141 mutex_lock(&wacom->lock);
1142
849e2f06 1143 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
5d7e7d47
EH
1144
1145 mutex_unlock(&wacom->lock);
1146
1147 return err < 0 ? err : count;
1148}
1149
1150#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1151static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1152 struct device_attribute *attr, const char *buf, size_t count) \
1153{ \
1154 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1155} \
e0984bc3 1156static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
5d7e7d47
EH
1157 NULL, wacom_btnimg##BUTTON_ID##_store)
1158
1159DEVICE_BTNIMG_ATTR(0);
1160DEVICE_BTNIMG_ATTR(1);
1161DEVICE_BTNIMG_ATTR(2);
1162DEVICE_BTNIMG_ATTR(3);
1163DEVICE_BTNIMG_ATTR(4);
1164DEVICE_BTNIMG_ATTR(5);
1165DEVICE_BTNIMG_ATTR(6);
1166DEVICE_BTNIMG_ATTR(7);
1167
09e7d941
PC
1168static struct attribute *cintiq_led_attrs[] = {
1169 &dev_attr_status_led0_select.attr,
1170 &dev_attr_status_led1_select.attr,
1171 NULL
1172};
1173
1174static struct attribute_group cintiq_led_attr_group = {
1175 .name = "wacom_led",
1176 .attrs = cintiq_led_attrs,
1177};
1178
1179static struct attribute *intuos4_led_attrs[] = {
5d7e7d47
EH
1180 &dev_attr_status0_luminance.attr,
1181 &dev_attr_status1_luminance.attr,
09e7d941 1182 &dev_attr_status_led0_select.attr,
5d7e7d47
EH
1183 &dev_attr_buttons_luminance.attr,
1184 &dev_attr_button0_rawimg.attr,
1185 &dev_attr_button1_rawimg.attr,
1186 &dev_attr_button2_rawimg.attr,
1187 &dev_attr_button3_rawimg.attr,
1188 &dev_attr_button4_rawimg.attr,
1189 &dev_attr_button5_rawimg.attr,
1190 &dev_attr_button6_rawimg.attr,
1191 &dev_attr_button7_rawimg.attr,
1192 NULL
1193};
1194
09e7d941 1195static struct attribute_group intuos4_led_attr_group = {
5d7e7d47 1196 .name = "wacom_led",
09e7d941 1197 .attrs = intuos4_led_attrs,
5d7e7d47
EH
1198};
1199
9b5b95dd
JG
1200static struct attribute *intuos5_led_attrs[] = {
1201 &dev_attr_status0_luminance.attr,
1202 &dev_attr_status_led0_select.attr,
1203 NULL
1204};
1205
1206static struct attribute_group intuos5_led_attr_group = {
1207 .name = "wacom_led",
1208 .attrs = intuos5_led_attrs,
1209};
1210
10c55cac
AAS
1211static struct attribute *generic_led_attrs[] = {
1212 &dev_attr_status0_luminance.attr,
1213 &dev_attr_status_led0_select.attr,
1214 NULL
1215};
1216
1217static struct attribute_group generic_led_attr_group = {
1218 .name = "wacom_led",
1219 .attrs = generic_led_attrs,
1220};
1221
2df68a88
BT
1222struct wacom_sysfs_group_devres {
1223 struct attribute_group *group;
1224 struct kobject *root;
1225};
1226
1227static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1228{
1229 struct wacom_sysfs_group_devres *devres = res;
1230 struct kobject *kobj = devres->root;
1231
1232 dev_dbg(dev, "%s: dropping reference to %s\n",
1233 __func__, devres->group->name);
1234 sysfs_remove_group(kobj, devres->group);
1235}
1236
f9036bd4
BT
1237static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1238 struct kobject *root,
1239 struct attribute_group *group)
2df68a88
BT
1240{
1241 struct wacom_sysfs_group_devres *devres;
1242 int error;
1243
1244 devres = devres_alloc(wacom_devm_sysfs_group_release,
1245 sizeof(struct wacom_sysfs_group_devres),
1246 GFP_KERNEL);
1247 if (!devres)
1248 return -ENOMEM;
1249
1250 devres->group = group;
f9036bd4 1251 devres->root = root;
2df68a88
BT
1252
1253 error = sysfs_create_group(devres->root, group);
097b8f62
AY
1254 if (error) {
1255 devres_free(devres);
2df68a88 1256 return error;
097b8f62 1257 }
2df68a88
BT
1258
1259 devres_add(&wacom->hdev->dev, devres);
1260
1261 return 0;
1262}
1263
f9036bd4
BT
1264static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1265 struct attribute_group *group)
1266{
1267 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1268 group);
1269}
1270
34736aa9 1271enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
97f5541f
BT
1272{
1273 struct wacom *wacom = led->wacom;
1274
1275 if (wacom->led.max_hlv)
1276 return led->hlv * LED_FULL / wacom->led.max_hlv;
1277
1278 if (wacom->led.max_llv)
1279 return led->llv * LED_FULL / wacom->led.max_llv;
1280
1281 /* device doesn't support brightness tuning */
1282 return LED_FULL;
1283}
1284
1285static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1286{
1287 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1288 struct wacom *wacom = led->wacom;
1289
1290 if (wacom->led.groups[led->group].select != led->id)
1291 return LED_OFF;
1292
1293 return wacom_leds_brightness_get(led);
1294}
1295
1296static int wacom_led_brightness_set(struct led_classdev *cdev,
1297 enum led_brightness brightness)
1298{
1299 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1300 struct wacom *wacom = led->wacom;
1301 int error;
1302
1303 mutex_lock(&wacom->lock);
1304
1305 if (!wacom->led.groups || (brightness == LED_OFF &&
1306 wacom->led.groups[led->group].select != led->id)) {
1307 error = 0;
1308 goto out;
1309 }
1310
1311 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1312 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1313
1314 wacom->led.groups[led->group].select = led->id;
1315
1316 error = wacom_led_control(wacom);
1317
1318out:
1319 mutex_unlock(&wacom->lock);
1320
1321 return error;
1322}
1323
1324static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1325 enum led_brightness brightness)
1326{
1327}
1328
1329static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1330 struct wacom_led *led, unsigned int group,
1331 unsigned int id, bool read_only)
1332{
1333 int error;
1334 char *name;
1335
1336 name = devm_kasprintf(dev, GFP_KERNEL,
1337 "%s::wacom-%d.%d",
1338 dev_name(dev),
1339 group,
1340 id);
1341 if (!name)
1342 return -ENOMEM;
1343
34736aa9
BT
1344 if (!read_only) {
1345 led->trigger.name = name;
1346 error = devm_led_trigger_register(dev, &led->trigger);
1347 if (error) {
1348 hid_err(wacom->hdev,
1349 "failed to register LED trigger %s: %d\n",
1350 led->cdev.name, error);
1351 return error;
1352 }
1353 }
1354
97f5541f
BT
1355 led->group = group;
1356 led->id = id;
1357 led->wacom = wacom;
1358 led->llv = wacom->led.llv;
1359 led->hlv = wacom->led.hlv;
1360 led->cdev.name = name;
1361 led->cdev.max_brightness = LED_FULL;
1362 led->cdev.flags = LED_HW_PLUGGABLE;
1363 led->cdev.brightness_get = __wacom_led_brightness_get;
34736aa9 1364 if (!read_only) {
97f5541f 1365 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
34736aa9
BT
1366 led->cdev.default_trigger = led->cdev.name;
1367 } else {
97f5541f 1368 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
34736aa9 1369 }
97f5541f
BT
1370
1371 error = devm_led_classdev_register(dev, &led->cdev);
1372 if (error) {
1373 hid_err(wacom->hdev,
1374 "failed to register LED %s: %d\n",
1375 led->cdev.name, error);
1376 led->cdev.name = NULL;
1377 return error;
1378 }
1379
1380 return 0;
1381}
1382
589e5060
BT
1383static void wacom_led_groups_release_one(void *data)
1384{
1385 struct wacom_group_leds *group = data;
1386
1387 devres_release_group(group->dev, group);
1388}
1389
97f5541f
BT
1390static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1391 struct wacom *wacom,
1392 int group_id, int count,
1393 bool read_only)
1394{
1395 struct wacom_led *leds;
1396 int i, error;
1397
1398 if (group_id >= wacom->led.count || count <= 0)
1399 return -EINVAL;
1400
1401 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1402 return -ENOMEM;
1403
a86854d0 1404 leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL);
97f5541f
BT
1405 if (!leds) {
1406 error = -ENOMEM;
1407 goto err;
1408 }
1409
1410 wacom->led.groups[group_id].leds = leds;
1411 wacom->led.groups[group_id].count = count;
1412
1413 for (i = 0; i < count; i++) {
1414 error = wacom_led_register_one(dev, wacom, &leds[i],
1415 group_id, i, read_only);
1416 if (error)
1417 goto err;
1418 }
1419
589e5060
BT
1420 wacom->led.groups[group_id].dev = dev;
1421
1422 devres_close_group(dev, &wacom->led.groups[group_id]);
1423
1424 /*
1425 * There is a bug (?) in devm_led_classdev_register() in which its
1426 * increments the refcount of the parent. If the parent is an input
1427 * device, that means the ref count never reaches 0 when
1428 * devm_input_device_release() gets called.
1429 * This means that the LEDs are still there after disconnect.
1430 * Manually force the release of the group so that the leds are released
1431 * once we are done using them.
1432 */
1433 error = devm_add_action_or_reset(&wacom->hdev->dev,
1434 wacom_led_groups_release_one,
1435 &wacom->led.groups[group_id]);
1436 if (error)
1437 return error;
1438
97f5541f
BT
1439 return 0;
1440
1441err:
1442 devres_release_group(dev, &wacom->led.groups[group_id]);
1443 return error;
1444}
1445
34736aa9
BT
1446struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1447 unsigned int id)
1448{
1449 struct wacom_group_leds *group;
1450
1451 if (group_id >= wacom->led.count)
1452 return NULL;
1453
1454 group = &wacom->led.groups[group_id];
1455
1456 if (!group->leds)
1457 return NULL;
1458
1459 id %= group->count;
1460
1461 return &group->leds[id];
1462}
1463
1464/**
1465 * wacom_led_next: gives the next available led with a wacom trigger.
1466 *
1467 * returns the next available struct wacom_led which has its default trigger
1468 * or the current one if none is available.
1469 */
1470struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1471{
1472 struct wacom_led *next_led;
1473 int group, next;
1474
1475 if (!wacom || !cur)
1476 return NULL;
1477
1478 group = cur->group;
1479 next = cur->id;
1480
1481 do {
1482 next_led = wacom_led_find(wacom, group, ++next);
1483 if (!next_led || next_led == cur)
1484 return next_led;
1485 } while (next_led->cdev.trigger != &next_led->trigger);
1486
1487 return next_led;
1488}
1489
a50aac71
BT
1490static void wacom_led_groups_release(void *data)
1491{
1492 struct wacom *wacom = data;
1493
1494 wacom->led.groups = NULL;
97f5541f 1495 wacom->led.count = 0;
a50aac71
BT
1496}
1497
1498static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1499{
97f5541f 1500 struct device *dev = &wacom->hdev->dev;
a50aac71
BT
1501 struct wacom_group_leds *groups;
1502 int error;
1503
a86854d0 1504 groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds),
a50aac71
BT
1505 GFP_KERNEL);
1506 if (!groups)
1507 return -ENOMEM;
1508
97f5541f 1509 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
a50aac71
BT
1510 if (error)
1511 return error;
1512
1513 wacom->led.groups = groups;
97f5541f
BT
1514 wacom->led.count = count;
1515
1516 return 0;
1517}
1518
1519static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1520 int led_per_group, bool read_only)
1521{
1522 struct device *dev;
1523 int i, error;
1524
1525 if (!wacom->wacom_wac.pad_input)
1526 return -EINVAL;
1527
1528 dev = &wacom->wacom_wac.pad_input->dev;
1529
1530 error = wacom_led_groups_allocate(wacom, group_count);
1531 if (error)
1532 return error;
1533
1534 for (i = 0; i < group_count; i++) {
1535 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1536 led_per_group,
1537 read_only);
1538 if (error)
1539 return error;
1540 }
a50aac71
BT
1541
1542 return 0;
1543}
1544
10c55cac 1545int wacom_initialize_leds(struct wacom *wacom)
5d7e7d47
EH
1546{
1547 int error;
1548
862cf553
JG
1549 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1550 return 0;
1551
09e7d941
PC
1552 /* Initialize default values */
1553 switch (wacom->wacom_wac.features.type) {
10c55cac
AAS
1554 case HID_GENERIC:
1555 if (!wacom->generic_has_leds)
1556 return 0;
1557 wacom->led.llv = 100;
1558 wacom->led.max_llv = 100;
1559
1560 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1561 if (error) {
1562 hid_err(wacom->hdev,
1563 "cannot create leds err: %d\n", error);
1564 return error;
1565 }
1566
1567 error = wacom_devm_sysfs_create_group(wacom,
1568 &generic_led_attr_group);
1569 break;
1570
a19fc986 1571 case INTUOS4S:
09e7d941 1572 case INTUOS4:
81af7e61 1573 case INTUOS4WL:
09e7d941 1574 case INTUOS4L:
f4fa9a6d 1575 wacom->led.llv = 10;
5d7e7d47 1576 wacom->led.hlv = 20;
97f5541f
BT
1577 wacom->led.max_llv = 127;
1578 wacom->led.max_hlv = 127;
5d7e7d47 1579 wacom->led.img_lum = 10;
a50aac71 1580
97f5541f 1581 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
a50aac71
BT
1582 if (error) {
1583 hid_err(wacom->hdev,
1584 "cannot create leds err: %d\n", error);
1585 return error;
1586 }
1587
2df68a88
BT
1588 error = wacom_devm_sysfs_create_group(wacom,
1589 &intuos4_led_attr_group);
09e7d941
PC
1590 break;
1591
246835fc 1592 case WACOM_24HD:
09e7d941 1593 case WACOM_21UX2:
09e7d941
PC
1594 wacom->led.llv = 0;
1595 wacom->led.hlv = 0;
1596 wacom->led.img_lum = 0;
5d7e7d47 1597
97f5541f 1598 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
a50aac71
BT
1599 if (error) {
1600 hid_err(wacom->hdev,
1601 "cannot create leds err: %d\n", error);
1602 return error;
1603 }
1604
2df68a88
BT
1605 error = wacom_devm_sysfs_create_group(wacom,
1606 &cintiq_led_attr_group);
09e7d941
PC
1607 break;
1608
9b5b95dd
JG
1609 case INTUOS5S:
1610 case INTUOS5:
1611 case INTUOS5L:
9a35c411
PC
1612 case INTUOSPS:
1613 case INTUOSPM:
1614 case INTUOSPL:
862cf553 1615 wacom->led.llv = 32;
97f5541f 1616 wacom->led.max_llv = 96;
862cf553 1617
97f5541f 1618 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
a50aac71
BT
1619 if (error) {
1620 hid_err(wacom->hdev,
1621 "cannot create leds err: %d\n", error);
1622 return error;
1623 }
1624
2df68a88
BT
1625 error = wacom_devm_sysfs_create_group(wacom,
1626 &intuos5_led_attr_group);
9b5b95dd
JG
1627 break;
1628
4922cd26
JG
1629 case INTUOSP2_BT:
1630 wacom->led.llv = 50;
1631 wacom->led.max_llv = 100;
1632 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1633 if (error) {
1634 hid_err(wacom->hdev,
1635 "cannot create leds err: %d\n", error);
1636 return error;
1637 }
1638 return 0;
1639
a50aac71 1640 case REMOTE:
97f5541f
BT
1641 wacom->led.llv = 255;
1642 wacom->led.max_llv = 255;
a50aac71
BT
1643 error = wacom_led_groups_allocate(wacom, 5);
1644 if (error) {
1645 hid_err(wacom->hdev,
1646 "cannot create leds err: %d\n", error);
1647 return error;
1648 }
1649 return 0;
1650
09e7d941
PC
1651 default:
1652 return 0;
5d7e7d47
EH
1653 }
1654
09e7d941 1655 if (error) {
c31a408f 1656 hid_err(wacom->hdev,
09e7d941
PC
1657 "cannot create sysfs group err: %d\n", error);
1658 return error;
1659 }
09e7d941 1660
5d7e7d47
EH
1661 return 0;
1662}
1663
a544c619
BT
1664static void wacom_init_work(struct work_struct *work)
1665{
1666 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1667
1668 _wacom_query_tablet_data(wacom);
1669 wacom_led_control(wacom);
1670}
1671
1672static void wacom_query_tablet_data(struct wacom *wacom)
1673{
1674 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1675}
1676
a1d552cc 1677static enum power_supply_property wacom_battery_props[] = {
9956953e 1678 POWER_SUPPLY_PROP_MODEL_NAME,
71fa641e 1679 POWER_SUPPLY_PROP_PRESENT,
ac8d1010 1680 POWER_SUPPLY_PROP_STATUS,
6e2a6e80 1681 POWER_SUPPLY_PROP_SCOPE,
a1d552cc
CB
1682 POWER_SUPPLY_PROP_CAPACITY
1683};
1684
1685static int wacom_battery_get_property(struct power_supply *psy,
1686 enum power_supply_property psp,
1687 union power_supply_propval *val)
1688{
59d69bc8 1689 struct wacom_battery *battery = power_supply_get_drvdata(psy);
a1d552cc
CB
1690 int ret = 0;
1691
1692 switch (psp) {
9956953e
BT
1693 case POWER_SUPPLY_PROP_MODEL_NAME:
1694 val->strval = battery->wacom->wacom_wac.name;
1695 break;
71fa641e 1696 case POWER_SUPPLY_PROP_PRESENT:
59d69bc8 1697 val->intval = battery->bat_connected;
71fa641e 1698 break;
6e2a6e80
BN
1699 case POWER_SUPPLY_PROP_SCOPE:
1700 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1701 break;
a1d552cc 1702 case POWER_SUPPLY_PROP_CAPACITY:
59d69bc8 1703 val->intval = battery->battery_capacity;
ac8d1010
BT
1704 break;
1705 case POWER_SUPPLY_PROP_STATUS:
16e45989
JG
1706 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1707 val->intval = battery->bat_status;
1708 else if (battery->bat_charging)
ac8d1010 1709 val->intval = POWER_SUPPLY_STATUS_CHARGING;
59d69bc8
BT
1710 else if (battery->battery_capacity == 100 &&
1711 battery->ps_connected)
ac8d1010 1712 val->intval = POWER_SUPPLY_STATUS_FULL;
59d69bc8 1713 else if (battery->ps_connected)
b0882cb7 1714 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
ac8d1010
BT
1715 else
1716 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
a1d552cc
CB
1717 break;
1718 default:
1719 ret = -EINVAL;
1720 break;
1721 }
1722
1723 return ret;
1724}
1725
59d69bc8
BT
1726static int __wacom_initialize_battery(struct wacom *wacom,
1727 struct wacom_battery *battery)
a1d552cc 1728{
d70420b9 1729 static atomic_t battery_no = ATOMIC_INIT(0);
b189da90 1730 struct device *dev = &wacom->hdev->dev;
59d69bc8 1731 struct power_supply_config psy_cfg = { .drv_data = battery, };
136ae5e9 1732 struct power_supply *ps_bat;
59d69bc8 1733 struct power_supply_desc *bat_desc = &battery->bat_desc;
d70420b9 1734 unsigned long n;
b189da90
BT
1735 int error;
1736
1737 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1738 return -ENOMEM;
a1d552cc 1739
9956953e
BT
1740 battery->wacom = wacom;
1741
59d69bc8
BT
1742 n = atomic_inc_return(&battery_no) - 1;
1743
1744 bat_desc->properties = wacom_battery_props;
1745 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1746 bat_desc->get_property = wacom_battery_get_property;
1747 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1748 bat_desc->name = battery->bat_name;
96983296 1749 bat_desc->type = POWER_SUPPLY_TYPE_USB;
59d69bc8
BT
1750 bat_desc->use_for_apm = 0;
1751
59d69bc8
BT
1752 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1753 if (IS_ERR(ps_bat)) {
1754 error = PTR_ERR(ps_bat);
1755 goto err;
1756 }
297d716f 1757
59d69bc8 1758 power_supply_powers(ps_bat, &wacom->hdev->dev);
7dbd229e 1759
59d69bc8 1760 battery->battery = ps_bat;
a1d552cc 1761
b189da90 1762 devres_close_group(dev, bat_desc);
7dbd229e 1763 return 0;
b189da90
BT
1764
1765err:
1766 devres_release_group(dev, bat_desc);
1767 return error;
a1d552cc
CB
1768}
1769
59d69bc8
BT
1770static int wacom_initialize_battery(struct wacom *wacom)
1771{
1772 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1773 return __wacom_initialize_battery(wacom, &wacom->battery);
1774
1775 return 0;
1776}
1777
a1d552cc
CB
1778static void wacom_destroy_battery(struct wacom *wacom)
1779{
59d69bc8
BT
1780 if (wacom->battery.battery) {
1781 devres_release_group(&wacom->hdev->dev,
1782 &wacom->battery.bat_desc);
1783 wacom->battery.battery = NULL;
b7af2bb8 1784 }
a1d552cc
CB
1785}
1786
f81a1295
BT
1787static ssize_t wacom_show_speed(struct device *dev,
1788 struct device_attribute
1789 *attr, char *buf)
1790{
ee79a8f8 1791 struct hid_device *hdev = to_hid_device(dev);
f81a1295
BT
1792 struct wacom *wacom = hid_get_drvdata(hdev);
1793
1794 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1795}
1796
1797static ssize_t wacom_store_speed(struct device *dev,
1798 struct device_attribute *attr,
1799 const char *buf, size_t count)
1800{
ee79a8f8 1801 struct hid_device *hdev = to_hid_device(dev);
f81a1295
BT
1802 struct wacom *wacom = hid_get_drvdata(hdev);
1803 u8 new_speed;
1804
1805 if (kstrtou8(buf, 0, &new_speed))
1806 return -EINVAL;
1807
1808 if (new_speed != 0 && new_speed != 1)
1809 return -EINVAL;
1810
1811 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1812
1813 return count;
1814}
1815
e0984bc3 1816static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
f81a1295
BT
1817 wacom_show_speed, wacom_store_speed);
1818
72b236d6
AS
1819
1820static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1821 struct kobj_attribute *kattr,
1822 char *buf, int index)
1823{
2cf83833 1824 struct device *dev = kobj_to_dev(kobj->parent);
ee79a8f8 1825 struct hid_device *hdev = to_hid_device(dev);
72b236d6
AS
1826 struct wacom *wacom = hid_get_drvdata(hdev);
1827 u8 mode;
1828
a50aac71 1829 mode = wacom->led.groups[index].select;
bc35f73a 1830 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
72b236d6
AS
1831}
1832
1833#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1834static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1835 struct kobj_attribute *kattr, char *buf) \
1836{ \
1837 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1838} \
1839static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1840 .attr = {.name = "remote_mode", \
1841 .mode = DEV_ATTR_RO_PERM}, \
1842 .show = wacom_show_remote##SET_ID##_mode, \
1843}; \
1844static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1845 &remote##SET_ID##_mode_attr.attr, \
1846 NULL \
1847}; \
1848static struct attribute_group remote##SET_ID##_serial_group = { \
1849 .name = NULL, \
1850 .attrs = remote##SET_ID##_serial_attrs, \
1851}
1852
1853DEVICE_EKR_ATTR_GROUP(0);
1854DEVICE_EKR_ATTR_GROUP(1);
1855DEVICE_EKR_ATTR_GROUP(2);
1856DEVICE_EKR_ATTR_GROUP(3);
1857DEVICE_EKR_ATTR_GROUP(4);
1858
e6f2813a
BT
1859static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1860 int index)
72b236d6
AS
1861{
1862 int error = 0;
83e6b40e 1863 struct wacom_remote *remote = wacom->remote;
72b236d6 1864
e7749f6e 1865 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
83e6b40e
BT
1866 GFP_KERNEL,
1867 "%d", serial);
e7749f6e 1868 if (!remote->remotes[index].group.name)
72b236d6 1869 return -ENOMEM;
72b236d6 1870
f9036bd4 1871 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
e7749f6e 1872 &remote->remotes[index].group);
72b236d6 1873 if (error) {
e7749f6e 1874 remote->remotes[index].group.name = NULL;
72b236d6
AS
1875 hid_err(wacom->hdev,
1876 "cannot create sysfs group err: %d\n", error);
72b236d6
AS
1877 return error;
1878 }
1879
1880 return 0;
1881}
1882
72b236d6
AS
1883static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1884{
1885 const size_t buf_size = 2;
1886 unsigned char *buf;
1887 int retval;
1888
1889 buf = kzalloc(buf_size, GFP_KERNEL);
1890 if (!buf)
1891 return -ENOMEM;
1892
1893 buf[0] = WAC_CMD_DELETE_PAIRING;
1894 buf[1] = selector;
1895
1896 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1897 buf_size, WAC_CMD_RETRIES);
1898 kfree(buf);
1899
1900 return retval;
1901}
1902
1903static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1904 struct kobj_attribute *attr,
1905 const char *buf, size_t count)
1906{
1907 unsigned char selector = 0;
2cf83833 1908 struct device *dev = kobj_to_dev(kobj->parent);
ee79a8f8 1909 struct hid_device *hdev = to_hid_device(dev);
72b236d6
AS
1910 struct wacom *wacom = hid_get_drvdata(hdev);
1911 int err;
1912
1913 if (!strncmp(buf, "*\n", 2)) {
1914 selector = WAC_CMD_UNPAIR_ALL;
1915 } else {
1916 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1917 buf);
1918 return -1;
1919 }
1920
1921 mutex_lock(&wacom->lock);
1922
1923 err = wacom_cmd_unpair_remote(wacom, selector);
1924 mutex_unlock(&wacom->lock);
1925
1926 return err < 0 ? err : count;
1927}
1928
1929static struct kobj_attribute unpair_remote_attr = {
1930 .attr = {.name = "unpair_remote", .mode = 0200},
1931 .store = wacom_store_unpair_remote,
1932};
1933
1934static const struct attribute *remote_unpair_attrs[] = {
1935 &unpair_remote_attr.attr,
1936 NULL
1937};
1938
83e6b40e
BT
1939static void wacom_remotes_destroy(void *data)
1940{
1941 struct wacom *wacom = data;
1942 struct wacom_remote *remote = wacom->remote;
1943
1944 if (!remote)
1945 return;
1946
1947 kobject_put(remote->remote_dir);
1948 kfifo_free(&remote->remote_fifo);
1949 wacom->remote = NULL;
1950}
1951
1952static int wacom_initialize_remotes(struct wacom *wacom)
72b236d6
AS
1953{
1954 int error = 0;
83e6b40e 1955 struct wacom_remote *remote;
72b236d6
AS
1956 int i;
1957
1958 if (wacom->wacom_wac.features.type != REMOTE)
1959 return 0;
1960
83e6b40e
BT
1961 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1962 GFP_KERNEL);
1963 if (!remote)
1964 return -ENOMEM;
72b236d6 1965
83e6b40e
BT
1966 wacom->remote = remote;
1967
1968 spin_lock_init(&remote->remote_lock);
1969
1970 error = kfifo_alloc(&remote->remote_fifo,
1971 5 * sizeof(struct wacom_remote_data),
1972 GFP_KERNEL);
1973 if (error) {
1974 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
72b236d6 1975 return -ENOMEM;
83e6b40e 1976 }
72b236d6 1977
e7749f6e
BT
1978 remote->remotes[0].group = remote0_serial_group;
1979 remote->remotes[1].group = remote1_serial_group;
1980 remote->remotes[2].group = remote2_serial_group;
1981 remote->remotes[3].group = remote3_serial_group;
1982 remote->remotes[4].group = remote4_serial_group;
83e6b40e
BT
1983
1984 remote->remote_dir = kobject_create_and_add("wacom_remote",
1985 &wacom->hdev->dev.kobj);
1986 if (!remote->remote_dir)
1987 return -ENOMEM;
1988
1989 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
72b236d6
AS
1990
1991 if (error) {
1992 hid_err(wacom->hdev,
1993 "cannot create sysfs group err: %d\n", error);
1994 return error;
1995 }
1996
1997 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
a50aac71 1998 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
e7749f6e 1999 remote->remotes[i].serial = 0;
72b236d6
AS
2000 }
2001
83e6b40e
BT
2002 error = devm_add_action_or_reset(&wacom->hdev->dev,
2003 wacom_remotes_destroy, wacom);
2004 if (error)
2005 return error;
2006
72b236d6
AS
2007 return 0;
2008}
2009
d2d13f18 2010static struct input_dev *wacom_allocate_input(struct wacom *wacom)
3aac0ef1
CB
2011{
2012 struct input_dev *input_dev;
b6c79f2c 2013 struct hid_device *hdev = wacom->hdev;
3aac0ef1 2014 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
3aac0ef1 2015
3dad188e 2016 input_dev = devm_input_allocate_device(&hdev->dev);
d2d13f18
BT
2017 if (!input_dev)
2018 return NULL;
3aac0ef1 2019
2bdd163c 2020 input_dev->name = wacom_wac->features.name;
b6c79f2c
BT
2021 input_dev->phys = hdev->phys;
2022 input_dev->dev.parent = &hdev->dev;
3aac0ef1
CB
2023 input_dev->open = wacom_open;
2024 input_dev->close = wacom_close;
b6c79f2c
BT
2025 input_dev->uniq = hdev->uniq;
2026 input_dev->id.bustype = hdev->bus;
2027 input_dev->id.vendor = hdev->vendor;
12969e3b 2028 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
b6c79f2c 2029 input_dev->id.version = hdev->version;
3aac0ef1
CB
2030 input_set_drvdata(input_dev, wacom);
2031
d2d13f18
BT
2032 return input_dev;
2033}
2034
d9f2d203
JG
2035static int wacom_allocate_inputs(struct wacom *wacom)
2036{
2037 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2038
2039 wacom_wac->pen_input = wacom_allocate_input(wacom);
2040 wacom_wac->touch_input = wacom_allocate_input(wacom);
2041 wacom_wac->pad_input = wacom_allocate_input(wacom);
3dad188e
BT
2042 if (!wacom_wac->pen_input ||
2043 !wacom_wac->touch_input ||
2044 !wacom_wac->pad_input)
d9f2d203 2045 return -ENOMEM;
d9f2d203 2046
2bdd163c 2047 wacom_wac->pen_input->name = wacom_wac->pen_name;
d9f2d203
JG
2048 wacom_wac->touch_input->name = wacom_wac->touch_name;
2049 wacom_wac->pad_input->name = wacom_wac->pad_name;
2050
2051 return 0;
2052}
2053
2546dacd
BT
2054static int wacom_register_inputs(struct wacom *wacom)
2055{
2a6cdbdd 2056 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2546dacd 2057 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2636a3f2 2058 int error = 0;
2546dacd 2059
2a6cdbdd
JG
2060 pen_input_dev = wacom_wac->pen_input;
2061 touch_input_dev = wacom_wac->touch_input;
2546dacd
BT
2062 pad_input_dev = wacom_wac->pad_input;
2063
2a6cdbdd 2064 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
2546dacd
BT
2065 return -EINVAL;
2066
2a6cdbdd
JG
2067 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
2068 if (error) {
2069 /* no pen in use on this interface */
2070 input_free_device(pen_input_dev);
2071 wacom_wac->pen_input = NULL;
2072 pen_input_dev = NULL;
2073 } else {
2074 error = input_register_device(pen_input_dev);
2075 if (error)
3dad188e 2076 goto fail;
2a6cdbdd
JG
2077 }
2078
2079 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
2080 if (error) {
2081 /* no touch in use on this interface */
2082 input_free_device(touch_input_dev);
2083 wacom_wac->touch_input = NULL;
2084 touch_input_dev = NULL;
2085 } else {
2086 error = input_register_device(touch_input_dev);
30ebc1ae 2087 if (error)
3dad188e 2088 goto fail;
30ebc1ae 2089 }
1963518b 2090
d2d13f18
BT
2091 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2092 if (error) {
2093 /* no pad in use on this interface */
2094 input_free_device(pad_input_dev);
2095 wacom_wac->pad_input = NULL;
2096 pad_input_dev = NULL;
2097 } else {
2098 error = input_register_device(pad_input_dev);
2099 if (error)
3dad188e 2100 goto fail;
d2d13f18
BT
2101 }
2102
1963518b 2103 return 0;
3aac0ef1 2104
3dad188e
BT
2105fail:
2106 wacom_wac->pad_input = NULL;
2a6cdbdd 2107 wacom_wac->touch_input = NULL;
2a6cdbdd 2108 wacom_wac->pen_input = NULL;
3aac0ef1
CB
2109 return error;
2110}
2111
0be01712
JG
2112/*
2113 * Not all devices report physical dimensions from HID.
2114 * Compute the default from hardcoded logical dimension
2115 * and resolution before driver overwrites them.
2116 */
2117static void wacom_set_default_phy(struct wacom_features *features)
2118{
2119 if (features->x_resolution) {
2120 features->x_phy = (features->x_max * 100) /
2121 features->x_resolution;
2122 features->y_phy = (features->y_max * 100) /
2123 features->y_resolution;
2124 }
2125}
2126
2127static void wacom_calculate_res(struct wacom_features *features)
2128{
2129 /* set unit to "100th of a mm" for devices not reported by HID */
2130 if (!features->unit) {
2131 features->unit = 0x11;
2132 features->unitExpo = -3;
2133 }
2134
2135 features->x_resolution = wacom_calc_hid_res(features->x_max,
2136 features->x_phy,
2137 features->unit,
2138 features->unitExpo);
2139 features->y_resolution = wacom_calc_hid_res(features->y_max,
2140 features->y_phy,
2141 features->unit,
2142 features->unitExpo);
2143}
2144
fce9957d
JG
2145void wacom_battery_work(struct work_struct *work)
2146{
d17d1f17 2147 struct wacom *wacom = container_of(work, struct wacom, battery_work);
fce9957d
JG
2148
2149 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
59d69bc8 2150 !wacom->battery.battery) {
fce9957d
JG
2151 wacom_initialize_battery(wacom);
2152 }
2153 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
59d69bc8 2154 wacom->battery.battery) {
fce9957d
JG
2155 wacom_destroy_battery(wacom);
2156 }
2157}
2158
01c846f9
BT
2159static size_t wacom_compute_pktlen(struct hid_device *hdev)
2160{
2161 struct hid_report_enum *report_enum;
2162 struct hid_report *report;
2163 size_t size = 0;
2164
2165 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2166
2167 list_for_each_entry(report, &report_enum->report_list, list) {
dabb05c6 2168 size_t report_size = hid_report_len(report);
01c846f9
BT
2169 if (report_size > size)
2170 size = report_size;
2171 }
2172
2173 return size;
2174}
2175
fd5f92b6 2176static void wacom_update_name(struct wacom *wacom, const char *suffix)
c24eab4e
PC
2177{
2178 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2179 struct wacom_features *features = &wacom_wac->features;
073b50bc 2180 char name[WACOM_NAME_MAX - 20]; /* Leave some room for suffixes */
c24eab4e
PC
2181
2182 /* Generic devices name unspecified */
2183 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
09dc28ac 2184 char *product_name = wacom->hdev->name;
f2209d4a 2185
09dc28ac
JG
2186 if (hid_is_using_ll_driver(wacom->hdev, &usb_hid_driver)) {
2187 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2188 struct usb_device *dev = interface_to_usbdev(intf);
2189 product_name = dev->product;
2190 }
f2209d4a 2191
09dc28ac
JG
2192 if (wacom->hdev->bus == BUS_I2C) {
2193 snprintf(name, sizeof(name), "%s %X",
2194 features->name, wacom->hdev->product);
2195 } else if (strstr(product_name, "Wacom") ||
2196 strstr(product_name, "wacom") ||
2197 strstr(product_name, "WACOM")) {
2198 strlcpy(name, product_name, sizeof(name));
c24eab4e 2199 } else {
09dc28ac 2200 snprintf(name, sizeof(name), "Wacom %s", product_name);
c24eab4e 2201 }
09dc28ac
JG
2202
2203 /* strip out excess whitespaces */
2204 while (1) {
2205 char *gap = strstr(name, " ");
2206 if (gap == NULL)
2207 break;
2208 /* shift everything including the terminator */
2209 memmove(gap, gap+1, strlen(gap));
2210 }
2211
2212 /* get rid of trailing whitespace */
2213 if (name[strlen(name)-1] == ' ')
2214 name[strlen(name)-1] = '\0';
c24eab4e 2215 } else {
44b5250b 2216 strlcpy(name, features->name, sizeof(name));
c24eab4e
PC
2217 }
2218
9956953e
BT
2219 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2220 name, suffix);
2221
c24eab4e 2222 /* Append the device type to the name */
2a6cdbdd 2223 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
fd5f92b6 2224 "%s%s Pen", name, suffix);
2a6cdbdd 2225 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
fd5f92b6 2226 "%s%s Finger", name, suffix);
c24eab4e 2227 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
fd5f92b6 2228 "%s%s Pad", name, suffix);
c24eab4e
PC
2229}
2230
84dfbd7f
BT
2231static void wacom_release_resources(struct wacom *wacom)
2232{
2233 struct hid_device *hdev = wacom->hdev;
2234
2235 if (!wacom->resources)
2236 return;
2237
2238 devres_release_group(&hdev->dev, wacom);
2239
2240 wacom->resources = false;
2241
2242 wacom->wacom_wac.pen_input = NULL;
2243 wacom->wacom_wac.touch_input = NULL;
2244 wacom->wacom_wac.pad_input = NULL;
2245}
2246
d2ec58ae
AAS
2247static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2248{
2249 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2250 wacom_wac->shared->type = wacom_wac->features.type;
2251 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2252 }
2253
d793ff81 2254 if (wacom_wac->has_mute_touch_switch) {
d2ec58ae 2255 wacom_wac->shared->has_mute_touch_switch = true;
d793ff81
PC
2256 wacom_wac->shared->is_touch_on = true;
2257 }
d2ec58ae
AAS
2258
2259 if (wacom_wac->shared->has_mute_touch_switch &&
2260 wacom_wac->shared->touch_input) {
2261 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2262 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2263 SW_MUTE_DEVICE);
2264 }
2265}
2266
fd5f92b6 2267static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
3bea733a 2268{
c58ac3a8
BT
2269 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2270 struct wacom_features *features = &wacom_wac->features;
2271 struct hid_device *hdev = wacom->hdev;
e33da8a5 2272 int error;
7704ac93 2273 unsigned int connect_mask = HID_CONNECT_HIDRAW;
3bea733a 2274
01c846f9 2275 features->pktlen = wacom_compute_pktlen(hdev);
c58ac3a8
BT
2276 if (features->pktlen > WACOM_PKGLEN_MAX)
2277 return -EINVAL;
3bea733a 2278
84dfbd7f
BT
2279 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2280 return -ENOMEM;
2281
2282 wacom->resources = true;
2283
3f14a63a
JG
2284 error = wacom_allocate_inputs(wacom);
2285 if (error)
3888b0d5 2286 goto fail;
494078b0 2287
8c97a765
BT
2288 /*
2289 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2290 * into debug mode for the touch part.
2291 * We ignore the other interfaces.
2292 */
2293 if (features->type == BAMBOO_PAD) {
2294 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2295 features->type = HID_GENERIC;
2296 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2297 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2298 error = -ENODEV;
3888b0d5 2299 goto fail;
8c97a765
BT
2300 }
2301 }
2302
401d7d10
PC
2303 /* set the default size in case we do not get them from hid */
2304 wacom_set_default_phy(features);
2305
f393ee2b 2306 /* Retrieve the physical and logical size for touch devices */
c669fb2b 2307 wacom_retrieve_hid_descriptor(hdev, features);
42f4f272 2308 wacom_setup_device_quirks(wacom);
042628ab 2309
aa86b18c
JG
2310 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2311 features->type != WIRELESS) {
8e116d31
JG
2312 error = features->type == HID_GENERIC ? -ENODEV : 0;
2313
042628ab 2314 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
8e116d31
JG
2315 hdev->name,
2316 error ? "Ignoring" : "Assuming pen");
2317
2318 if (error)
3888b0d5 2319 goto fail;
042628ab 2320
aa86b18c 2321 features->device_type |= WACOM_DEVICETYPE_PEN;
042628ab
JG
2322 }
2323
401d7d10
PC
2324 wacom_calculate_res(features);
2325
fd5f92b6 2326 wacom_update_name(wacom, wireless ? " (WL)" : "");
4492efff 2327
8b407359
AAS
2328 /* pen only Bamboo neither support touch nor pad */
2329 if ((features->type == BAMBOO_PEN) &&
2330 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2331 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2332 error = -ENODEV;
2333 goto fail;
2334 }
2335
a9ce7856
JG
2336 error = wacom_add_shared_data(hdev);
2337 if (error)
2338 goto fail;
49b764ae 2339
ccad85cc 2340 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
f81a1295
BT
2341 (features->quirks & WACOM_QUIRK_BATTERY)) {
2342 error = wacom_initialize_battery(wacom);
2343 if (error)
3888b0d5 2344 goto fail;
f81a1295
BT
2345 }
2346
3f14a63a
JG
2347 error = wacom_register_inputs(wacom);
2348 if (error)
3888b0d5 2349 goto fail;
f81a1295 2350
b62f6465 2351 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
85d2c77b
BT
2352 error = wacom_initialize_leds(wacom);
2353 if (error)
3888b0d5 2354 goto fail;
85d2c77b 2355
83e6b40e 2356 error = wacom_initialize_remotes(wacom);
b62f6465 2357 if (error)
3888b0d5 2358 goto fail;
b62f6465
BT
2359 }
2360
7704ac93
BT
2361 if (features->type == HID_GENERIC)
2362 connect_mask |= HID_CONNECT_DRIVER;
2363
29b47391 2364 /* Regular HID work starts now */
7704ac93 2365 error = hid_hw_start(hdev, connect_mask);
29b47391
BT
2366 if (error) {
2367 hid_err(hdev, "hw start failed\n");
3888b0d5 2368 goto fail;
d3825d51 2369 }
961794a0 2370
fd5f92b6
BT
2371 if (!wireless) {
2372 /* Note that if query fails it is not a hard failure */
a544c619 2373 wacom_query_tablet_data(wacom);
fd5f92b6 2374 }
86e88f0e
JG
2375
2376 /* touch only Bamboo doesn't support pen */
2377 if ((features->type == BAMBOO_TOUCH) &&
2378 (features->device_type & WACOM_DEVICETYPE_PEN)) {
4d20c332
AAS
2379 cancel_delayed_work_sync(&wacom->init_work);
2380 _wacom_query_tablet_data(wacom);
86e88f0e 2381 error = -ENODEV;
b62f6465 2382 goto fail_quirks;
86e88f0e
JG
2383 }
2384
ccad85cc 2385 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
29b47391
BT
2386 error = hid_hw_open(hdev);
2387
d2ec58ae 2388 wacom_set_shared_values(wacom_wac);
84dfbd7f
BT
2389 devres_close_group(&hdev->dev, wacom);
2390
3bea733a
PC
2391 return 0;
2392
b62f6465 2393fail_quirks:
c58ac3a8 2394 hid_hw_stop(hdev);
3888b0d5 2395fail:
84dfbd7f 2396 wacom_release_resources(wacom);
c58ac3a8
BT
2397 return error;
2398}
2399
a2f091af
BT
2400static void wacom_wireless_work(struct work_struct *work)
2401{
d17d1f17 2402 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
a2f091af
BT
2403 struct usb_device *usbdev = wacom->usbdev;
2404 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2405 struct hid_device *hdev1, *hdev2;
2406 struct wacom *wacom1, *wacom2;
2407 struct wacom_wac *wacom_wac1, *wacom_wac2;
2408 int error;
2409
2410 /*
2411 * Regardless if this is a disconnect or a new tablet,
2412 * remove any existing input and battery devices.
2413 */
2414
2415 wacom_destroy_battery(wacom);
2416
2417 /* Stylus interface */
2418 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2419 wacom1 = hid_get_drvdata(hdev1);
2420 wacom_wac1 = &(wacom1->wacom_wac);
84dfbd7f 2421 wacom_release_resources(wacom1);
a2f091af
BT
2422
2423 /* Touch interface */
2424 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2425 wacom2 = hid_get_drvdata(hdev2);
2426 wacom_wac2 = &(wacom2->wacom_wac);
84dfbd7f 2427 wacom_release_resources(wacom2);
a2f091af
BT
2428
2429 if (wacom_wac->pid == 0) {
2430 hid_info(wacom->hdev, "wireless tablet disconnected\n");
a2f091af
BT
2431 } else {
2432 const struct hid_device_id *id = wacom_ids;
2433
2434 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2435 wacom_wac->pid);
2436
2437 while (id->bus) {
2438 if (id->vendor == USB_VENDOR_ID_WACOM &&
2439 id->product == wacom_wac->pid)
2440 break;
2441 id++;
2442 }
2443
2444 if (!id->bus) {
2445 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2446 return;
2447 }
2448
2449 /* Stylus interface */
2450 wacom_wac1->features =
2451 *((struct wacom_features *)id->driver_data);
fd5f92b6 2452
a2f091af 2453 wacom_wac1->pid = wacom_wac->pid;
fd5f92b6
BT
2454 hid_hw_stop(hdev1);
2455 error = wacom_parse_and_register(wacom1, true);
a2f091af
BT
2456 if (error)
2457 goto fail;
2458
2459 /* Touch interface */
2460 if (wacom_wac1->features.touch_max ||
2461 (wacom_wac1->features.type >= INTUOSHT &&
2462 wacom_wac1->features.type <= BAMBOO_PT)) {
2463 wacom_wac2->features =
2464 *((struct wacom_features *)id->driver_data);
a2f091af 2465 wacom_wac2->pid = wacom_wac->pid;
fd5f92b6
BT
2466 hid_hw_stop(hdev2);
2467 error = wacom_parse_and_register(wacom2, true);
a2f091af
BT
2468 if (error)
2469 goto fail;
a2f091af
BT
2470 }
2471
9956953e
BT
2472 strlcpy(wacom_wac->name, wacom_wac1->name,
2473 sizeof(wacom_wac->name));
a2f091af
BT
2474 error = wacom_initialize_battery(wacom);
2475 if (error)
2476 goto fail;
2477 }
2478
2479 return;
2480
2481fail:
84dfbd7f 2482 wacom_release_resources(wacom1);
84dfbd7f 2483 wacom_release_resources(wacom2);
a2f091af
BT
2484 return;
2485}
2486
04bfa27b
BT
2487static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2488{
2489 struct wacom_remote *remote = wacom->remote;
e7749f6e 2490 u32 serial = remote->remotes[index].serial;
04bfa27b 2491 int i;
7c35dc3c
BT
2492 unsigned long flags;
2493
791ae273
AAS
2494 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2495 if (remote->remotes[i].serial == serial) {
04bfa27b 2496
791ae273
AAS
2497 spin_lock_irqsave(&remote->remote_lock, flags);
2498 remote->remotes[i].registered = false;
2499 spin_unlock_irqrestore(&remote->remote_lock, flags);
9f1015d4 2500
791ae273
AAS
2501 if (remote->remotes[i].battery.battery)
2502 devres_release_group(&wacom->hdev->dev,
2503 &remote->remotes[i].battery.bat_desc);
2504
2505 if (remote->remotes[i].group.name)
2506 devres_release_group(&wacom->hdev->dev,
2507 &remote->remotes[i]);
04bfa27b 2508
e7749f6e
BT
2509 remote->remotes[i].serial = 0;
2510 remote->remotes[i].group.name = NULL;
9f1015d4 2511 remote->remotes[i].battery.battery = NULL;
04bfa27b
BT
2512 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2513 }
2514 }
2515}
2516
2517static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2518 unsigned int index)
2519{
2520 struct wacom_remote *remote = wacom->remote;
f9036bd4 2521 struct device *dev = &wacom->hdev->dev;
04bfa27b
BT
2522 int error, k;
2523
2524 /* A remote can pair more than once with an EKR,
2525 * check to make sure this serial isn't already paired.
2526 */
2527 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
e7749f6e 2528 if (remote->remotes[k].serial == serial)
04bfa27b
BT
2529 break;
2530 }
2531
2532 if (k < WACOM_MAX_REMOTES) {
e7749f6e 2533 remote->remotes[index].serial = serial;
04bfa27b
BT
2534 return 0;
2535 }
2536
e7749f6e 2537 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
f9036bd4
BT
2538 return -ENOMEM;
2539
04bfa27b
BT
2540 error = wacom_remote_create_attr_group(wacom, serial, index);
2541 if (error)
f9036bd4 2542 goto fail;
04bfa27b 2543
7c35dc3c
BT
2544 remote->remotes[index].input = wacom_allocate_input(wacom);
2545 if (!remote->remotes[index].input) {
2546 error = -ENOMEM;
2547 goto fail;
2548 }
2549 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2550 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2551
2552 if (!remote->remotes[index].input->name) {
2553 error = -EINVAL;
2554 goto fail;
2555 }
2556
2557 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2558 &wacom->wacom_wac);
2559 if (error)
2560 goto fail;
2561
e7749f6e 2562 remote->remotes[index].serial = serial;
04bfa27b 2563
7c35dc3c
BT
2564 error = input_register_device(remote->remotes[index].input);
2565 if (error)
2566 goto fail;
2567
97f5541f
BT
2568 error = wacom_led_groups_alloc_and_register_one(
2569 &remote->remotes[index].input->dev,
2570 wacom, index, 3, true);
2571 if (error)
2572 goto fail;
2573
7c35dc3c
BT
2574 remote->remotes[index].registered = true;
2575
e7749f6e 2576 devres_close_group(dev, &remote->remotes[index]);
04bfa27b 2577 return 0;
f9036bd4
BT
2578
2579fail:
e7749f6e
BT
2580 devres_release_group(dev, &remote->remotes[index]);
2581 remote->remotes[index].serial = 0;
f9036bd4 2582 return error;
04bfa27b
BT
2583}
2584
9f1015d4
BT
2585static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2586{
2587 struct wacom_remote *remote = wacom->remote;
2588 int error;
2589
2590 if (!remote->remotes[index].registered)
2591 return 0;
2592
2593 if (remote->remotes[index].battery.battery)
2594 return 0;
2595
2596 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2597 return 0;
2598
2599 error = __wacom_initialize_battery(wacom,
2600 &wacom->remote->remotes[index].battery);
2601 if (error)
2602 return error;
2603
2604 return 0;
2605}
2606
e6f2813a
BT
2607static void wacom_remote_work(struct work_struct *work)
2608{
2609 struct wacom *wacom = container_of(work, struct wacom, remote_work);
83e6b40e 2610 struct wacom_remote *remote = wacom->remote;
e6f2813a
BT
2611 struct wacom_remote_data data;
2612 unsigned long flags;
2613 unsigned int count;
2614 u32 serial;
04bfa27b 2615 int i;
e6f2813a 2616
83e6b40e 2617 spin_lock_irqsave(&remote->remote_lock, flags);
e6f2813a 2618
83e6b40e 2619 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
e6f2813a
BT
2620
2621 if (count != sizeof(data)) {
2622 hid_err(wacom->hdev,
2623 "workitem triggered without status available\n");
83e6b40e 2624 spin_unlock_irqrestore(&remote->remote_lock, flags);
e6f2813a
BT
2625 return;
2626 }
2627
83e6b40e 2628 if (!kfifo_is_empty(&remote->remote_fifo))
e6f2813a
BT
2629 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2630
83e6b40e 2631 spin_unlock_irqrestore(&remote->remote_lock, flags);
e6f2813a
BT
2632
2633 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2634 serial = data.remote[i].serial;
2635 if (data.remote[i].connected) {
2636
9f1015d4
BT
2637 if (remote->remotes[i].serial == serial) {
2638 wacom_remote_attach_battery(wacom, i);
e6f2813a 2639 continue;
9f1015d4 2640 }
e6f2813a 2641
e7749f6e 2642 if (remote->remotes[i].serial)
04bfa27b 2643 wacom_remote_destroy_one(wacom, i);
e6f2813a 2644
04bfa27b 2645 wacom_remote_create_one(wacom, serial, i);
e6f2813a 2646
e7749f6e 2647 } else if (remote->remotes[i].serial) {
04bfa27b 2648 wacom_remote_destroy_one(wacom, i);
e6f2813a
BT
2649 }
2650 }
2651}
2652
4082da80
BT
2653static void wacom_mode_change_work(struct work_struct *work)
2654{
2655 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2656 struct wacom_shared *shared = wacom->wacom_wac.shared;
2657 struct wacom *wacom1 = NULL;
2658 struct wacom *wacom2 = NULL;
2659 bool is_direct = wacom->wacom_wac.is_direct_mode;
2660 int error = 0;
2661
2662 if (shared->pen) {
2663 wacom1 = hid_get_drvdata(shared->pen);
2664 wacom_release_resources(wacom1);
2665 hid_hw_stop(wacom1->hdev);
2666 wacom1->wacom_wac.has_mode_change = true;
2667 wacom1->wacom_wac.is_direct_mode = is_direct;
2668 }
2669
2670 if (shared->touch) {
2671 wacom2 = hid_get_drvdata(shared->touch);
2672 wacom_release_resources(wacom2);
2673 hid_hw_stop(wacom2->hdev);
2674 wacom2->wacom_wac.has_mode_change = true;
2675 wacom2->wacom_wac.is_direct_mode = is_direct;
2676 }
2677
2678 if (wacom1) {
2679 error = wacom_parse_and_register(wacom1, false);
2680 if (error)
2681 return;
2682 }
2683
2684 if (wacom2) {
2685 error = wacom_parse_and_register(wacom2, false);
2686 if (error)
2687 return;
2688 }
2689
a2f091af
BT
2690 return;
2691}
2692
c58ac3a8
BT
2693static int wacom_probe(struct hid_device *hdev,
2694 const struct hid_device_id *id)
2695{
2696 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2697 struct usb_device *dev = interface_to_usbdev(intf);
2698 struct wacom *wacom;
2699 struct wacom_wac *wacom_wac;
2700 struct wacom_features *features;
2701 int error;
2702
2703 if (!id->driver_data)
2704 return -EINVAL;
2705
2706 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2707
2708 /* hid-core sets this quirk for the boot interface */
2709 hdev->quirks &= ~HID_QUIRK_NOGET;
2710
19b64330 2711 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
c58ac3a8
BT
2712 if (!wacom)
2713 return -ENOMEM;
2714
2715 hid_set_drvdata(hdev, wacom);
2716 wacom->hdev = hdev;
2717
2718 wacom_wac = &wacom->wacom_wac;
2719 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2720 features = &wacom_wac->features;
2721
362c571b
BT
2722 if (features->check_for_hid_type && features->hid_type != hdev->type)
2723 return -ENODEV;
c58ac3a8 2724
83417206
JG
2725 error = kfifo_alloc(&wacom_wac->pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
2726 if (error)
362c571b 2727 return error;
83417206 2728
c6fa1aeb 2729 wacom_wac->hid_data.inputmode = -1;
326ea2a9 2730 wacom_wac->mode_report = -1;
c6fa1aeb 2731
c58ac3a8
BT
2732 wacom->usbdev = dev;
2733 wacom->intf = intf;
2734 mutex_init(&wacom->lock);
a544c619 2735 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
d17d1f17
BT
2736 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2737 INIT_WORK(&wacom->battery_work, wacom_battery_work);
e6f2813a 2738 INIT_WORK(&wacom->remote_work, wacom_remote_work);
4082da80 2739 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
c58ac3a8
BT
2740
2741 /* ask for the report descriptor to be loaded by HID */
2742 error = hid_parse(hdev);
2743 if (error) {
2744 hid_err(hdev, "parse failed\n");
362c571b 2745 return error;
c58ac3a8
BT
2746 }
2747
fd5f92b6 2748 error = wacom_parse_and_register(wacom, false);
c58ac3a8 2749 if (error)
362c571b 2750 return error;
c58ac3a8
BT
2751
2752 if (hdev->bus == BUS_BLUETOOTH) {
2753 error = device_create_file(&hdev->dev, &dev_attr_speed);
2754 if (error)
2755 hid_warn(hdev,
2756 "can't create sysfs speed attribute err: %d\n",
2757 error);
2758 }
2759
2760 return 0;
3bea733a
PC
2761}
2762
29b47391 2763static void wacom_remove(struct hid_device *hdev)
3bea733a 2764{
29b47391 2765 struct wacom *wacom = hid_get_drvdata(hdev);
f6205161
BT
2766 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2767 struct wacom_features *features = &wacom_wac->features;
2768
2769 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2770 hid_hw_close(hdev);
3bea733a 2771
29b47391 2772 hid_hw_stop(hdev);
e7224094 2773
a544c619 2774 cancel_delayed_work_sync(&wacom->init_work);
d17d1f17
BT
2775 cancel_work_sync(&wacom->wireless_work);
2776 cancel_work_sync(&wacom->battery_work);
e6f2813a 2777 cancel_work_sync(&wacom->remote_work);
4082da80 2778 cancel_work_sync(&wacom->mode_change_work);
f81a1295
BT
2779 if (hdev->bus == BUS_BLUETOOTH)
2780 device_remove_file(&hdev->dev, &dev_attr_speed);
e7224094 2781
c0265a94
BT
2782 /* make sure we don't trigger the LEDs */
2783 wacom_led_groups_release(wacom);
b6b1f19b
AAS
2784
2785 if (wacom->wacom_wac.features.type != REMOTE)
2786 wacom_release_resources(wacom);
5b779fc5 2787
83417206 2788 kfifo_free(&wacom_wac->pen_fifo);
e7224094
ON
2789}
2790
41a74581 2791#ifdef CONFIG_PM
29b47391 2792static int wacom_resume(struct hid_device *hdev)
e7224094 2793{
29b47391 2794 struct wacom *wacom = hid_get_drvdata(hdev);
e7224094
ON
2795
2796 mutex_lock(&wacom->lock);
38101475
PC
2797
2798 /* switch to wacom mode first */
a544c619 2799 _wacom_query_tablet_data(wacom);
5d7e7d47 2800 wacom_led_control(wacom);
38101475 2801
e7224094
ON
2802 mutex_unlock(&wacom->lock);
2803
29b47391 2804 return 0;
e7224094
ON
2805}
2806
29b47391 2807static int wacom_reset_resume(struct hid_device *hdev)
e7224094 2808{
29b47391 2809 return wacom_resume(hdev);
3bea733a 2810}
41a74581 2811#endif /* CONFIG_PM */
3bea733a 2812
29b47391 2813static struct hid_driver wacom_driver = {
3bea733a 2814 .name = "wacom",
b036f6fb 2815 .id_table = wacom_ids,
3bea733a 2816 .probe = wacom_probe,
29b47391 2817 .remove = wacom_remove,
7704ac93 2818 .report = wacom_wac_report,
29b47391 2819#ifdef CONFIG_PM
e7224094
ON
2820 .resume = wacom_resume,
2821 .reset_resume = wacom_reset_resume,
29b47391
BT
2822#endif
2823 .raw_event = wacom_raw_event,
3bea733a 2824};
29b47391 2825module_hid_driver(wacom_driver);
f2e0a7d4
BT
2826
2827MODULE_VERSION(DRIVER_VERSION);
2828MODULE_AUTHOR(DRIVER_AUTHOR);
2829MODULE_DESCRIPTION(DRIVER_DESC);
7021b600 2830MODULE_LICENSE("GPL");