drm/i915/backlight: Enable nits based luminance
[drm/drm-intel.git] / drivers / hid / wacom_sys.c
blob8125383932ec778cb37d64b69c1294ece4358237
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * USB Wacom tablet support - system specific code
4 */
6 #include "wacom_wac.h"
7 #include "wacom.h"
8 #include <linux/input/mt.h>
10 #define WAC_MSG_RETRIES 5
11 #define WAC_CMD_RETRIES 10
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)
17 static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
18 size_t size, unsigned int retries)
20 int retval;
22 do {
23 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
24 HID_REQ_GET_REPORT);
25 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
27 if (retval < 0)
28 hid_err(hdev, "wacom_get_report: ran out of retries "
29 "(last error = %d)\n", retval);
31 return retval;
34 static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
35 size_t size, unsigned int retries)
37 int retval;
39 do {
40 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
41 HID_REQ_SET_REPORT);
42 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
44 if (retval < 0)
45 hid_err(hdev, "wacom_set_report: ran out of retries "
46 "(last error = %d)\n", retval);
48 return retval;
51 static void wacom_wac_queue_insert(struct hid_device *hdev,
52 struct kfifo_rec_ptr_2 *fifo,
53 u8 *raw_data, int size)
55 bool warned = false;
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;
62 kfifo_skip(fifo);
65 kfifo_in(fifo, raw_data, size);
68 static void wacom_wac_queue_flush(struct hid_device *hdev,
69 struct kfifo_rec_ptr_2 *fifo)
71 while (!kfifo_is_empty(fifo)) {
72 u8 buf[WACOM_PKGLEN_MAX];
73 int size;
74 int err;
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);
85 static int wacom_wac_pen_serial_enforce(struct hid_device *hdev,
86 struct hid_report *report, u8 *raw_data, int report_size)
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;
95 if (wacom_wac->serial[0] || !(features->quirks & WACOM_QUIRK_TOOLSERIAL))
96 return 0;
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;
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;
114 offset = field->report_offset;
115 size = field->report_size;
116 value = hid_field_extract(hdev, raw_data+1, offset + j * size, size);
118 /* If we go out of range, we need to flush the queue ASAP */
119 if (equivalent_usage == HID_DG_INRANGE)
120 value = !value;
122 if (value) {
123 flush = true;
124 switch (equivalent_usage) {
125 case HID_DG_TOOLSERIALNUMBER:
126 wacom_wac->serial[0] = value;
127 break;
129 case WACOM_HID_WD_SERIALHI:
130 wacom_wac->serial[0] |= ((__u64)value) << 32;
131 break;
133 case WACOM_HID_WD_TOOLTYPE:
134 wacom_wac->id[0] = value;
135 break;
138 else {
139 insert = true;
144 if (flush)
145 wacom_wac_queue_flush(hdev, wacom_wac->pen_fifo);
146 else if (insert)
147 wacom_wac_queue_insert(hdev, wacom_wac->pen_fifo,
148 raw_data, report_size);
150 return insert && !flush;
153 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
154 u8 *raw_data, int size)
156 struct wacom *wacom = hid_get_drvdata(hdev);
158 if (wacom->wacom_wac.features.type == BOOTLOADER)
159 return 0;
161 if (size > WACOM_PKGLEN_MAX)
162 return 1;
164 if (wacom_wac_pen_serial_enforce(hdev, report, raw_data, size))
165 return -1;
167 memcpy(wacom->wacom_wac.data, raw_data, size);
169 wacom_wac_irq(&wacom->wacom_wac, size);
171 return 0;
174 static int wacom_open(struct input_dev *dev)
176 struct wacom *wacom = input_get_drvdata(dev);
178 return hid_hw_open(wacom->hdev);
181 static void wacom_close(struct input_dev *dev)
183 struct wacom *wacom = input_get_drvdata(dev);
186 * wacom->hdev should never be null, but surprisingly, I had the case
187 * once while unplugging the Wacom Wireless Receiver.
189 if (wacom->hdev)
190 hid_hw_close(wacom->hdev);
194 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
196 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
197 unsigned unit, int exponent)
199 struct hid_field field = {
200 .logical_maximum = logical_extents,
201 .physical_maximum = physical_extents,
202 .unit = unit,
203 .unit_exponent = exponent,
206 return hidinput_calc_abs_res(&field, ABS_X);
209 static void wacom_hid_usage_quirk(struct hid_device *hdev,
210 struct hid_field *field, struct hid_usage *usage)
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);
217 * The Dell Canvas 27 needs to be switched to its vendor-defined
218 * report to provide the best resolution.
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;
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.
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;
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;
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.
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];
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;
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;
292 static void wacom_feature_mapping(struct hid_device *hdev,
293 struct hid_field *field, struct hid_usage *usage)
295 struct wacom *wacom = hid_get_drvdata(hdev);
296 struct wacom_features *features = &wacom->wacom_wac.features;
297 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
298 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
299 u8 *data;
300 int ret;
301 u32 n;
303 wacom_hid_usage_quirk(hdev, field, usage);
305 switch (equivalent_usage) {
306 case WACOM_HID_WD_TOUCH_RING_SETTING:
307 wacom->generic_has_leds = true;
308 break;
309 case HID_DG_CONTACTMAX:
310 /* leave touch_max as is if predefined */
311 if (!features->touch_max) {
312 /* read manually */
313 n = hid_report_len(field->report);
314 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
315 if (!data)
316 break;
317 data[0] = field->report->id;
318 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
319 data, n, WAC_CMD_RETRIES);
320 if (ret == n && features->type == HID_GENERIC) {
321 ret = hid_report_raw_event(hdev,
322 HID_FEATURE_REPORT, data, n, 0);
323 } else if (ret == 2 && features->type != HID_GENERIC) {
324 features->touch_max = data[1];
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);
332 kfree(data);
334 break;
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;
342 hid_data->inputmode = field->report->id;
343 hid_data->inputmode_index = usage->usage_index;
344 break;
346 case HID_UP_DIGITIZER:
347 if (field->report->id == 0x0B &&
348 (field->application == WACOM_HID_G9_PEN ||
349 field->application == WACOM_HID_G11_PEN)) {
350 wacom->wacom_wac.mode_report = field->report->id;
351 wacom->wacom_wac.mode_value = 0;
353 break;
355 case WACOM_HID_WD_DATAMODE:
356 wacom->wacom_wac.mode_report = field->report->id;
357 wacom->wacom_wac.mode_value = 2;
358 break;
360 case WACOM_HID_UP_G9:
361 case WACOM_HID_UP_G11:
362 if (field->report->id == 0x03 &&
363 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
364 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
365 wacom->wacom_wac.mode_report = field->report->id;
366 wacom->wacom_wac.mode_value = 0;
368 break;
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__);
388 kfree(data);
389 break;
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.
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.
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.
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.
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.
422 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
423 * data. We deal with them after returning from this function.
425 static void wacom_usage_mapping(struct hid_device *hdev,
426 struct hid_field *field, struct hid_usage *usage)
428 struct wacom *wacom = hid_get_drvdata(hdev);
429 struct wacom_features *features = &wacom->wacom_wac.features;
430 bool finger = WACOM_FINGER_FIELD(field);
431 bool pen = WACOM_PEN_FIELD(field);
432 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
435 * Requiring Stylus Usage will ignore boot mouse
436 * X/Y values and some cases of invalid Digitizer X/Y
437 * values commonly reported.
439 if (pen)
440 features->device_type |= WACOM_DEVICETYPE_PEN;
441 else if (finger)
442 features->device_type |= WACOM_DEVICETYPE_TOUCH;
443 else
444 return;
446 wacom_hid_usage_quirk(hdev, field, usage);
448 switch (equivalent_usage) {
449 case HID_GD_X:
450 features->x_max = field->logical_maximum;
451 if (finger) {
452 features->x_phy = field->physical_maximum;
453 if ((features->type != BAMBOO_PT) &&
454 (features->type != BAMBOO_TOUCH)) {
455 features->unit = field->unit;
456 features->unitExpo = field->unit_exponent;
459 break;
460 case HID_GD_Y:
461 features->y_max = field->logical_maximum;
462 if (finger) {
463 features->y_phy = field->physical_maximum;
464 if ((features->type != BAMBOO_PT) &&
465 (features->type != BAMBOO_TOUCH)) {
466 features->unit = field->unit;
467 features->unitExpo = field->unit_exponent;
470 break;
471 case HID_DG_TIPPRESSURE:
472 if (pen)
473 features->pressure_max = field->logical_maximum;
474 break;
477 if (features->type == HID_GENERIC)
478 wacom_wac_usage_mapping(hdev, field, usage);
481 static void wacom_post_parse_hid(struct hid_device *hdev,
482 struct wacom_features *features)
484 struct wacom *wacom = hid_get_drvdata(hdev);
485 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
487 if (features->type == HID_GENERIC) {
488 /* Any last-minute generic device setup */
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;
496 if (features->touch_max > 1) {
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);
509 static void wacom_parse_hid(struct hid_device *hdev,
510 struct wacom_features *features)
512 struct hid_report_enum *rep_enum;
513 struct hid_report *hreport;
514 int i, j;
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;
524 for (j = 0; j < hreport->field[i]->maxusage; j++) {
525 wacom_feature_mapping(hdev, hreport->field[i],
526 hreport->field[i]->usage + j);
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) {
535 if (!hreport->maxfield)
536 continue;
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);
544 wacom_post_parse_hid(hdev, features);
547 static int wacom_hid_set_device_mode(struct hid_device *hdev)
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;
554 if (hid_data->inputmode < 0)
555 return 0;
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);
563 return 0;
566 static int wacom_set_device_mode(struct hid_device *hdev,
567 struct wacom_wac *wacom_wac)
569 u8 *rep_data;
570 struct hid_report *r;
571 struct hid_report_enum *re;
572 u32 length;
573 int error = -ENOMEM, limit = 0;
575 if (wacom_wac->mode_report < 0)
576 return 0;
578 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
579 r = re->report_id_hash[wacom_wac->mode_report];
580 if (!r)
581 return -EINVAL;
583 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
584 if (!rep_data)
585 return -ENOMEM;
587 length = hid_report_len(r);
589 do {
590 rep_data[0] = wacom_wac->mode_report;
591 rep_data[1] = wacom_wac->mode_value;
593 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
594 length, 1);
595 if (error >= 0)
596 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
597 rep_data, length, 1);
598 } while (error >= 0 &&
599 rep_data[1] != wacom_wac->mode_report &&
600 limit++ < WAC_MSG_RETRIES);
602 kfree(rep_data);
604 return error < 0 ? error : 0;
607 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
608 struct wacom_features *features)
610 struct wacom *wacom = hid_get_drvdata(hdev);
611 int ret;
612 u8 rep_data[2];
614 switch (features->type) {
615 case GRAPHIRE_BT:
616 rep_data[0] = 0x03;
617 rep_data[1] = 0x00;
618 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
621 if (ret >= 0) {
622 rep_data[0] = speed == 0 ? 0x05 : 0x06;
623 rep_data[1] = 0x00;
625 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
626 rep_data, 2, 3);
628 if (ret >= 0) {
629 wacom->wacom_wac.bt_high_speed = speed;
630 return 0;
635 * Note that if the raw queries fail, it's not a hard failure
636 * and it is safe to continue
638 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
639 rep_data[0], ret);
640 break;
641 case INTUOS4WL:
642 if (speed == 1)
643 wacom->wacom_wac.bt_features &= ~0x20;
644 else
645 wacom->wacom_wac.bt_features |= 0x20;
647 rep_data[0] = 0x03;
648 rep_data[1] = wacom->wacom_wac.bt_features;
650 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
652 if (ret >= 0)
653 wacom->wacom_wac.bt_high_speed = speed;
654 break;
657 return 0;
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.
667 static int _wacom_query_tablet_data(struct wacom *wacom)
669 struct hid_device *hdev = wacom->hdev;
670 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
671 struct wacom_features *features = &wacom_wac->features;
673 if (hdev->bus == BUS_BLUETOOTH)
674 return wacom_bt_query_tablet_data(hdev, 1, features);
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;
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;
700 wacom_set_device_mode(hdev, wacom_wac);
702 if (features->type == HID_GENERIC)
703 return wacom_hid_set_device_mode(hdev);
705 return 0;
708 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
709 struct wacom_features *features)
711 struct wacom *wacom = hid_get_drvdata(hdev);
712 struct usb_interface *intf = wacom->intf;
714 /* default features */
715 features->x_fuzz = 4;
716 features->y_fuzz = 4;
717 features->pressure_fuzz = 0;
718 features->distance_fuzz = 1;
719 features->tilt_fuzz = 1;
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.
727 if (features->type == WIRELESS && intf) {
728 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
729 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
730 else
731 features->device_type = WACOM_DEVICETYPE_NONE;
732 return;
735 wacom_parse_hid(hdev, features);
738 struct wacom_hdev_data {
739 struct list_head list;
740 struct kref kref;
741 struct hid_device *dev;
742 struct wacom_shared shared;
745 static LIST_HEAD(wacom_udev_list);
746 static DEFINE_MUTEX(wacom_udev_list_lock);
748 static bool wacom_are_sibling(struct hid_device *hdev,
749 struct hid_device *sibling)
751 struct wacom *wacom = hid_get_drvdata(hdev);
752 struct wacom_features *features = &wacom->wacom_wac.features;
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;
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;
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.
769 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
770 if (!hid_compare_device_paths(hdev, sibling, '/'))
771 return false;
772 } else {
773 if (!hid_compare_device_paths(hdev, sibling, '.'))
774 return false;
777 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
778 if (features->type != HID_GENERIC)
779 return true;
782 * Direct-input devices may not be siblings of indirect-input
783 * devices.
785 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
786 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
787 return false;
790 * Indirect-input devices may not be siblings of direct-input
791 * devices.
793 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
794 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
795 return false;
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;
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;
808 * No reason could be found for these two devices to NOT be
809 * siblings, so there's a good chance they ARE siblings
811 return true;
814 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
816 struct wacom_hdev_data *data;
818 /* Try to find an already-probed interface from the same device */
819 list_for_each_entry(data, &wacom_udev_list, list) {
820 if (hid_compare_device_paths(hdev, data->dev, '/')) {
821 kref_get(&data->kref);
822 return data;
826 /* Fallback to finding devices that appear to be "siblings" */
827 list_for_each_entry(data, &wacom_udev_list, list) {
828 if (wacom_are_sibling(hdev, data->dev)) {
829 kref_get(&data->kref);
830 return data;
834 return NULL;
837 static void wacom_release_shared_data(struct kref *kref)
839 struct wacom_hdev_data *data =
840 container_of(kref, struct wacom_hdev_data, kref);
842 mutex_lock(&wacom_udev_list_lock);
843 list_del(&data->list);
844 mutex_unlock(&wacom_udev_list_lock);
846 kfree(data);
849 static void wacom_remove_shared_data(void *res)
851 struct wacom *wacom = res;
852 struct wacom_hdev_data *data;
853 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
855 if (wacom_wac->shared) {
856 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
857 shared);
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;
864 kref_put(&data->kref, wacom_release_shared_data);
865 wacom_wac->shared = NULL;
869 static int wacom_add_shared_data(struct hid_device *hdev)
871 struct wacom *wacom = hid_get_drvdata(hdev);
872 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
873 struct wacom_hdev_data *data;
874 int retval = 0;
876 mutex_lock(&wacom_udev_list_lock);
878 data = wacom_get_hdev_data(hdev);
879 if (!data) {
880 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
881 if (!data) {
882 mutex_unlock(&wacom_udev_list_lock);
883 return -ENOMEM;
886 kref_init(&data->kref);
887 data->dev = hdev;
888 list_add_tail(&data->list, &wacom_udev_list);
891 mutex_unlock(&wacom_udev_list_lock);
893 wacom_wac->shared = &data->shared;
895 retval = devm_add_action_or_reset(&hdev->dev, wacom_remove_shared_data, wacom);
896 if (retval)
897 return retval;
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;
904 return retval;
907 static int wacom_led_control(struct wacom *wacom)
909 unsigned char *buf;
910 int retval;
911 unsigned char report_id = WAC_CMD_LED_CONTROL;
912 int buf_size = 9;
914 if (!wacom->led.groups)
915 return -ENOTSUPP;
917 if (wacom->wacom_wac.features.type == REMOTE)
918 return -ENOTSUPP;
920 if (wacom->wacom_wac.pid) { /* wireless connected */
921 report_id = WAC_CMD_WL_LED_CONTROL;
922 buf_size = 13;
924 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
925 report_id = WAC_CMD_WL_INTUOSP2;
926 buf_size = 51;
928 buf = kzalloc(buf_size, GFP_KERNEL);
929 if (!buf)
930 return -ENOMEM;
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;
937 } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
938 wacom->wacom_wac.features.type <= INTUOSPL)) {
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
944 int ring_led = wacom->led.groups[0].select & 0x03;
945 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
946 int crop_lum = 0;
947 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
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;
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;
968 else {
969 int led = wacom->led.groups[0].select | 0x4;
971 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
972 wacom->wacom_wac.features.type == WACOM_24HD)
973 led |= (wacom->led.groups[1].select << 4) | 0x40;
975 buf[0] = report_id;
976 buf[1] = led;
977 buf[2] = wacom->led.llv;
978 buf[3] = wacom->led.hlv;
979 buf[4] = wacom->led.img_lum;
982 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
983 WAC_CMD_RETRIES);
984 kfree(buf);
986 return retval;
989 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
990 const unsigned len, const void *img)
992 unsigned char *buf;
993 int i, retval;
994 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
996 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
997 if (!buf)
998 return -ENOMEM;
1000 /* Send 'start' command */
1001 buf[0] = WAC_CMD_ICON_START;
1002 buf[1] = 1;
1003 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1004 WAC_CMD_RETRIES);
1005 if (retval < 0)
1006 goto out;
1008 buf[0] = xfer_id;
1009 buf[1] = button_id & 0x07;
1010 for (i = 0; i < 4; i++) {
1011 buf[2] = i;
1012 memcpy(buf + 3, img + i * chunk_len, chunk_len);
1014 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
1015 buf, chunk_len + 3, WAC_CMD_RETRIES);
1016 if (retval < 0)
1017 break;
1020 /* Send 'stop' */
1021 buf[0] = WAC_CMD_ICON_START;
1022 buf[1] = 0;
1023 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1024 WAC_CMD_RETRIES);
1026 out:
1027 kfree(buf);
1028 return retval;
1031 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
1032 const char *buf, size_t count)
1034 struct hid_device *hdev = to_hid_device(dev);
1035 struct wacom *wacom = hid_get_drvdata(hdev);
1036 unsigned int id;
1037 int err;
1039 err = kstrtouint(buf, 10, &id);
1040 if (err)
1041 return err;
1043 mutex_lock(&wacom->lock);
1045 wacom->led.groups[set_id].select = id & 0x3;
1046 err = wacom_led_control(wacom);
1048 mutex_unlock(&wacom->lock);
1050 return err < 0 ? err : count;
1053 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
1054 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
1055 struct device_attribute *attr, const char *buf, size_t count) \
1057 return wacom_led_select_store(dev, SET_ID, buf, count); \
1059 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
1060 struct device_attribute *attr, char *buf) \
1062 struct hid_device *hdev = to_hid_device(dev);\
1063 struct wacom *wacom = hid_get_drvdata(hdev); \
1064 return scnprintf(buf, PAGE_SIZE, "%d\n", \
1065 wacom->led.groups[SET_ID].select); \
1067 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
1068 wacom_led##SET_ID##_select_show, \
1069 wacom_led##SET_ID##_select_store)
1071 DEVICE_LED_SELECT_ATTR(0);
1072 DEVICE_LED_SELECT_ATTR(1);
1074 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
1075 const char *buf, size_t count)
1077 unsigned int value;
1078 int err;
1080 err = kstrtouint(buf, 10, &value);
1081 if (err)
1082 return err;
1084 mutex_lock(&wacom->lock);
1086 *dest = value & 0x7f;
1087 for (unsigned int i = 0; i < wacom->led.count; i++) {
1088 struct wacom_group_leds *group = &wacom->led.groups[i];
1090 for (unsigned int j = 0; j < group->count; j++) {
1091 if (dest == &wacom->led.llv)
1092 group->leds[j].llv = *dest;
1093 else if (dest == &wacom->led.hlv)
1094 group->leds[j].hlv = *dest;
1098 err = wacom_led_control(wacom);
1100 mutex_unlock(&wacom->lock);
1102 return err < 0 ? err : count;
1105 #define DEVICE_LUMINANCE_ATTR(name, field) \
1106 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
1107 struct device_attribute *attr, const char *buf, size_t count) \
1109 struct hid_device *hdev = to_hid_device(dev);\
1110 struct wacom *wacom = hid_get_drvdata(hdev); \
1112 return wacom_luminance_store(wacom, &wacom->led.field, \
1113 buf, count); \
1115 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
1116 struct device_attribute *attr, char *buf) \
1118 struct wacom *wacom = dev_get_drvdata(dev); \
1119 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
1121 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
1122 wacom_##name##_luminance_show, \
1123 wacom_##name##_luminance_store)
1125 DEVICE_LUMINANCE_ATTR(status0, llv);
1126 DEVICE_LUMINANCE_ATTR(status1, hlv);
1127 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
1129 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
1130 const char *buf, size_t count)
1132 struct hid_device *hdev = to_hid_device(dev);
1133 struct wacom *wacom = hid_get_drvdata(hdev);
1134 int err;
1135 unsigned len;
1136 u8 xfer_id;
1138 if (hdev->bus == BUS_BLUETOOTH) {
1139 len = 256;
1140 xfer_id = WAC_CMD_ICON_BT_XFER;
1141 } else {
1142 len = 1024;
1143 xfer_id = WAC_CMD_ICON_XFER;
1146 if (count != len)
1147 return -EINVAL;
1149 mutex_lock(&wacom->lock);
1151 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
1153 mutex_unlock(&wacom->lock);
1155 return err < 0 ? err : count;
1158 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1159 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1160 struct device_attribute *attr, const char *buf, size_t count) \
1162 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1164 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
1165 NULL, wacom_btnimg##BUTTON_ID##_store)
1167 DEVICE_BTNIMG_ATTR(0);
1168 DEVICE_BTNIMG_ATTR(1);
1169 DEVICE_BTNIMG_ATTR(2);
1170 DEVICE_BTNIMG_ATTR(3);
1171 DEVICE_BTNIMG_ATTR(4);
1172 DEVICE_BTNIMG_ATTR(5);
1173 DEVICE_BTNIMG_ATTR(6);
1174 DEVICE_BTNIMG_ATTR(7);
1176 static struct attribute *cintiq_led_attrs[] = {
1177 &dev_attr_status_led0_select.attr,
1178 &dev_attr_status_led1_select.attr,
1179 NULL
1182 static const struct attribute_group cintiq_led_attr_group = {
1183 .name = "wacom_led",
1184 .attrs = cintiq_led_attrs,
1187 static struct attribute *intuos4_led_attrs[] = {
1188 &dev_attr_status0_luminance.attr,
1189 &dev_attr_status1_luminance.attr,
1190 &dev_attr_status_led0_select.attr,
1191 &dev_attr_buttons_luminance.attr,
1192 &dev_attr_button0_rawimg.attr,
1193 &dev_attr_button1_rawimg.attr,
1194 &dev_attr_button2_rawimg.attr,
1195 &dev_attr_button3_rawimg.attr,
1196 &dev_attr_button4_rawimg.attr,
1197 &dev_attr_button5_rawimg.attr,
1198 &dev_attr_button6_rawimg.attr,
1199 &dev_attr_button7_rawimg.attr,
1200 NULL
1203 static const struct attribute_group intuos4_led_attr_group = {
1204 .name = "wacom_led",
1205 .attrs = intuos4_led_attrs,
1208 static struct attribute *intuos5_led_attrs[] = {
1209 &dev_attr_status0_luminance.attr,
1210 &dev_attr_status_led0_select.attr,
1211 NULL
1214 static const struct attribute_group intuos5_led_attr_group = {
1215 .name = "wacom_led",
1216 .attrs = intuos5_led_attrs,
1219 static struct attribute *generic_led_attrs[] = {
1220 &dev_attr_status0_luminance.attr,
1221 &dev_attr_status_led0_select.attr,
1222 NULL
1225 static const struct attribute_group generic_led_attr_group = {
1226 .name = "wacom_led",
1227 .attrs = generic_led_attrs,
1230 struct wacom_sysfs_group_devres {
1231 const struct attribute_group *group;
1232 struct kobject *root;
1235 static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1237 struct wacom_sysfs_group_devres *devres = res;
1238 struct kobject *kobj = devres->root;
1240 dev_dbg(dev, "%s: dropping reference to %s\n",
1241 __func__, devres->group->name);
1242 sysfs_remove_group(kobj, devres->group);
1245 static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1246 struct kobject *root,
1247 const struct attribute_group *group)
1249 struct wacom_sysfs_group_devres *devres;
1250 int error;
1252 devres = devres_alloc(wacom_devm_sysfs_group_release,
1253 sizeof(struct wacom_sysfs_group_devres),
1254 GFP_KERNEL);
1255 if (!devres)
1256 return -ENOMEM;
1258 devres->group = group;
1259 devres->root = root;
1261 error = sysfs_create_group(devres->root, group);
1262 if (error) {
1263 devres_free(devres);
1264 return error;
1267 devres_add(&wacom->hdev->dev, devres);
1269 return 0;
1272 static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1273 const struct attribute_group *group)
1275 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1276 group);
1279 static void wacom_devm_kfifo_release(struct device *dev, void *res)
1281 struct kfifo_rec_ptr_2 *devres = res;
1283 kfifo_free(devres);
1286 static int wacom_devm_kfifo_alloc(struct wacom *wacom)
1288 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1289 struct kfifo_rec_ptr_2 *pen_fifo;
1290 int error;
1292 pen_fifo = devres_alloc(wacom_devm_kfifo_release,
1293 sizeof(struct kfifo_rec_ptr_2),
1294 GFP_KERNEL);
1296 if (!pen_fifo)
1297 return -ENOMEM;
1299 error = kfifo_alloc(pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
1300 if (error) {
1301 devres_free(pen_fifo);
1302 return error;
1305 devres_add(&wacom->hdev->dev, pen_fifo);
1306 wacom_wac->pen_fifo = pen_fifo;
1308 return 0;
1311 enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
1313 struct wacom *wacom = led->wacom;
1315 if (wacom->led.max_hlv)
1316 return wacom_rescale(led->hlv, wacom->led.max_hlv, LED_FULL);
1318 if (wacom->led.max_llv)
1319 return wacom_rescale(led->llv, wacom->led.max_llv, LED_FULL);
1321 /* device doesn't support brightness tuning */
1322 return LED_FULL;
1325 static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1327 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1328 struct wacom *wacom = led->wacom;
1330 if (wacom->led.groups[led->group].select != led->id)
1331 return LED_OFF;
1333 return wacom_leds_brightness_get(led);
1336 static int wacom_led_brightness_set(struct led_classdev *cdev,
1337 enum led_brightness brightness)
1339 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1340 struct wacom *wacom = led->wacom;
1341 int error;
1343 mutex_lock(&wacom->lock);
1345 if (!wacom->led.groups || (brightness == LED_OFF &&
1346 wacom->led.groups[led->group].select != led->id)) {
1347 error = 0;
1348 goto out;
1351 led->llv = wacom->led.llv = wacom_rescale(brightness, LED_FULL, wacom->led.max_llv);
1352 led->hlv = wacom->led.hlv = wacom_rescale(brightness, LED_FULL, wacom->led.max_hlv);
1354 wacom->led.groups[led->group].select = led->id;
1356 error = wacom_led_control(wacom);
1358 out:
1359 mutex_unlock(&wacom->lock);
1361 return error;
1364 static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1365 enum led_brightness brightness)
1369 static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1370 struct wacom_led *led, unsigned int group,
1371 unsigned int id, bool read_only)
1373 int error;
1374 char *name;
1376 name = devm_kasprintf(dev, GFP_KERNEL,
1377 "%s::wacom-%d.%d",
1378 dev_name(dev),
1379 group,
1380 id);
1381 if (!name)
1382 return -ENOMEM;
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;
1393 if (!read_only) {
1394 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
1395 led->cdev.default_trigger = led->cdev.name;
1396 } else {
1397 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
1400 if (!read_only) {
1401 led->trigger.name = name;
1402 if (id == wacom->led.groups[group].select)
1403 led->trigger.brightness = wacom_leds_brightness_get(led);
1404 error = devm_led_trigger_register(dev, &led->trigger);
1405 if (error) {
1406 hid_err(wacom->hdev,
1407 "failed to register LED trigger %s: %d\n",
1408 led->cdev.name, error);
1409 return error;
1413 error = devm_led_classdev_register(dev, &led->cdev);
1414 if (error) {
1415 hid_err(wacom->hdev,
1416 "failed to register LED %s: %d\n",
1417 led->cdev.name, error);
1418 led->cdev.name = NULL;
1419 return error;
1422 return 0;
1425 static void wacom_led_groups_release_one(void *data)
1427 struct wacom_group_leds *group = data;
1429 devres_release_group(group->dev, group);
1432 static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1433 struct wacom *wacom,
1434 int group_id, int count,
1435 bool read_only)
1437 struct wacom_led *leds;
1438 int i, error;
1440 if (group_id >= wacom->led.count || count <= 0)
1441 return -EINVAL;
1443 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1444 return -ENOMEM;
1446 leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL);
1447 if (!leds) {
1448 error = -ENOMEM;
1449 goto err;
1452 wacom->led.groups[group_id].leds = leds;
1453 wacom->led.groups[group_id].count = count;
1455 for (i = 0; i < count; i++) {
1456 error = wacom_led_register_one(dev, wacom, &leds[i],
1457 group_id, i, read_only);
1458 if (error)
1459 goto err;
1462 wacom->led.groups[group_id].dev = dev;
1464 devres_close_group(dev, &wacom->led.groups[group_id]);
1467 * There is a bug (?) in devm_led_classdev_register() in which its
1468 * increments the refcount of the parent. If the parent is an input
1469 * device, that means the ref count never reaches 0 when
1470 * devm_input_device_release() gets called.
1471 * This means that the LEDs are still there after disconnect.
1472 * Manually force the release of the group so that the leds are released
1473 * once we are done using them.
1475 error = devm_add_action_or_reset(&wacom->hdev->dev,
1476 wacom_led_groups_release_one,
1477 &wacom->led.groups[group_id]);
1478 if (error)
1479 return error;
1481 return 0;
1483 err:
1484 devres_release_group(dev, &wacom->led.groups[group_id]);
1485 return error;
1488 struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1489 unsigned int id)
1491 struct wacom_group_leds *group;
1493 if (group_id >= wacom->led.count)
1494 return NULL;
1496 group = &wacom->led.groups[group_id];
1498 if (!group->leds)
1499 return NULL;
1501 id %= group->count;
1503 return &group->leds[id];
1507 * wacom_led_next: gives the next available led with a wacom trigger.
1509 * returns the next available struct wacom_led which has its default trigger
1510 * or the current one if none is available.
1512 struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1514 struct wacom_led *next_led;
1515 int group, next;
1517 if (!wacom || !cur)
1518 return NULL;
1520 group = cur->group;
1521 next = cur->id;
1523 do {
1524 next_led = wacom_led_find(wacom, group, ++next);
1525 if (!next_led || next_led == cur)
1526 return next_led;
1527 } while (next_led->cdev.trigger != &next_led->trigger);
1529 return next_led;
1532 static void wacom_led_groups_release(void *data)
1534 struct wacom *wacom = data;
1536 wacom->led.groups = NULL;
1537 wacom->led.count = 0;
1540 static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1542 struct device *dev = &wacom->hdev->dev;
1543 struct wacom_group_leds *groups;
1544 int error;
1546 groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds),
1547 GFP_KERNEL);
1548 if (!groups)
1549 return -ENOMEM;
1551 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1552 if (error)
1553 return error;
1555 wacom->led.groups = groups;
1556 wacom->led.count = count;
1558 return 0;
1561 static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1562 int led_per_group, bool read_only)
1564 struct device *dev;
1565 int i, error;
1567 if (!wacom->wacom_wac.pad_input)
1568 return -EINVAL;
1570 dev = &wacom->wacom_wac.pad_input->dev;
1572 error = wacom_led_groups_allocate(wacom, group_count);
1573 if (error)
1574 return error;
1576 for (i = 0; i < group_count; i++) {
1577 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1578 led_per_group,
1579 read_only);
1580 if (error)
1581 return error;
1584 return 0;
1587 int wacom_initialize_leds(struct wacom *wacom)
1589 int error;
1591 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1592 return 0;
1594 /* Initialize default values */
1595 switch (wacom->wacom_wac.features.type) {
1596 case HID_GENERIC:
1597 if (!wacom->generic_has_leds)
1598 return 0;
1599 wacom->led.llv = 100;
1600 wacom->led.max_llv = 100;
1602 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1603 if (error) {
1604 hid_err(wacom->hdev,
1605 "cannot create leds err: %d\n", error);
1606 return error;
1609 error = wacom_devm_sysfs_create_group(wacom,
1610 &generic_led_attr_group);
1611 break;
1613 case INTUOS4S:
1614 case INTUOS4:
1615 case INTUOS4WL:
1616 case INTUOS4L:
1617 wacom->led.llv = 10;
1618 wacom->led.hlv = 20;
1619 wacom->led.max_llv = 127;
1620 wacom->led.max_hlv = 127;
1621 wacom->led.img_lum = 10;
1623 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1624 if (error) {
1625 hid_err(wacom->hdev,
1626 "cannot create leds err: %d\n", error);
1627 return error;
1630 error = wacom_devm_sysfs_create_group(wacom,
1631 &intuos4_led_attr_group);
1632 break;
1634 case WACOM_24HD:
1635 case WACOM_21UX2:
1636 wacom->led.llv = 0;
1637 wacom->led.hlv = 0;
1638 wacom->led.img_lum = 0;
1640 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1641 if (error) {
1642 hid_err(wacom->hdev,
1643 "cannot create leds err: %d\n", error);
1644 return error;
1647 error = wacom_devm_sysfs_create_group(wacom,
1648 &cintiq_led_attr_group);
1649 break;
1651 case INTUOS5S:
1652 case INTUOS5:
1653 case INTUOS5L:
1654 case INTUOSPS:
1655 case INTUOSPM:
1656 case INTUOSPL:
1657 wacom->led.llv = 32;
1658 wacom->led.max_llv = 96;
1660 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1661 if (error) {
1662 hid_err(wacom->hdev,
1663 "cannot create leds err: %d\n", error);
1664 return error;
1667 error = wacom_devm_sysfs_create_group(wacom,
1668 &intuos5_led_attr_group);
1669 break;
1671 case INTUOSP2_BT:
1672 wacom->led.llv = 50;
1673 wacom->led.max_llv = 100;
1674 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1675 if (error) {
1676 hid_err(wacom->hdev,
1677 "cannot create leds err: %d\n", error);
1678 return error;
1680 return 0;
1682 case REMOTE:
1683 wacom->led.llv = 255;
1684 wacom->led.max_llv = 255;
1685 error = wacom_led_groups_allocate(wacom, 5);
1686 if (error) {
1687 hid_err(wacom->hdev,
1688 "cannot create leds err: %d\n", error);
1689 return error;
1691 return 0;
1693 default:
1694 return 0;
1697 if (error) {
1698 hid_err(wacom->hdev,
1699 "cannot create sysfs group err: %d\n", error);
1700 return error;
1703 return 0;
1706 static void wacom_init_work(struct work_struct *work)
1708 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1710 _wacom_query_tablet_data(wacom);
1711 wacom_led_control(wacom);
1714 static void wacom_query_tablet_data(struct wacom *wacom)
1716 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1719 static enum power_supply_property wacom_battery_props[] = {
1720 POWER_SUPPLY_PROP_MODEL_NAME,
1721 POWER_SUPPLY_PROP_PRESENT,
1722 POWER_SUPPLY_PROP_STATUS,
1723 POWER_SUPPLY_PROP_SCOPE,
1724 POWER_SUPPLY_PROP_CAPACITY
1727 static int wacom_battery_get_property(struct power_supply *psy,
1728 enum power_supply_property psp,
1729 union power_supply_propval *val)
1731 struct wacom_battery *battery = power_supply_get_drvdata(psy);
1732 int ret = 0;
1734 switch (psp) {
1735 case POWER_SUPPLY_PROP_MODEL_NAME:
1736 val->strval = battery->wacom->wacom_wac.name;
1737 break;
1738 case POWER_SUPPLY_PROP_PRESENT:
1739 val->intval = battery->bat_connected;
1740 break;
1741 case POWER_SUPPLY_PROP_SCOPE:
1742 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1743 break;
1744 case POWER_SUPPLY_PROP_CAPACITY:
1745 val->intval = battery->battery_capacity;
1746 break;
1747 case POWER_SUPPLY_PROP_STATUS:
1748 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1749 val->intval = battery->bat_status;
1750 else if (battery->bat_charging)
1751 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1752 else if (battery->battery_capacity == 100 &&
1753 battery->ps_connected)
1754 val->intval = POWER_SUPPLY_STATUS_FULL;
1755 else if (battery->ps_connected)
1756 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1757 else
1758 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1759 break;
1760 default:
1761 ret = -EINVAL;
1762 break;
1765 return ret;
1768 static int __wacom_initialize_battery(struct wacom *wacom,
1769 struct wacom_battery *battery)
1771 static atomic_t battery_no = ATOMIC_INIT(0);
1772 struct device *dev = &wacom->hdev->dev;
1773 struct power_supply_config psy_cfg = { .drv_data = battery, };
1774 struct power_supply *ps_bat;
1775 struct power_supply_desc *bat_desc = &battery->bat_desc;
1776 unsigned long n;
1777 int error;
1779 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1780 return -ENOMEM;
1782 battery->wacom = wacom;
1784 n = atomic_inc_return(&battery_no) - 1;
1786 bat_desc->properties = wacom_battery_props;
1787 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1788 bat_desc->get_property = wacom_battery_get_property;
1789 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1790 bat_desc->name = battery->bat_name;
1791 bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1792 bat_desc->use_for_apm = 0;
1794 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1795 if (IS_ERR(ps_bat)) {
1796 error = PTR_ERR(ps_bat);
1797 goto err;
1800 power_supply_powers(ps_bat, &wacom->hdev->dev);
1802 battery->battery = ps_bat;
1804 devres_close_group(dev, bat_desc);
1805 return 0;
1807 err:
1808 devres_release_group(dev, bat_desc);
1809 return error;
1812 static int wacom_initialize_battery(struct wacom *wacom)
1814 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1815 return __wacom_initialize_battery(wacom, &wacom->battery);
1817 return 0;
1820 static void wacom_destroy_battery(struct wacom *wacom)
1822 if (wacom->battery.battery) {
1823 devres_release_group(&wacom->hdev->dev,
1824 &wacom->battery.bat_desc);
1825 wacom->battery.battery = NULL;
1829 static void wacom_aes_battery_handler(struct work_struct *work)
1831 struct wacom *wacom = container_of(work, struct wacom, aes_battery_work.work);
1833 wacom_destroy_battery(wacom);
1836 static ssize_t wacom_show_speed(struct device *dev,
1837 struct device_attribute
1838 *attr, char *buf)
1840 struct hid_device *hdev = to_hid_device(dev);
1841 struct wacom *wacom = hid_get_drvdata(hdev);
1843 return sysfs_emit(buf, "%i\n", wacom->wacom_wac.bt_high_speed);
1846 static ssize_t wacom_store_speed(struct device *dev,
1847 struct device_attribute *attr,
1848 const char *buf, size_t count)
1850 struct hid_device *hdev = to_hid_device(dev);
1851 struct wacom *wacom = hid_get_drvdata(hdev);
1852 u8 new_speed;
1854 if (kstrtou8(buf, 0, &new_speed))
1855 return -EINVAL;
1857 if (new_speed != 0 && new_speed != 1)
1858 return -EINVAL;
1860 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1862 return count;
1865 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1866 wacom_show_speed, wacom_store_speed);
1869 static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1870 struct kobj_attribute *kattr,
1871 char *buf, int index)
1873 struct device *dev = kobj_to_dev(kobj->parent);
1874 struct hid_device *hdev = to_hid_device(dev);
1875 struct wacom *wacom = hid_get_drvdata(hdev);
1876 u8 mode;
1878 mode = wacom->led.groups[index].select;
1879 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
1882 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1883 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1884 struct kobj_attribute *kattr, char *buf) \
1886 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1888 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1889 .attr = {.name = "remote_mode", \
1890 .mode = DEV_ATTR_RO_PERM}, \
1891 .show = wacom_show_remote##SET_ID##_mode, \
1892 }; \
1893 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1894 &remote##SET_ID##_mode_attr.attr, \
1895 NULL \
1896 }; \
1897 static const struct attribute_group remote##SET_ID##_serial_group = { \
1898 .name = NULL, \
1899 .attrs = remote##SET_ID##_serial_attrs, \
1902 DEVICE_EKR_ATTR_GROUP(0);
1903 DEVICE_EKR_ATTR_GROUP(1);
1904 DEVICE_EKR_ATTR_GROUP(2);
1905 DEVICE_EKR_ATTR_GROUP(3);
1906 DEVICE_EKR_ATTR_GROUP(4);
1908 static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1909 int index)
1911 int error = 0;
1912 struct wacom_remote *remote = wacom->remote;
1914 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1915 GFP_KERNEL,
1916 "%d", serial);
1917 if (!remote->remotes[index].group.name)
1918 return -ENOMEM;
1920 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1921 &remote->remotes[index].group);
1922 if (error) {
1923 remote->remotes[index].group.name = NULL;
1924 hid_err(wacom->hdev,
1925 "cannot create sysfs group err: %d\n", error);
1926 return error;
1929 return 0;
1932 static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1934 const size_t buf_size = 2;
1935 unsigned char *buf;
1936 int retval;
1938 buf = kzalloc(buf_size, GFP_KERNEL);
1939 if (!buf)
1940 return -ENOMEM;
1942 buf[0] = WAC_CMD_DELETE_PAIRING;
1943 buf[1] = selector;
1945 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1946 buf_size, WAC_CMD_RETRIES);
1947 kfree(buf);
1949 return retval;
1952 static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1953 struct kobj_attribute *attr,
1954 const char *buf, size_t count)
1956 unsigned char selector = 0;
1957 struct device *dev = kobj_to_dev(kobj->parent);
1958 struct hid_device *hdev = to_hid_device(dev);
1959 struct wacom *wacom = hid_get_drvdata(hdev);
1960 int err;
1962 if (!strncmp(buf, "*\n", 2)) {
1963 selector = WAC_CMD_UNPAIR_ALL;
1964 } else {
1965 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1966 buf);
1967 return -1;
1970 mutex_lock(&wacom->lock);
1972 err = wacom_cmd_unpair_remote(wacom, selector);
1973 mutex_unlock(&wacom->lock);
1975 return err < 0 ? err : count;
1978 static struct kobj_attribute unpair_remote_attr = {
1979 .attr = {.name = "unpair_remote", .mode = 0200},
1980 .store = wacom_store_unpair_remote,
1983 static const struct attribute *remote_unpair_attrs[] = {
1984 &unpair_remote_attr.attr,
1985 NULL
1988 static void wacom_remotes_destroy(void *data)
1990 struct wacom *wacom = data;
1991 struct wacom_remote *remote = wacom->remote;
1993 if (!remote)
1994 return;
1996 kobject_put(remote->remote_dir);
1997 kfifo_free(&remote->remote_fifo);
1998 wacom->remote = NULL;
2001 static int wacom_initialize_remotes(struct wacom *wacom)
2003 int error = 0;
2004 struct wacom_remote *remote;
2005 int i;
2007 if (wacom->wacom_wac.features.type != REMOTE)
2008 return 0;
2010 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
2011 GFP_KERNEL);
2012 if (!remote)
2013 return -ENOMEM;
2015 wacom->remote = remote;
2017 spin_lock_init(&remote->remote_lock);
2019 error = kfifo_alloc(&remote->remote_fifo,
2020 5 * sizeof(struct wacom_remote_work_data),
2021 GFP_KERNEL);
2022 if (error) {
2023 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
2024 return -ENOMEM;
2027 remote->remotes[0].group = remote0_serial_group;
2028 remote->remotes[1].group = remote1_serial_group;
2029 remote->remotes[2].group = remote2_serial_group;
2030 remote->remotes[3].group = remote3_serial_group;
2031 remote->remotes[4].group = remote4_serial_group;
2033 remote->remote_dir = kobject_create_and_add("wacom_remote",
2034 &wacom->hdev->dev.kobj);
2035 if (!remote->remote_dir)
2036 return -ENOMEM;
2038 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
2040 if (error) {
2041 hid_err(wacom->hdev,
2042 "cannot create sysfs group err: %d\n", error);
2043 return error;
2046 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2047 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2048 remote->remotes[i].serial = 0;
2051 error = devm_add_action_or_reset(&wacom->hdev->dev,
2052 wacom_remotes_destroy, wacom);
2053 if (error)
2054 return error;
2056 return 0;
2059 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
2061 struct input_dev *input_dev;
2062 struct hid_device *hdev = wacom->hdev;
2063 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2065 input_dev = devm_input_allocate_device(&hdev->dev);
2066 if (!input_dev)
2067 return NULL;
2069 input_dev->name = wacom_wac->features.name;
2070 input_dev->phys = hdev->phys;
2071 input_dev->dev.parent = &hdev->dev;
2072 input_dev->open = wacom_open;
2073 input_dev->close = wacom_close;
2074 input_dev->uniq = hdev->uniq;
2075 input_dev->id.bustype = hdev->bus;
2076 input_dev->id.vendor = hdev->vendor;
2077 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
2078 input_dev->id.version = hdev->version;
2079 input_set_drvdata(input_dev, wacom);
2081 return input_dev;
2084 static int wacom_allocate_inputs(struct wacom *wacom)
2086 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2088 wacom_wac->pen_input = wacom_allocate_input(wacom);
2089 wacom_wac->touch_input = wacom_allocate_input(wacom);
2090 wacom_wac->pad_input = wacom_allocate_input(wacom);
2091 if (!wacom_wac->pen_input ||
2092 !wacom_wac->touch_input ||
2093 !wacom_wac->pad_input)
2094 return -ENOMEM;
2096 wacom_wac->pen_input->name = wacom_wac->pen_name;
2097 wacom_wac->touch_input->name = wacom_wac->touch_name;
2098 wacom_wac->pad_input->name = wacom_wac->pad_name;
2100 return 0;
2103 static int wacom_setup_inputs(struct wacom *wacom)
2105 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2106 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2107 int error = 0;
2109 pen_input_dev = wacom_wac->pen_input;
2110 touch_input_dev = wacom_wac->touch_input;
2111 pad_input_dev = wacom_wac->pad_input;
2113 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
2114 return -EINVAL;
2116 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
2117 if (error) {
2118 /* no pen in use on this interface */
2119 input_free_device(pen_input_dev);
2120 wacom_wac->pen_input = NULL;
2121 pen_input_dev = NULL;
2124 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
2125 if (error) {
2126 /* no touch in use on this interface */
2127 input_free_device(touch_input_dev);
2128 wacom_wac->touch_input = NULL;
2129 touch_input_dev = NULL;
2132 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2133 if (error) {
2134 /* no pad events using this interface */
2135 input_free_device(pad_input_dev);
2136 wacom_wac->pad_input = NULL;
2137 pad_input_dev = NULL;
2140 return 0;
2143 static int wacom_register_inputs(struct wacom *wacom)
2145 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2146 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2147 int error = 0;
2149 pen_input_dev = wacom_wac->pen_input;
2150 touch_input_dev = wacom_wac->touch_input;
2151 pad_input_dev = wacom_wac->pad_input;
2153 if (pen_input_dev) {
2154 error = input_register_device(pen_input_dev);
2155 if (error)
2156 goto fail;
2159 if (touch_input_dev) {
2160 error = input_register_device(touch_input_dev);
2161 if (error)
2162 goto fail;
2165 if (pad_input_dev) {
2166 error = input_register_device(pad_input_dev);
2167 if (error)
2168 goto fail;
2171 return 0;
2173 fail:
2174 wacom_wac->pad_input = NULL;
2175 wacom_wac->touch_input = NULL;
2176 wacom_wac->pen_input = NULL;
2177 return error;
2181 * Not all devices report physical dimensions from HID.
2182 * Compute the default from hardcoded logical dimension
2183 * and resolution before driver overwrites them.
2185 static void wacom_set_default_phy(struct wacom_features *features)
2187 if (features->x_resolution) {
2188 features->x_phy = (features->x_max * 100) /
2189 features->x_resolution;
2190 features->y_phy = (features->y_max * 100) /
2191 features->y_resolution;
2195 static void wacom_calculate_res(struct wacom_features *features)
2197 /* set unit to "100th of a mm" for devices not reported by HID */
2198 if (!features->unit) {
2199 features->unit = 0x11;
2200 features->unitExpo = -3;
2203 features->x_resolution = wacom_calc_hid_res(features->x_max,
2204 features->x_phy,
2205 features->unit,
2206 features->unitExpo);
2207 features->y_resolution = wacom_calc_hid_res(features->y_max,
2208 features->y_phy,
2209 features->unit,
2210 features->unitExpo);
2213 void wacom_battery_work(struct work_struct *work)
2215 struct wacom *wacom = container_of(work, struct wacom, battery_work);
2217 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2218 !wacom->battery.battery) {
2219 wacom_initialize_battery(wacom);
2221 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2222 wacom->battery.battery) {
2223 wacom_destroy_battery(wacom);
2227 static size_t wacom_compute_pktlen(struct hid_device *hdev)
2229 struct hid_report_enum *report_enum;
2230 struct hid_report *report;
2231 size_t size = 0;
2233 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2235 list_for_each_entry(report, &report_enum->report_list, list) {
2236 size_t report_size = hid_report_len(report);
2237 if (report_size > size)
2238 size = report_size;
2241 return size;
2244 static void wacom_update_name(struct wacom *wacom, const char *suffix)
2246 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2247 struct wacom_features *features = &wacom_wac->features;
2248 char name[WACOM_NAME_MAX - 20]; /* Leave some room for suffixes */
2250 /* Generic devices name unspecified */
2251 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
2252 char *product_name = wacom->hdev->name;
2254 if (hid_is_usb(wacom->hdev)) {
2255 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2256 struct usb_device *dev = interface_to_usbdev(intf);
2257 if (dev->product != NULL)
2258 product_name = dev->product;
2261 if (wacom->hdev->bus == BUS_I2C) {
2262 snprintf(name, sizeof(name), "%s %X",
2263 features->name, wacom->hdev->product);
2264 } else if (strstr(product_name, "Wacom") ||
2265 strstr(product_name, "wacom") ||
2266 strstr(product_name, "WACOM")) {
2267 if (strscpy(name, product_name, sizeof(name)) < 0) {
2268 hid_warn(wacom->hdev, "String overflow while assembling device name");
2270 } else {
2271 snprintf(name, sizeof(name), "Wacom %s", product_name);
2274 /* strip out excess whitespaces */
2275 while (1) {
2276 char *gap = strstr(name, " ");
2277 if (gap == NULL)
2278 break;
2279 /* shift everything including the terminator */
2280 memmove(gap, gap+1, strlen(gap));
2283 /* get rid of trailing whitespace */
2284 if (name[strlen(name)-1] == ' ')
2285 name[strlen(name)-1] = '\0';
2286 } else {
2287 if (strscpy(name, features->name, sizeof(name)) < 0) {
2288 hid_warn(wacom->hdev, "String overflow while assembling device name");
2292 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2293 name, suffix);
2295 /* Append the device type to the name */
2296 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
2297 "%s%s Pen", name, suffix);
2298 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
2299 "%s%s Finger", name, suffix);
2300 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
2301 "%s%s Pad", name, suffix);
2304 static void wacom_release_resources(struct wacom *wacom)
2306 struct hid_device *hdev = wacom->hdev;
2308 if (!wacom->resources)
2309 return;
2311 devres_release_group(&hdev->dev, wacom);
2313 wacom->resources = false;
2315 wacom->wacom_wac.pen_input = NULL;
2316 wacom->wacom_wac.touch_input = NULL;
2317 wacom->wacom_wac.pad_input = NULL;
2320 static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2322 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2323 wacom_wac->shared->type = wacom_wac->features.type;
2324 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2327 if (wacom_wac->has_mute_touch_switch) {
2328 wacom_wac->shared->has_mute_touch_switch = true;
2329 /* Hardware touch switch may be off. Wait until
2330 * we know the switch state to decide is_touch_on.
2331 * Softkey state should be initialized to "on" to
2332 * match historic default.
2334 if (wacom_wac->is_soft_touch_switch)
2335 wacom_wac->shared->is_touch_on = true;
2338 if (wacom_wac->shared->has_mute_touch_switch &&
2339 wacom_wac->shared->touch_input) {
2340 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2341 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2342 SW_MUTE_DEVICE);
2346 static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
2348 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2349 struct wacom_features *features = &wacom_wac->features;
2350 struct hid_device *hdev = wacom->hdev;
2351 int error;
2352 unsigned int connect_mask = HID_CONNECT_HIDRAW;
2354 features->pktlen = wacom_compute_pktlen(hdev);
2355 if (features->pktlen > WACOM_PKGLEN_MAX)
2356 return -EINVAL;
2358 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2359 return -ENOMEM;
2361 wacom->resources = true;
2363 error = wacom_allocate_inputs(wacom);
2364 if (error)
2365 goto fail;
2368 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2369 * into debug mode for the touch part.
2370 * We ignore the other interfaces.
2372 if (features->type == BAMBOO_PAD) {
2373 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2374 features->type = HID_GENERIC;
2375 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2376 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2377 error = -ENODEV;
2378 goto fail;
2382 /* set the default size in case we do not get them from hid */
2383 wacom_set_default_phy(features);
2385 /* Retrieve the physical and logical size for touch devices */
2386 wacom_retrieve_hid_descriptor(hdev, features);
2387 wacom_setup_device_quirks(wacom);
2389 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2390 features->type != WIRELESS) {
2391 error = features->type == HID_GENERIC ? -ENODEV : 0;
2393 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
2394 hdev->name,
2395 error ? "Ignoring" : "Assuming pen");
2397 if (error)
2398 goto fail;
2400 features->device_type |= WACOM_DEVICETYPE_PEN;
2403 wacom_calculate_res(features);
2405 wacom_update_name(wacom, wireless ? " (WL)" : "");
2407 /* pen only Bamboo neither support touch nor pad */
2408 if ((features->type == BAMBOO_PEN) &&
2409 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2410 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2411 error = -ENODEV;
2412 goto fail;
2415 error = wacom_add_shared_data(hdev);
2416 if (error)
2417 goto fail;
2419 error = wacom_setup_inputs(wacom);
2420 if (error)
2421 goto fail;
2423 if (features->type == HID_GENERIC)
2424 connect_mask |= HID_CONNECT_DRIVER;
2426 /* Regular HID work starts now */
2427 error = hid_hw_start(hdev, connect_mask);
2428 if (error) {
2429 hid_err(hdev, "hw start failed\n");
2430 goto fail;
2433 error = wacom_register_inputs(wacom);
2434 if (error)
2435 goto fail;
2437 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
2438 error = wacom_initialize_leds(wacom);
2439 if (error)
2440 goto fail;
2442 error = wacom_initialize_remotes(wacom);
2443 if (error)
2444 goto fail;
2447 if (!wireless) {
2448 /* Note that if query fails it is not a hard failure */
2449 wacom_query_tablet_data(wacom);
2452 /* touch only Bamboo doesn't support pen */
2453 if ((features->type == BAMBOO_TOUCH) &&
2454 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2455 cancel_delayed_work_sync(&wacom->init_work);
2456 _wacom_query_tablet_data(wacom);
2457 error = -ENODEV;
2458 goto fail_quirks;
2461 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR) {
2462 error = hid_hw_open(hdev);
2463 if (error) {
2464 hid_err(hdev, "hw open failed\n");
2465 goto fail_quirks;
2469 wacom_set_shared_values(wacom_wac);
2470 devres_close_group(&hdev->dev, wacom);
2472 return 0;
2474 fail_quirks:
2475 hid_hw_stop(hdev);
2476 fail:
2477 wacom_release_resources(wacom);
2478 return error;
2481 static void wacom_wireless_work(struct work_struct *work)
2483 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2484 struct usb_device *usbdev = wacom->usbdev;
2485 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2486 struct hid_device *hdev1, *hdev2;
2487 struct wacom *wacom1, *wacom2;
2488 struct wacom_wac *wacom_wac1, *wacom_wac2;
2489 int error;
2492 * Regardless if this is a disconnect or a new tablet,
2493 * remove any existing input and battery devices.
2496 wacom_destroy_battery(wacom);
2498 if (!usbdev)
2499 return;
2501 /* Stylus interface */
2502 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2503 wacom1 = hid_get_drvdata(hdev1);
2504 wacom_wac1 = &(wacom1->wacom_wac);
2505 wacom_release_resources(wacom1);
2507 /* Touch interface */
2508 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2509 wacom2 = hid_get_drvdata(hdev2);
2510 wacom_wac2 = &(wacom2->wacom_wac);
2511 wacom_release_resources(wacom2);
2513 if (wacom_wac->pid == 0) {
2514 hid_info(wacom->hdev, "wireless tablet disconnected\n");
2515 } else {
2516 const struct hid_device_id *id = wacom_ids;
2518 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2519 wacom_wac->pid);
2521 while (id->bus) {
2522 if (id->vendor == USB_VENDOR_ID_WACOM &&
2523 id->product == wacom_wac->pid)
2524 break;
2525 id++;
2528 if (!id->bus) {
2529 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2530 return;
2533 /* Stylus interface */
2534 wacom_wac1->features =
2535 *((struct wacom_features *)id->driver_data);
2537 wacom_wac1->pid = wacom_wac->pid;
2538 hid_hw_stop(hdev1);
2539 error = wacom_parse_and_register(wacom1, true);
2540 if (error)
2541 goto fail;
2543 /* Touch interface */
2544 if (wacom_wac1->features.touch_max ||
2545 (wacom_wac1->features.type >= INTUOSHT &&
2546 wacom_wac1->features.type <= BAMBOO_PT)) {
2547 wacom_wac2->features =
2548 *((struct wacom_features *)id->driver_data);
2549 wacom_wac2->pid = wacom_wac->pid;
2550 hid_hw_stop(hdev2);
2551 error = wacom_parse_and_register(wacom2, true);
2552 if (error)
2553 goto fail;
2556 if (strscpy(wacom_wac->name, wacom_wac1->name,
2557 sizeof(wacom_wac->name)) < 0) {
2558 hid_warn(wacom->hdev, "String overflow while assembling device name");
2562 return;
2564 fail:
2565 wacom_release_resources(wacom1);
2566 wacom_release_resources(wacom2);
2567 return;
2570 static void wacom_remote_destroy_battery(struct wacom *wacom, int index)
2572 struct wacom_remote *remote = wacom->remote;
2574 if (remote->remotes[index].battery.battery) {
2575 devres_release_group(&wacom->hdev->dev,
2576 &remote->remotes[index].battery.bat_desc);
2577 remote->remotes[index].battery.battery = NULL;
2578 remote->remotes[index].active_time = 0;
2582 static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2584 struct wacom_remote *remote = wacom->remote;
2585 u32 serial = remote->remotes[index].serial;
2586 int i;
2587 unsigned long flags;
2589 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2590 if (remote->remotes[i].serial == serial) {
2592 spin_lock_irqsave(&remote->remote_lock, flags);
2593 remote->remotes[i].registered = false;
2594 spin_unlock_irqrestore(&remote->remote_lock, flags);
2596 wacom_remote_destroy_battery(wacom, i);
2598 if (remote->remotes[i].group.name)
2599 devres_release_group(&wacom->hdev->dev,
2600 &remote->remotes[i]);
2602 remote->remotes[i].serial = 0;
2603 remote->remotes[i].group.name = NULL;
2604 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2609 static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2610 unsigned int index)
2612 struct wacom_remote *remote = wacom->remote;
2613 struct device *dev = &wacom->hdev->dev;
2614 int error, k;
2616 /* A remote can pair more than once with an EKR,
2617 * check to make sure this serial isn't already paired.
2619 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2620 if (remote->remotes[k].serial == serial)
2621 break;
2624 if (k < WACOM_MAX_REMOTES) {
2625 remote->remotes[index].serial = serial;
2626 return 0;
2629 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2630 return -ENOMEM;
2632 error = wacom_remote_create_attr_group(wacom, serial, index);
2633 if (error)
2634 goto fail;
2636 remote->remotes[index].input = wacom_allocate_input(wacom);
2637 if (!remote->remotes[index].input) {
2638 error = -ENOMEM;
2639 goto fail;
2641 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2642 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2644 if (!remote->remotes[index].input->name) {
2645 error = -EINVAL;
2646 goto fail;
2649 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2650 &wacom->wacom_wac);
2651 if (error)
2652 goto fail;
2654 remote->remotes[index].serial = serial;
2656 error = input_register_device(remote->remotes[index].input);
2657 if (error)
2658 goto fail;
2660 error = wacom_led_groups_alloc_and_register_one(
2661 &remote->remotes[index].input->dev,
2662 wacom, index, 3, true);
2663 if (error)
2664 goto fail;
2666 remote->remotes[index].registered = true;
2668 devres_close_group(dev, &remote->remotes[index]);
2669 return 0;
2671 fail:
2672 devres_release_group(dev, &remote->remotes[index]);
2673 remote->remotes[index].serial = 0;
2674 return error;
2677 static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2679 struct wacom_remote *remote = wacom->remote;
2680 int error;
2682 if (!remote->remotes[index].registered)
2683 return 0;
2685 if (remote->remotes[index].battery.battery)
2686 return 0;
2688 if (!remote->remotes[index].active_time)
2689 return 0;
2691 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2692 return 0;
2694 error = __wacom_initialize_battery(wacom,
2695 &wacom->remote->remotes[index].battery);
2696 if (error)
2697 return error;
2699 return 0;
2702 static void wacom_remote_work(struct work_struct *work)
2704 struct wacom *wacom = container_of(work, struct wacom, remote_work);
2705 struct wacom_remote *remote = wacom->remote;
2706 ktime_t kt = ktime_get();
2707 struct wacom_remote_work_data remote_work_data;
2708 unsigned long flags;
2709 unsigned int count;
2710 u32 work_serial;
2711 int i;
2713 spin_lock_irqsave(&remote->remote_lock, flags);
2715 count = kfifo_out(&remote->remote_fifo, &remote_work_data,
2716 sizeof(remote_work_data));
2718 if (count != sizeof(remote_work_data)) {
2719 hid_err(wacom->hdev,
2720 "workitem triggered without status available\n");
2721 spin_unlock_irqrestore(&remote->remote_lock, flags);
2722 return;
2725 if (!kfifo_is_empty(&remote->remote_fifo))
2726 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2728 spin_unlock_irqrestore(&remote->remote_lock, flags);
2730 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2731 work_serial = remote_work_data.remote[i].serial;
2732 if (work_serial) {
2734 if (kt - remote->remotes[i].active_time > WACOM_REMOTE_BATTERY_TIMEOUT
2735 && remote->remotes[i].active_time != 0)
2736 wacom_remote_destroy_battery(wacom, i);
2738 if (remote->remotes[i].serial == work_serial) {
2739 wacom_remote_attach_battery(wacom, i);
2740 continue;
2743 if (remote->remotes[i].serial)
2744 wacom_remote_destroy_one(wacom, i);
2746 wacom_remote_create_one(wacom, work_serial, i);
2748 } else if (remote->remotes[i].serial) {
2749 wacom_remote_destroy_one(wacom, i);
2754 static void wacom_mode_change_work(struct work_struct *work)
2756 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2757 struct wacom_shared *shared = wacom->wacom_wac.shared;
2758 struct wacom *wacom1 = NULL;
2759 struct wacom *wacom2 = NULL;
2760 bool is_direct = wacom->wacom_wac.is_direct_mode;
2761 int error = 0;
2763 if (shared->pen) {
2764 wacom1 = hid_get_drvdata(shared->pen);
2765 wacom_release_resources(wacom1);
2766 hid_hw_stop(wacom1->hdev);
2767 wacom1->wacom_wac.has_mode_change = true;
2768 wacom1->wacom_wac.is_direct_mode = is_direct;
2771 if (shared->touch) {
2772 wacom2 = hid_get_drvdata(shared->touch);
2773 wacom_release_resources(wacom2);
2774 hid_hw_stop(wacom2->hdev);
2775 wacom2->wacom_wac.has_mode_change = true;
2776 wacom2->wacom_wac.is_direct_mode = is_direct;
2779 if (wacom1) {
2780 error = wacom_parse_and_register(wacom1, false);
2781 if (error)
2782 return;
2785 if (wacom2) {
2786 error = wacom_parse_and_register(wacom2, false);
2787 if (error)
2788 return;
2791 return;
2794 static int wacom_probe(struct hid_device *hdev,
2795 const struct hid_device_id *id)
2797 struct wacom *wacom;
2798 struct wacom_wac *wacom_wac;
2799 struct wacom_features *features;
2800 int error;
2802 if (!id->driver_data)
2803 return -EINVAL;
2805 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2807 /* hid-core sets this quirk for the boot interface */
2808 hdev->quirks &= ~HID_QUIRK_NOGET;
2810 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2811 if (!wacom)
2812 return -ENOMEM;
2814 hid_set_drvdata(hdev, wacom);
2815 wacom->hdev = hdev;
2817 wacom_wac = &wacom->wacom_wac;
2818 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2819 features = &wacom_wac->features;
2821 if (features->check_for_hid_type && features->hid_type != hdev->type)
2822 return -ENODEV;
2824 error = wacom_devm_kfifo_alloc(wacom);
2825 if (error)
2826 return error;
2828 wacom_wac->hid_data.inputmode = -1;
2829 wacom_wac->mode_report = -1;
2831 if (hid_is_usb(hdev)) {
2832 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2833 struct usb_device *dev = interface_to_usbdev(intf);
2835 wacom->usbdev = dev;
2836 wacom->intf = intf;
2839 mutex_init(&wacom->lock);
2840 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
2841 INIT_DELAYED_WORK(&wacom->aes_battery_work, wacom_aes_battery_handler);
2842 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2843 INIT_WORK(&wacom->battery_work, wacom_battery_work);
2844 INIT_WORK(&wacom->remote_work, wacom_remote_work);
2845 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
2846 timer_setup(&wacom->idleprox_timer, &wacom_idleprox_timeout, TIMER_DEFERRABLE);
2848 /* ask for the report descriptor to be loaded by HID */
2849 error = hid_parse(hdev);
2850 if (error) {
2851 hid_err(hdev, "parse failed\n");
2852 return error;
2855 if (features->type == BOOTLOADER) {
2856 hid_warn(hdev, "Using device in hidraw-only mode");
2857 return hid_hw_start(hdev, HID_CONNECT_HIDRAW);
2860 error = wacom_parse_and_register(wacom, false);
2861 if (error)
2862 return error;
2864 if (hdev->bus == BUS_BLUETOOTH) {
2865 error = device_create_file(&hdev->dev, &dev_attr_speed);
2866 if (error)
2867 hid_warn(hdev,
2868 "can't create sysfs speed attribute err: %d\n",
2869 error);
2872 wacom_wac->probe_complete = true;
2873 return 0;
2876 static void wacom_remove(struct hid_device *hdev)
2878 struct wacom *wacom = hid_get_drvdata(hdev);
2879 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2880 struct wacom_features *features = &wacom_wac->features;
2882 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2883 hid_hw_close(hdev);
2885 hid_hw_stop(hdev);
2887 cancel_delayed_work_sync(&wacom->init_work);
2888 cancel_work_sync(&wacom->wireless_work);
2889 cancel_work_sync(&wacom->battery_work);
2890 cancel_work_sync(&wacom->remote_work);
2891 cancel_work_sync(&wacom->mode_change_work);
2892 del_timer_sync(&wacom->idleprox_timer);
2893 if (hdev->bus == BUS_BLUETOOTH)
2894 device_remove_file(&hdev->dev, &dev_attr_speed);
2896 /* make sure we don't trigger the LEDs */
2897 wacom_led_groups_release(wacom);
2899 if (wacom->wacom_wac.features.type != REMOTE)
2900 wacom_release_resources(wacom);
2903 #ifdef CONFIG_PM
2904 static int wacom_resume(struct hid_device *hdev)
2906 struct wacom *wacom = hid_get_drvdata(hdev);
2908 mutex_lock(&wacom->lock);
2910 /* switch to wacom mode first */
2911 _wacom_query_tablet_data(wacom);
2912 wacom_led_control(wacom);
2914 mutex_unlock(&wacom->lock);
2916 return 0;
2919 static int wacom_reset_resume(struct hid_device *hdev)
2921 return wacom_resume(hdev);
2923 #endif /* CONFIG_PM */
2925 static struct hid_driver wacom_driver = {
2926 .name = "wacom",
2927 .id_table = wacom_ids,
2928 .probe = wacom_probe,
2929 .remove = wacom_remove,
2930 .report = wacom_wac_report,
2931 #ifdef CONFIG_PM
2932 .resume = wacom_resume,
2933 .reset_resume = wacom_reset_resume,
2934 #endif
2935 .raw_event = wacom_raw_event,
2937 module_hid_driver(wacom_driver);
2939 MODULE_VERSION(DRIVER_VERSION);
2940 MODULE_AUTHOR(DRIVER_AUTHOR);
2941 MODULE_DESCRIPTION(DRIVER_DESC);
2942 MODULE_LICENSE("GPL");