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