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