1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * USB Wacom tablet support - system specific code
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)
23 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
25 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
28 hid_err(hdev, "wacom_get_report: ran out of retries "
29 "(last error = %d)\n", retval);
34 static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
35 size_t size, unsigned int retries)
40 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
42 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
45 hid_err(hdev, "wacom_set_report: ran out of retries "
46 "(last error = %d)\n", 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)
57 while (kfifo_avail(fifo) < size) {
59 hid_warn(hdev, "%s: kfifo has filled, starting to drop events\n", __func__);
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];
76 size = kfifo_out(fifo, buf, sizeof(buf));
77 err = hid_report_raw_event(hdev, HID_INPUT_REPORT, buf, size, false);
79 hid_warn(hdev, "%s: unable to flush event due to error %d\n",
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;
95 if (wacom_wac->serial[0] || !(features->quirks & WACOM_QUIRK_TOOLSERIAL))
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);
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)
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)
124 switch (equivalent_usage) {
125 case HID_DG_TOOLSERIALNUMBER:
126 wacom_wac->serial[0] = value;
129 case WACOM_HID_WD_SERIALHI:
130 wacom_wac->serial[0] |= ((__u64)value) << 32;
133 case WACOM_HID_WD_TOOLTYPE:
134 wacom_wac->id[0] = value;
145 wacom_wac_queue_flush(hdev, wacom_wac->pen_fifo);
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 (size > WACOM_PKGLEN_MAX)
161 if (wacom_wac_pen_serial_enforce(hdev, report, raw_data, size))
164 memcpy(wacom->wacom_wac.data, raw_data, size);
166 wacom_wac_irq(&wacom->wacom_wac, size);
171 static int wacom_open(struct input_dev *dev)
173 struct wacom *wacom = input_get_drvdata(dev);
175 return hid_hw_open(wacom->hdev);
178 static void wacom_close(struct input_dev *dev)
180 struct wacom *wacom = input_get_drvdata(dev);
183 * wacom->hdev should never be null, but surprisingly, I had the case
184 * once while unplugging the Wacom Wireless Receiver.
187 hid_hw_close(wacom->hdev);
191 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
193 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
194 unsigned unit, int exponent)
196 struct hid_field field = {
197 .logical_maximum = logical_extents,
198 .physical_maximum = physical_extents,
200 .unit_exponent = exponent,
203 return hidinput_calc_abs_res(&field, ABS_X);
206 static void wacom_hid_usage_quirk(struct hid_device *hdev,
207 struct hid_field *field, struct hid_usage *usage)
209 struct wacom *wacom = hid_get_drvdata(hdev);
210 struct wacom_features *features = &wacom->wacom_wac.features;
211 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
214 * The Dell Canvas 27 needs to be switched to its vendor-defined
215 * report to provide the best resolution.
217 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
218 hdev->product == 0x4200 &&
219 field->application == HID_UP_MSVENDOR) {
220 wacom->wacom_wac.mode_report = field->report->id;
221 wacom->wacom_wac.mode_value = 2;
225 * ISDv4 devices which predate HID's adoption of the
226 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
227 * position instead. We can accurately detect if a
228 * usage with that value should be HID_DG_BARRELSWITCH2
229 * based on the surrounding usages, which have remained
230 * constant across generations.
232 if (features->type == HID_GENERIC &&
233 usage->hid == 0x000D0000 &&
234 field->application == HID_DG_PEN &&
235 field->physical == HID_DG_STYLUS) {
236 int i = usage->usage_index;
238 if (i-4 >= 0 && i+1 < field->maxusage &&
239 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
240 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
241 field->usage[i-2].hid == HID_DG_ERASER &&
242 field->usage[i-1].hid == HID_DG_INVERT &&
243 field->usage[i+1].hid == HID_DG_INRANGE) {
244 usage->hid = HID_DG_BARRELSWITCH2;
249 * Wacom's AES devices use different vendor-defined usages to
250 * report serial number information compared to their branded
251 * hardware. The usages are also sometimes ill-defined and do
252 * not have the correct logical min/max values set. Lets patch
253 * the descriptor to use the branded usage convention and fix
256 if (usage->hid == WACOM_HID_WT_SERIALNUMBER &&
257 field->report_size == 16 &&
258 field->index + 2 < field->report->maxfield) {
259 struct hid_field *a = field->report->field[field->index + 1];
260 struct hid_field *b = field->report->field[field->index + 2];
262 if (a->maxusage > 0 &&
263 a->usage[0].hid == HID_DG_TOOLSERIALNUMBER &&
264 a->report_size == 32 &&
266 b->usage[0].hid == 0xFF000000 &&
267 b->report_size == 8) {
268 features->quirks |= WACOM_QUIRK_AESPEN;
269 usage->hid = WACOM_HID_WD_TOOLTYPE;
270 field->logical_minimum = S16_MIN;
271 field->logical_maximum = S16_MAX;
272 a->logical_minimum = S32_MIN;
273 a->logical_maximum = S32_MAX;
274 b->usage[0].hid = WACOM_HID_WD_SERIALHI;
275 b->logical_minimum = 0;
276 b->logical_maximum = U8_MAX;
280 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
281 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
282 hdev->product == 0x0358 &&
283 WACOM_PEN_FIELD(field) &&
284 equivalent_usage == HID_GD_Y) {
285 field->logical_maximum = 43200;
289 static void wacom_feature_mapping(struct hid_device *hdev,
290 struct hid_field *field, struct hid_usage *usage)
292 struct wacom *wacom = hid_get_drvdata(hdev);
293 struct wacom_features *features = &wacom->wacom_wac.features;
294 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
295 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
300 wacom_hid_usage_quirk(hdev, field, usage);
302 switch (equivalent_usage) {
303 case WACOM_HID_WD_TOUCH_RING_SETTING:
304 wacom->generic_has_leds = true;
306 case HID_DG_CONTACTMAX:
307 /* leave touch_max as is if predefined */
308 if (!features->touch_max) {
310 n = hid_report_len(field->report);
311 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
314 data[0] = field->report->id;
315 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
316 data, n, WAC_CMD_RETRIES);
317 if (ret == n && features->type == HID_GENERIC) {
318 ret = hid_report_raw_event(hdev,
319 HID_FEATURE_REPORT, data, n, 0);
320 } else if (ret == 2 && features->type != HID_GENERIC) {
321 features->touch_max = data[1];
323 features->touch_max = 16;
324 hid_warn(hdev, "wacom_feature_mapping: "
325 "could not get HID_DG_CONTACTMAX, "
326 "defaulting to %d\n",
327 features->touch_max);
332 case HID_DG_INPUTMODE:
333 /* Ignore if value index is out of bounds. */
334 if (usage->usage_index >= field->report_count) {
335 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
339 hid_data->inputmode = field->report->id;
340 hid_data->inputmode_index = usage->usage_index;
343 case HID_UP_DIGITIZER:
344 if (field->report->id == 0x0B &&
345 (field->application == WACOM_HID_G9_PEN ||
346 field->application == WACOM_HID_G11_PEN)) {
347 wacom->wacom_wac.mode_report = field->report->id;
348 wacom->wacom_wac.mode_value = 0;
352 case WACOM_HID_WD_DATAMODE:
353 wacom->wacom_wac.mode_report = field->report->id;
354 wacom->wacom_wac.mode_value = 2;
357 case WACOM_HID_UP_G9:
358 case WACOM_HID_UP_G11:
359 if (field->report->id == 0x03 &&
360 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
361 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
362 wacom->wacom_wac.mode_report = field->report->id;
363 wacom->wacom_wac.mode_value = 0;
366 case WACOM_HID_WD_OFFSETLEFT:
367 case WACOM_HID_WD_OFFSETTOP:
368 case WACOM_HID_WD_OFFSETRIGHT:
369 case WACOM_HID_WD_OFFSETBOTTOM:
371 n = hid_report_len(field->report);
372 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
375 data[0] = field->report->id;
376 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
377 data, n, WAC_CMD_RETRIES);
379 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
382 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
391 * Interface Descriptor of wacom devices can be incomplete and
392 * inconsistent so wacom_features table is used to store stylus
393 * device's packet lengths, various maximum values, and tablet
394 * resolution based on product ID's.
396 * For devices that contain 2 interfaces, wacom_features table is
397 * inaccurate for the touch interface. Since the Interface Descriptor
398 * for touch interfaces has pretty complete data, this function exists
399 * to query tablet for this missing information instead of hard coding in
400 * an additional table.
402 * A typical Interface Descriptor for a stylus will contain a
403 * boot mouse application collection that is not of interest and this
404 * function will ignore it.
406 * It also contains a digitizer application collection that also is not
407 * of interest since any information it contains would be duplicate
408 * of what is in wacom_features. Usually it defines a report of an array
409 * of bytes that could be used as max length of the stylus packet returned.
410 * If it happens to define a Digitizer-Stylus Physical Collection then
411 * the X and Y logical values contain valid data but it is ignored.
413 * A typical Interface Descriptor for a touch interface will contain a
414 * Digitizer-Finger Physical Collection which will define both logical
415 * X/Y maximum as well as the physical size of tablet. Since touch
416 * interfaces haven't supported pressure or distance, this is enough
417 * information to override invalid values in the wacom_features table.
419 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
420 * data. We deal with them after returning from this function.
422 static void wacom_usage_mapping(struct hid_device *hdev,
423 struct hid_field *field, struct hid_usage *usage)
425 struct wacom *wacom = hid_get_drvdata(hdev);
426 struct wacom_features *features = &wacom->wacom_wac.features;
427 bool finger = WACOM_FINGER_FIELD(field);
428 bool pen = WACOM_PEN_FIELD(field);
429 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
432 * Requiring Stylus Usage will ignore boot mouse
433 * X/Y values and some cases of invalid Digitizer X/Y
434 * values commonly reported.
437 features->device_type |= WACOM_DEVICETYPE_PEN;
439 features->device_type |= WACOM_DEVICETYPE_TOUCH;
443 wacom_hid_usage_quirk(hdev, field, usage);
445 switch (equivalent_usage) {
447 features->x_max = field->logical_maximum;
449 features->x_phy = field->physical_maximum;
450 if ((features->type != BAMBOO_PT) &&
451 (features->type != BAMBOO_TOUCH)) {
452 features->unit = field->unit;
453 features->unitExpo = field->unit_exponent;
458 features->y_max = field->logical_maximum;
460 features->y_phy = field->physical_maximum;
461 if ((features->type != BAMBOO_PT) &&
462 (features->type != BAMBOO_TOUCH)) {
463 features->unit = field->unit;
464 features->unitExpo = field->unit_exponent;
468 case HID_DG_TIPPRESSURE:
470 features->pressure_max = field->logical_maximum;
474 if (features->type == HID_GENERIC)
475 wacom_wac_usage_mapping(hdev, field, usage);
478 static void wacom_post_parse_hid(struct hid_device *hdev,
479 struct wacom_features *features)
481 struct wacom *wacom = hid_get_drvdata(hdev);
482 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
484 if (features->type == HID_GENERIC) {
485 /* Any last-minute generic device setup */
486 if (wacom_wac->has_mode_change) {
487 if (wacom_wac->is_direct_mode)
488 features->device_type |= WACOM_DEVICETYPE_DIRECT;
490 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
493 if (features->touch_max > 1) {
494 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
495 input_mt_init_slots(wacom_wac->touch_input,
496 wacom_wac->features.touch_max,
499 input_mt_init_slots(wacom_wac->touch_input,
500 wacom_wac->features.touch_max,
506 static void wacom_parse_hid(struct hid_device *hdev,
507 struct wacom_features *features)
509 struct hid_report_enum *rep_enum;
510 struct hid_report *hreport;
513 /* check features first */
514 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
515 list_for_each_entry(hreport, &rep_enum->report_list, list) {
516 for (i = 0; i < hreport->maxfield; i++) {
517 /* Ignore if report count is out of bounds. */
518 if (hreport->field[i]->report_count < 1)
521 for (j = 0; j < hreport->field[i]->maxusage; j++) {
522 wacom_feature_mapping(hdev, hreport->field[i],
523 hreport->field[i]->usage + j);
528 /* now check the input usages */
529 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
530 list_for_each_entry(hreport, &rep_enum->report_list, list) {
532 if (!hreport->maxfield)
535 for (i = 0; i < hreport->maxfield; i++)
536 for (j = 0; j < hreport->field[i]->maxusage; j++)
537 wacom_usage_mapping(hdev, hreport->field[i],
538 hreport->field[i]->usage + j);
541 wacom_post_parse_hid(hdev, features);
544 static int wacom_hid_set_device_mode(struct hid_device *hdev)
546 struct wacom *wacom = hid_get_drvdata(hdev);
547 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
548 struct hid_report *r;
549 struct hid_report_enum *re;
551 if (hid_data->inputmode < 0)
554 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
555 r = re->report_id_hash[hid_data->inputmode];
557 r->field[0]->value[hid_data->inputmode_index] = 2;
558 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
563 static int wacom_set_device_mode(struct hid_device *hdev,
564 struct wacom_wac *wacom_wac)
567 struct hid_report *r;
568 struct hid_report_enum *re;
570 int error = -ENOMEM, limit = 0;
572 if (wacom_wac->mode_report < 0)
575 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
576 r = re->report_id_hash[wacom_wac->mode_report];
580 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
584 length = hid_report_len(r);
587 rep_data[0] = wacom_wac->mode_report;
588 rep_data[1] = wacom_wac->mode_value;
590 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
593 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
594 rep_data, length, 1);
595 } while (error >= 0 &&
596 rep_data[1] != wacom_wac->mode_report &&
597 limit++ < WAC_MSG_RETRIES);
601 return error < 0 ? error : 0;
604 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
605 struct wacom_features *features)
607 struct wacom *wacom = hid_get_drvdata(hdev);
611 switch (features->type) {
615 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
619 rep_data[0] = speed == 0 ? 0x05 : 0x06;
622 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
626 wacom->wacom_wac.bt_high_speed = speed;
632 * Note that if the raw queries fail, it's not a hard failure
633 * and it is safe to continue
635 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
640 wacom->wacom_wac.bt_features &= ~0x20;
642 wacom->wacom_wac.bt_features |= 0x20;
645 rep_data[1] = wacom->wacom_wac.bt_features;
647 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
650 wacom->wacom_wac.bt_high_speed = speed;
658 * Switch the tablet into its most-capable mode. Wacom tablets are
659 * typically configured to power-up in a mode which sends mouse-like
660 * reports to the OS. To get absolute position, pressure data, etc.
661 * from the tablet, it is necessary to switch the tablet out of this
662 * mode and into one which sends the full range of tablet data.
664 static int _wacom_query_tablet_data(struct wacom *wacom)
666 struct hid_device *hdev = wacom->hdev;
667 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
668 struct wacom_features *features = &wacom_wac->features;
670 if (hdev->bus == BUS_BLUETOOTH)
671 return wacom_bt_query_tablet_data(hdev, 1, features);
673 if (features->type != HID_GENERIC) {
674 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
675 if (features->type > TABLETPC) {
676 /* MT Tablet PC touch */
677 wacom_wac->mode_report = 3;
678 wacom_wac->mode_value = 4;
679 } else if (features->type == WACOM_24HDT) {
680 wacom_wac->mode_report = 18;
681 wacom_wac->mode_value = 2;
682 } else if (features->type == WACOM_27QHDT) {
683 wacom_wac->mode_report = 131;
684 wacom_wac->mode_value = 2;
685 } else if (features->type == BAMBOO_PAD) {
686 wacom_wac->mode_report = 2;
687 wacom_wac->mode_value = 2;
689 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
690 if (features->type <= BAMBOO_PT) {
691 wacom_wac->mode_report = 2;
692 wacom_wac->mode_value = 2;
697 wacom_set_device_mode(hdev, wacom_wac);
699 if (features->type == HID_GENERIC)
700 return wacom_hid_set_device_mode(hdev);
705 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
706 struct wacom_features *features)
708 struct wacom *wacom = hid_get_drvdata(hdev);
709 struct usb_interface *intf = wacom->intf;
711 /* default features */
712 features->x_fuzz = 4;
713 features->y_fuzz = 4;
714 features->pressure_fuzz = 0;
715 features->distance_fuzz = 1;
716 features->tilt_fuzz = 1;
719 * The wireless device HID is basic and layout conflicts with
720 * other tablets (monitor and touch interface can look like pen).
721 * Skip the query for this type and modify defaults based on
724 if (features->type == WIRELESS && intf) {
725 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
726 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
728 features->device_type = WACOM_DEVICETYPE_NONE;
732 wacom_parse_hid(hdev, features);
735 struct wacom_hdev_data {
736 struct list_head list;
738 struct hid_device *dev;
739 struct wacom_shared shared;
742 static LIST_HEAD(wacom_udev_list);
743 static DEFINE_MUTEX(wacom_udev_list_lock);
745 static bool wacom_are_sibling(struct hid_device *hdev,
746 struct hid_device *sibling)
748 struct wacom *wacom = hid_get_drvdata(hdev);
749 struct wacom_features *features = &wacom->wacom_wac.features;
750 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
751 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
752 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
753 __u32 oPid = features->oPid ? features->oPid : hdev->product;
755 /* The defined oVid/oPid must match that of the sibling */
756 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
758 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
762 * Devices with the same VID/PID must share the same physical
763 * device path, while those with different VID/PID must share
764 * the same physical parent device path.
766 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
767 if (!hid_compare_device_paths(hdev, sibling, '/'))
770 if (!hid_compare_device_paths(hdev, sibling, '.'))
774 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
775 if (features->type != HID_GENERIC)
779 * Direct-input devices may not be siblings of indirect-input
782 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
783 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
787 * Indirect-input devices may not be siblings of direct-input
790 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
791 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
794 /* Pen devices may only be siblings of touch devices */
795 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
796 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
799 /* Touch devices may only be siblings of pen devices */
800 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
801 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
805 * No reason could be found for these two devices to NOT be
806 * siblings, so there's a good chance they ARE siblings
811 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
813 struct wacom_hdev_data *data;
815 /* Try to find an already-probed interface from the same device */
816 list_for_each_entry(data, &wacom_udev_list, list) {
817 if (hid_compare_device_paths(hdev, data->dev, '/')) {
818 kref_get(&data->kref);
823 /* Fallback to finding devices that appear to be "siblings" */
824 list_for_each_entry(data, &wacom_udev_list, list) {
825 if (wacom_are_sibling(hdev, data->dev)) {
826 kref_get(&data->kref);
834 static void wacom_release_shared_data(struct kref *kref)
836 struct wacom_hdev_data *data =
837 container_of(kref, struct wacom_hdev_data, kref);
839 mutex_lock(&wacom_udev_list_lock);
840 list_del(&data->list);
841 mutex_unlock(&wacom_udev_list_lock);
846 static void wacom_remove_shared_data(void *res)
848 struct wacom *wacom = res;
849 struct wacom_hdev_data *data;
850 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
852 if (wacom_wac->shared) {
853 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
856 if (wacom_wac->shared->touch == wacom->hdev)
857 wacom_wac->shared->touch = NULL;
858 else if (wacom_wac->shared->pen == wacom->hdev)
859 wacom_wac->shared->pen = NULL;
861 kref_put(&data->kref, wacom_release_shared_data);
862 wacom_wac->shared = NULL;
866 static int wacom_add_shared_data(struct hid_device *hdev)
868 struct wacom *wacom = hid_get_drvdata(hdev);
869 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
870 struct wacom_hdev_data *data;
873 mutex_lock(&wacom_udev_list_lock);
875 data = wacom_get_hdev_data(hdev);
877 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
879 mutex_unlock(&wacom_udev_list_lock);
883 kref_init(&data->kref);
885 list_add_tail(&data->list, &wacom_udev_list);
888 mutex_unlock(&wacom_udev_list_lock);
890 wacom_wac->shared = &data->shared;
892 retval = devm_add_action_or_reset(&hdev->dev, wacom_remove_shared_data, wacom);
896 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
897 wacom_wac->shared->touch = hdev;
898 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
899 wacom_wac->shared->pen = hdev;
904 static int wacom_led_control(struct wacom *wacom)
908 unsigned char report_id = WAC_CMD_LED_CONTROL;
911 if (!wacom->led.groups)
914 if (wacom->wacom_wac.features.type == REMOTE)
917 if (wacom->wacom_wac.pid) { /* wireless connected */
918 report_id = WAC_CMD_WL_LED_CONTROL;
921 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
922 report_id = WAC_CMD_WL_INTUOSP2;
925 buf = kzalloc(buf_size, GFP_KERNEL);
929 if (wacom->wacom_wac.features.type == HID_GENERIC) {
930 buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
931 buf[1] = wacom->led.llv;
932 buf[2] = wacom->led.groups[0].select & 0x03;
934 } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
935 wacom->wacom_wac.features.type <= INTUOSPL)) {
937 * Touch Ring and crop mark LED luminance may take on
938 * one of four values:
939 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
941 int ring_led = wacom->led.groups[0].select & 0x03;
942 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
944 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
947 if (wacom->wacom_wac.pid) {
948 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
949 buf, buf_size, WAC_CMD_RETRIES);
955 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
957 buf[4] = 100; // Power Connection LED (ORANGE)
958 buf[5] = 100; // BT Connection LED (BLUE)
959 buf[6] = 100; // Paper Mode (RED?)
960 buf[7] = 100; // Paper Mode (GREEN?)
961 buf[8] = 100; // Paper Mode (BLUE?)
962 buf[9] = wacom->led.llv;
963 buf[10] = wacom->led.groups[0].select & 0x03;
966 int led = wacom->led.groups[0].select | 0x4;
968 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
969 wacom->wacom_wac.features.type == WACOM_24HD)
970 led |= (wacom->led.groups[1].select << 4) | 0x40;
974 buf[2] = wacom->led.llv;
975 buf[3] = wacom->led.hlv;
976 buf[4] = wacom->led.img_lum;
979 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
986 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
987 const unsigned len, const void *img)
991 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
993 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
997 /* Send 'start' command */
998 buf[0] = WAC_CMD_ICON_START;
1000 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1006 buf[1] = button_id & 0x07;
1007 for (i = 0; i < 4; i++) {
1009 memcpy(buf + 3, img + i * chunk_len, chunk_len);
1011 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
1012 buf, chunk_len + 3, WAC_CMD_RETRIES);
1018 buf[0] = WAC_CMD_ICON_START;
1020 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1028 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
1029 const char *buf, size_t count)
1031 struct hid_device *hdev = to_hid_device(dev);
1032 struct wacom *wacom = hid_get_drvdata(hdev);
1036 err = kstrtouint(buf, 10, &id);
1040 mutex_lock(&wacom->lock);
1042 wacom->led.groups[set_id].select = id & 0x3;
1043 err = wacom_led_control(wacom);
1045 mutex_unlock(&wacom->lock);
1047 return err < 0 ? err : count;
1050 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
1051 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
1052 struct device_attribute *attr, const char *buf, size_t count) \
1054 return wacom_led_select_store(dev, SET_ID, buf, count); \
1056 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
1057 struct device_attribute *attr, char *buf) \
1059 struct hid_device *hdev = to_hid_device(dev);\
1060 struct wacom *wacom = hid_get_drvdata(hdev); \
1061 return scnprintf(buf, PAGE_SIZE, "%d\n", \
1062 wacom->led.groups[SET_ID].select); \
1064 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
1065 wacom_led##SET_ID##_select_show, \
1066 wacom_led##SET_ID##_select_store)
1068 DEVICE_LED_SELECT_ATTR(0);
1069 DEVICE_LED_SELECT_ATTR(1);
1071 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
1072 const char *buf, size_t count)
1077 err = kstrtouint(buf, 10, &value);
1081 mutex_lock(&wacom->lock);
1083 *dest = value & 0x7f;
1084 err = wacom_led_control(wacom);
1086 mutex_unlock(&wacom->lock);
1088 return err < 0 ? err : count;
1091 #define DEVICE_LUMINANCE_ATTR(name, field) \
1092 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
1093 struct device_attribute *attr, const char *buf, size_t count) \
1095 struct hid_device *hdev = to_hid_device(dev);\
1096 struct wacom *wacom = hid_get_drvdata(hdev); \
1098 return wacom_luminance_store(wacom, &wacom->led.field, \
1101 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
1102 struct device_attribute *attr, char *buf) \
1104 struct wacom *wacom = dev_get_drvdata(dev); \
1105 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
1107 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
1108 wacom_##name##_luminance_show, \
1109 wacom_##name##_luminance_store)
1111 DEVICE_LUMINANCE_ATTR(status0, llv);
1112 DEVICE_LUMINANCE_ATTR(status1, hlv);
1113 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
1115 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
1116 const char *buf, size_t count)
1118 struct hid_device *hdev = to_hid_device(dev);
1119 struct wacom *wacom = hid_get_drvdata(hdev);
1124 if (hdev->bus == BUS_BLUETOOTH) {
1126 xfer_id = WAC_CMD_ICON_BT_XFER;
1129 xfer_id = WAC_CMD_ICON_XFER;
1135 mutex_lock(&wacom->lock);
1137 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
1139 mutex_unlock(&wacom->lock);
1141 return err < 0 ? err : count;
1144 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1145 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1146 struct device_attribute *attr, const char *buf, size_t count) \
1148 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1150 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
1151 NULL, wacom_btnimg##BUTTON_ID##_store)
1153 DEVICE_BTNIMG_ATTR(0);
1154 DEVICE_BTNIMG_ATTR(1);
1155 DEVICE_BTNIMG_ATTR(2);
1156 DEVICE_BTNIMG_ATTR(3);
1157 DEVICE_BTNIMG_ATTR(4);
1158 DEVICE_BTNIMG_ATTR(5);
1159 DEVICE_BTNIMG_ATTR(6);
1160 DEVICE_BTNIMG_ATTR(7);
1162 static struct attribute *cintiq_led_attrs[] = {
1163 &dev_attr_status_led0_select.attr,
1164 &dev_attr_status_led1_select.attr,
1168 static const struct attribute_group cintiq_led_attr_group = {
1169 .name = "wacom_led",
1170 .attrs = cintiq_led_attrs,
1173 static struct attribute *intuos4_led_attrs[] = {
1174 &dev_attr_status0_luminance.attr,
1175 &dev_attr_status1_luminance.attr,
1176 &dev_attr_status_led0_select.attr,
1177 &dev_attr_buttons_luminance.attr,
1178 &dev_attr_button0_rawimg.attr,
1179 &dev_attr_button1_rawimg.attr,
1180 &dev_attr_button2_rawimg.attr,
1181 &dev_attr_button3_rawimg.attr,
1182 &dev_attr_button4_rawimg.attr,
1183 &dev_attr_button5_rawimg.attr,
1184 &dev_attr_button6_rawimg.attr,
1185 &dev_attr_button7_rawimg.attr,
1189 static const struct attribute_group intuos4_led_attr_group = {
1190 .name = "wacom_led",
1191 .attrs = intuos4_led_attrs,
1194 static struct attribute *intuos5_led_attrs[] = {
1195 &dev_attr_status0_luminance.attr,
1196 &dev_attr_status_led0_select.attr,
1200 static const struct attribute_group intuos5_led_attr_group = {
1201 .name = "wacom_led",
1202 .attrs = intuos5_led_attrs,
1205 static struct attribute *generic_led_attrs[] = {
1206 &dev_attr_status0_luminance.attr,
1207 &dev_attr_status_led0_select.attr,
1211 static const struct attribute_group generic_led_attr_group = {
1212 .name = "wacom_led",
1213 .attrs = generic_led_attrs,
1216 struct wacom_sysfs_group_devres {
1217 const struct attribute_group *group;
1218 struct kobject *root;
1221 static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1223 struct wacom_sysfs_group_devres *devres = res;
1224 struct kobject *kobj = devres->root;
1226 dev_dbg(dev, "%s: dropping reference to %s\n",
1227 __func__, devres->group->name);
1228 sysfs_remove_group(kobj, devres->group);
1231 static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1232 struct kobject *root,
1233 const struct attribute_group *group)
1235 struct wacom_sysfs_group_devres *devres;
1238 devres = devres_alloc(wacom_devm_sysfs_group_release,
1239 sizeof(struct wacom_sysfs_group_devres),
1244 devres->group = group;
1245 devres->root = root;
1247 error = sysfs_create_group(devres->root, group);
1249 devres_free(devres);
1253 devres_add(&wacom->hdev->dev, devres);
1258 static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1259 const struct attribute_group *group)
1261 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1265 static void wacom_devm_kfifo_release(struct device *dev, void *res)
1267 struct kfifo_rec_ptr_2 *devres = res;
1272 static int wacom_devm_kfifo_alloc(struct wacom *wacom)
1274 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1275 struct kfifo_rec_ptr_2 *pen_fifo;
1278 pen_fifo = devres_alloc(wacom_devm_kfifo_release,
1279 sizeof(struct kfifo_rec_ptr_2),
1285 error = kfifo_alloc(pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
1287 devres_free(pen_fifo);
1291 devres_add(&wacom->hdev->dev, pen_fifo);
1292 wacom_wac->pen_fifo = pen_fifo;
1297 enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
1299 struct wacom *wacom = led->wacom;
1301 if (wacom->led.max_hlv)
1302 return led->hlv * LED_FULL / wacom->led.max_hlv;
1304 if (wacom->led.max_llv)
1305 return led->llv * LED_FULL / wacom->led.max_llv;
1307 /* device doesn't support brightness tuning */
1311 static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1313 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1314 struct wacom *wacom = led->wacom;
1316 if (wacom->led.groups[led->group].select != led->id)
1319 return wacom_leds_brightness_get(led);
1322 static int wacom_led_brightness_set(struct led_classdev *cdev,
1323 enum led_brightness brightness)
1325 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1326 struct wacom *wacom = led->wacom;
1329 mutex_lock(&wacom->lock);
1331 if (!wacom->led.groups || (brightness == LED_OFF &&
1332 wacom->led.groups[led->group].select != led->id)) {
1337 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1338 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1340 wacom->led.groups[led->group].select = led->id;
1342 error = wacom_led_control(wacom);
1345 mutex_unlock(&wacom->lock);
1350 static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1351 enum led_brightness brightness)
1355 static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1356 struct wacom_led *led, unsigned int group,
1357 unsigned int id, bool read_only)
1362 name = devm_kasprintf(dev, GFP_KERNEL,
1371 led->trigger.name = name;
1372 error = devm_led_trigger_register(dev, &led->trigger);
1374 hid_err(wacom->hdev,
1375 "failed to register LED trigger %s: %d\n",
1376 led->cdev.name, error);
1384 led->llv = wacom->led.llv;
1385 led->hlv = wacom->led.hlv;
1386 led->cdev.name = name;
1387 led->cdev.max_brightness = LED_FULL;
1388 led->cdev.flags = LED_HW_PLUGGABLE;
1389 led->cdev.brightness_get = __wacom_led_brightness_get;
1391 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
1392 led->cdev.default_trigger = led->cdev.name;
1394 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
1397 error = devm_led_classdev_register(dev, &led->cdev);
1399 hid_err(wacom->hdev,
1400 "failed to register LED %s: %d\n",
1401 led->cdev.name, error);
1402 led->cdev.name = NULL;
1409 static void wacom_led_groups_release_one(void *data)
1411 struct wacom_group_leds *group = data;
1413 devres_release_group(group->dev, group);
1416 static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1417 struct wacom *wacom,
1418 int group_id, int count,
1421 struct wacom_led *leds;
1424 if (group_id >= wacom->led.count || count <= 0)
1427 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1430 leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL);
1436 wacom->led.groups[group_id].leds = leds;
1437 wacom->led.groups[group_id].count = count;
1439 for (i = 0; i < count; i++) {
1440 error = wacom_led_register_one(dev, wacom, &leds[i],
1441 group_id, i, read_only);
1446 wacom->led.groups[group_id].dev = dev;
1448 devres_close_group(dev, &wacom->led.groups[group_id]);
1451 * There is a bug (?) in devm_led_classdev_register() in which its
1452 * increments the refcount of the parent. If the parent is an input
1453 * device, that means the ref count never reaches 0 when
1454 * devm_input_device_release() gets called.
1455 * This means that the LEDs are still there after disconnect.
1456 * Manually force the release of the group so that the leds are released
1457 * once we are done using them.
1459 error = devm_add_action_or_reset(&wacom->hdev->dev,
1460 wacom_led_groups_release_one,
1461 &wacom->led.groups[group_id]);
1468 devres_release_group(dev, &wacom->led.groups[group_id]);
1472 struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1475 struct wacom_group_leds *group;
1477 if (group_id >= wacom->led.count)
1480 group = &wacom->led.groups[group_id];
1487 return &group->leds[id];
1491 * wacom_led_next: gives the next available led with a wacom trigger.
1493 * returns the next available struct wacom_led which has its default trigger
1494 * or the current one if none is available.
1496 struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1498 struct wacom_led *next_led;
1508 next_led = wacom_led_find(wacom, group, ++next);
1509 if (!next_led || next_led == cur)
1511 } while (next_led->cdev.trigger != &next_led->trigger);
1516 static void wacom_led_groups_release(void *data)
1518 struct wacom *wacom = data;
1520 wacom->led.groups = NULL;
1521 wacom->led.count = 0;
1524 static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1526 struct device *dev = &wacom->hdev->dev;
1527 struct wacom_group_leds *groups;
1530 groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds),
1535 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1539 wacom->led.groups = groups;
1540 wacom->led.count = count;
1545 static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1546 int led_per_group, bool read_only)
1551 if (!wacom->wacom_wac.pad_input)
1554 dev = &wacom->wacom_wac.pad_input->dev;
1556 error = wacom_led_groups_allocate(wacom, group_count);
1560 for (i = 0; i < group_count; i++) {
1561 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1571 int wacom_initialize_leds(struct wacom *wacom)
1575 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1578 /* Initialize default values */
1579 switch (wacom->wacom_wac.features.type) {
1581 if (!wacom->generic_has_leds)
1583 wacom->led.llv = 100;
1584 wacom->led.max_llv = 100;
1586 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1588 hid_err(wacom->hdev,
1589 "cannot create leds err: %d\n", error);
1593 error = wacom_devm_sysfs_create_group(wacom,
1594 &generic_led_attr_group);
1601 wacom->led.llv = 10;
1602 wacom->led.hlv = 20;
1603 wacom->led.max_llv = 127;
1604 wacom->led.max_hlv = 127;
1605 wacom->led.img_lum = 10;
1607 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1609 hid_err(wacom->hdev,
1610 "cannot create leds err: %d\n", error);
1614 error = wacom_devm_sysfs_create_group(wacom,
1615 &intuos4_led_attr_group);
1622 wacom->led.img_lum = 0;
1624 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1626 hid_err(wacom->hdev,
1627 "cannot create leds err: %d\n", error);
1631 error = wacom_devm_sysfs_create_group(wacom,
1632 &cintiq_led_attr_group);
1641 wacom->led.llv = 32;
1642 wacom->led.max_llv = 96;
1644 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1646 hid_err(wacom->hdev,
1647 "cannot create leds err: %d\n", error);
1651 error = wacom_devm_sysfs_create_group(wacom,
1652 &intuos5_led_attr_group);
1656 wacom->led.llv = 50;
1657 wacom->led.max_llv = 100;
1658 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1660 hid_err(wacom->hdev,
1661 "cannot create leds err: %d\n", error);
1667 wacom->led.llv = 255;
1668 wacom->led.max_llv = 255;
1669 error = wacom_led_groups_allocate(wacom, 5);
1671 hid_err(wacom->hdev,
1672 "cannot create leds err: %d\n", error);
1682 hid_err(wacom->hdev,
1683 "cannot create sysfs group err: %d\n", error);
1690 static void wacom_init_work(struct work_struct *work)
1692 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1694 _wacom_query_tablet_data(wacom);
1695 wacom_led_control(wacom);
1698 static void wacom_query_tablet_data(struct wacom *wacom)
1700 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1703 static enum power_supply_property wacom_battery_props[] = {
1704 POWER_SUPPLY_PROP_MODEL_NAME,
1705 POWER_SUPPLY_PROP_PRESENT,
1706 POWER_SUPPLY_PROP_STATUS,
1707 POWER_SUPPLY_PROP_SCOPE,
1708 POWER_SUPPLY_PROP_CAPACITY
1711 static int wacom_battery_get_property(struct power_supply *psy,
1712 enum power_supply_property psp,
1713 union power_supply_propval *val)
1715 struct wacom_battery *battery = power_supply_get_drvdata(psy);
1719 case POWER_SUPPLY_PROP_MODEL_NAME:
1720 val->strval = battery->wacom->wacom_wac.name;
1722 case POWER_SUPPLY_PROP_PRESENT:
1723 val->intval = battery->bat_connected;
1725 case POWER_SUPPLY_PROP_SCOPE:
1726 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1728 case POWER_SUPPLY_PROP_CAPACITY:
1729 val->intval = battery->battery_capacity;
1731 case POWER_SUPPLY_PROP_STATUS:
1732 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1733 val->intval = battery->bat_status;
1734 else if (battery->bat_charging)
1735 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1736 else if (battery->battery_capacity == 100 &&
1737 battery->ps_connected)
1738 val->intval = POWER_SUPPLY_STATUS_FULL;
1739 else if (battery->ps_connected)
1740 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1742 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1752 static int __wacom_initialize_battery(struct wacom *wacom,
1753 struct wacom_battery *battery)
1755 static atomic_t battery_no = ATOMIC_INIT(0);
1756 struct device *dev = &wacom->hdev->dev;
1757 struct power_supply_config psy_cfg = { .drv_data = battery, };
1758 struct power_supply *ps_bat;
1759 struct power_supply_desc *bat_desc = &battery->bat_desc;
1763 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1766 battery->wacom = wacom;
1768 n = atomic_inc_return(&battery_no) - 1;
1770 bat_desc->properties = wacom_battery_props;
1771 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1772 bat_desc->get_property = wacom_battery_get_property;
1773 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1774 bat_desc->name = battery->bat_name;
1775 bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1776 bat_desc->use_for_apm = 0;
1778 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1779 if (IS_ERR(ps_bat)) {
1780 error = PTR_ERR(ps_bat);
1784 power_supply_powers(ps_bat, &wacom->hdev->dev);
1786 battery->battery = ps_bat;
1788 devres_close_group(dev, bat_desc);
1792 devres_release_group(dev, bat_desc);
1796 static int wacom_initialize_battery(struct wacom *wacom)
1798 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1799 return __wacom_initialize_battery(wacom, &wacom->battery);
1804 static void wacom_destroy_battery(struct wacom *wacom)
1806 if (wacom->battery.battery) {
1807 devres_release_group(&wacom->hdev->dev,
1808 &wacom->battery.bat_desc);
1809 wacom->battery.battery = NULL;
1813 static ssize_t wacom_show_speed(struct device *dev,
1814 struct device_attribute
1817 struct hid_device *hdev = to_hid_device(dev);
1818 struct wacom *wacom = hid_get_drvdata(hdev);
1820 return sysfs_emit(buf, "%i\n", wacom->wacom_wac.bt_high_speed);
1823 static ssize_t wacom_store_speed(struct device *dev,
1824 struct device_attribute *attr,
1825 const char *buf, size_t count)
1827 struct hid_device *hdev = to_hid_device(dev);
1828 struct wacom *wacom = hid_get_drvdata(hdev);
1831 if (kstrtou8(buf, 0, &new_speed))
1834 if (new_speed != 0 && new_speed != 1)
1837 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1842 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1843 wacom_show_speed, wacom_store_speed);
1846 static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1847 struct kobj_attribute *kattr,
1848 char *buf, int index)
1850 struct device *dev = kobj_to_dev(kobj->parent);
1851 struct hid_device *hdev = to_hid_device(dev);
1852 struct wacom *wacom = hid_get_drvdata(hdev);
1855 mode = wacom->led.groups[index].select;
1856 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
1859 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1860 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1861 struct kobj_attribute *kattr, char *buf) \
1863 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1865 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1866 .attr = {.name = "remote_mode", \
1867 .mode = DEV_ATTR_RO_PERM}, \
1868 .show = wacom_show_remote##SET_ID##_mode, \
1870 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1871 &remote##SET_ID##_mode_attr.attr, \
1874 static const struct attribute_group remote##SET_ID##_serial_group = { \
1876 .attrs = remote##SET_ID##_serial_attrs, \
1879 DEVICE_EKR_ATTR_GROUP(0);
1880 DEVICE_EKR_ATTR_GROUP(1);
1881 DEVICE_EKR_ATTR_GROUP(2);
1882 DEVICE_EKR_ATTR_GROUP(3);
1883 DEVICE_EKR_ATTR_GROUP(4);
1885 static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1889 struct wacom_remote *remote = wacom->remote;
1891 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1894 if (!remote->remotes[index].group.name)
1897 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1898 &remote->remotes[index].group);
1900 remote->remotes[index].group.name = NULL;
1901 hid_err(wacom->hdev,
1902 "cannot create sysfs group err: %d\n", error);
1909 static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1911 const size_t buf_size = 2;
1915 buf = kzalloc(buf_size, GFP_KERNEL);
1919 buf[0] = WAC_CMD_DELETE_PAIRING;
1922 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1923 buf_size, WAC_CMD_RETRIES);
1929 static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1930 struct kobj_attribute *attr,
1931 const char *buf, size_t count)
1933 unsigned char selector = 0;
1934 struct device *dev = kobj_to_dev(kobj->parent);
1935 struct hid_device *hdev = to_hid_device(dev);
1936 struct wacom *wacom = hid_get_drvdata(hdev);
1939 if (!strncmp(buf, "*\n", 2)) {
1940 selector = WAC_CMD_UNPAIR_ALL;
1942 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1947 mutex_lock(&wacom->lock);
1949 err = wacom_cmd_unpair_remote(wacom, selector);
1950 mutex_unlock(&wacom->lock);
1952 return err < 0 ? err : count;
1955 static struct kobj_attribute unpair_remote_attr = {
1956 .attr = {.name = "unpair_remote", .mode = 0200},
1957 .store = wacom_store_unpair_remote,
1960 static const struct attribute *remote_unpair_attrs[] = {
1961 &unpair_remote_attr.attr,
1965 static void wacom_remotes_destroy(void *data)
1967 struct wacom *wacom = data;
1968 struct wacom_remote *remote = wacom->remote;
1973 kobject_put(remote->remote_dir);
1974 kfifo_free(&remote->remote_fifo);
1975 wacom->remote = NULL;
1978 static int wacom_initialize_remotes(struct wacom *wacom)
1981 struct wacom_remote *remote;
1984 if (wacom->wacom_wac.features.type != REMOTE)
1987 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1992 wacom->remote = remote;
1994 spin_lock_init(&remote->remote_lock);
1996 error = kfifo_alloc(&remote->remote_fifo,
1997 5 * sizeof(struct wacom_remote_data),
2000 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
2004 remote->remotes[0].group = remote0_serial_group;
2005 remote->remotes[1].group = remote1_serial_group;
2006 remote->remotes[2].group = remote2_serial_group;
2007 remote->remotes[3].group = remote3_serial_group;
2008 remote->remotes[4].group = remote4_serial_group;
2010 remote->remote_dir = kobject_create_and_add("wacom_remote",
2011 &wacom->hdev->dev.kobj);
2012 if (!remote->remote_dir)
2015 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
2018 hid_err(wacom->hdev,
2019 "cannot create sysfs group err: %d\n", error);
2023 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2024 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2025 remote->remotes[i].serial = 0;
2028 error = devm_add_action_or_reset(&wacom->hdev->dev,
2029 wacom_remotes_destroy, wacom);
2036 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
2038 struct input_dev *input_dev;
2039 struct hid_device *hdev = wacom->hdev;
2040 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2042 input_dev = devm_input_allocate_device(&hdev->dev);
2046 input_dev->name = wacom_wac->features.name;
2047 input_dev->phys = hdev->phys;
2048 input_dev->dev.parent = &hdev->dev;
2049 input_dev->open = wacom_open;
2050 input_dev->close = wacom_close;
2051 input_dev->uniq = hdev->uniq;
2052 input_dev->id.bustype = hdev->bus;
2053 input_dev->id.vendor = hdev->vendor;
2054 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
2055 input_dev->id.version = hdev->version;
2056 input_set_drvdata(input_dev, wacom);
2061 static int wacom_allocate_inputs(struct wacom *wacom)
2063 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2065 wacom_wac->pen_input = wacom_allocate_input(wacom);
2066 wacom_wac->touch_input = wacom_allocate_input(wacom);
2067 wacom_wac->pad_input = wacom_allocate_input(wacom);
2068 if (!wacom_wac->pen_input ||
2069 !wacom_wac->touch_input ||
2070 !wacom_wac->pad_input)
2073 wacom_wac->pen_input->name = wacom_wac->pen_name;
2074 wacom_wac->touch_input->name = wacom_wac->touch_name;
2075 wacom_wac->pad_input->name = wacom_wac->pad_name;
2080 static int wacom_register_inputs(struct wacom *wacom)
2082 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2083 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2086 pen_input_dev = wacom_wac->pen_input;
2087 touch_input_dev = wacom_wac->touch_input;
2088 pad_input_dev = wacom_wac->pad_input;
2090 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
2093 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
2095 /* no pen in use on this interface */
2096 input_free_device(pen_input_dev);
2097 wacom_wac->pen_input = NULL;
2098 pen_input_dev = NULL;
2100 error = input_register_device(pen_input_dev);
2105 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
2107 /* no touch in use on this interface */
2108 input_free_device(touch_input_dev);
2109 wacom_wac->touch_input = NULL;
2110 touch_input_dev = NULL;
2112 error = input_register_device(touch_input_dev);
2117 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2119 /* no pad events using this interface */
2120 input_free_device(pad_input_dev);
2121 wacom_wac->pad_input = NULL;
2122 pad_input_dev = NULL;
2124 error = input_register_device(pad_input_dev);
2132 wacom_wac->pad_input = NULL;
2133 wacom_wac->touch_input = NULL;
2134 wacom_wac->pen_input = NULL;
2139 * Not all devices report physical dimensions from HID.
2140 * Compute the default from hardcoded logical dimension
2141 * and resolution before driver overwrites them.
2143 static void wacom_set_default_phy(struct wacom_features *features)
2145 if (features->x_resolution) {
2146 features->x_phy = (features->x_max * 100) /
2147 features->x_resolution;
2148 features->y_phy = (features->y_max * 100) /
2149 features->y_resolution;
2153 static void wacom_calculate_res(struct wacom_features *features)
2155 /* set unit to "100th of a mm" for devices not reported by HID */
2156 if (!features->unit) {
2157 features->unit = 0x11;
2158 features->unitExpo = -3;
2161 features->x_resolution = wacom_calc_hid_res(features->x_max,
2164 features->unitExpo);
2165 features->y_resolution = wacom_calc_hid_res(features->y_max,
2168 features->unitExpo);
2171 void wacom_battery_work(struct work_struct *work)
2173 struct wacom *wacom = container_of(work, struct wacom, battery_work);
2175 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2176 !wacom->battery.battery) {
2177 wacom_initialize_battery(wacom);
2179 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2180 wacom->battery.battery) {
2181 wacom_destroy_battery(wacom);
2185 static size_t wacom_compute_pktlen(struct hid_device *hdev)
2187 struct hid_report_enum *report_enum;
2188 struct hid_report *report;
2191 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2193 list_for_each_entry(report, &report_enum->report_list, list) {
2194 size_t report_size = hid_report_len(report);
2195 if (report_size > size)
2202 static void wacom_update_name(struct wacom *wacom, const char *suffix)
2204 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2205 struct wacom_features *features = &wacom_wac->features;
2206 char name[WACOM_NAME_MAX - 20]; /* Leave some room for suffixes */
2208 /* Generic devices name unspecified */
2209 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
2210 char *product_name = wacom->hdev->name;
2212 if (hid_is_usb(wacom->hdev)) {
2213 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2214 struct usb_device *dev = interface_to_usbdev(intf);
2215 product_name = dev->product;
2218 if (wacom->hdev->bus == BUS_I2C) {
2219 snprintf(name, sizeof(name), "%s %X",
2220 features->name, wacom->hdev->product);
2221 } else if (strstr(product_name, "Wacom") ||
2222 strstr(product_name, "wacom") ||
2223 strstr(product_name, "WACOM")) {
2224 strscpy(name, product_name, sizeof(name));
2226 snprintf(name, sizeof(name), "Wacom %s", product_name);
2229 /* strip out excess whitespaces */
2231 char *gap = strstr(name, " ");
2234 /* shift everything including the terminator */
2235 memmove(gap, gap+1, strlen(gap));
2238 /* get rid of trailing whitespace */
2239 if (name[strlen(name)-1] == ' ')
2240 name[strlen(name)-1] = '\0';
2242 strscpy(name, features->name, sizeof(name));
2245 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2248 /* Append the device type to the name */
2249 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
2250 "%s%s Pen", name, suffix);
2251 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
2252 "%s%s Finger", name, suffix);
2253 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
2254 "%s%s Pad", name, suffix);
2257 static void wacom_release_resources(struct wacom *wacom)
2259 struct hid_device *hdev = wacom->hdev;
2261 if (!wacom->resources)
2264 devres_release_group(&hdev->dev, wacom);
2266 wacom->resources = false;
2268 wacom->wacom_wac.pen_input = NULL;
2269 wacom->wacom_wac.touch_input = NULL;
2270 wacom->wacom_wac.pad_input = NULL;
2273 static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2275 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2276 wacom_wac->shared->type = wacom_wac->features.type;
2277 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2280 if (wacom_wac->has_mute_touch_switch) {
2281 wacom_wac->shared->has_mute_touch_switch = true;
2282 /* Hardware touch switch may be off. Wait until
2283 * we know the switch state to decide is_touch_on.
2284 * Softkey state should be initialized to "on" to
2285 * match historic default.
2287 if (wacom_wac->is_soft_touch_switch)
2288 wacom_wac->shared->is_touch_on = true;
2291 if (wacom_wac->shared->has_mute_touch_switch &&
2292 wacom_wac->shared->touch_input) {
2293 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2294 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2299 static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
2301 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2302 struct wacom_features *features = &wacom_wac->features;
2303 struct hid_device *hdev = wacom->hdev;
2305 unsigned int connect_mask = HID_CONNECT_HIDRAW;
2307 features->pktlen = wacom_compute_pktlen(hdev);
2308 if (features->pktlen > WACOM_PKGLEN_MAX)
2311 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2314 wacom->resources = true;
2316 error = wacom_allocate_inputs(wacom);
2321 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2322 * into debug mode for the touch part.
2323 * We ignore the other interfaces.
2325 if (features->type == BAMBOO_PAD) {
2326 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2327 features->type = HID_GENERIC;
2328 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2329 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2335 /* set the default size in case we do not get them from hid */
2336 wacom_set_default_phy(features);
2338 /* Retrieve the physical and logical size for touch devices */
2339 wacom_retrieve_hid_descriptor(hdev, features);
2340 wacom_setup_device_quirks(wacom);
2342 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2343 features->type != WIRELESS) {
2344 error = features->type == HID_GENERIC ? -ENODEV : 0;
2346 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
2348 error ? "Ignoring" : "Assuming pen");
2353 features->device_type |= WACOM_DEVICETYPE_PEN;
2356 wacom_calculate_res(features);
2358 wacom_update_name(wacom, wireless ? " (WL)" : "");
2360 /* pen only Bamboo neither support touch nor pad */
2361 if ((features->type == BAMBOO_PEN) &&
2362 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2363 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2368 error = wacom_add_shared_data(hdev);
2372 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2373 (features->quirks & WACOM_QUIRK_BATTERY)) {
2374 error = wacom_initialize_battery(wacom);
2379 error = wacom_register_inputs(wacom);
2383 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
2384 error = wacom_initialize_leds(wacom);
2388 error = wacom_initialize_remotes(wacom);
2393 if (features->type == HID_GENERIC)
2394 connect_mask |= HID_CONNECT_DRIVER;
2396 /* Regular HID work starts now */
2397 error = hid_hw_start(hdev, connect_mask);
2399 hid_err(hdev, "hw start failed\n");
2404 /* Note that if query fails it is not a hard failure */
2405 wacom_query_tablet_data(wacom);
2408 /* touch only Bamboo doesn't support pen */
2409 if ((features->type == BAMBOO_TOUCH) &&
2410 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2411 cancel_delayed_work_sync(&wacom->init_work);
2412 _wacom_query_tablet_data(wacom);
2417 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2418 error = hid_hw_open(hdev);
2420 wacom_set_shared_values(wacom_wac);
2421 devres_close_group(&hdev->dev, wacom);
2428 wacom_release_resources(wacom);
2432 static void wacom_wireless_work(struct work_struct *work)
2434 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2435 struct usb_device *usbdev = wacom->usbdev;
2436 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2437 struct hid_device *hdev1, *hdev2;
2438 struct wacom *wacom1, *wacom2;
2439 struct wacom_wac *wacom_wac1, *wacom_wac2;
2443 * Regardless if this is a disconnect or a new tablet,
2444 * remove any existing input and battery devices.
2447 wacom_destroy_battery(wacom);
2452 /* Stylus interface */
2453 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2454 wacom1 = hid_get_drvdata(hdev1);
2455 wacom_wac1 = &(wacom1->wacom_wac);
2456 wacom_release_resources(wacom1);
2458 /* Touch interface */
2459 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2460 wacom2 = hid_get_drvdata(hdev2);
2461 wacom_wac2 = &(wacom2->wacom_wac);
2462 wacom_release_resources(wacom2);
2464 if (wacom_wac->pid == 0) {
2465 hid_info(wacom->hdev, "wireless tablet disconnected\n");
2467 const struct hid_device_id *id = wacom_ids;
2469 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2473 if (id->vendor == USB_VENDOR_ID_WACOM &&
2474 id->product == wacom_wac->pid)
2480 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2484 /* Stylus interface */
2485 wacom_wac1->features =
2486 *((struct wacom_features *)id->driver_data);
2488 wacom_wac1->pid = wacom_wac->pid;
2490 error = wacom_parse_and_register(wacom1, true);
2494 /* Touch interface */
2495 if (wacom_wac1->features.touch_max ||
2496 (wacom_wac1->features.type >= INTUOSHT &&
2497 wacom_wac1->features.type <= BAMBOO_PT)) {
2498 wacom_wac2->features =
2499 *((struct wacom_features *)id->driver_data);
2500 wacom_wac2->pid = wacom_wac->pid;
2502 error = wacom_parse_and_register(wacom2, true);
2507 strscpy(wacom_wac->name, wacom_wac1->name,
2508 sizeof(wacom_wac->name));
2509 error = wacom_initialize_battery(wacom);
2517 wacom_release_resources(wacom1);
2518 wacom_release_resources(wacom2);
2522 static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2524 struct wacom_remote *remote = wacom->remote;
2525 u32 serial = remote->remotes[index].serial;
2527 unsigned long flags;
2529 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2530 if (remote->remotes[i].serial == serial) {
2532 spin_lock_irqsave(&remote->remote_lock, flags);
2533 remote->remotes[i].registered = false;
2534 spin_unlock_irqrestore(&remote->remote_lock, flags);
2536 if (remote->remotes[i].battery.battery)
2537 devres_release_group(&wacom->hdev->dev,
2538 &remote->remotes[i].battery.bat_desc);
2540 if (remote->remotes[i].group.name)
2541 devres_release_group(&wacom->hdev->dev,
2542 &remote->remotes[i]);
2544 remote->remotes[i].serial = 0;
2545 remote->remotes[i].group.name = NULL;
2546 remote->remotes[i].battery.battery = NULL;
2547 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2552 static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2555 struct wacom_remote *remote = wacom->remote;
2556 struct device *dev = &wacom->hdev->dev;
2559 /* A remote can pair more than once with an EKR,
2560 * check to make sure this serial isn't already paired.
2562 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2563 if (remote->remotes[k].serial == serial)
2567 if (k < WACOM_MAX_REMOTES) {
2568 remote->remotes[index].serial = serial;
2572 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2575 error = wacom_remote_create_attr_group(wacom, serial, index);
2579 remote->remotes[index].input = wacom_allocate_input(wacom);
2580 if (!remote->remotes[index].input) {
2584 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2585 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2587 if (!remote->remotes[index].input->name) {
2592 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2597 remote->remotes[index].serial = serial;
2599 error = input_register_device(remote->remotes[index].input);
2603 error = wacom_led_groups_alloc_and_register_one(
2604 &remote->remotes[index].input->dev,
2605 wacom, index, 3, true);
2609 remote->remotes[index].registered = true;
2611 devres_close_group(dev, &remote->remotes[index]);
2615 devres_release_group(dev, &remote->remotes[index]);
2616 remote->remotes[index].serial = 0;
2620 static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2622 struct wacom_remote *remote = wacom->remote;
2625 if (!remote->remotes[index].registered)
2628 if (remote->remotes[index].battery.battery)
2631 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2634 error = __wacom_initialize_battery(wacom,
2635 &wacom->remote->remotes[index].battery);
2642 static void wacom_remote_work(struct work_struct *work)
2644 struct wacom *wacom = container_of(work, struct wacom, remote_work);
2645 struct wacom_remote *remote = wacom->remote;
2646 struct wacom_remote_data data;
2647 unsigned long flags;
2652 spin_lock_irqsave(&remote->remote_lock, flags);
2654 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2656 if (count != sizeof(data)) {
2657 hid_err(wacom->hdev,
2658 "workitem triggered without status available\n");
2659 spin_unlock_irqrestore(&remote->remote_lock, flags);
2663 if (!kfifo_is_empty(&remote->remote_fifo))
2664 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2666 spin_unlock_irqrestore(&remote->remote_lock, flags);
2668 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2669 serial = data.remote[i].serial;
2670 if (data.remote[i].connected) {
2672 if (remote->remotes[i].serial == serial) {
2673 wacom_remote_attach_battery(wacom, i);
2677 if (remote->remotes[i].serial)
2678 wacom_remote_destroy_one(wacom, i);
2680 wacom_remote_create_one(wacom, serial, i);
2682 } else if (remote->remotes[i].serial) {
2683 wacom_remote_destroy_one(wacom, i);
2688 static void wacom_mode_change_work(struct work_struct *work)
2690 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2691 struct wacom_shared *shared = wacom->wacom_wac.shared;
2692 struct wacom *wacom1 = NULL;
2693 struct wacom *wacom2 = NULL;
2694 bool is_direct = wacom->wacom_wac.is_direct_mode;
2698 wacom1 = hid_get_drvdata(shared->pen);
2699 wacom_release_resources(wacom1);
2700 hid_hw_stop(wacom1->hdev);
2701 wacom1->wacom_wac.has_mode_change = true;
2702 wacom1->wacom_wac.is_direct_mode = is_direct;
2705 if (shared->touch) {
2706 wacom2 = hid_get_drvdata(shared->touch);
2707 wacom_release_resources(wacom2);
2708 hid_hw_stop(wacom2->hdev);
2709 wacom2->wacom_wac.has_mode_change = true;
2710 wacom2->wacom_wac.is_direct_mode = is_direct;
2714 error = wacom_parse_and_register(wacom1, false);
2720 error = wacom_parse_and_register(wacom2, false);
2728 static int wacom_probe(struct hid_device *hdev,
2729 const struct hid_device_id *id)
2731 struct wacom *wacom;
2732 struct wacom_wac *wacom_wac;
2733 struct wacom_features *features;
2736 if (!id->driver_data)
2739 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2741 /* hid-core sets this quirk for the boot interface */
2742 hdev->quirks &= ~HID_QUIRK_NOGET;
2744 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2748 hid_set_drvdata(hdev, wacom);
2751 wacom_wac = &wacom->wacom_wac;
2752 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2753 features = &wacom_wac->features;
2755 if (features->check_for_hid_type && features->hid_type != hdev->type)
2758 error = wacom_devm_kfifo_alloc(wacom);
2762 wacom_wac->hid_data.inputmode = -1;
2763 wacom_wac->mode_report = -1;
2765 if (hid_is_usb(hdev)) {
2766 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2767 struct usb_device *dev = interface_to_usbdev(intf);
2769 wacom->usbdev = dev;
2773 mutex_init(&wacom->lock);
2774 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
2775 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2776 INIT_WORK(&wacom->battery_work, wacom_battery_work);
2777 INIT_WORK(&wacom->remote_work, wacom_remote_work);
2778 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
2779 timer_setup(&wacom->idleprox_timer, &wacom_idleprox_timeout, TIMER_DEFERRABLE);
2781 /* ask for the report descriptor to be loaded by HID */
2782 error = hid_parse(hdev);
2784 hid_err(hdev, "parse failed\n");
2788 error = wacom_parse_and_register(wacom, false);
2792 if (hdev->bus == BUS_BLUETOOTH) {
2793 error = device_create_file(&hdev->dev, &dev_attr_speed);
2796 "can't create sysfs speed attribute err: %d\n",
2800 wacom_wac->probe_complete = true;
2804 static void wacom_remove(struct hid_device *hdev)
2806 struct wacom *wacom = hid_get_drvdata(hdev);
2807 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2808 struct wacom_features *features = &wacom_wac->features;
2810 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2815 cancel_delayed_work_sync(&wacom->init_work);
2816 cancel_work_sync(&wacom->wireless_work);
2817 cancel_work_sync(&wacom->battery_work);
2818 cancel_work_sync(&wacom->remote_work);
2819 cancel_work_sync(&wacom->mode_change_work);
2820 del_timer_sync(&wacom->idleprox_timer);
2821 if (hdev->bus == BUS_BLUETOOTH)
2822 device_remove_file(&hdev->dev, &dev_attr_speed);
2824 /* make sure we don't trigger the LEDs */
2825 wacom_led_groups_release(wacom);
2827 if (wacom->wacom_wac.features.type != REMOTE)
2828 wacom_release_resources(wacom);
2832 static int wacom_resume(struct hid_device *hdev)
2834 struct wacom *wacom = hid_get_drvdata(hdev);
2836 mutex_lock(&wacom->lock);
2838 /* switch to wacom mode first */
2839 _wacom_query_tablet_data(wacom);
2840 wacom_led_control(wacom);
2842 mutex_unlock(&wacom->lock);
2847 static int wacom_reset_resume(struct hid_device *hdev)
2849 return wacom_resume(hdev);
2851 #endif /* CONFIG_PM */
2853 static struct hid_driver wacom_driver = {
2855 .id_table = wacom_ids,
2856 .probe = wacom_probe,
2857 .remove = wacom_remove,
2858 .report = wacom_wac_report,
2860 .resume = wacom_resume,
2861 .reset_resume = wacom_reset_resume,
2863 .raw_event = wacom_raw_event,
2865 module_hid_driver(wacom_driver);
2867 MODULE_VERSION(DRIVER_VERSION);
2868 MODULE_AUTHOR(DRIVER_AUTHOR);
2869 MODULE_DESCRIPTION(DRIVER_DESC);
2870 MODULE_LICENSE("GPL");