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