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

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of linux/drivers/hid/wacom_sys.c