1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drivers/input/tablet/wacom_sys.c
5 * USB Wacom tablet support - system specific code
11 #include "wacom_wac.h"
13 #include <linux/input/mt.h>
15 #define WAC_MSG_RETRIES 5
16 #define WAC_CMD_RETRIES 10
18 #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
19 #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
20 #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
22 static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
23 size_t size, unsigned int retries)
28 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
30 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
33 hid_err(hdev, "wacom_get_report: ran out of retries "
34 "(last error = %d)\n", retval);
39 static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
40 size_t size, unsigned int retries)
45 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
47 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
50 hid_err(hdev, "wacom_set_report: ran out of retries "
51 "(last error = %d)\n", retval);
56 static void wacom_wac_queue_insert(struct hid_device *hdev,
57 struct kfifo_rec_ptr_2 *fifo,
58 u8 *raw_data, int size)
62 while (kfifo_avail(fifo) < size) {
64 hid_warn(hdev, "%s: kfifo has filled, starting to drop events\n", __func__);
70 kfifo_in(fifo, raw_data, size);
73 static void wacom_wac_queue_flush(struct hid_device *hdev,
74 struct kfifo_rec_ptr_2 *fifo)
76 while (!kfifo_is_empty(fifo)) {
77 u8 buf[WACOM_PKGLEN_MAX];
81 size = kfifo_out(fifo, buf, sizeof(buf));
82 err = hid_report_raw_event(hdev, HID_INPUT_REPORT, buf, size, false);
84 hid_warn(hdev, "%s: unable to flush event due to error %d\n",
90 static int wacom_wac_pen_serial_enforce(struct hid_device *hdev,
91 struct hid_report *report, u8 *raw_data, int size)
93 struct wacom *wacom = hid_get_drvdata(hdev);
94 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
95 struct wacom_features *features = &wacom_wac->features;
100 if (wacom_wac->serial[0] || !(features->quirks & WACOM_QUIRK_TOOLSERIAL))
103 /* Queue events which have invalid tool type or serial number */
104 for (i = 0; i < report->maxfield; i++) {
105 for (j = 0; j < report->field[i]->maxusage; j++) {
106 struct hid_field *field = report->field[i];
107 struct hid_usage *usage = &field->usage[j];
108 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
113 if (equivalent_usage != HID_DG_INRANGE &&
114 equivalent_usage != HID_DG_TOOLSERIALNUMBER &&
115 equivalent_usage != WACOM_HID_WD_SERIALHI &&
116 equivalent_usage != WACOM_HID_WD_TOOLTYPE)
119 offset = field->report_offset;
120 size = field->report_size;
121 value = hid_field_extract(hdev, raw_data+1, offset + j * size, size);
123 /* If we go out of range, we need to flush the queue ASAP */
124 if (equivalent_usage == HID_DG_INRANGE)
129 switch (equivalent_usage) {
130 case HID_DG_TOOLSERIALNUMBER:
131 wacom_wac->serial[0] = value;
134 case WACOM_HID_WD_SERIALHI:
135 wacom_wac->serial[0] |= ((__u64)value) << 32;
138 case WACOM_HID_WD_TOOLTYPE:
139 wacom_wac->id[0] = value;
150 wacom_wac_queue_flush(hdev, &wacom_wac->pen_fifo);
152 wacom_wac_queue_insert(hdev, &wacom_wac->pen_fifo, raw_data, size);
154 return insert && !flush;
157 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
158 u8 *raw_data, int size)
160 struct wacom *wacom = hid_get_drvdata(hdev);
162 if (size > WACOM_PKGLEN_MAX)
165 if (wacom_wac_pen_serial_enforce(hdev, report, raw_data, size))
168 memcpy(wacom->wacom_wac.data, raw_data, size);
170 wacom_wac_irq(&wacom->wacom_wac, size);
175 static int wacom_open(struct input_dev *dev)
177 struct wacom *wacom = input_get_drvdata(dev);
179 return hid_hw_open(wacom->hdev);
182 static void wacom_close(struct input_dev *dev)
184 struct wacom *wacom = input_get_drvdata(dev);
187 * wacom->hdev should never be null, but surprisingly, I had the case
188 * once while unplugging the Wacom Wireless Receiver.
191 hid_hw_close(wacom->hdev);
195 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
197 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
198 unsigned unit, int exponent)
200 struct hid_field field = {
201 .logical_maximum = logical_extents,
202 .physical_maximum = physical_extents,
204 .unit_exponent = exponent,
207 return hidinput_calc_abs_res(&field, ABS_X);
210 static void wacom_hid_usage_quirk(struct hid_device *hdev,
211 struct hid_field *field, struct hid_usage *usage)
213 struct wacom *wacom = hid_get_drvdata(hdev);
214 struct wacom_features *features = &wacom->wacom_wac.features;
215 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
218 * The Dell Canvas 27 needs to be switched to its vendor-defined
219 * report to provide the best resolution.
221 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
222 hdev->product == 0x4200 &&
223 field->application == HID_UP_MSVENDOR) {
224 wacom->wacom_wac.mode_report = field->report->id;
225 wacom->wacom_wac.mode_value = 2;
229 * ISDv4 devices which predate HID's adoption of the
230 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
231 * position instead. We can accurately detect if a
232 * usage with that value should be HID_DG_BARRELSWITCH2
233 * based on the surrounding usages, which have remained
234 * constant across generations.
236 if (features->type == HID_GENERIC &&
237 usage->hid == 0x000D0000 &&
238 field->application == HID_DG_PEN &&
239 field->physical == HID_DG_STYLUS) {
240 int i = usage->usage_index;
242 if (i-4 >= 0 && i+1 < field->maxusage &&
243 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
244 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
245 field->usage[i-2].hid == HID_DG_ERASER &&
246 field->usage[i-1].hid == HID_DG_INVERT &&
247 field->usage[i+1].hid == HID_DG_INRANGE) {
248 usage->hid = HID_DG_BARRELSWITCH2;
253 * Wacom's AES devices use different vendor-defined usages to
254 * report serial number information compared to their branded
255 * hardware. The usages are also sometimes ill-defined and do
256 * not have the correct logical min/max values set. Lets patch
257 * the descriptor to use the branded usage convention and fix
260 if (usage->hid == WACOM_HID_WT_SERIALNUMBER &&
261 field->report_size == 16 &&
262 field->index + 2 < field->report->maxfield) {
263 struct hid_field *a = field->report->field[field->index + 1];
264 struct hid_field *b = field->report->field[field->index + 2];
266 if (a->maxusage > 0 &&
267 a->usage[0].hid == HID_DG_TOOLSERIALNUMBER &&
268 a->report_size == 32 &&
270 b->usage[0].hid == 0xFF000000 &&
271 b->report_size == 8) {
272 features->quirks |= WACOM_QUIRK_AESPEN;
273 usage->hid = WACOM_HID_WD_TOOLTYPE;
274 field->logical_minimum = S16_MIN;
275 field->logical_maximum = S16_MAX;
276 a->logical_minimum = S32_MIN;
277 a->logical_maximum = S32_MAX;
278 b->usage[0].hid = WACOM_HID_WD_SERIALHI;
279 b->logical_minimum = 0;
280 b->logical_maximum = U8_MAX;
284 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
285 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
286 hdev->product == 0x0358 &&
287 WACOM_PEN_FIELD(field) &&
288 equivalent_usage == HID_GD_Y) {
289 field->logical_maximum = 43200;
293 static void wacom_feature_mapping(struct hid_device *hdev,
294 struct hid_field *field, struct hid_usage *usage)
296 struct wacom *wacom = hid_get_drvdata(hdev);
297 struct wacom_features *features = &wacom->wacom_wac.features;
298 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
299 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
304 wacom_hid_usage_quirk(hdev, field, usage);
306 switch (equivalent_usage) {
307 case HID_DG_CONTACTMAX:
308 /* leave touch_max as is if predefined */
309 if (!features->touch_max) {
311 data = kzalloc(2, GFP_KERNEL);
314 data[0] = field->report->id;
315 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
316 data, 2, WAC_CMD_RETRIES);
318 features->touch_max = data[1];
320 features->touch_max = 16;
321 hid_warn(hdev, "wacom_feature_mapping: "
322 "could not get HID_DG_CONTACTMAX, "
323 "defaulting to %d\n",
324 features->touch_max);
329 case HID_DG_INPUTMODE:
330 /* Ignore if value index is out of bounds. */
331 if (usage->usage_index >= field->report_count) {
332 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
336 hid_data->inputmode = field->report->id;
337 hid_data->inputmode_index = usage->usage_index;
340 case HID_UP_DIGITIZER:
341 if (field->report->id == 0x0B &&
342 (field->application == WACOM_HID_G9_PEN ||
343 field->application == WACOM_HID_G11_PEN)) {
344 wacom->wacom_wac.mode_report = field->report->id;
345 wacom->wacom_wac.mode_value = 0;
349 case WACOM_HID_WD_DATAMODE:
350 wacom->wacom_wac.mode_report = field->report->id;
351 wacom->wacom_wac.mode_value = 2;
354 case WACOM_HID_UP_G9:
355 case WACOM_HID_UP_G11:
356 if (field->report->id == 0x03 &&
357 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
358 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
359 wacom->wacom_wac.mode_report = field->report->id;
360 wacom->wacom_wac.mode_value = 0;
363 case WACOM_HID_WD_OFFSETLEFT:
364 case WACOM_HID_WD_OFFSETTOP:
365 case WACOM_HID_WD_OFFSETRIGHT:
366 case WACOM_HID_WD_OFFSETBOTTOM:
368 n = hid_report_len(field->report);
369 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
372 data[0] = field->report->id;
373 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
374 data, n, WAC_CMD_RETRIES);
376 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
379 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
388 * Interface Descriptor of wacom devices can be incomplete and
389 * inconsistent so wacom_features table is used to store stylus
390 * device's packet lengths, various maximum values, and tablet
391 * resolution based on product ID's.
393 * For devices that contain 2 interfaces, wacom_features table is
394 * inaccurate for the touch interface. Since the Interface Descriptor
395 * for touch interfaces has pretty complete data, this function exists
396 * to query tablet for this missing information instead of hard coding in
397 * an additional table.
399 * A typical Interface Descriptor for a stylus will contain a
400 * boot mouse application collection that is not of interest and this
401 * function will ignore it.
403 * It also contains a digitizer application collection that also is not
404 * of interest since any information it contains would be duplicate
405 * of what is in wacom_features. Usually it defines a report of an array
406 * of bytes that could be used as max length of the stylus packet returned.
407 * If it happens to define a Digitizer-Stylus Physical Collection then
408 * the X and Y logical values contain valid data but it is ignored.
410 * A typical Interface Descriptor for a touch interface will contain a
411 * Digitizer-Finger Physical Collection which will define both logical
412 * X/Y maximum as well as the physical size of tablet. Since touch
413 * interfaces haven't supported pressure or distance, this is enough
414 * information to override invalid values in the wacom_features table.
416 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
417 * data. We deal with them after returning from this function.
419 static void wacom_usage_mapping(struct hid_device *hdev,
420 struct hid_field *field, struct hid_usage *usage)
422 struct wacom *wacom = hid_get_drvdata(hdev);
423 struct wacom_features *features = &wacom->wacom_wac.features;
424 bool finger = WACOM_FINGER_FIELD(field);
425 bool pen = WACOM_PEN_FIELD(field);
426 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
429 * Requiring Stylus Usage will ignore boot mouse
430 * X/Y values and some cases of invalid Digitizer X/Y
431 * values commonly reported.
434 features->device_type |= WACOM_DEVICETYPE_PEN;
436 features->device_type |= WACOM_DEVICETYPE_TOUCH;
440 wacom_hid_usage_quirk(hdev, field, usage);
442 switch (equivalent_usage) {
444 features->x_max = field->logical_maximum;
446 features->x_phy = field->physical_maximum;
447 if ((features->type != BAMBOO_PT) &&
448 (features->type != BAMBOO_TOUCH)) {
449 features->unit = field->unit;
450 features->unitExpo = field->unit_exponent;
455 features->y_max = field->logical_maximum;
457 features->y_phy = field->physical_maximum;
458 if ((features->type != BAMBOO_PT) &&
459 (features->type != BAMBOO_TOUCH)) {
460 features->unit = field->unit;
461 features->unitExpo = field->unit_exponent;
465 case HID_DG_TIPPRESSURE:
467 features->pressure_max = field->logical_maximum;
471 if (features->type == HID_GENERIC)
472 wacom_wac_usage_mapping(hdev, field, usage);
475 static void wacom_post_parse_hid(struct hid_device *hdev,
476 struct wacom_features *features)
478 struct wacom *wacom = hid_get_drvdata(hdev);
479 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
481 if (features->type == HID_GENERIC) {
482 /* Any last-minute generic device setup */
483 if (wacom_wac->has_mode_change) {
484 if (wacom_wac->is_direct_mode)
485 features->device_type |= WACOM_DEVICETYPE_DIRECT;
487 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
490 if (features->touch_max > 1) {
491 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
492 input_mt_init_slots(wacom_wac->touch_input,
493 wacom_wac->features.touch_max,
496 input_mt_init_slots(wacom_wac->touch_input,
497 wacom_wac->features.touch_max,
503 static void wacom_parse_hid(struct hid_device *hdev,
504 struct wacom_features *features)
506 struct hid_report_enum *rep_enum;
507 struct hid_report *hreport;
510 /* check features first */
511 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
512 list_for_each_entry(hreport, &rep_enum->report_list, list) {
513 for (i = 0; i < hreport->maxfield; i++) {
514 /* Ignore if report count is out of bounds. */
515 if (hreport->field[i]->report_count < 1)
518 for (j = 0; j < hreport->field[i]->maxusage; j++) {
519 wacom_feature_mapping(hdev, hreport->field[i],
520 hreport->field[i]->usage + j);
525 /* now check the input usages */
526 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
527 list_for_each_entry(hreport, &rep_enum->report_list, list) {
529 if (!hreport->maxfield)
532 for (i = 0; i < hreport->maxfield; i++)
533 for (j = 0; j < hreport->field[i]->maxusage; j++)
534 wacom_usage_mapping(hdev, hreport->field[i],
535 hreport->field[i]->usage + j);
538 wacom_post_parse_hid(hdev, features);
541 static int wacom_hid_set_device_mode(struct hid_device *hdev)
543 struct wacom *wacom = hid_get_drvdata(hdev);
544 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
545 struct hid_report *r;
546 struct hid_report_enum *re;
548 if (hid_data->inputmode < 0)
551 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
552 r = re->report_id_hash[hid_data->inputmode];
554 r->field[0]->value[hid_data->inputmode_index] = 2;
555 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
560 static int wacom_set_device_mode(struct hid_device *hdev,
561 struct wacom_wac *wacom_wac)
564 struct hid_report *r;
565 struct hid_report_enum *re;
567 int error = -ENOMEM, limit = 0;
569 if (wacom_wac->mode_report < 0)
572 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
573 r = re->report_id_hash[wacom_wac->mode_report];
577 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
581 length = hid_report_len(r);
584 rep_data[0] = wacom_wac->mode_report;
585 rep_data[1] = wacom_wac->mode_value;
587 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
590 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
591 rep_data, length, 1);
592 } while (error >= 0 &&
593 rep_data[1] != wacom_wac->mode_report &&
594 limit++ < WAC_MSG_RETRIES);
598 return error < 0 ? error : 0;
601 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
602 struct wacom_features *features)
604 struct wacom *wacom = hid_get_drvdata(hdev);
608 switch (features->type) {
612 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
616 rep_data[0] = speed == 0 ? 0x05 : 0x06;
619 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
623 wacom->wacom_wac.bt_high_speed = speed;
629 * Note that if the raw queries fail, it's not a hard failure
630 * and it is safe to continue
632 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
637 wacom->wacom_wac.bt_features &= ~0x20;
639 wacom->wacom_wac.bt_features |= 0x20;
642 rep_data[1] = wacom->wacom_wac.bt_features;
644 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
647 wacom->wacom_wac.bt_high_speed = speed;
655 * Switch the tablet into its most-capable mode. Wacom tablets are
656 * typically configured to power-up in a mode which sends mouse-like
657 * reports to the OS. To get absolute position, pressure data, etc.
658 * from the tablet, it is necessary to switch the tablet out of this
659 * mode and into one which sends the full range of tablet data.
661 static int _wacom_query_tablet_data(struct wacom *wacom)
663 struct hid_device *hdev = wacom->hdev;
664 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
665 struct wacom_features *features = &wacom_wac->features;
667 if (hdev->bus == BUS_BLUETOOTH)
668 return wacom_bt_query_tablet_data(hdev, 1, features);
670 if (features->type != HID_GENERIC) {
671 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
672 if (features->type > TABLETPC) {
673 /* MT Tablet PC touch */
674 wacom_wac->mode_report = 3;
675 wacom_wac->mode_value = 4;
676 } else if (features->type == WACOM_24HDT) {
677 wacom_wac->mode_report = 18;
678 wacom_wac->mode_value = 2;
679 } else if (features->type == WACOM_27QHDT) {
680 wacom_wac->mode_report = 131;
681 wacom_wac->mode_value = 2;
682 } else if (features->type == BAMBOO_PAD) {
683 wacom_wac->mode_report = 2;
684 wacom_wac->mode_value = 2;
686 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
687 if (features->type <= BAMBOO_PT) {
688 wacom_wac->mode_report = 2;
689 wacom_wac->mode_value = 2;
694 wacom_set_device_mode(hdev, wacom_wac);
696 if (features->type == HID_GENERIC)
697 return wacom_hid_set_device_mode(hdev);
702 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
703 struct wacom_features *features)
705 struct wacom *wacom = hid_get_drvdata(hdev);
706 struct usb_interface *intf = wacom->intf;
708 /* default features */
709 features->x_fuzz = 4;
710 features->y_fuzz = 4;
711 features->pressure_fuzz = 0;
712 features->distance_fuzz = 1;
713 features->tilt_fuzz = 1;
716 * The wireless device HID is basic and layout conflicts with
717 * other tablets (monitor and touch interface can look like pen).
718 * Skip the query for this type and modify defaults based on
721 if (features->type == WIRELESS) {
722 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
723 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
725 features->device_type = WACOM_DEVICETYPE_NONE;
729 wacom_parse_hid(hdev, features);
732 struct wacom_hdev_data {
733 struct list_head list;
735 struct hid_device *dev;
736 struct wacom_shared shared;
739 static LIST_HEAD(wacom_udev_list);
740 static DEFINE_MUTEX(wacom_udev_list_lock);
742 static bool wacom_are_sibling(struct hid_device *hdev,
743 struct hid_device *sibling)
745 struct wacom *wacom = hid_get_drvdata(hdev);
746 struct wacom_features *features = &wacom->wacom_wac.features;
747 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
748 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
749 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
750 __u32 oPid = features->oPid ? features->oPid : hdev->product;
752 /* The defined oVid/oPid must match that of the sibling */
753 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
755 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
759 * Devices with the same VID/PID must share the same physical
760 * device path, while those with different VID/PID must share
761 * the same physical parent device path.
763 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
764 if (!hid_compare_device_paths(hdev, sibling, '/'))
767 if (!hid_compare_device_paths(hdev, sibling, '.'))
771 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
772 if (features->type != HID_GENERIC)
776 * Direct-input devices may not be siblings of indirect-input
779 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
780 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
784 * Indirect-input devices may not be siblings of direct-input
787 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
788 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
791 /* Pen devices may only be siblings of touch devices */
792 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
793 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
796 /* Touch devices may only be siblings of pen devices */
797 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
798 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
802 * No reason could be found for these two devices to NOT be
803 * siblings, so there's a good chance they ARE siblings
808 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
810 struct wacom_hdev_data *data;
812 /* Try to find an already-probed interface from the same device */
813 list_for_each_entry(data, &wacom_udev_list, list) {
814 if (hid_compare_device_paths(hdev, data->dev, '/')) {
815 kref_get(&data->kref);
820 /* Fallback to finding devices that appear to be "siblings" */
821 list_for_each_entry(data, &wacom_udev_list, list) {
822 if (wacom_are_sibling(hdev, data->dev)) {
823 kref_get(&data->kref);
831 static void wacom_release_shared_data(struct kref *kref)
833 struct wacom_hdev_data *data =
834 container_of(kref, struct wacom_hdev_data, kref);
836 mutex_lock(&wacom_udev_list_lock);
837 list_del(&data->list);
838 mutex_unlock(&wacom_udev_list_lock);
843 static void wacom_remove_shared_data(void *res)
845 struct wacom *wacom = res;
846 struct wacom_hdev_data *data;
847 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
849 if (wacom_wac->shared) {
850 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
853 if (wacom_wac->shared->touch == wacom->hdev)
854 wacom_wac->shared->touch = NULL;
855 else if (wacom_wac->shared->pen == wacom->hdev)
856 wacom_wac->shared->pen = NULL;
858 kref_put(&data->kref, wacom_release_shared_data);
859 wacom_wac->shared = NULL;
863 static int wacom_add_shared_data(struct hid_device *hdev)
865 struct wacom *wacom = hid_get_drvdata(hdev);
866 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
867 struct wacom_hdev_data *data;
870 mutex_lock(&wacom_udev_list_lock);
872 data = wacom_get_hdev_data(hdev);
874 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
880 kref_init(&data->kref);
882 list_add_tail(&data->list, &wacom_udev_list);
885 wacom_wac->shared = &data->shared;
887 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
889 mutex_unlock(&wacom_udev_list_lock);
890 wacom_remove_shared_data(wacom);
894 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
895 wacom_wac->shared->touch = hdev;
896 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
897 wacom_wac->shared->pen = hdev;
900 mutex_unlock(&wacom_udev_list_lock);
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 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 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 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 struct attribute_group generic_led_attr_group = {
1212 .name = "wacom_led",
1213 .attrs = generic_led_attrs,
1216 struct wacom_sysfs_group_devres {
1217 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 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 struct attribute_group *group)
1261 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1265 enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
1267 struct wacom *wacom = led->wacom;
1269 if (wacom->led.max_hlv)
1270 return led->hlv * LED_FULL / wacom->led.max_hlv;
1272 if (wacom->led.max_llv)
1273 return led->llv * LED_FULL / wacom->led.max_llv;
1275 /* device doesn't support brightness tuning */
1279 static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1281 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1282 struct wacom *wacom = led->wacom;
1284 if (wacom->led.groups[led->group].select != led->id)
1287 return wacom_leds_brightness_get(led);
1290 static int wacom_led_brightness_set(struct led_classdev *cdev,
1291 enum led_brightness brightness)
1293 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1294 struct wacom *wacom = led->wacom;
1297 mutex_lock(&wacom->lock);
1299 if (!wacom->led.groups || (brightness == LED_OFF &&
1300 wacom->led.groups[led->group].select != led->id)) {
1305 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1306 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1308 wacom->led.groups[led->group].select = led->id;
1310 error = wacom_led_control(wacom);
1313 mutex_unlock(&wacom->lock);
1318 static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1319 enum led_brightness brightness)
1323 static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1324 struct wacom_led *led, unsigned int group,
1325 unsigned int id, bool read_only)
1330 name = devm_kasprintf(dev, GFP_KERNEL,
1339 led->trigger.name = name;
1340 error = devm_led_trigger_register(dev, &led->trigger);
1342 hid_err(wacom->hdev,
1343 "failed to register LED trigger %s: %d\n",
1344 led->cdev.name, error);
1352 led->llv = wacom->led.llv;
1353 led->hlv = wacom->led.hlv;
1354 led->cdev.name = name;
1355 led->cdev.max_brightness = LED_FULL;
1356 led->cdev.flags = LED_HW_PLUGGABLE;
1357 led->cdev.brightness_get = __wacom_led_brightness_get;
1359 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
1360 led->cdev.default_trigger = led->cdev.name;
1362 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
1365 error = devm_led_classdev_register(dev, &led->cdev);
1367 hid_err(wacom->hdev,
1368 "failed to register LED %s: %d\n",
1369 led->cdev.name, error);
1370 led->cdev.name = NULL;
1377 static void wacom_led_groups_release_one(void *data)
1379 struct wacom_group_leds *group = data;
1381 devres_release_group(group->dev, group);
1384 static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1385 struct wacom *wacom,
1386 int group_id, int count,
1389 struct wacom_led *leds;
1392 if (group_id >= wacom->led.count || count <= 0)
1395 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1398 leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL);
1404 wacom->led.groups[group_id].leds = leds;
1405 wacom->led.groups[group_id].count = count;
1407 for (i = 0; i < count; i++) {
1408 error = wacom_led_register_one(dev, wacom, &leds[i],
1409 group_id, i, read_only);
1414 wacom->led.groups[group_id].dev = dev;
1416 devres_close_group(dev, &wacom->led.groups[group_id]);
1419 * There is a bug (?) in devm_led_classdev_register() in which its
1420 * increments the refcount of the parent. If the parent is an input
1421 * device, that means the ref count never reaches 0 when
1422 * devm_input_device_release() gets called.
1423 * This means that the LEDs are still there after disconnect.
1424 * Manually force the release of the group so that the leds are released
1425 * once we are done using them.
1427 error = devm_add_action_or_reset(&wacom->hdev->dev,
1428 wacom_led_groups_release_one,
1429 &wacom->led.groups[group_id]);
1436 devres_release_group(dev, &wacom->led.groups[group_id]);
1440 struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1443 struct wacom_group_leds *group;
1445 if (group_id >= wacom->led.count)
1448 group = &wacom->led.groups[group_id];
1455 return &group->leds[id];
1459 * wacom_led_next: gives the next available led with a wacom trigger.
1461 * returns the next available struct wacom_led which has its default trigger
1462 * or the current one if none is available.
1464 struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1466 struct wacom_led *next_led;
1476 next_led = wacom_led_find(wacom, group, ++next);
1477 if (!next_led || next_led == cur)
1479 } while (next_led->cdev.trigger != &next_led->trigger);
1484 static void wacom_led_groups_release(void *data)
1486 struct wacom *wacom = data;
1488 wacom->led.groups = NULL;
1489 wacom->led.count = 0;
1492 static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1494 struct device *dev = &wacom->hdev->dev;
1495 struct wacom_group_leds *groups;
1498 groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds),
1503 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1507 wacom->led.groups = groups;
1508 wacom->led.count = count;
1513 static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1514 int led_per_group, bool read_only)
1519 if (!wacom->wacom_wac.pad_input)
1522 dev = &wacom->wacom_wac.pad_input->dev;
1524 error = wacom_led_groups_allocate(wacom, group_count);
1528 for (i = 0; i < group_count; i++) {
1529 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1539 int wacom_initialize_leds(struct wacom *wacom)
1543 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1546 /* Initialize default values */
1547 switch (wacom->wacom_wac.features.type) {
1549 if (!wacom->generic_has_leds)
1551 wacom->led.llv = 100;
1552 wacom->led.max_llv = 100;
1554 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1556 hid_err(wacom->hdev,
1557 "cannot create leds err: %d\n", error);
1561 error = wacom_devm_sysfs_create_group(wacom,
1562 &generic_led_attr_group);
1569 wacom->led.llv = 10;
1570 wacom->led.hlv = 20;
1571 wacom->led.max_llv = 127;
1572 wacom->led.max_hlv = 127;
1573 wacom->led.img_lum = 10;
1575 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1577 hid_err(wacom->hdev,
1578 "cannot create leds err: %d\n", error);
1582 error = wacom_devm_sysfs_create_group(wacom,
1583 &intuos4_led_attr_group);
1590 wacom->led.img_lum = 0;
1592 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1594 hid_err(wacom->hdev,
1595 "cannot create leds err: %d\n", error);
1599 error = wacom_devm_sysfs_create_group(wacom,
1600 &cintiq_led_attr_group);
1609 wacom->led.llv = 32;
1610 wacom->led.max_llv = 96;
1612 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1614 hid_err(wacom->hdev,
1615 "cannot create leds err: %d\n", error);
1619 error = wacom_devm_sysfs_create_group(wacom,
1620 &intuos5_led_attr_group);
1624 wacom->led.llv = 50;
1625 wacom->led.max_llv = 100;
1626 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1628 hid_err(wacom->hdev,
1629 "cannot create leds err: %d\n", error);
1635 wacom->led.llv = 255;
1636 wacom->led.max_llv = 255;
1637 error = wacom_led_groups_allocate(wacom, 5);
1639 hid_err(wacom->hdev,
1640 "cannot create leds err: %d\n", error);
1650 hid_err(wacom->hdev,
1651 "cannot create sysfs group err: %d\n", error);
1658 static void wacom_init_work(struct work_struct *work)
1660 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1662 _wacom_query_tablet_data(wacom);
1663 wacom_led_control(wacom);
1666 static void wacom_query_tablet_data(struct wacom *wacom)
1668 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1671 static enum power_supply_property wacom_battery_props[] = {
1672 POWER_SUPPLY_PROP_MODEL_NAME,
1673 POWER_SUPPLY_PROP_PRESENT,
1674 POWER_SUPPLY_PROP_STATUS,
1675 POWER_SUPPLY_PROP_SCOPE,
1676 POWER_SUPPLY_PROP_CAPACITY
1679 static int wacom_battery_get_property(struct power_supply *psy,
1680 enum power_supply_property psp,
1681 union power_supply_propval *val)
1683 struct wacom_battery *battery = power_supply_get_drvdata(psy);
1687 case POWER_SUPPLY_PROP_MODEL_NAME:
1688 val->strval = battery->wacom->wacom_wac.name;
1690 case POWER_SUPPLY_PROP_PRESENT:
1691 val->intval = battery->bat_connected;
1693 case POWER_SUPPLY_PROP_SCOPE:
1694 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1696 case POWER_SUPPLY_PROP_CAPACITY:
1697 val->intval = battery->battery_capacity;
1699 case POWER_SUPPLY_PROP_STATUS:
1700 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1701 val->intval = battery->bat_status;
1702 else if (battery->bat_charging)
1703 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1704 else if (battery->battery_capacity == 100 &&
1705 battery->ps_connected)
1706 val->intval = POWER_SUPPLY_STATUS_FULL;
1707 else if (battery->ps_connected)
1708 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1710 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1720 static int __wacom_initialize_battery(struct wacom *wacom,
1721 struct wacom_battery *battery)
1723 static atomic_t battery_no = ATOMIC_INIT(0);
1724 struct device *dev = &wacom->hdev->dev;
1725 struct power_supply_config psy_cfg = { .drv_data = battery, };
1726 struct power_supply *ps_bat;
1727 struct power_supply_desc *bat_desc = &battery->bat_desc;
1731 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1734 battery->wacom = wacom;
1736 n = atomic_inc_return(&battery_no) - 1;
1738 bat_desc->properties = wacom_battery_props;
1739 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1740 bat_desc->get_property = wacom_battery_get_property;
1741 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1742 bat_desc->name = battery->bat_name;
1743 bat_desc->type = POWER_SUPPLY_TYPE_USB;
1744 bat_desc->use_for_apm = 0;
1746 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1747 if (IS_ERR(ps_bat)) {
1748 error = PTR_ERR(ps_bat);
1752 power_supply_powers(ps_bat, &wacom->hdev->dev);
1754 battery->battery = ps_bat;
1756 devres_close_group(dev, bat_desc);
1760 devres_release_group(dev, bat_desc);
1764 static int wacom_initialize_battery(struct wacom *wacom)
1766 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1767 return __wacom_initialize_battery(wacom, &wacom->battery);
1772 static void wacom_destroy_battery(struct wacom *wacom)
1774 if (wacom->battery.battery) {
1775 devres_release_group(&wacom->hdev->dev,
1776 &wacom->battery.bat_desc);
1777 wacom->battery.battery = NULL;
1781 static ssize_t wacom_show_speed(struct device *dev,
1782 struct device_attribute
1785 struct hid_device *hdev = to_hid_device(dev);
1786 struct wacom *wacom = hid_get_drvdata(hdev);
1788 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1791 static ssize_t wacom_store_speed(struct device *dev,
1792 struct device_attribute *attr,
1793 const char *buf, size_t count)
1795 struct hid_device *hdev = to_hid_device(dev);
1796 struct wacom *wacom = hid_get_drvdata(hdev);
1799 if (kstrtou8(buf, 0, &new_speed))
1802 if (new_speed != 0 && new_speed != 1)
1805 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1810 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1811 wacom_show_speed, wacom_store_speed);
1814 static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1815 struct kobj_attribute *kattr,
1816 char *buf, int index)
1818 struct device *dev = kobj_to_dev(kobj->parent);
1819 struct hid_device *hdev = to_hid_device(dev);
1820 struct wacom *wacom = hid_get_drvdata(hdev);
1823 mode = wacom->led.groups[index].select;
1824 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
1827 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1828 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1829 struct kobj_attribute *kattr, char *buf) \
1831 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1833 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1834 .attr = {.name = "remote_mode", \
1835 .mode = DEV_ATTR_RO_PERM}, \
1836 .show = wacom_show_remote##SET_ID##_mode, \
1838 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1839 &remote##SET_ID##_mode_attr.attr, \
1842 static struct attribute_group remote##SET_ID##_serial_group = { \
1844 .attrs = remote##SET_ID##_serial_attrs, \
1847 DEVICE_EKR_ATTR_GROUP(0);
1848 DEVICE_EKR_ATTR_GROUP(1);
1849 DEVICE_EKR_ATTR_GROUP(2);
1850 DEVICE_EKR_ATTR_GROUP(3);
1851 DEVICE_EKR_ATTR_GROUP(4);
1853 static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1857 struct wacom_remote *remote = wacom->remote;
1859 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1862 if (!remote->remotes[index].group.name)
1865 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1866 &remote->remotes[index].group);
1868 remote->remotes[index].group.name = NULL;
1869 hid_err(wacom->hdev,
1870 "cannot create sysfs group err: %d\n", error);
1877 static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1879 const size_t buf_size = 2;
1883 buf = kzalloc(buf_size, GFP_KERNEL);
1887 buf[0] = WAC_CMD_DELETE_PAIRING;
1890 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1891 buf_size, WAC_CMD_RETRIES);
1897 static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1898 struct kobj_attribute *attr,
1899 const char *buf, size_t count)
1901 unsigned char selector = 0;
1902 struct device *dev = kobj_to_dev(kobj->parent);
1903 struct hid_device *hdev = to_hid_device(dev);
1904 struct wacom *wacom = hid_get_drvdata(hdev);
1907 if (!strncmp(buf, "*\n", 2)) {
1908 selector = WAC_CMD_UNPAIR_ALL;
1910 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1915 mutex_lock(&wacom->lock);
1917 err = wacom_cmd_unpair_remote(wacom, selector);
1918 mutex_unlock(&wacom->lock);
1920 return err < 0 ? err : count;
1923 static struct kobj_attribute unpair_remote_attr = {
1924 .attr = {.name = "unpair_remote", .mode = 0200},
1925 .store = wacom_store_unpair_remote,
1928 static const struct attribute *remote_unpair_attrs[] = {
1929 &unpair_remote_attr.attr,
1933 static void wacom_remotes_destroy(void *data)
1935 struct wacom *wacom = data;
1936 struct wacom_remote *remote = wacom->remote;
1941 kobject_put(remote->remote_dir);
1942 kfifo_free(&remote->remote_fifo);
1943 wacom->remote = NULL;
1946 static int wacom_initialize_remotes(struct wacom *wacom)
1949 struct wacom_remote *remote;
1952 if (wacom->wacom_wac.features.type != REMOTE)
1955 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1960 wacom->remote = remote;
1962 spin_lock_init(&remote->remote_lock);
1964 error = kfifo_alloc(&remote->remote_fifo,
1965 5 * sizeof(struct wacom_remote_data),
1968 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1972 remote->remotes[0].group = remote0_serial_group;
1973 remote->remotes[1].group = remote1_serial_group;
1974 remote->remotes[2].group = remote2_serial_group;
1975 remote->remotes[3].group = remote3_serial_group;
1976 remote->remotes[4].group = remote4_serial_group;
1978 remote->remote_dir = kobject_create_and_add("wacom_remote",
1979 &wacom->hdev->dev.kobj);
1980 if (!remote->remote_dir)
1983 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
1986 hid_err(wacom->hdev,
1987 "cannot create sysfs group err: %d\n", error);
1991 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1992 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
1993 remote->remotes[i].serial = 0;
1996 error = devm_add_action_or_reset(&wacom->hdev->dev,
1997 wacom_remotes_destroy, wacom);
2004 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
2006 struct input_dev *input_dev;
2007 struct hid_device *hdev = wacom->hdev;
2008 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2010 input_dev = devm_input_allocate_device(&hdev->dev);
2014 input_dev->name = wacom_wac->features.name;
2015 input_dev->phys = hdev->phys;
2016 input_dev->dev.parent = &hdev->dev;
2017 input_dev->open = wacom_open;
2018 input_dev->close = wacom_close;
2019 input_dev->uniq = hdev->uniq;
2020 input_dev->id.bustype = hdev->bus;
2021 input_dev->id.vendor = hdev->vendor;
2022 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
2023 input_dev->id.version = hdev->version;
2024 input_set_drvdata(input_dev, wacom);
2029 static int wacom_allocate_inputs(struct wacom *wacom)
2031 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2033 wacom_wac->pen_input = wacom_allocate_input(wacom);
2034 wacom_wac->touch_input = wacom_allocate_input(wacom);
2035 wacom_wac->pad_input = wacom_allocate_input(wacom);
2036 if (!wacom_wac->pen_input ||
2037 !wacom_wac->touch_input ||
2038 !wacom_wac->pad_input)
2041 wacom_wac->pen_input->name = wacom_wac->pen_name;
2042 wacom_wac->touch_input->name = wacom_wac->touch_name;
2043 wacom_wac->pad_input->name = wacom_wac->pad_name;
2048 static int wacom_register_inputs(struct wacom *wacom)
2050 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2051 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2054 pen_input_dev = wacom_wac->pen_input;
2055 touch_input_dev = wacom_wac->touch_input;
2056 pad_input_dev = wacom_wac->pad_input;
2058 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
2061 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
2063 /* no pen in use on this interface */
2064 input_free_device(pen_input_dev);
2065 wacom_wac->pen_input = NULL;
2066 pen_input_dev = NULL;
2068 error = input_register_device(pen_input_dev);
2073 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
2075 /* no touch in use on this interface */
2076 input_free_device(touch_input_dev);
2077 wacom_wac->touch_input = NULL;
2078 touch_input_dev = NULL;
2080 error = input_register_device(touch_input_dev);
2085 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2087 /* no pad in use on this interface */
2088 input_free_device(pad_input_dev);
2089 wacom_wac->pad_input = NULL;
2090 pad_input_dev = NULL;
2092 error = input_register_device(pad_input_dev);
2100 wacom_wac->pad_input = NULL;
2101 wacom_wac->touch_input = NULL;
2102 wacom_wac->pen_input = NULL;
2107 * Not all devices report physical dimensions from HID.
2108 * Compute the default from hardcoded logical dimension
2109 * and resolution before driver overwrites them.
2111 static void wacom_set_default_phy(struct wacom_features *features)
2113 if (features->x_resolution) {
2114 features->x_phy = (features->x_max * 100) /
2115 features->x_resolution;
2116 features->y_phy = (features->y_max * 100) /
2117 features->y_resolution;
2121 static void wacom_calculate_res(struct wacom_features *features)
2123 /* set unit to "100th of a mm" for devices not reported by HID */
2124 if (!features->unit) {
2125 features->unit = 0x11;
2126 features->unitExpo = -3;
2129 features->x_resolution = wacom_calc_hid_res(features->x_max,
2132 features->unitExpo);
2133 features->y_resolution = wacom_calc_hid_res(features->y_max,
2136 features->unitExpo);
2139 void wacom_battery_work(struct work_struct *work)
2141 struct wacom *wacom = container_of(work, struct wacom, battery_work);
2143 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2144 !wacom->battery.battery) {
2145 wacom_initialize_battery(wacom);
2147 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2148 wacom->battery.battery) {
2149 wacom_destroy_battery(wacom);
2153 static size_t wacom_compute_pktlen(struct hid_device *hdev)
2155 struct hid_report_enum *report_enum;
2156 struct hid_report *report;
2159 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2161 list_for_each_entry(report, &report_enum->report_list, list) {
2162 size_t report_size = hid_report_len(report);
2163 if (report_size > size)
2170 static void wacom_update_name(struct wacom *wacom, const char *suffix)
2172 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2173 struct wacom_features *features = &wacom_wac->features;
2174 char name[WACOM_NAME_MAX];
2176 /* Generic devices name unspecified */
2177 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
2178 char *product_name = wacom->hdev->name;
2180 if (hid_is_using_ll_driver(wacom->hdev, &usb_hid_driver)) {
2181 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2182 struct usb_device *dev = interface_to_usbdev(intf);
2183 product_name = dev->product;
2186 if (wacom->hdev->bus == BUS_I2C) {
2187 snprintf(name, sizeof(name), "%s %X",
2188 features->name, wacom->hdev->product);
2189 } else if (strstr(product_name, "Wacom") ||
2190 strstr(product_name, "wacom") ||
2191 strstr(product_name, "WACOM")) {
2192 strlcpy(name, product_name, sizeof(name));
2194 snprintf(name, sizeof(name), "Wacom %s", product_name);
2197 /* strip out excess whitespaces */
2199 char *gap = strstr(name, " ");
2202 /* shift everything including the terminator */
2203 memmove(gap, gap+1, strlen(gap));
2206 /* get rid of trailing whitespace */
2207 if (name[strlen(name)-1] == ' ')
2208 name[strlen(name)-1] = '\0';
2210 strlcpy(name, features->name, sizeof(name));
2213 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2216 /* Append the device type to the name */
2217 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
2218 "%s%s Pen", name, suffix);
2219 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
2220 "%s%s Finger", name, suffix);
2221 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
2222 "%s%s Pad", name, suffix);
2225 static void wacom_release_resources(struct wacom *wacom)
2227 struct hid_device *hdev = wacom->hdev;
2229 if (!wacom->resources)
2232 devres_release_group(&hdev->dev, wacom);
2234 wacom->resources = false;
2236 wacom->wacom_wac.pen_input = NULL;
2237 wacom->wacom_wac.touch_input = NULL;
2238 wacom->wacom_wac.pad_input = NULL;
2241 static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2243 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2244 wacom_wac->shared->type = wacom_wac->features.type;
2245 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2248 if (wacom_wac->has_mute_touch_switch) {
2249 wacom_wac->shared->has_mute_touch_switch = true;
2250 wacom_wac->shared->is_touch_on = true;
2253 if (wacom_wac->shared->has_mute_touch_switch &&
2254 wacom_wac->shared->touch_input) {
2255 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2256 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2261 static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
2263 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2264 struct wacom_features *features = &wacom_wac->features;
2265 struct hid_device *hdev = wacom->hdev;
2267 unsigned int connect_mask = HID_CONNECT_HIDRAW;
2269 features->pktlen = wacom_compute_pktlen(hdev);
2270 if (features->pktlen > WACOM_PKGLEN_MAX)
2273 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2276 wacom->resources = true;
2278 error = wacom_allocate_inputs(wacom);
2283 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2284 * into debug mode for the touch part.
2285 * We ignore the other interfaces.
2287 if (features->type == BAMBOO_PAD) {
2288 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2289 features->type = HID_GENERIC;
2290 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2291 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2297 /* set the default size in case we do not get them from hid */
2298 wacom_set_default_phy(features);
2300 /* Retrieve the physical and logical size for touch devices */
2301 wacom_retrieve_hid_descriptor(hdev, features);
2302 wacom_setup_device_quirks(wacom);
2304 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2305 features->type != WIRELESS) {
2306 error = features->type == HID_GENERIC ? -ENODEV : 0;
2308 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
2310 error ? "Ignoring" : "Assuming pen");
2315 features->device_type |= WACOM_DEVICETYPE_PEN;
2318 wacom_calculate_res(features);
2320 wacom_update_name(wacom, wireless ? " (WL)" : "");
2322 /* pen only Bamboo neither support touch nor pad */
2323 if ((features->type == BAMBOO_PEN) &&
2324 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2325 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2330 error = wacom_add_shared_data(hdev);
2334 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2335 (features->quirks & WACOM_QUIRK_BATTERY)) {
2336 error = wacom_initialize_battery(wacom);
2341 error = wacom_register_inputs(wacom);
2345 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
2346 error = wacom_initialize_leds(wacom);
2350 error = wacom_initialize_remotes(wacom);
2355 if (features->type == HID_GENERIC)
2356 connect_mask |= HID_CONNECT_DRIVER;
2358 /* Regular HID work starts now */
2359 error = hid_hw_start(hdev, connect_mask);
2361 hid_err(hdev, "hw start failed\n");
2366 /* Note that if query fails it is not a hard failure */
2367 wacom_query_tablet_data(wacom);
2370 /* touch only Bamboo doesn't support pen */
2371 if ((features->type == BAMBOO_TOUCH) &&
2372 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2373 cancel_delayed_work_sync(&wacom->init_work);
2374 _wacom_query_tablet_data(wacom);
2379 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2380 error = hid_hw_open(hdev);
2382 wacom_set_shared_values(wacom_wac);
2383 devres_close_group(&hdev->dev, wacom);
2390 wacom_release_resources(wacom);
2394 static void wacom_wireless_work(struct work_struct *work)
2396 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2397 struct usb_device *usbdev = wacom->usbdev;
2398 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2399 struct hid_device *hdev1, *hdev2;
2400 struct wacom *wacom1, *wacom2;
2401 struct wacom_wac *wacom_wac1, *wacom_wac2;
2405 * Regardless if this is a disconnect or a new tablet,
2406 * remove any existing input and battery devices.
2409 wacom_destroy_battery(wacom);
2411 /* Stylus interface */
2412 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2413 wacom1 = hid_get_drvdata(hdev1);
2414 wacom_wac1 = &(wacom1->wacom_wac);
2415 wacom_release_resources(wacom1);
2417 /* Touch interface */
2418 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2419 wacom2 = hid_get_drvdata(hdev2);
2420 wacom_wac2 = &(wacom2->wacom_wac);
2421 wacom_release_resources(wacom2);
2423 if (wacom_wac->pid == 0) {
2424 hid_info(wacom->hdev, "wireless tablet disconnected\n");
2426 const struct hid_device_id *id = wacom_ids;
2428 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2432 if (id->vendor == USB_VENDOR_ID_WACOM &&
2433 id->product == wacom_wac->pid)
2439 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2443 /* Stylus interface */
2444 wacom_wac1->features =
2445 *((struct wacom_features *)id->driver_data);
2447 wacom_wac1->pid = wacom_wac->pid;
2449 error = wacom_parse_and_register(wacom1, true);
2453 /* Touch interface */
2454 if (wacom_wac1->features.touch_max ||
2455 (wacom_wac1->features.type >= INTUOSHT &&
2456 wacom_wac1->features.type <= BAMBOO_PT)) {
2457 wacom_wac2->features =
2458 *((struct wacom_features *)id->driver_data);
2459 wacom_wac2->pid = wacom_wac->pid;
2461 error = wacom_parse_and_register(wacom2, true);
2466 strlcpy(wacom_wac->name, wacom_wac1->name,
2467 sizeof(wacom_wac->name));
2468 error = wacom_initialize_battery(wacom);
2476 wacom_release_resources(wacom1);
2477 wacom_release_resources(wacom2);
2481 static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2483 struct wacom_remote *remote = wacom->remote;
2484 u32 serial = remote->remotes[index].serial;
2486 unsigned long flags;
2488 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2489 if (remote->remotes[i].serial == serial) {
2491 spin_lock_irqsave(&remote->remote_lock, flags);
2492 remote->remotes[i].registered = false;
2493 spin_unlock_irqrestore(&remote->remote_lock, flags);
2495 if (remote->remotes[i].battery.battery)
2496 devres_release_group(&wacom->hdev->dev,
2497 &remote->remotes[i].battery.bat_desc);
2499 if (remote->remotes[i].group.name)
2500 devres_release_group(&wacom->hdev->dev,
2501 &remote->remotes[i]);
2503 remote->remotes[i].serial = 0;
2504 remote->remotes[i].group.name = NULL;
2505 remote->remotes[i].battery.battery = NULL;
2506 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2511 static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2514 struct wacom_remote *remote = wacom->remote;
2515 struct device *dev = &wacom->hdev->dev;
2518 /* A remote can pair more than once with an EKR,
2519 * check to make sure this serial isn't already paired.
2521 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2522 if (remote->remotes[k].serial == serial)
2526 if (k < WACOM_MAX_REMOTES) {
2527 remote->remotes[index].serial = serial;
2531 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2534 error = wacom_remote_create_attr_group(wacom, serial, index);
2538 remote->remotes[index].input = wacom_allocate_input(wacom);
2539 if (!remote->remotes[index].input) {
2543 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2544 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2546 if (!remote->remotes[index].input->name) {
2551 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2556 remote->remotes[index].serial = serial;
2558 error = input_register_device(remote->remotes[index].input);
2562 error = wacom_led_groups_alloc_and_register_one(
2563 &remote->remotes[index].input->dev,
2564 wacom, index, 3, true);
2568 remote->remotes[index].registered = true;
2570 devres_close_group(dev, &remote->remotes[index]);
2574 devres_release_group(dev, &remote->remotes[index]);
2575 remote->remotes[index].serial = 0;
2579 static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2581 struct wacom_remote *remote = wacom->remote;
2584 if (!remote->remotes[index].registered)
2587 if (remote->remotes[index].battery.battery)
2590 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2593 error = __wacom_initialize_battery(wacom,
2594 &wacom->remote->remotes[index].battery);
2601 static void wacom_remote_work(struct work_struct *work)
2603 struct wacom *wacom = container_of(work, struct wacom, remote_work);
2604 struct wacom_remote *remote = wacom->remote;
2605 struct wacom_remote_data data;
2606 unsigned long flags;
2611 spin_lock_irqsave(&remote->remote_lock, flags);
2613 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2615 if (count != sizeof(data)) {
2616 hid_err(wacom->hdev,
2617 "workitem triggered without status available\n");
2618 spin_unlock_irqrestore(&remote->remote_lock, flags);
2622 if (!kfifo_is_empty(&remote->remote_fifo))
2623 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2625 spin_unlock_irqrestore(&remote->remote_lock, flags);
2627 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2628 serial = data.remote[i].serial;
2629 if (data.remote[i].connected) {
2631 if (remote->remotes[i].serial == serial) {
2632 wacom_remote_attach_battery(wacom, i);
2636 if (remote->remotes[i].serial)
2637 wacom_remote_destroy_one(wacom, i);
2639 wacom_remote_create_one(wacom, serial, i);
2641 } else if (remote->remotes[i].serial) {
2642 wacom_remote_destroy_one(wacom, i);
2647 static void wacom_mode_change_work(struct work_struct *work)
2649 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2650 struct wacom_shared *shared = wacom->wacom_wac.shared;
2651 struct wacom *wacom1 = NULL;
2652 struct wacom *wacom2 = NULL;
2653 bool is_direct = wacom->wacom_wac.is_direct_mode;
2657 wacom1 = hid_get_drvdata(shared->pen);
2658 wacom_release_resources(wacom1);
2659 hid_hw_stop(wacom1->hdev);
2660 wacom1->wacom_wac.has_mode_change = true;
2661 wacom1->wacom_wac.is_direct_mode = is_direct;
2664 if (shared->touch) {
2665 wacom2 = hid_get_drvdata(shared->touch);
2666 wacom_release_resources(wacom2);
2667 hid_hw_stop(wacom2->hdev);
2668 wacom2->wacom_wac.has_mode_change = true;
2669 wacom2->wacom_wac.is_direct_mode = is_direct;
2673 error = wacom_parse_and_register(wacom1, false);
2679 error = wacom_parse_and_register(wacom2, false);
2687 static int wacom_probe(struct hid_device *hdev,
2688 const struct hid_device_id *id)
2690 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2691 struct usb_device *dev = interface_to_usbdev(intf);
2692 struct wacom *wacom;
2693 struct wacom_wac *wacom_wac;
2694 struct wacom_features *features;
2697 if (!id->driver_data)
2700 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2702 /* hid-core sets this quirk for the boot interface */
2703 hdev->quirks &= ~HID_QUIRK_NOGET;
2705 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2709 hid_set_drvdata(hdev, wacom);
2712 wacom_wac = &wacom->wacom_wac;
2713 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2714 features = &wacom_wac->features;
2716 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2721 error = kfifo_alloc(&wacom_wac->pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
2725 wacom_wac->hid_data.inputmode = -1;
2726 wacom_wac->mode_report = -1;
2728 wacom->usbdev = dev;
2730 mutex_init(&wacom->lock);
2731 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
2732 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2733 INIT_WORK(&wacom->battery_work, wacom_battery_work);
2734 INIT_WORK(&wacom->remote_work, wacom_remote_work);
2735 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
2737 /* ask for the report descriptor to be loaded by HID */
2738 error = hid_parse(hdev);
2740 hid_err(hdev, "parse failed\n");
2744 error = wacom_parse_and_register(wacom, false);
2748 if (hdev->bus == BUS_BLUETOOTH) {
2749 error = device_create_file(&hdev->dev, &dev_attr_speed);
2752 "can't create sysfs speed attribute err: %d\n",
2759 hid_set_drvdata(hdev, NULL);
2763 static void wacom_remove(struct hid_device *hdev)
2765 struct wacom *wacom = hid_get_drvdata(hdev);
2766 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2767 struct wacom_features *features = &wacom_wac->features;
2769 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2774 cancel_delayed_work_sync(&wacom->init_work);
2775 cancel_work_sync(&wacom->wireless_work);
2776 cancel_work_sync(&wacom->battery_work);
2777 cancel_work_sync(&wacom->remote_work);
2778 cancel_work_sync(&wacom->mode_change_work);
2779 if (hdev->bus == BUS_BLUETOOTH)
2780 device_remove_file(&hdev->dev, &dev_attr_speed);
2782 /* make sure we don't trigger the LEDs */
2783 wacom_led_groups_release(wacom);
2785 if (wacom->wacom_wac.features.type != REMOTE)
2786 wacom_release_resources(wacom);
2788 kfifo_free(&wacom_wac->pen_fifo);
2790 hid_set_drvdata(hdev, NULL);
2794 static int wacom_resume(struct hid_device *hdev)
2796 struct wacom *wacom = hid_get_drvdata(hdev);
2798 mutex_lock(&wacom->lock);
2800 /* switch to wacom mode first */
2801 _wacom_query_tablet_data(wacom);
2802 wacom_led_control(wacom);
2804 mutex_unlock(&wacom->lock);
2809 static int wacom_reset_resume(struct hid_device *hdev)
2811 return wacom_resume(hdev);
2813 #endif /* CONFIG_PM */
2815 static struct hid_driver wacom_driver = {
2817 .id_table = wacom_ids,
2818 .probe = wacom_probe,
2819 .remove = wacom_remove,
2820 .report = wacom_wac_report,
2822 .resume = wacom_resume,
2823 .reset_resume = wacom_reset_resume,
2825 .raw_event = wacom_raw_event,
2827 module_hid_driver(wacom_driver);
2829 MODULE_VERSION(DRIVER_VERSION);
2830 MODULE_AUTHOR(DRIVER_AUTHOR);
2831 MODULE_DESCRIPTION(DRIVER_DESC);
2832 MODULE_LICENSE("GPL");