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