2 * drivers/input/tablet/wacom_sys.c
4 * USB Wacom tablet support - system specific code
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include "wacom_wac.h"
16 #include <linux/input/mt.h>
18 #define WAC_MSG_RETRIES 5
19 #define WAC_CMD_RETRIES 10
21 #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
22 #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
23 #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
25 static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
26 size_t size, unsigned int retries)
31 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
33 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
36 hid_err(hdev, "wacom_get_report: ran out of retries "
37 "(last error = %d)\n", retval);
42 static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
43 size_t size, unsigned int retries)
48 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
50 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
53 hid_err(hdev, "wacom_set_report: ran out of retries "
54 "(last error = %d)\n", retval);
59 static void wacom_wac_queue_insert(struct hid_device *hdev,
60 struct kfifo_rec_ptr_2 *fifo,
61 u8 *raw_data, int size)
65 while (kfifo_avail(fifo) < size) {
67 hid_warn(hdev, "%s: kfifo has filled, starting to drop events\n", __func__);
73 kfifo_in(fifo, raw_data, size);
76 static void wacom_wac_queue_flush(struct hid_device *hdev,
77 struct kfifo_rec_ptr_2 *fifo)
79 while (!kfifo_is_empty(fifo)) {
80 u8 buf[WACOM_PKGLEN_MAX];
84 size = kfifo_out(fifo, buf, sizeof(buf));
85 err = hid_report_raw_event(hdev, HID_INPUT_REPORT, buf, size, false);
87 hid_warn(hdev, "%s: unable to flush event due to error %d\n",
93 static int wacom_wac_pen_serial_enforce(struct hid_device *hdev,
94 struct hid_report *report, u8 *raw_data, int report_size)
96 struct wacom *wacom = hid_get_drvdata(hdev);
97 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
98 struct wacom_features *features = &wacom_wac->features;
103 if (wacom_wac->serial[0] || !(features->quirks & WACOM_QUIRK_TOOLSERIAL))
106 /* Queue events which have invalid tool type or serial number */
107 for (i = 0; i < report->maxfield; i++) {
108 for (j = 0; j < report->field[i]->maxusage; j++) {
109 struct hid_field *field = report->field[i];
110 struct hid_usage *usage = &field->usage[j];
111 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
116 if (equivalent_usage != HID_DG_INRANGE &&
117 equivalent_usage != HID_DG_TOOLSERIALNUMBER &&
118 equivalent_usage != WACOM_HID_WD_SERIALHI &&
119 equivalent_usage != WACOM_HID_WD_TOOLTYPE)
122 offset = field->report_offset;
123 size = field->report_size;
124 value = hid_field_extract(hdev, raw_data+1, offset + j * size, size);
126 /* If we go out of range, we need to flush the queue ASAP */
127 if (equivalent_usage == HID_DG_INRANGE)
132 switch (equivalent_usage) {
133 case HID_DG_TOOLSERIALNUMBER:
134 wacom_wac->serial[0] = value;
137 case WACOM_HID_WD_SERIALHI:
138 wacom_wac->serial[0] |= ((__u64)value) << 32;
141 case WACOM_HID_WD_TOOLTYPE:
142 wacom_wac->id[0] = value;
153 wacom_wac_queue_flush(hdev, wacom_wac->pen_fifo);
155 wacom_wac_queue_insert(hdev, wacom_wac->pen_fifo,
156 raw_data, report_size);
158 return insert && !flush;
161 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
162 u8 *raw_data, int size)
164 struct wacom *wacom = hid_get_drvdata(hdev);
166 if (size > WACOM_PKGLEN_MAX)
169 if (wacom_wac_pen_serial_enforce(hdev, report, raw_data, size))
172 memcpy(wacom->wacom_wac.data, raw_data, size);
174 wacom_wac_irq(&wacom->wacom_wac, size);
179 static int wacom_open(struct input_dev *dev)
181 struct wacom *wacom = input_get_drvdata(dev);
183 return hid_hw_open(wacom->hdev);
186 static void wacom_close(struct input_dev *dev)
188 struct wacom *wacom = input_get_drvdata(dev);
191 * wacom->hdev should never be null, but surprisingly, I had the case
192 * once while unplugging the Wacom Wireless Receiver.
195 hid_hw_close(wacom->hdev);
199 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
201 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
202 unsigned unit, int exponent)
204 struct hid_field field = {
205 .logical_maximum = logical_extents,
206 .physical_maximum = physical_extents,
208 .unit_exponent = exponent,
211 return hidinput_calc_abs_res(&field, ABS_X);
214 static void wacom_hid_usage_quirk(struct hid_device *hdev,
215 struct hid_field *field, struct hid_usage *usage)
217 struct wacom *wacom = hid_get_drvdata(hdev);
218 struct wacom_features *features = &wacom->wacom_wac.features;
219 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
222 * The Dell Canvas 27 needs to be switched to its vendor-defined
223 * report to provide the best resolution.
225 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
226 hdev->product == 0x4200 &&
227 field->application == HID_UP_MSVENDOR) {
228 wacom->wacom_wac.mode_report = field->report->id;
229 wacom->wacom_wac.mode_value = 2;
233 * ISDv4 devices which predate HID's adoption of the
234 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
235 * position instead. We can accurately detect if a
236 * usage with that value should be HID_DG_BARRELSWITCH2
237 * based on the surrounding usages, which have remained
238 * constant across generations.
240 if (features->type == HID_GENERIC &&
241 usage->hid == 0x000D0000 &&
242 field->application == HID_DG_PEN &&
243 field->physical == HID_DG_STYLUS) {
244 int i = usage->usage_index;
246 if (i-4 >= 0 && i+1 < field->maxusage &&
247 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
248 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
249 field->usage[i-2].hid == HID_DG_ERASER &&
250 field->usage[i-1].hid == HID_DG_INVERT &&
251 field->usage[i+1].hid == HID_DG_INRANGE) {
252 usage->hid = HID_DG_BARRELSWITCH2;
256 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
257 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
258 hdev->product == 0x0358 &&
259 WACOM_PEN_FIELD(field) &&
260 equivalent_usage == HID_GD_Y) {
261 field->logical_maximum = 43200;
265 static void wacom_feature_mapping(struct hid_device *hdev,
266 struct hid_field *field, struct hid_usage *usage)
268 struct wacom *wacom = hid_get_drvdata(hdev);
269 struct wacom_features *features = &wacom->wacom_wac.features;
270 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
271 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
276 wacom_hid_usage_quirk(hdev, field, usage);
278 switch (equivalent_usage) {
279 case WACOM_HID_WD_TOUCH_RING_SETTING:
280 wacom->generic_has_leds = true;
282 case HID_DG_CONTACTMAX:
283 /* leave touch_max as is if predefined */
284 if (!features->touch_max) {
286 n = hid_report_len(field->report);
287 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
290 data[0] = field->report->id;
291 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
292 data, n, WAC_CMD_RETRIES);
293 if (ret == n && features->type == HID_GENERIC) {
294 ret = hid_report_raw_event(hdev,
295 HID_FEATURE_REPORT, data, n, 0);
296 } else if (ret == 2 && features->type != HID_GENERIC) {
297 features->touch_max = data[1];
299 features->touch_max = 16;
300 hid_warn(hdev, "wacom_feature_mapping: "
301 "could not get HID_DG_CONTACTMAX, "
302 "defaulting to %d\n",
303 features->touch_max);
308 case HID_DG_INPUTMODE:
309 /* Ignore if value index is out of bounds. */
310 if (usage->usage_index >= field->report_count) {
311 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
315 hid_data->inputmode = field->report->id;
316 hid_data->inputmode_index = usage->usage_index;
319 case HID_UP_DIGITIZER:
320 if (field->report->id == 0x0B &&
321 (field->application == WACOM_HID_G9_PEN ||
322 field->application == WACOM_HID_G11_PEN)) {
323 wacom->wacom_wac.mode_report = field->report->id;
324 wacom->wacom_wac.mode_value = 0;
328 case WACOM_HID_WD_DATAMODE:
329 wacom->wacom_wac.mode_report = field->report->id;
330 wacom->wacom_wac.mode_value = 2;
333 case WACOM_HID_UP_G9:
334 case WACOM_HID_UP_G11:
335 if (field->report->id == 0x03 &&
336 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
337 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
338 wacom->wacom_wac.mode_report = field->report->id;
339 wacom->wacom_wac.mode_value = 0;
342 case WACOM_HID_WD_OFFSETLEFT:
343 case WACOM_HID_WD_OFFSETTOP:
344 case WACOM_HID_WD_OFFSETRIGHT:
345 case WACOM_HID_WD_OFFSETBOTTOM:
347 n = hid_report_len(field->report);
348 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
351 data[0] = field->report->id;
352 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
353 data, n, WAC_CMD_RETRIES);
355 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
358 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
367 * Interface Descriptor of wacom devices can be incomplete and
368 * inconsistent so wacom_features table is used to store stylus
369 * device's packet lengths, various maximum values, and tablet
370 * resolution based on product ID's.
372 * For devices that contain 2 interfaces, wacom_features table is
373 * inaccurate for the touch interface. Since the Interface Descriptor
374 * for touch interfaces has pretty complete data, this function exists
375 * to query tablet for this missing information instead of hard coding in
376 * an additional table.
378 * A typical Interface Descriptor for a stylus will contain a
379 * boot mouse application collection that is not of interest and this
380 * function will ignore it.
382 * It also contains a digitizer application collection that also is not
383 * of interest since any information it contains would be duplicate
384 * of what is in wacom_features. Usually it defines a report of an array
385 * of bytes that could be used as max length of the stylus packet returned.
386 * If it happens to define a Digitizer-Stylus Physical Collection then
387 * the X and Y logical values contain valid data but it is ignored.
389 * A typical Interface Descriptor for a touch interface will contain a
390 * Digitizer-Finger Physical Collection which will define both logical
391 * X/Y maximum as well as the physical size of tablet. Since touch
392 * interfaces haven't supported pressure or distance, this is enough
393 * information to override invalid values in the wacom_features table.
395 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
396 * data. We deal with them after returning from this function.
398 static void wacom_usage_mapping(struct hid_device *hdev,
399 struct hid_field *field, struct hid_usage *usage)
401 struct wacom *wacom = hid_get_drvdata(hdev);
402 struct wacom_features *features = &wacom->wacom_wac.features;
403 bool finger = WACOM_FINGER_FIELD(field);
404 bool pen = WACOM_PEN_FIELD(field);
405 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
408 * Requiring Stylus Usage will ignore boot mouse
409 * X/Y values and some cases of invalid Digitizer X/Y
410 * values commonly reported.
413 features->device_type |= WACOM_DEVICETYPE_PEN;
415 features->device_type |= WACOM_DEVICETYPE_TOUCH;
419 wacom_hid_usage_quirk(hdev, field, usage);
421 switch (equivalent_usage) {
423 features->x_max = field->logical_maximum;
425 features->x_phy = field->physical_maximum;
426 if ((features->type != BAMBOO_PT) &&
427 (features->type != BAMBOO_TOUCH)) {
428 features->unit = field->unit;
429 features->unitExpo = field->unit_exponent;
434 features->y_max = field->logical_maximum;
436 features->y_phy = field->physical_maximum;
437 if ((features->type != BAMBOO_PT) &&
438 (features->type != BAMBOO_TOUCH)) {
439 features->unit = field->unit;
440 features->unitExpo = field->unit_exponent;
444 case HID_DG_TIPPRESSURE:
446 features->pressure_max = field->logical_maximum;
450 if (features->type == HID_GENERIC)
451 wacom_wac_usage_mapping(hdev, field, usage);
454 static void wacom_post_parse_hid(struct hid_device *hdev,
455 struct wacom_features *features)
457 struct wacom *wacom = hid_get_drvdata(hdev);
458 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
460 if (features->type == HID_GENERIC) {
461 /* Any last-minute generic device setup */
462 if (wacom_wac->has_mode_change) {
463 if (wacom_wac->is_direct_mode)
464 features->device_type |= WACOM_DEVICETYPE_DIRECT;
466 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
469 if (features->touch_max > 1) {
470 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
471 input_mt_init_slots(wacom_wac->touch_input,
472 wacom_wac->features.touch_max,
475 input_mt_init_slots(wacom_wac->touch_input,
476 wacom_wac->features.touch_max,
482 static void wacom_parse_hid(struct hid_device *hdev,
483 struct wacom_features *features)
485 struct hid_report_enum *rep_enum;
486 struct hid_report *hreport;
489 /* check features first */
490 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
491 list_for_each_entry(hreport, &rep_enum->report_list, list) {
492 for (i = 0; i < hreport->maxfield; i++) {
493 /* Ignore if report count is out of bounds. */
494 if (hreport->field[i]->report_count < 1)
497 for (j = 0; j < hreport->field[i]->maxusage; j++) {
498 wacom_feature_mapping(hdev, hreport->field[i],
499 hreport->field[i]->usage + j);
504 /* now check the input usages */
505 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
506 list_for_each_entry(hreport, &rep_enum->report_list, list) {
508 if (!hreport->maxfield)
511 for (i = 0; i < hreport->maxfield; i++)
512 for (j = 0; j < hreport->field[i]->maxusage; j++)
513 wacom_usage_mapping(hdev, hreport->field[i],
514 hreport->field[i]->usage + j);
517 wacom_post_parse_hid(hdev, features);
520 static int wacom_hid_set_device_mode(struct hid_device *hdev)
522 struct wacom *wacom = hid_get_drvdata(hdev);
523 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
524 struct hid_report *r;
525 struct hid_report_enum *re;
527 if (hid_data->inputmode < 0)
530 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
531 r = re->report_id_hash[hid_data->inputmode];
533 r->field[0]->value[hid_data->inputmode_index] = 2;
534 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
539 static int wacom_set_device_mode(struct hid_device *hdev,
540 struct wacom_wac *wacom_wac)
543 struct hid_report *r;
544 struct hid_report_enum *re;
546 int error = -ENOMEM, limit = 0;
548 if (wacom_wac->mode_report < 0)
551 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
552 r = re->report_id_hash[wacom_wac->mode_report];
556 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
560 length = hid_report_len(r);
563 rep_data[0] = wacom_wac->mode_report;
564 rep_data[1] = wacom_wac->mode_value;
566 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
569 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
570 rep_data, length, 1);
571 } while (error >= 0 &&
572 rep_data[1] != wacom_wac->mode_report &&
573 limit++ < WAC_MSG_RETRIES);
577 return error < 0 ? error : 0;
580 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
581 struct wacom_features *features)
583 struct wacom *wacom = hid_get_drvdata(hdev);
587 switch (features->type) {
591 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
595 rep_data[0] = speed == 0 ? 0x05 : 0x06;
598 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
602 wacom->wacom_wac.bt_high_speed = speed;
608 * Note that if the raw queries fail, it's not a hard failure
609 * and it is safe to continue
611 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
616 wacom->wacom_wac.bt_features &= ~0x20;
618 wacom->wacom_wac.bt_features |= 0x20;
621 rep_data[1] = wacom->wacom_wac.bt_features;
623 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
626 wacom->wacom_wac.bt_high_speed = speed;
634 * Switch the tablet into its most-capable mode. Wacom tablets are
635 * typically configured to power-up in a mode which sends mouse-like
636 * reports to the OS. To get absolute position, pressure data, etc.
637 * from the tablet, it is necessary to switch the tablet out of this
638 * mode and into one which sends the full range of tablet data.
640 static int _wacom_query_tablet_data(struct wacom *wacom)
642 struct hid_device *hdev = wacom->hdev;
643 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
644 struct wacom_features *features = &wacom_wac->features;
646 if (hdev->bus == BUS_BLUETOOTH)
647 return wacom_bt_query_tablet_data(hdev, 1, features);
649 if (features->type != HID_GENERIC) {
650 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
651 if (features->type > TABLETPC) {
652 /* MT Tablet PC touch */
653 wacom_wac->mode_report = 3;
654 wacom_wac->mode_value = 4;
655 } else if (features->type == WACOM_24HDT) {
656 wacom_wac->mode_report = 18;
657 wacom_wac->mode_value = 2;
658 } else if (features->type == WACOM_27QHDT) {
659 wacom_wac->mode_report = 131;
660 wacom_wac->mode_value = 2;
661 } else if (features->type == BAMBOO_PAD) {
662 wacom_wac->mode_report = 2;
663 wacom_wac->mode_value = 2;
665 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
666 if (features->type <= BAMBOO_PT) {
667 wacom_wac->mode_report = 2;
668 wacom_wac->mode_value = 2;
673 wacom_set_device_mode(hdev, wacom_wac);
675 if (features->type == HID_GENERIC)
676 return wacom_hid_set_device_mode(hdev);
681 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
682 struct wacom_features *features)
684 struct wacom *wacom = hid_get_drvdata(hdev);
685 struct usb_interface *intf = wacom->intf;
687 /* default features */
688 features->x_fuzz = 4;
689 features->y_fuzz = 4;
690 features->pressure_fuzz = 0;
691 features->distance_fuzz = 1;
692 features->tilt_fuzz = 1;
695 * The wireless device HID is basic and layout conflicts with
696 * other tablets (monitor and touch interface can look like pen).
697 * Skip the query for this type and modify defaults based on
700 if (features->type == WIRELESS && intf) {
701 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
702 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
704 features->device_type = WACOM_DEVICETYPE_NONE;
708 wacom_parse_hid(hdev, features);
711 struct wacom_hdev_data {
712 struct list_head list;
714 struct hid_device *dev;
715 struct wacom_shared shared;
718 static LIST_HEAD(wacom_udev_list);
719 static DEFINE_MUTEX(wacom_udev_list_lock);
721 static bool wacom_are_sibling(struct hid_device *hdev,
722 struct hid_device *sibling)
724 struct wacom *wacom = hid_get_drvdata(hdev);
725 struct wacom_features *features = &wacom->wacom_wac.features;
726 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
727 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
728 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
729 __u32 oPid = features->oPid ? features->oPid : hdev->product;
731 /* The defined oVid/oPid must match that of the sibling */
732 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
734 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
738 * Devices with the same VID/PID must share the same physical
739 * device path, while those with different VID/PID must share
740 * the same physical parent device path.
742 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
743 if (!hid_compare_device_paths(hdev, sibling, '/'))
746 if (!hid_compare_device_paths(hdev, sibling, '.'))
750 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
751 if (features->type != HID_GENERIC)
755 * Direct-input devices may not be siblings of indirect-input
758 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
759 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
763 * Indirect-input devices may not be siblings of direct-input
766 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
767 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
770 /* Pen devices may only be siblings of touch devices */
771 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
772 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
775 /* Touch devices may only be siblings of pen devices */
776 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
777 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
781 * No reason could be found for these two devices to NOT be
782 * siblings, so there's a good chance they ARE siblings
787 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
789 struct wacom_hdev_data *data;
791 /* Try to find an already-probed interface from the same device */
792 list_for_each_entry(data, &wacom_udev_list, list) {
793 if (hid_compare_device_paths(hdev, data->dev, '/')) {
794 kref_get(&data->kref);
799 /* Fallback to finding devices that appear to be "siblings" */
800 list_for_each_entry(data, &wacom_udev_list, list) {
801 if (wacom_are_sibling(hdev, data->dev)) {
802 kref_get(&data->kref);
810 static void wacom_release_shared_data(struct kref *kref)
812 struct wacom_hdev_data *data =
813 container_of(kref, struct wacom_hdev_data, kref);
815 mutex_lock(&wacom_udev_list_lock);
816 list_del(&data->list);
817 mutex_unlock(&wacom_udev_list_lock);
822 static void wacom_remove_shared_data(void *res)
824 struct wacom *wacom = res;
825 struct wacom_hdev_data *data;
826 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
828 if (wacom_wac->shared) {
829 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
832 if (wacom_wac->shared->touch == wacom->hdev)
833 wacom_wac->shared->touch = NULL;
834 else if (wacom_wac->shared->pen == wacom->hdev)
835 wacom_wac->shared->pen = NULL;
837 kref_put(&data->kref, wacom_release_shared_data);
838 wacom_wac->shared = NULL;
842 static int wacom_add_shared_data(struct hid_device *hdev)
844 struct wacom *wacom = hid_get_drvdata(hdev);
845 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
846 struct wacom_hdev_data *data;
849 mutex_lock(&wacom_udev_list_lock);
851 data = wacom_get_hdev_data(hdev);
853 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
859 kref_init(&data->kref);
861 list_add_tail(&data->list, &wacom_udev_list);
864 wacom_wac->shared = &data->shared;
866 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
868 mutex_unlock(&wacom_udev_list_lock);
869 wacom_remove_shared_data(wacom);
873 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
874 wacom_wac->shared->touch = hdev;
875 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
876 wacom_wac->shared->pen = hdev;
879 mutex_unlock(&wacom_udev_list_lock);
883 static int wacom_led_control(struct wacom *wacom)
887 unsigned char report_id = WAC_CMD_LED_CONTROL;
890 if (!wacom->led.groups)
893 if (wacom->wacom_wac.features.type == REMOTE)
896 if (wacom->wacom_wac.pid) { /* wireless connected */
897 report_id = WAC_CMD_WL_LED_CONTROL;
900 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
901 report_id = WAC_CMD_WL_INTUOSP2;
904 buf = kzalloc(buf_size, GFP_KERNEL);
908 if (wacom->wacom_wac.features.type == HID_GENERIC) {
909 buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
910 buf[1] = wacom->led.llv;
911 buf[2] = wacom->led.groups[0].select & 0x03;
913 } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
914 wacom->wacom_wac.features.type <= INTUOSPL)) {
916 * Touch Ring and crop mark LED luminance may take on
917 * one of four values:
918 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
920 int ring_led = wacom->led.groups[0].select & 0x03;
921 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
923 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
926 if (wacom->wacom_wac.pid) {
927 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
928 buf, buf_size, WAC_CMD_RETRIES);
934 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
936 buf[4] = 100; // Power Connection LED (ORANGE)
937 buf[5] = 100; // BT Connection LED (BLUE)
938 buf[6] = 100; // Paper Mode (RED?)
939 buf[7] = 100; // Paper Mode (GREEN?)
940 buf[8] = 100; // Paper Mode (BLUE?)
941 buf[9] = wacom->led.llv;
942 buf[10] = wacom->led.groups[0].select & 0x03;
945 int led = wacom->led.groups[0].select | 0x4;
947 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
948 wacom->wacom_wac.features.type == WACOM_24HD)
949 led |= (wacom->led.groups[1].select << 4) | 0x40;
953 buf[2] = wacom->led.llv;
954 buf[3] = wacom->led.hlv;
955 buf[4] = wacom->led.img_lum;
958 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
965 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
966 const unsigned len, const void *img)
970 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
972 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
976 /* Send 'start' command */
977 buf[0] = WAC_CMD_ICON_START;
979 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
985 buf[1] = button_id & 0x07;
986 for (i = 0; i < 4; i++) {
988 memcpy(buf + 3, img + i * chunk_len, chunk_len);
990 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
991 buf, chunk_len + 3, WAC_CMD_RETRIES);
997 buf[0] = WAC_CMD_ICON_START;
999 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1007 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
1008 const char *buf, size_t count)
1010 struct hid_device *hdev = to_hid_device(dev);
1011 struct wacom *wacom = hid_get_drvdata(hdev);
1015 err = kstrtouint(buf, 10, &id);
1019 mutex_lock(&wacom->lock);
1021 wacom->led.groups[set_id].select = id & 0x3;
1022 err = wacom_led_control(wacom);
1024 mutex_unlock(&wacom->lock);
1026 return err < 0 ? err : count;
1029 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
1030 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
1031 struct device_attribute *attr, const char *buf, size_t count) \
1033 return wacom_led_select_store(dev, SET_ID, buf, count); \
1035 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
1036 struct device_attribute *attr, char *buf) \
1038 struct hid_device *hdev = to_hid_device(dev);\
1039 struct wacom *wacom = hid_get_drvdata(hdev); \
1040 return scnprintf(buf, PAGE_SIZE, "%d\n", \
1041 wacom->led.groups[SET_ID].select); \
1043 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
1044 wacom_led##SET_ID##_select_show, \
1045 wacom_led##SET_ID##_select_store)
1047 DEVICE_LED_SELECT_ATTR(0);
1048 DEVICE_LED_SELECT_ATTR(1);
1050 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
1051 const char *buf, size_t count)
1056 err = kstrtouint(buf, 10, &value);
1060 mutex_lock(&wacom->lock);
1062 *dest = value & 0x7f;
1063 err = wacom_led_control(wacom);
1065 mutex_unlock(&wacom->lock);
1067 return err < 0 ? err : count;
1070 #define DEVICE_LUMINANCE_ATTR(name, field) \
1071 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
1072 struct device_attribute *attr, const char *buf, size_t count) \
1074 struct hid_device *hdev = to_hid_device(dev);\
1075 struct wacom *wacom = hid_get_drvdata(hdev); \
1077 return wacom_luminance_store(wacom, &wacom->led.field, \
1080 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
1081 struct device_attribute *attr, char *buf) \
1083 struct wacom *wacom = dev_get_drvdata(dev); \
1084 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
1086 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
1087 wacom_##name##_luminance_show, \
1088 wacom_##name##_luminance_store)
1090 DEVICE_LUMINANCE_ATTR(status0, llv);
1091 DEVICE_LUMINANCE_ATTR(status1, hlv);
1092 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
1094 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
1095 const char *buf, size_t count)
1097 struct hid_device *hdev = to_hid_device(dev);
1098 struct wacom *wacom = hid_get_drvdata(hdev);
1103 if (hdev->bus == BUS_BLUETOOTH) {
1105 xfer_id = WAC_CMD_ICON_BT_XFER;
1108 xfer_id = WAC_CMD_ICON_XFER;
1114 mutex_lock(&wacom->lock);
1116 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
1118 mutex_unlock(&wacom->lock);
1120 return err < 0 ? err : count;
1123 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1124 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1125 struct device_attribute *attr, const char *buf, size_t count) \
1127 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1129 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
1130 NULL, wacom_btnimg##BUTTON_ID##_store)
1132 DEVICE_BTNIMG_ATTR(0);
1133 DEVICE_BTNIMG_ATTR(1);
1134 DEVICE_BTNIMG_ATTR(2);
1135 DEVICE_BTNIMG_ATTR(3);
1136 DEVICE_BTNIMG_ATTR(4);
1137 DEVICE_BTNIMG_ATTR(5);
1138 DEVICE_BTNIMG_ATTR(6);
1139 DEVICE_BTNIMG_ATTR(7);
1141 static struct attribute *cintiq_led_attrs[] = {
1142 &dev_attr_status_led0_select.attr,
1143 &dev_attr_status_led1_select.attr,
1147 static struct attribute_group cintiq_led_attr_group = {
1148 .name = "wacom_led",
1149 .attrs = cintiq_led_attrs,
1152 static struct attribute *intuos4_led_attrs[] = {
1153 &dev_attr_status0_luminance.attr,
1154 &dev_attr_status1_luminance.attr,
1155 &dev_attr_status_led0_select.attr,
1156 &dev_attr_buttons_luminance.attr,
1157 &dev_attr_button0_rawimg.attr,
1158 &dev_attr_button1_rawimg.attr,
1159 &dev_attr_button2_rawimg.attr,
1160 &dev_attr_button3_rawimg.attr,
1161 &dev_attr_button4_rawimg.attr,
1162 &dev_attr_button5_rawimg.attr,
1163 &dev_attr_button6_rawimg.attr,
1164 &dev_attr_button7_rawimg.attr,
1168 static struct attribute_group intuos4_led_attr_group = {
1169 .name = "wacom_led",
1170 .attrs = intuos4_led_attrs,
1173 static struct attribute *intuos5_led_attrs[] = {
1174 &dev_attr_status0_luminance.attr,
1175 &dev_attr_status_led0_select.attr,
1179 static struct attribute_group intuos5_led_attr_group = {
1180 .name = "wacom_led",
1181 .attrs = intuos5_led_attrs,
1184 static struct attribute *generic_led_attrs[] = {
1185 &dev_attr_status0_luminance.attr,
1186 &dev_attr_status_led0_select.attr,
1190 static struct attribute_group generic_led_attr_group = {
1191 .name = "wacom_led",
1192 .attrs = generic_led_attrs,
1195 struct wacom_sysfs_group_devres {
1196 struct attribute_group *group;
1197 struct kobject *root;
1200 static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1202 struct wacom_sysfs_group_devres *devres = res;
1203 struct kobject *kobj = devres->root;
1205 dev_dbg(dev, "%s: dropping reference to %s\n",
1206 __func__, devres->group->name);
1207 sysfs_remove_group(kobj, devres->group);
1210 static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1211 struct kobject *root,
1212 struct attribute_group *group)
1214 struct wacom_sysfs_group_devres *devres;
1217 devres = devres_alloc(wacom_devm_sysfs_group_release,
1218 sizeof(struct wacom_sysfs_group_devres),
1223 devres->group = group;
1224 devres->root = root;
1226 error = sysfs_create_group(devres->root, group);
1228 devres_free(devres);
1232 devres_add(&wacom->hdev->dev, devres);
1237 static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1238 struct attribute_group *group)
1240 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1244 static void wacom_devm_kfifo_release(struct device *dev, void *res)
1246 struct kfifo_rec_ptr_2 *devres = res;
1251 static int wacom_devm_kfifo_alloc(struct wacom *wacom)
1253 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1254 struct kfifo_rec_ptr_2 *pen_fifo;
1257 pen_fifo = devres_alloc(wacom_devm_kfifo_release,
1258 sizeof(struct kfifo_rec_ptr_2),
1264 error = kfifo_alloc(pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
1266 devres_free(pen_fifo);
1270 devres_add(&wacom->hdev->dev, pen_fifo);
1271 wacom_wac->pen_fifo = pen_fifo;
1276 enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
1278 struct wacom *wacom = led->wacom;
1280 if (wacom->led.max_hlv)
1281 return led->hlv * LED_FULL / wacom->led.max_hlv;
1283 if (wacom->led.max_llv)
1284 return led->llv * LED_FULL / wacom->led.max_llv;
1286 /* device doesn't support brightness tuning */
1290 static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1292 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1293 struct wacom *wacom = led->wacom;
1295 if (wacom->led.groups[led->group].select != led->id)
1298 return wacom_leds_brightness_get(led);
1301 static int wacom_led_brightness_set(struct led_classdev *cdev,
1302 enum led_brightness brightness)
1304 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1305 struct wacom *wacom = led->wacom;
1308 mutex_lock(&wacom->lock);
1310 if (!wacom->led.groups || (brightness == LED_OFF &&
1311 wacom->led.groups[led->group].select != led->id)) {
1316 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1317 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1319 wacom->led.groups[led->group].select = led->id;
1321 error = wacom_led_control(wacom);
1324 mutex_unlock(&wacom->lock);
1329 static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1330 enum led_brightness brightness)
1334 static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1335 struct wacom_led *led, unsigned int group,
1336 unsigned int id, bool read_only)
1341 name = devm_kasprintf(dev, GFP_KERNEL,
1350 led->trigger.name = name;
1351 error = devm_led_trigger_register(dev, &led->trigger);
1353 hid_err(wacom->hdev,
1354 "failed to register LED trigger %s: %d\n",
1355 led->cdev.name, error);
1363 led->llv = wacom->led.llv;
1364 led->hlv = wacom->led.hlv;
1365 led->cdev.name = name;
1366 led->cdev.max_brightness = LED_FULL;
1367 led->cdev.flags = LED_HW_PLUGGABLE;
1368 led->cdev.brightness_get = __wacom_led_brightness_get;
1370 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
1371 led->cdev.default_trigger = led->cdev.name;
1373 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
1376 error = devm_led_classdev_register(dev, &led->cdev);
1378 hid_err(wacom->hdev,
1379 "failed to register LED %s: %d\n",
1380 led->cdev.name, error);
1381 led->cdev.name = NULL;
1388 static void wacom_led_groups_release_one(void *data)
1390 struct wacom_group_leds *group = data;
1392 devres_release_group(group->dev, group);
1395 static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1396 struct wacom *wacom,
1397 int group_id, int count,
1400 struct wacom_led *leds;
1403 if (group_id >= wacom->led.count || count <= 0)
1406 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1409 leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL);
1415 wacom->led.groups[group_id].leds = leds;
1416 wacom->led.groups[group_id].count = count;
1418 for (i = 0; i < count; i++) {
1419 error = wacom_led_register_one(dev, wacom, &leds[i],
1420 group_id, i, read_only);
1425 wacom->led.groups[group_id].dev = dev;
1427 devres_close_group(dev, &wacom->led.groups[group_id]);
1430 * There is a bug (?) in devm_led_classdev_register() in which its
1431 * increments the refcount of the parent. If the parent is an input
1432 * device, that means the ref count never reaches 0 when
1433 * devm_input_device_release() gets called.
1434 * This means that the LEDs are still there after disconnect.
1435 * Manually force the release of the group so that the leds are released
1436 * once we are done using them.
1438 error = devm_add_action_or_reset(&wacom->hdev->dev,
1439 wacom_led_groups_release_one,
1440 &wacom->led.groups[group_id]);
1447 devres_release_group(dev, &wacom->led.groups[group_id]);
1451 struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1454 struct wacom_group_leds *group;
1456 if (group_id >= wacom->led.count)
1459 group = &wacom->led.groups[group_id];
1466 return &group->leds[id];
1470 * wacom_led_next: gives the next available led with a wacom trigger.
1472 * returns the next available struct wacom_led which has its default trigger
1473 * or the current one if none is available.
1475 struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1477 struct wacom_led *next_led;
1487 next_led = wacom_led_find(wacom, group, ++next);
1488 if (!next_led || next_led == cur)
1490 } while (next_led->cdev.trigger != &next_led->trigger);
1495 static void wacom_led_groups_release(void *data)
1497 struct wacom *wacom = data;
1499 wacom->led.groups = NULL;
1500 wacom->led.count = 0;
1503 static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1505 struct device *dev = &wacom->hdev->dev;
1506 struct wacom_group_leds *groups;
1509 groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds),
1514 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1518 wacom->led.groups = groups;
1519 wacom->led.count = count;
1524 static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1525 int led_per_group, bool read_only)
1530 if (!wacom->wacom_wac.pad_input)
1533 dev = &wacom->wacom_wac.pad_input->dev;
1535 error = wacom_led_groups_allocate(wacom, group_count);
1539 for (i = 0; i < group_count; i++) {
1540 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1550 int wacom_initialize_leds(struct wacom *wacom)
1554 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1557 /* Initialize default values */
1558 switch (wacom->wacom_wac.features.type) {
1560 if (!wacom->generic_has_leds)
1562 wacom->led.llv = 100;
1563 wacom->led.max_llv = 100;
1565 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1567 hid_err(wacom->hdev,
1568 "cannot create leds err: %d\n", error);
1572 error = wacom_devm_sysfs_create_group(wacom,
1573 &generic_led_attr_group);
1580 wacom->led.llv = 10;
1581 wacom->led.hlv = 20;
1582 wacom->led.max_llv = 127;
1583 wacom->led.max_hlv = 127;
1584 wacom->led.img_lum = 10;
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 &intuos4_led_attr_group);
1601 wacom->led.img_lum = 0;
1603 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1605 hid_err(wacom->hdev,
1606 "cannot create leds err: %d\n", error);
1610 error = wacom_devm_sysfs_create_group(wacom,
1611 &cintiq_led_attr_group);
1620 wacom->led.llv = 32;
1621 wacom->led.max_llv = 96;
1623 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1625 hid_err(wacom->hdev,
1626 "cannot create leds err: %d\n", error);
1630 error = wacom_devm_sysfs_create_group(wacom,
1631 &intuos5_led_attr_group);
1635 wacom->led.llv = 50;
1636 wacom->led.max_llv = 100;
1637 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1639 hid_err(wacom->hdev,
1640 "cannot create leds err: %d\n", error);
1646 wacom->led.llv = 255;
1647 wacom->led.max_llv = 255;
1648 error = wacom_led_groups_allocate(wacom, 5);
1650 hid_err(wacom->hdev,
1651 "cannot create leds err: %d\n", error);
1661 hid_err(wacom->hdev,
1662 "cannot create sysfs group err: %d\n", error);
1669 static void wacom_init_work(struct work_struct *work)
1671 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1673 _wacom_query_tablet_data(wacom);
1674 wacom_led_control(wacom);
1677 static void wacom_query_tablet_data(struct wacom *wacom)
1679 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1682 static enum power_supply_property wacom_battery_props[] = {
1683 POWER_SUPPLY_PROP_MODEL_NAME,
1684 POWER_SUPPLY_PROP_PRESENT,
1685 POWER_SUPPLY_PROP_STATUS,
1686 POWER_SUPPLY_PROP_SCOPE,
1687 POWER_SUPPLY_PROP_CAPACITY
1690 static int wacom_battery_get_property(struct power_supply *psy,
1691 enum power_supply_property psp,
1692 union power_supply_propval *val)
1694 struct wacom_battery *battery = power_supply_get_drvdata(psy);
1698 case POWER_SUPPLY_PROP_MODEL_NAME:
1699 val->strval = battery->wacom->wacom_wac.name;
1701 case POWER_SUPPLY_PROP_PRESENT:
1702 val->intval = battery->bat_connected;
1704 case POWER_SUPPLY_PROP_SCOPE:
1705 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1707 case POWER_SUPPLY_PROP_CAPACITY:
1708 val->intval = battery->battery_capacity;
1710 case POWER_SUPPLY_PROP_STATUS:
1711 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1712 val->intval = battery->bat_status;
1713 else if (battery->bat_charging)
1714 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1715 else if (battery->battery_capacity == 100 &&
1716 battery->ps_connected)
1717 val->intval = POWER_SUPPLY_STATUS_FULL;
1718 else if (battery->ps_connected)
1719 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1721 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1731 static int __wacom_initialize_battery(struct wacom *wacom,
1732 struct wacom_battery *battery)
1734 static atomic_t battery_no = ATOMIC_INIT(0);
1735 struct device *dev = &wacom->hdev->dev;
1736 struct power_supply_config psy_cfg = { .drv_data = battery, };
1737 struct power_supply *ps_bat;
1738 struct power_supply_desc *bat_desc = &battery->bat_desc;
1742 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1745 battery->wacom = wacom;
1747 n = atomic_inc_return(&battery_no) - 1;
1749 bat_desc->properties = wacom_battery_props;
1750 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1751 bat_desc->get_property = wacom_battery_get_property;
1752 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1753 bat_desc->name = battery->bat_name;
1754 bat_desc->type = POWER_SUPPLY_TYPE_USB;
1755 bat_desc->use_for_apm = 0;
1757 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1758 if (IS_ERR(ps_bat)) {
1759 error = PTR_ERR(ps_bat);
1763 power_supply_powers(ps_bat, &wacom->hdev->dev);
1765 battery->battery = ps_bat;
1767 devres_close_group(dev, bat_desc);
1771 devres_release_group(dev, bat_desc);
1775 static int wacom_initialize_battery(struct wacom *wacom)
1777 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1778 return __wacom_initialize_battery(wacom, &wacom->battery);
1783 static void wacom_destroy_battery(struct wacom *wacom)
1785 if (wacom->battery.battery) {
1786 devres_release_group(&wacom->hdev->dev,
1787 &wacom->battery.bat_desc);
1788 wacom->battery.battery = NULL;
1792 static ssize_t wacom_show_speed(struct device *dev,
1793 struct device_attribute
1796 struct hid_device *hdev = to_hid_device(dev);
1797 struct wacom *wacom = hid_get_drvdata(hdev);
1799 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1802 static ssize_t wacom_store_speed(struct device *dev,
1803 struct device_attribute *attr,
1804 const char *buf, size_t count)
1806 struct hid_device *hdev = to_hid_device(dev);
1807 struct wacom *wacom = hid_get_drvdata(hdev);
1810 if (kstrtou8(buf, 0, &new_speed))
1813 if (new_speed != 0 && new_speed != 1)
1816 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1821 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1822 wacom_show_speed, wacom_store_speed);
1825 static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1826 struct kobj_attribute *kattr,
1827 char *buf, int index)
1829 struct device *dev = kobj_to_dev(kobj->parent);
1830 struct hid_device *hdev = to_hid_device(dev);
1831 struct wacom *wacom = hid_get_drvdata(hdev);
1834 mode = wacom->led.groups[index].select;
1835 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
1838 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1839 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1840 struct kobj_attribute *kattr, char *buf) \
1842 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1844 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1845 .attr = {.name = "remote_mode", \
1846 .mode = DEV_ATTR_RO_PERM}, \
1847 .show = wacom_show_remote##SET_ID##_mode, \
1849 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1850 &remote##SET_ID##_mode_attr.attr, \
1853 static struct attribute_group remote##SET_ID##_serial_group = { \
1855 .attrs = remote##SET_ID##_serial_attrs, \
1858 DEVICE_EKR_ATTR_GROUP(0);
1859 DEVICE_EKR_ATTR_GROUP(1);
1860 DEVICE_EKR_ATTR_GROUP(2);
1861 DEVICE_EKR_ATTR_GROUP(3);
1862 DEVICE_EKR_ATTR_GROUP(4);
1864 static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1868 struct wacom_remote *remote = wacom->remote;
1870 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1873 if (!remote->remotes[index].group.name)
1876 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1877 &remote->remotes[index].group);
1879 remote->remotes[index].group.name = NULL;
1880 hid_err(wacom->hdev,
1881 "cannot create sysfs group err: %d\n", error);
1888 static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1890 const size_t buf_size = 2;
1894 buf = kzalloc(buf_size, GFP_KERNEL);
1898 buf[0] = WAC_CMD_DELETE_PAIRING;
1901 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1902 buf_size, WAC_CMD_RETRIES);
1908 static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1909 struct kobj_attribute *attr,
1910 const char *buf, size_t count)
1912 unsigned char selector = 0;
1913 struct device *dev = kobj_to_dev(kobj->parent);
1914 struct hid_device *hdev = to_hid_device(dev);
1915 struct wacom *wacom = hid_get_drvdata(hdev);
1918 if (!strncmp(buf, "*\n", 2)) {
1919 selector = WAC_CMD_UNPAIR_ALL;
1921 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1926 mutex_lock(&wacom->lock);
1928 err = wacom_cmd_unpair_remote(wacom, selector);
1929 mutex_unlock(&wacom->lock);
1931 return err < 0 ? err : count;
1934 static struct kobj_attribute unpair_remote_attr = {
1935 .attr = {.name = "unpair_remote", .mode = 0200},
1936 .store = wacom_store_unpair_remote,
1939 static const struct attribute *remote_unpair_attrs[] = {
1940 &unpair_remote_attr.attr,
1944 static void wacom_remotes_destroy(void *data)
1946 struct wacom *wacom = data;
1947 struct wacom_remote *remote = wacom->remote;
1952 kobject_put(remote->remote_dir);
1953 kfifo_free(&remote->remote_fifo);
1954 wacom->remote = NULL;
1957 static int wacom_initialize_remotes(struct wacom *wacom)
1960 struct wacom_remote *remote;
1963 if (wacom->wacom_wac.features.type != REMOTE)
1966 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1971 wacom->remote = remote;
1973 spin_lock_init(&remote->remote_lock);
1975 error = kfifo_alloc(&remote->remote_fifo,
1976 5 * sizeof(struct wacom_remote_data),
1979 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1983 remote->remotes[0].group = remote0_serial_group;
1984 remote->remotes[1].group = remote1_serial_group;
1985 remote->remotes[2].group = remote2_serial_group;
1986 remote->remotes[3].group = remote3_serial_group;
1987 remote->remotes[4].group = remote4_serial_group;
1989 remote->remote_dir = kobject_create_and_add("wacom_remote",
1990 &wacom->hdev->dev.kobj);
1991 if (!remote->remote_dir)
1994 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
1997 hid_err(wacom->hdev,
1998 "cannot create sysfs group err: %d\n", error);
2002 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2003 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2004 remote->remotes[i].serial = 0;
2007 error = devm_add_action_or_reset(&wacom->hdev->dev,
2008 wacom_remotes_destroy, wacom);
2015 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
2017 struct input_dev *input_dev;
2018 struct hid_device *hdev = wacom->hdev;
2019 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2021 input_dev = devm_input_allocate_device(&hdev->dev);
2025 input_dev->name = wacom_wac->features.name;
2026 input_dev->phys = hdev->phys;
2027 input_dev->dev.parent = &hdev->dev;
2028 input_dev->open = wacom_open;
2029 input_dev->close = wacom_close;
2030 input_dev->uniq = hdev->uniq;
2031 input_dev->id.bustype = hdev->bus;
2032 input_dev->id.vendor = hdev->vendor;
2033 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
2034 input_dev->id.version = hdev->version;
2035 input_set_drvdata(input_dev, wacom);
2040 static int wacom_allocate_inputs(struct wacom *wacom)
2042 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2044 wacom_wac->pen_input = wacom_allocate_input(wacom);
2045 wacom_wac->touch_input = wacom_allocate_input(wacom);
2046 wacom_wac->pad_input = wacom_allocate_input(wacom);
2047 if (!wacom_wac->pen_input ||
2048 !wacom_wac->touch_input ||
2049 !wacom_wac->pad_input)
2052 wacom_wac->pen_input->name = wacom_wac->pen_name;
2053 wacom_wac->touch_input->name = wacom_wac->touch_name;
2054 wacom_wac->pad_input->name = wacom_wac->pad_name;
2059 static int wacom_register_inputs(struct wacom *wacom)
2061 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2062 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2065 pen_input_dev = wacom_wac->pen_input;
2066 touch_input_dev = wacom_wac->touch_input;
2067 pad_input_dev = wacom_wac->pad_input;
2069 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
2072 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
2074 /* no pen in use on this interface */
2075 input_free_device(pen_input_dev);
2076 wacom_wac->pen_input = NULL;
2077 pen_input_dev = NULL;
2079 error = input_register_device(pen_input_dev);
2084 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
2086 /* no touch in use on this interface */
2087 input_free_device(touch_input_dev);
2088 wacom_wac->touch_input = NULL;
2089 touch_input_dev = NULL;
2091 error = input_register_device(touch_input_dev);
2096 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2098 /* no pad in use on this interface */
2099 input_free_device(pad_input_dev);
2100 wacom_wac->pad_input = NULL;
2101 pad_input_dev = NULL;
2103 error = input_register_device(pad_input_dev);
2111 wacom_wac->pad_input = NULL;
2112 wacom_wac->touch_input = NULL;
2113 wacom_wac->pen_input = NULL;
2118 * Not all devices report physical dimensions from HID.
2119 * Compute the default from hardcoded logical dimension
2120 * and resolution before driver overwrites them.
2122 static void wacom_set_default_phy(struct wacom_features *features)
2124 if (features->x_resolution) {
2125 features->x_phy = (features->x_max * 100) /
2126 features->x_resolution;
2127 features->y_phy = (features->y_max * 100) /
2128 features->y_resolution;
2132 static void wacom_calculate_res(struct wacom_features *features)
2134 /* set unit to "100th of a mm" for devices not reported by HID */
2135 if (!features->unit) {
2136 features->unit = 0x11;
2137 features->unitExpo = -3;
2140 features->x_resolution = wacom_calc_hid_res(features->x_max,
2143 features->unitExpo);
2144 features->y_resolution = wacom_calc_hid_res(features->y_max,
2147 features->unitExpo);
2150 void wacom_battery_work(struct work_struct *work)
2152 struct wacom *wacom = container_of(work, struct wacom, battery_work);
2154 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2155 !wacom->battery.battery) {
2156 wacom_initialize_battery(wacom);
2158 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2159 wacom->battery.battery) {
2160 wacom_destroy_battery(wacom);
2164 static size_t wacom_compute_pktlen(struct hid_device *hdev)
2166 struct hid_report_enum *report_enum;
2167 struct hid_report *report;
2170 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2172 list_for_each_entry(report, &report_enum->report_list, list) {
2173 size_t report_size = hid_report_len(report);
2174 if (report_size > size)
2181 static void wacom_update_name(struct wacom *wacom, const char *suffix)
2183 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2184 struct wacom_features *features = &wacom_wac->features;
2185 char name[WACOM_NAME_MAX - 20]; /* Leave some room for suffixes */
2187 /* Generic devices name unspecified */
2188 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
2189 char *product_name = wacom->hdev->name;
2191 if (hid_is_usb(wacom->hdev)) {
2192 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2193 struct usb_device *dev = interface_to_usbdev(intf);
2194 product_name = dev->product;
2197 if (wacom->hdev->bus == BUS_I2C) {
2198 snprintf(name, sizeof(name), "%s %X",
2199 features->name, wacom->hdev->product);
2200 } else if (strstr(product_name, "Wacom") ||
2201 strstr(product_name, "wacom") ||
2202 strstr(product_name, "WACOM")) {
2203 strlcpy(name, product_name, sizeof(name));
2205 snprintf(name, sizeof(name), "Wacom %s", product_name);
2208 /* strip out excess whitespaces */
2210 char *gap = strstr(name, " ");
2213 /* shift everything including the terminator */
2214 memmove(gap, gap+1, strlen(gap));
2217 /* get rid of trailing whitespace */
2218 if (name[strlen(name)-1] == ' ')
2219 name[strlen(name)-1] = '\0';
2221 strlcpy(name, features->name, sizeof(name));
2224 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2227 /* Append the device type to the name */
2228 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
2229 "%s%s Pen", name, suffix);
2230 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
2231 "%s%s Finger", name, suffix);
2232 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
2233 "%s%s Pad", name, suffix);
2236 static void wacom_release_resources(struct wacom *wacom)
2238 struct hid_device *hdev = wacom->hdev;
2240 if (!wacom->resources)
2243 devres_release_group(&hdev->dev, wacom);
2245 wacom->resources = false;
2247 wacom->wacom_wac.pen_input = NULL;
2248 wacom->wacom_wac.touch_input = NULL;
2249 wacom->wacom_wac.pad_input = NULL;
2252 static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2254 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2255 wacom_wac->shared->type = wacom_wac->features.type;
2256 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2259 if (wacom_wac->has_mute_touch_switch) {
2260 wacom_wac->shared->has_mute_touch_switch = true;
2261 wacom_wac->shared->is_touch_on = true;
2264 if (wacom_wac->shared->has_mute_touch_switch &&
2265 wacom_wac->shared->touch_input) {
2266 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2267 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2272 static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
2274 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2275 struct wacom_features *features = &wacom_wac->features;
2276 struct hid_device *hdev = wacom->hdev;
2278 unsigned int connect_mask = HID_CONNECT_HIDRAW;
2280 features->pktlen = wacom_compute_pktlen(hdev);
2281 if (features->pktlen > WACOM_PKGLEN_MAX)
2284 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2287 wacom->resources = true;
2289 error = wacom_allocate_inputs(wacom);
2294 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2295 * into debug mode for the touch part.
2296 * We ignore the other interfaces.
2298 if (features->type == BAMBOO_PAD) {
2299 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2300 features->type = HID_GENERIC;
2301 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2302 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2308 /* set the default size in case we do not get them from hid */
2309 wacom_set_default_phy(features);
2311 /* Retrieve the physical and logical size for touch devices */
2312 wacom_retrieve_hid_descriptor(hdev, features);
2313 wacom_setup_device_quirks(wacom);
2315 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2316 features->type != WIRELESS) {
2317 error = features->type == HID_GENERIC ? -ENODEV : 0;
2319 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
2321 error ? "Ignoring" : "Assuming pen");
2326 features->device_type |= WACOM_DEVICETYPE_PEN;
2329 wacom_calculate_res(features);
2331 wacom_update_name(wacom, wireless ? " (WL)" : "");
2333 /* pen only Bamboo neither support touch nor pad */
2334 if ((features->type == BAMBOO_PEN) &&
2335 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2336 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2341 error = wacom_add_shared_data(hdev);
2345 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2346 (features->quirks & WACOM_QUIRK_BATTERY)) {
2347 error = wacom_initialize_battery(wacom);
2352 error = wacom_register_inputs(wacom);
2356 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
2357 error = wacom_initialize_leds(wacom);
2361 error = wacom_initialize_remotes(wacom);
2366 if (features->type == HID_GENERIC)
2367 connect_mask |= HID_CONNECT_DRIVER;
2369 /* Regular HID work starts now */
2370 error = hid_hw_start(hdev, connect_mask);
2372 hid_err(hdev, "hw start failed\n");
2377 /* Note that if query fails it is not a hard failure */
2378 wacom_query_tablet_data(wacom);
2381 /* touch only Bamboo doesn't support pen */
2382 if ((features->type == BAMBOO_TOUCH) &&
2383 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2384 cancel_delayed_work_sync(&wacom->init_work);
2385 _wacom_query_tablet_data(wacom);
2390 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2391 error = hid_hw_open(hdev);
2393 wacom_set_shared_values(wacom_wac);
2394 devres_close_group(&hdev->dev, wacom);
2401 wacom_release_resources(wacom);
2405 static void wacom_wireless_work(struct work_struct *work)
2407 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2408 struct usb_device *usbdev = wacom->usbdev;
2409 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2410 struct hid_device *hdev1, *hdev2;
2411 struct wacom *wacom1, *wacom2;
2412 struct wacom_wac *wacom_wac1, *wacom_wac2;
2416 * Regardless if this is a disconnect or a new tablet,
2417 * remove any existing input and battery devices.
2420 wacom_destroy_battery(wacom);
2425 /* Stylus interface */
2426 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2427 wacom1 = hid_get_drvdata(hdev1);
2428 wacom_wac1 = &(wacom1->wacom_wac);
2429 wacom_release_resources(wacom1);
2431 /* Touch interface */
2432 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2433 wacom2 = hid_get_drvdata(hdev2);
2434 wacom_wac2 = &(wacom2->wacom_wac);
2435 wacom_release_resources(wacom2);
2437 if (wacom_wac->pid == 0) {
2438 hid_info(wacom->hdev, "wireless tablet disconnected\n");
2440 const struct hid_device_id *id = wacom_ids;
2442 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2446 if (id->vendor == USB_VENDOR_ID_WACOM &&
2447 id->product == wacom_wac->pid)
2453 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2457 /* Stylus interface */
2458 wacom_wac1->features =
2459 *((struct wacom_features *)id->driver_data);
2461 wacom_wac1->pid = wacom_wac->pid;
2463 error = wacom_parse_and_register(wacom1, true);
2467 /* Touch interface */
2468 if (wacom_wac1->features.touch_max ||
2469 (wacom_wac1->features.type >= INTUOSHT &&
2470 wacom_wac1->features.type <= BAMBOO_PT)) {
2471 wacom_wac2->features =
2472 *((struct wacom_features *)id->driver_data);
2473 wacom_wac2->pid = wacom_wac->pid;
2475 error = wacom_parse_and_register(wacom2, true);
2480 strlcpy(wacom_wac->name, wacom_wac1->name,
2481 sizeof(wacom_wac->name));
2482 error = wacom_initialize_battery(wacom);
2490 wacom_release_resources(wacom1);
2491 wacom_release_resources(wacom2);
2495 static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2497 struct wacom_remote *remote = wacom->remote;
2498 u32 serial = remote->remotes[index].serial;
2500 unsigned long flags;
2502 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2503 if (remote->remotes[i].serial == serial) {
2505 spin_lock_irqsave(&remote->remote_lock, flags);
2506 remote->remotes[i].registered = false;
2507 spin_unlock_irqrestore(&remote->remote_lock, flags);
2509 if (remote->remotes[i].battery.battery)
2510 devres_release_group(&wacom->hdev->dev,
2511 &remote->remotes[i].battery.bat_desc);
2513 if (remote->remotes[i].group.name)
2514 devres_release_group(&wacom->hdev->dev,
2515 &remote->remotes[i]);
2517 remote->remotes[i].serial = 0;
2518 remote->remotes[i].group.name = NULL;
2519 remote->remotes[i].battery.battery = NULL;
2520 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2525 static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2528 struct wacom_remote *remote = wacom->remote;
2529 struct device *dev = &wacom->hdev->dev;
2532 /* A remote can pair more than once with an EKR,
2533 * check to make sure this serial isn't already paired.
2535 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2536 if (remote->remotes[k].serial == serial)
2540 if (k < WACOM_MAX_REMOTES) {
2541 remote->remotes[index].serial = serial;
2545 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2548 error = wacom_remote_create_attr_group(wacom, serial, index);
2552 remote->remotes[index].input = wacom_allocate_input(wacom);
2553 if (!remote->remotes[index].input) {
2557 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2558 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2560 if (!remote->remotes[index].input->name) {
2565 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2570 remote->remotes[index].serial = serial;
2572 error = input_register_device(remote->remotes[index].input);
2576 error = wacom_led_groups_alloc_and_register_one(
2577 &remote->remotes[index].input->dev,
2578 wacom, index, 3, true);
2582 remote->remotes[index].registered = true;
2584 devres_close_group(dev, &remote->remotes[index]);
2588 devres_release_group(dev, &remote->remotes[index]);
2589 remote->remotes[index].serial = 0;
2593 static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2595 struct wacom_remote *remote = wacom->remote;
2598 if (!remote->remotes[index].registered)
2601 if (remote->remotes[index].battery.battery)
2604 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2607 error = __wacom_initialize_battery(wacom,
2608 &wacom->remote->remotes[index].battery);
2615 static void wacom_remote_work(struct work_struct *work)
2617 struct wacom *wacom = container_of(work, struct wacom, remote_work);
2618 struct wacom_remote *remote = wacom->remote;
2619 struct wacom_remote_data data;
2620 unsigned long flags;
2625 spin_lock_irqsave(&remote->remote_lock, flags);
2627 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2629 if (count != sizeof(data)) {
2630 hid_err(wacom->hdev,
2631 "workitem triggered without status available\n");
2632 spin_unlock_irqrestore(&remote->remote_lock, flags);
2636 if (!kfifo_is_empty(&remote->remote_fifo))
2637 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2639 spin_unlock_irqrestore(&remote->remote_lock, flags);
2641 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2642 serial = data.remote[i].serial;
2643 if (data.remote[i].connected) {
2645 if (remote->remotes[i].serial == serial) {
2646 wacom_remote_attach_battery(wacom, i);
2650 if (remote->remotes[i].serial)
2651 wacom_remote_destroy_one(wacom, i);
2653 wacom_remote_create_one(wacom, serial, i);
2655 } else if (remote->remotes[i].serial) {
2656 wacom_remote_destroy_one(wacom, i);
2661 static void wacom_mode_change_work(struct work_struct *work)
2663 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2664 struct wacom_shared *shared = wacom->wacom_wac.shared;
2665 struct wacom *wacom1 = NULL;
2666 struct wacom *wacom2 = NULL;
2667 bool is_direct = wacom->wacom_wac.is_direct_mode;
2671 wacom1 = hid_get_drvdata(shared->pen);
2672 wacom_release_resources(wacom1);
2673 hid_hw_stop(wacom1->hdev);
2674 wacom1->wacom_wac.has_mode_change = true;
2675 wacom1->wacom_wac.is_direct_mode = is_direct;
2678 if (shared->touch) {
2679 wacom2 = hid_get_drvdata(shared->touch);
2680 wacom_release_resources(wacom2);
2681 hid_hw_stop(wacom2->hdev);
2682 wacom2->wacom_wac.has_mode_change = true;
2683 wacom2->wacom_wac.is_direct_mode = is_direct;
2687 error = wacom_parse_and_register(wacom1, false);
2693 error = wacom_parse_and_register(wacom2, false);
2701 static int wacom_probe(struct hid_device *hdev,
2702 const struct hid_device_id *id)
2704 struct wacom *wacom;
2705 struct wacom_wac *wacom_wac;
2706 struct wacom_features *features;
2709 if (!id->driver_data)
2712 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2714 /* hid-core sets this quirk for the boot interface */
2715 hdev->quirks &= ~HID_QUIRK_NOGET;
2717 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2721 hid_set_drvdata(hdev, wacom);
2724 wacom_wac = &wacom->wacom_wac;
2725 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2726 features = &wacom_wac->features;
2728 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2733 error = wacom_devm_kfifo_alloc(wacom);
2737 wacom_wac->hid_data.inputmode = -1;
2738 wacom_wac->mode_report = -1;
2740 if (hid_is_usb(hdev)) {
2741 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2742 struct usb_device *dev = interface_to_usbdev(intf);
2744 wacom->usbdev = dev;
2748 mutex_init(&wacom->lock);
2749 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
2750 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2751 INIT_WORK(&wacom->battery_work, wacom_battery_work);
2752 INIT_WORK(&wacom->remote_work, wacom_remote_work);
2753 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
2755 /* ask for the report descriptor to be loaded by HID */
2756 error = hid_parse(hdev);
2758 hid_err(hdev, "parse failed\n");
2762 error = wacom_parse_and_register(wacom, false);
2766 if (hdev->bus == BUS_BLUETOOTH) {
2767 error = device_create_file(&hdev->dev, &dev_attr_speed);
2770 "can't create sysfs speed attribute err: %d\n",
2777 hid_set_drvdata(hdev, NULL);
2781 static void wacom_remove(struct hid_device *hdev)
2783 struct wacom *wacom = hid_get_drvdata(hdev);
2784 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2785 struct wacom_features *features = &wacom_wac->features;
2787 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2792 cancel_delayed_work_sync(&wacom->init_work);
2793 cancel_work_sync(&wacom->wireless_work);
2794 cancel_work_sync(&wacom->battery_work);
2795 cancel_work_sync(&wacom->remote_work);
2796 cancel_work_sync(&wacom->mode_change_work);
2797 if (hdev->bus == BUS_BLUETOOTH)
2798 device_remove_file(&hdev->dev, &dev_attr_speed);
2800 /* make sure we don't trigger the LEDs */
2801 wacom_led_groups_release(wacom);
2803 if (wacom->wacom_wac.features.type != REMOTE)
2804 wacom_release_resources(wacom);
2806 hid_set_drvdata(hdev, NULL);
2810 static int wacom_resume(struct hid_device *hdev)
2812 struct wacom *wacom = hid_get_drvdata(hdev);
2814 mutex_lock(&wacom->lock);
2816 /* switch to wacom mode first */
2817 _wacom_query_tablet_data(wacom);
2818 wacom_led_control(wacom);
2820 mutex_unlock(&wacom->lock);
2825 static int wacom_reset_resume(struct hid_device *hdev)
2827 return wacom_resume(hdev);
2829 #endif /* CONFIG_PM */
2831 static struct hid_driver wacom_driver = {
2833 .id_table = wacom_ids,
2834 .probe = wacom_probe,
2835 .remove = wacom_remove,
2836 .report = wacom_wac_report,
2838 .resume = wacom_resume,
2839 .reset_resume = wacom_reset_resume,
2841 .raw_event = wacom_raw_event,
2843 module_hid_driver(wacom_driver);
2845 MODULE_VERSION(DRIVER_VERSION);
2846 MODULE_AUTHOR(DRIVER_AUTHOR);
2847 MODULE_DESCRIPTION(DRIVER_DESC);
2848 MODULE_LICENSE("GPL");