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 int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
60 u8 *raw_data, int size)
62 struct wacom *wacom = hid_get_drvdata(hdev);
64 if (size > WACOM_PKGLEN_MAX)
67 memcpy(wacom->wacom_wac.data, raw_data, size);
69 wacom_wac_irq(&wacom->wacom_wac, size);
74 static int wacom_open(struct input_dev *dev)
76 struct wacom *wacom = input_get_drvdata(dev);
78 return hid_hw_open(wacom->hdev);
81 static void wacom_close(struct input_dev *dev)
83 struct wacom *wacom = input_get_drvdata(dev);
86 * wacom->hdev should never be null, but surprisingly, I had the case
87 * once while unplugging the Wacom Wireless Receiver.
90 hid_hw_close(wacom->hdev);
94 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
96 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
97 unsigned unit, int exponent)
99 struct hid_field field = {
100 .logical_maximum = logical_extents,
101 .physical_maximum = physical_extents,
103 .unit_exponent = exponent,
106 return hidinput_calc_abs_res(&field, ABS_X);
109 static void wacom_feature_mapping(struct hid_device *hdev,
110 struct hid_field *field, struct hid_usage *usage)
112 struct wacom *wacom = hid_get_drvdata(hdev);
113 struct wacom_features *features = &wacom->wacom_wac.features;
114 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
119 switch (usage->hid) {
120 case HID_DG_CONTACTMAX:
121 /* leave touch_max as is if predefined */
122 if (!features->touch_max) {
124 data = kzalloc(2, GFP_KERNEL);
127 data[0] = field->report->id;
128 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
129 data, 2, WAC_CMD_RETRIES);
131 features->touch_max = data[1];
133 features->touch_max = 16;
134 hid_warn(hdev, "wacom_feature_mapping: "
135 "could not get HID_DG_CONTACTMAX, "
136 "defaulting to %d\n",
137 features->touch_max);
142 case HID_DG_INPUTMODE:
143 /* Ignore if value index is out of bounds. */
144 if (usage->usage_index >= field->report_count) {
145 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
149 hid_data->inputmode = field->report->id;
150 hid_data->inputmode_index = usage->usage_index;
153 case HID_UP_DIGITIZER:
154 if (field->report->id == 0x0B &&
155 (field->application == WACOM_HID_G9_PEN ||
156 field->application == WACOM_HID_G11_PEN)) {
157 wacom->wacom_wac.mode_report = field->report->id;
158 wacom->wacom_wac.mode_value = 0;
162 case WACOM_HID_WD_DATAMODE:
163 wacom->wacom_wac.mode_report = field->report->id;
164 wacom->wacom_wac.mode_value = 2;
167 case WACOM_HID_UP_G9:
168 case WACOM_HID_UP_G11:
169 if (field->report->id == 0x03 &&
170 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
171 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
172 wacom->wacom_wac.mode_report = field->report->id;
173 wacom->wacom_wac.mode_value = 0;
176 case WACOM_HID_WD_OFFSETLEFT:
177 case WACOM_HID_WD_OFFSETTOP:
178 case WACOM_HID_WD_OFFSETRIGHT:
179 case WACOM_HID_WD_OFFSETBOTTOM:
181 n = hid_report_len(field->report);
182 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
185 data[0] = field->report->id;
186 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
187 data, n, WAC_CMD_RETRIES);
189 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
192 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
201 * Interface Descriptor of wacom devices can be incomplete and
202 * inconsistent so wacom_features table is used to store stylus
203 * device's packet lengths, various maximum values, and tablet
204 * resolution based on product ID's.
206 * For devices that contain 2 interfaces, wacom_features table is
207 * inaccurate for the touch interface. Since the Interface Descriptor
208 * for touch interfaces has pretty complete data, this function exists
209 * to query tablet for this missing information instead of hard coding in
210 * an additional table.
212 * A typical Interface Descriptor for a stylus will contain a
213 * boot mouse application collection that is not of interest and this
214 * function will ignore it.
216 * It also contains a digitizer application collection that also is not
217 * of interest since any information it contains would be duplicate
218 * of what is in wacom_features. Usually it defines a report of an array
219 * of bytes that could be used as max length of the stylus packet returned.
220 * If it happens to define a Digitizer-Stylus Physical Collection then
221 * the X and Y logical values contain valid data but it is ignored.
223 * A typical Interface Descriptor for a touch interface will contain a
224 * Digitizer-Finger Physical Collection which will define both logical
225 * X/Y maximum as well as the physical size of tablet. Since touch
226 * interfaces haven't supported pressure or distance, this is enough
227 * information to override invalid values in the wacom_features table.
229 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
230 * data. We deal with them after returning from this function.
232 static void wacom_usage_mapping(struct hid_device *hdev,
233 struct hid_field *field, struct hid_usage *usage)
235 struct wacom *wacom = hid_get_drvdata(hdev);
236 struct wacom_features *features = &wacom->wacom_wac.features;
237 bool finger = WACOM_FINGER_FIELD(field);
238 bool pen = WACOM_PEN_FIELD(field);
241 * Requiring Stylus Usage will ignore boot mouse
242 * X/Y values and some cases of invalid Digitizer X/Y
243 * values commonly reported.
246 features->device_type |= WACOM_DEVICETYPE_PEN;
248 features->device_type |= WACOM_DEVICETYPE_TOUCH;
253 * Bamboo models do not support HID_DG_CONTACTMAX.
254 * And, Bamboo Pen only descriptor contains touch.
256 if (features->type > BAMBOO_PT) {
257 /* ISDv4 touch devices at least supports one touch point */
258 if (finger && !features->touch_max)
259 features->touch_max = 1;
263 * ISDv4 devices which predate HID's adoption of the
264 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
265 * position instead. We can accurately detect if a
266 * usage with that value should be HID_DG_BARRELSWITCH2
267 * based on the surrounding usages, which have remained
268 * constant across generations.
270 if (features->type == HID_GENERIC &&
271 usage->hid == 0x000D0000 &&
272 field->application == HID_DG_PEN &&
273 field->physical == HID_DG_STYLUS) {
274 int i = usage->usage_index;
276 if (i-4 >= 0 && i+1 < field->maxusage &&
277 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
278 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
279 field->usage[i-2].hid == HID_DG_ERASER &&
280 field->usage[i-1].hid == HID_DG_INVERT &&
281 field->usage[i+1].hid == HID_DG_INRANGE) {
282 usage->hid = HID_DG_BARRELSWITCH2;
286 switch (usage->hid) {
288 features->x_max = field->logical_maximum;
290 features->x_phy = field->physical_maximum;
291 if ((features->type != BAMBOO_PT) &&
292 (features->type != BAMBOO_TOUCH)) {
293 features->unit = field->unit;
294 features->unitExpo = field->unit_exponent;
299 features->y_max = field->logical_maximum;
301 features->y_phy = field->physical_maximum;
302 if ((features->type != BAMBOO_PT) &&
303 (features->type != BAMBOO_TOUCH)) {
304 features->unit = field->unit;
305 features->unitExpo = field->unit_exponent;
309 case HID_DG_TIPPRESSURE:
311 features->pressure_max = field->logical_maximum;
315 if (features->type == HID_GENERIC)
316 wacom_wac_usage_mapping(hdev, field, usage);
319 static void wacom_post_parse_hid(struct hid_device *hdev,
320 struct wacom_features *features)
322 struct wacom *wacom = hid_get_drvdata(hdev);
323 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
325 if (features->type == HID_GENERIC) {
326 /* Any last-minute generic device setup */
327 if (features->touch_max > 1) {
328 input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
334 static void wacom_parse_hid(struct hid_device *hdev,
335 struct wacom_features *features)
337 struct hid_report_enum *rep_enum;
338 struct hid_report *hreport;
341 /* check features first */
342 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
343 list_for_each_entry(hreport, &rep_enum->report_list, list) {
344 for (i = 0; i < hreport->maxfield; i++) {
345 /* Ignore if report count is out of bounds. */
346 if (hreport->field[i]->report_count < 1)
349 for (j = 0; j < hreport->field[i]->maxusage; j++) {
350 wacom_feature_mapping(hdev, hreport->field[i],
351 hreport->field[i]->usage + j);
356 /* now check the input usages */
357 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
358 list_for_each_entry(hreport, &rep_enum->report_list, list) {
360 if (!hreport->maxfield)
363 for (i = 0; i < hreport->maxfield; i++)
364 for (j = 0; j < hreport->field[i]->maxusage; j++)
365 wacom_usage_mapping(hdev, hreport->field[i],
366 hreport->field[i]->usage + j);
369 wacom_post_parse_hid(hdev, features);
372 static int wacom_hid_set_device_mode(struct hid_device *hdev)
374 struct wacom *wacom = hid_get_drvdata(hdev);
375 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
376 struct hid_report *r;
377 struct hid_report_enum *re;
379 if (hid_data->inputmode < 0)
382 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
383 r = re->report_id_hash[hid_data->inputmode];
385 r->field[0]->value[hid_data->inputmode_index] = 2;
386 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
391 static int wacom_set_device_mode(struct hid_device *hdev,
392 struct wacom_wac *wacom_wac)
395 struct hid_report *r;
396 struct hid_report_enum *re;
398 int error = -ENOMEM, limit = 0;
400 if (wacom_wac->mode_report < 0)
403 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
404 r = re->report_id_hash[wacom_wac->mode_report];
408 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
412 length = hid_report_len(r);
415 rep_data[0] = wacom_wac->mode_report;
416 rep_data[1] = wacom_wac->mode_value;
418 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
421 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
422 rep_data, length, 1);
423 } while (error >= 0 &&
424 rep_data[1] != wacom_wac->mode_report &&
425 limit++ < WAC_MSG_RETRIES);
429 return error < 0 ? error : 0;
432 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
433 struct wacom_features *features)
435 struct wacom *wacom = hid_get_drvdata(hdev);
439 switch (features->type) {
443 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
447 rep_data[0] = speed == 0 ? 0x05 : 0x06;
450 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
454 wacom->wacom_wac.bt_high_speed = speed;
460 * Note that if the raw queries fail, it's not a hard failure
461 * and it is safe to continue
463 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
468 wacom->wacom_wac.bt_features &= ~0x20;
470 wacom->wacom_wac.bt_features |= 0x20;
473 rep_data[1] = wacom->wacom_wac.bt_features;
475 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
478 wacom->wacom_wac.bt_high_speed = speed;
486 * Switch the tablet into its most-capable mode. Wacom tablets are
487 * typically configured to power-up in a mode which sends mouse-like
488 * reports to the OS. To get absolute position, pressure data, etc.
489 * from the tablet, it is necessary to switch the tablet out of this
490 * mode and into one which sends the full range of tablet data.
492 static int _wacom_query_tablet_data(struct wacom *wacom)
494 struct hid_device *hdev = wacom->hdev;
495 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
496 struct wacom_features *features = &wacom_wac->features;
498 if (hdev->bus == BUS_BLUETOOTH)
499 return wacom_bt_query_tablet_data(hdev, 1, features);
501 if (features->type != HID_GENERIC) {
502 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
503 if (features->type > TABLETPC) {
504 /* MT Tablet PC touch */
505 wacom_wac->mode_report = 3;
506 wacom_wac->mode_value = 4;
507 } else if (features->type == WACOM_24HDT) {
508 wacom_wac->mode_report = 18;
509 wacom_wac->mode_value = 2;
510 } else if (features->type == WACOM_27QHDT) {
511 wacom_wac->mode_report = 131;
512 wacom_wac->mode_value = 2;
513 } else if (features->type == BAMBOO_PAD) {
514 wacom_wac->mode_report = 2;
515 wacom_wac->mode_value = 2;
517 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
518 if (features->type <= BAMBOO_PT) {
519 wacom_wac->mode_report = 2;
520 wacom_wac->mode_value = 2;
525 wacom_set_device_mode(hdev, wacom_wac);
527 if (features->type == HID_GENERIC)
528 return wacom_hid_set_device_mode(hdev);
533 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
534 struct wacom_features *features)
536 struct wacom *wacom = hid_get_drvdata(hdev);
537 struct usb_interface *intf = wacom->intf;
539 /* default features */
540 features->x_fuzz = 4;
541 features->y_fuzz = 4;
542 features->pressure_fuzz = 0;
543 features->distance_fuzz = 1;
544 features->tilt_fuzz = 1;
547 * The wireless device HID is basic and layout conflicts with
548 * other tablets (monitor and touch interface can look like pen).
549 * Skip the query for this type and modify defaults based on
552 if (features->type == WIRELESS) {
553 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
554 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
556 features->device_type = WACOM_DEVICETYPE_NONE;
560 wacom_parse_hid(hdev, features);
563 struct wacom_hdev_data {
564 struct list_head list;
566 struct hid_device *dev;
567 struct wacom_shared shared;
570 static LIST_HEAD(wacom_udev_list);
571 static DEFINE_MUTEX(wacom_udev_list_lock);
573 static bool compare_device_paths(struct hid_device *hdev_a,
574 struct hid_device *hdev_b, char separator)
576 int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
577 int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
579 if (n1 != n2 || n1 <= 0 || n2 <= 0)
582 return !strncmp(hdev_a->phys, hdev_b->phys, n1);
585 static bool wacom_are_sibling(struct hid_device *hdev,
586 struct hid_device *sibling)
588 struct wacom *wacom = hid_get_drvdata(hdev);
589 struct wacom_features *features = &wacom->wacom_wac.features;
590 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
591 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
592 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
593 __u32 oPid = features->oPid ? features->oPid : hdev->product;
595 /* The defined oVid/oPid must match that of the sibling */
596 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
598 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
602 * Devices with the same VID/PID must share the same physical
603 * device path, while those with different VID/PID must share
604 * the same physical parent device path.
606 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
607 if (!compare_device_paths(hdev, sibling, '/'))
610 if (!compare_device_paths(hdev, sibling, '.'))
614 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
615 if (features->type != HID_GENERIC)
619 * Direct-input devices may not be siblings of indirect-input
622 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
623 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
627 * Indirect-input devices may not be siblings of direct-input
630 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
631 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
634 /* Pen devices may only be siblings of touch devices */
635 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
636 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
639 /* Touch devices may only be siblings of pen devices */
640 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
641 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
645 * No reason could be found for these two devices to NOT be
646 * siblings, so there's a good chance they ARE siblings
651 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
653 struct wacom_hdev_data *data;
655 /* Try to find an already-probed interface from the same device */
656 list_for_each_entry(data, &wacom_udev_list, list) {
657 if (compare_device_paths(hdev, data->dev, '/'))
661 /* Fallback to finding devices that appear to be "siblings" */
662 list_for_each_entry(data, &wacom_udev_list, list) {
663 if (wacom_are_sibling(hdev, data->dev)) {
664 kref_get(&data->kref);
672 static void wacom_release_shared_data(struct kref *kref)
674 struct wacom_hdev_data *data =
675 container_of(kref, struct wacom_hdev_data, kref);
677 mutex_lock(&wacom_udev_list_lock);
678 list_del(&data->list);
679 mutex_unlock(&wacom_udev_list_lock);
684 static void wacom_remove_shared_data(void *res)
686 struct wacom *wacom = res;
687 struct wacom_hdev_data *data;
688 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
690 if (wacom_wac->shared) {
691 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
694 if (wacom_wac->shared->touch == wacom->hdev)
695 wacom_wac->shared->touch = NULL;
696 else if (wacom_wac->shared->pen == wacom->hdev)
697 wacom_wac->shared->pen = NULL;
699 kref_put(&data->kref, wacom_release_shared_data);
700 wacom_wac->shared = NULL;
704 static int wacom_add_shared_data(struct hid_device *hdev)
706 struct wacom *wacom = hid_get_drvdata(hdev);
707 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
708 struct wacom_hdev_data *data;
711 mutex_lock(&wacom_udev_list_lock);
713 data = wacom_get_hdev_data(hdev);
715 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
721 kref_init(&data->kref);
723 list_add_tail(&data->list, &wacom_udev_list);
726 wacom_wac->shared = &data->shared;
728 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
730 mutex_unlock(&wacom_udev_list_lock);
731 wacom_remove_shared_data(wacom);
735 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
736 wacom_wac->shared->touch = hdev;
737 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
738 wacom_wac->shared->pen = hdev;
741 mutex_unlock(&wacom_udev_list_lock);
745 static int wacom_led_control(struct wacom *wacom)
749 unsigned char report_id = WAC_CMD_LED_CONTROL;
752 if (!wacom->led.groups)
755 if (wacom->wacom_wac.pid) { /* wireless connected */
756 report_id = WAC_CMD_WL_LED_CONTROL;
759 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
760 report_id = WAC_CMD_WL_INTUOSP2;
763 buf = kzalloc(buf_size, GFP_KERNEL);
767 if (wacom->wacom_wac.features.type >= INTUOS5S &&
768 wacom->wacom_wac.features.type <= INTUOSPL) {
770 * Touch Ring and crop mark LED luminance may take on
771 * one of four values:
772 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
774 int ring_led = wacom->led.groups[0].select & 0x03;
775 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
777 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
780 if (wacom->wacom_wac.pid) {
781 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
782 buf, buf_size, WAC_CMD_RETRIES);
788 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
790 buf[4] = 100; // Power Connection LED (ORANGE)
791 buf[5] = 100; // BT Connection LED (BLUE)
792 buf[6] = 100; // Paper Mode (RED?)
793 buf[7] = 100; // Paper Mode (GREEN?)
794 buf[8] = 100; // Paper Mode (BLUE?)
795 buf[9] = wacom->led.llv;
796 buf[10] = wacom->led.groups[0].select & 0x03;
799 int led = wacom->led.groups[0].select | 0x4;
801 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
802 wacom->wacom_wac.features.type == WACOM_24HD)
803 led |= (wacom->led.groups[1].select << 4) | 0x40;
807 buf[2] = wacom->led.llv;
808 buf[3] = wacom->led.hlv;
809 buf[4] = wacom->led.img_lum;
812 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
819 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
820 const unsigned len, const void *img)
824 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
826 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
830 /* Send 'start' command */
831 buf[0] = WAC_CMD_ICON_START;
833 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
839 buf[1] = button_id & 0x07;
840 for (i = 0; i < 4; i++) {
842 memcpy(buf + 3, img + i * chunk_len, chunk_len);
844 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
845 buf, chunk_len + 3, WAC_CMD_RETRIES);
851 buf[0] = WAC_CMD_ICON_START;
853 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
861 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
862 const char *buf, size_t count)
864 struct hid_device *hdev = to_hid_device(dev);
865 struct wacom *wacom = hid_get_drvdata(hdev);
869 err = kstrtouint(buf, 10, &id);
873 mutex_lock(&wacom->lock);
875 wacom->led.groups[set_id].select = id & 0x3;
876 err = wacom_led_control(wacom);
878 mutex_unlock(&wacom->lock);
880 return err < 0 ? err : count;
883 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
884 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
885 struct device_attribute *attr, const char *buf, size_t count) \
887 return wacom_led_select_store(dev, SET_ID, buf, count); \
889 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
890 struct device_attribute *attr, char *buf) \
892 struct hid_device *hdev = to_hid_device(dev);\
893 struct wacom *wacom = hid_get_drvdata(hdev); \
894 return scnprintf(buf, PAGE_SIZE, "%d\n", \
895 wacom->led.groups[SET_ID].select); \
897 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
898 wacom_led##SET_ID##_select_show, \
899 wacom_led##SET_ID##_select_store)
901 DEVICE_LED_SELECT_ATTR(0);
902 DEVICE_LED_SELECT_ATTR(1);
904 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
905 const char *buf, size_t count)
910 err = kstrtouint(buf, 10, &value);
914 mutex_lock(&wacom->lock);
916 *dest = value & 0x7f;
917 err = wacom_led_control(wacom);
919 mutex_unlock(&wacom->lock);
921 return err < 0 ? err : count;
924 #define DEVICE_LUMINANCE_ATTR(name, field) \
925 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
926 struct device_attribute *attr, const char *buf, size_t count) \
928 struct hid_device *hdev = to_hid_device(dev);\
929 struct wacom *wacom = hid_get_drvdata(hdev); \
931 return wacom_luminance_store(wacom, &wacom->led.field, \
934 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
935 struct device_attribute *attr, char *buf) \
937 struct wacom *wacom = dev_get_drvdata(dev); \
938 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
940 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
941 wacom_##name##_luminance_show, \
942 wacom_##name##_luminance_store)
944 DEVICE_LUMINANCE_ATTR(status0, llv);
945 DEVICE_LUMINANCE_ATTR(status1, hlv);
946 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
948 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
949 const char *buf, size_t count)
951 struct hid_device *hdev = to_hid_device(dev);
952 struct wacom *wacom = hid_get_drvdata(hdev);
957 if (hdev->bus == BUS_BLUETOOTH) {
959 xfer_id = WAC_CMD_ICON_BT_XFER;
962 xfer_id = WAC_CMD_ICON_XFER;
968 mutex_lock(&wacom->lock);
970 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
972 mutex_unlock(&wacom->lock);
974 return err < 0 ? err : count;
977 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
978 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
979 struct device_attribute *attr, const char *buf, size_t count) \
981 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
983 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
984 NULL, wacom_btnimg##BUTTON_ID##_store)
986 DEVICE_BTNIMG_ATTR(0);
987 DEVICE_BTNIMG_ATTR(1);
988 DEVICE_BTNIMG_ATTR(2);
989 DEVICE_BTNIMG_ATTR(3);
990 DEVICE_BTNIMG_ATTR(4);
991 DEVICE_BTNIMG_ATTR(5);
992 DEVICE_BTNIMG_ATTR(6);
993 DEVICE_BTNIMG_ATTR(7);
995 static struct attribute *cintiq_led_attrs[] = {
996 &dev_attr_status_led0_select.attr,
997 &dev_attr_status_led1_select.attr,
1001 static struct attribute_group cintiq_led_attr_group = {
1002 .name = "wacom_led",
1003 .attrs = cintiq_led_attrs,
1006 static struct attribute *intuos4_led_attrs[] = {
1007 &dev_attr_status0_luminance.attr,
1008 &dev_attr_status1_luminance.attr,
1009 &dev_attr_status_led0_select.attr,
1010 &dev_attr_buttons_luminance.attr,
1011 &dev_attr_button0_rawimg.attr,
1012 &dev_attr_button1_rawimg.attr,
1013 &dev_attr_button2_rawimg.attr,
1014 &dev_attr_button3_rawimg.attr,
1015 &dev_attr_button4_rawimg.attr,
1016 &dev_attr_button5_rawimg.attr,
1017 &dev_attr_button6_rawimg.attr,
1018 &dev_attr_button7_rawimg.attr,
1022 static struct attribute_group intuos4_led_attr_group = {
1023 .name = "wacom_led",
1024 .attrs = intuos4_led_attrs,
1027 static struct attribute *intuos5_led_attrs[] = {
1028 &dev_attr_status0_luminance.attr,
1029 &dev_attr_status_led0_select.attr,
1033 static struct attribute_group intuos5_led_attr_group = {
1034 .name = "wacom_led",
1035 .attrs = intuos5_led_attrs,
1038 struct wacom_sysfs_group_devres {
1039 struct attribute_group *group;
1040 struct kobject *root;
1043 static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1045 struct wacom_sysfs_group_devres *devres = res;
1046 struct kobject *kobj = devres->root;
1048 dev_dbg(dev, "%s: dropping reference to %s\n",
1049 __func__, devres->group->name);
1050 sysfs_remove_group(kobj, devres->group);
1053 static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1054 struct kobject *root,
1055 struct attribute_group *group)
1057 struct wacom_sysfs_group_devres *devres;
1060 devres = devres_alloc(wacom_devm_sysfs_group_release,
1061 sizeof(struct wacom_sysfs_group_devres),
1066 devres->group = group;
1067 devres->root = root;
1069 error = sysfs_create_group(devres->root, group);
1073 devres_add(&wacom->hdev->dev, devres);
1078 static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1079 struct attribute_group *group)
1081 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1085 enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
1087 struct wacom *wacom = led->wacom;
1089 if (wacom->led.max_hlv)
1090 return led->hlv * LED_FULL / wacom->led.max_hlv;
1092 if (wacom->led.max_llv)
1093 return led->llv * LED_FULL / wacom->led.max_llv;
1095 /* device doesn't support brightness tuning */
1099 static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1101 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1102 struct wacom *wacom = led->wacom;
1104 if (wacom->led.groups[led->group].select != led->id)
1107 return wacom_leds_brightness_get(led);
1110 static int wacom_led_brightness_set(struct led_classdev *cdev,
1111 enum led_brightness brightness)
1113 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1114 struct wacom *wacom = led->wacom;
1117 mutex_lock(&wacom->lock);
1119 if (!wacom->led.groups || (brightness == LED_OFF &&
1120 wacom->led.groups[led->group].select != led->id)) {
1125 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1126 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1128 wacom->led.groups[led->group].select = led->id;
1130 error = wacom_led_control(wacom);
1133 mutex_unlock(&wacom->lock);
1138 static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1139 enum led_brightness brightness)
1143 static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1144 struct wacom_led *led, unsigned int group,
1145 unsigned int id, bool read_only)
1150 name = devm_kasprintf(dev, GFP_KERNEL,
1159 led->trigger.name = name;
1160 error = devm_led_trigger_register(dev, &led->trigger);
1162 hid_err(wacom->hdev,
1163 "failed to register LED trigger %s: %d\n",
1164 led->cdev.name, error);
1172 led->llv = wacom->led.llv;
1173 led->hlv = wacom->led.hlv;
1174 led->cdev.name = name;
1175 led->cdev.max_brightness = LED_FULL;
1176 led->cdev.flags = LED_HW_PLUGGABLE;
1177 led->cdev.brightness_get = __wacom_led_brightness_get;
1179 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
1180 led->cdev.default_trigger = led->cdev.name;
1182 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
1185 error = devm_led_classdev_register(dev, &led->cdev);
1187 hid_err(wacom->hdev,
1188 "failed to register LED %s: %d\n",
1189 led->cdev.name, error);
1190 led->cdev.name = NULL;
1197 static void wacom_led_groups_release_one(void *data)
1199 struct wacom_group_leds *group = data;
1201 devres_release_group(group->dev, group);
1204 static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1205 struct wacom *wacom,
1206 int group_id, int count,
1209 struct wacom_led *leds;
1212 if (group_id >= wacom->led.count || count <= 0)
1215 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1218 leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
1224 wacom->led.groups[group_id].leds = leds;
1225 wacom->led.groups[group_id].count = count;
1227 for (i = 0; i < count; i++) {
1228 error = wacom_led_register_one(dev, wacom, &leds[i],
1229 group_id, i, read_only);
1234 wacom->led.groups[group_id].dev = dev;
1236 devres_close_group(dev, &wacom->led.groups[group_id]);
1239 * There is a bug (?) in devm_led_classdev_register() in which its
1240 * increments the refcount of the parent. If the parent is an input
1241 * device, that means the ref count never reaches 0 when
1242 * devm_input_device_release() gets called.
1243 * This means that the LEDs are still there after disconnect.
1244 * Manually force the release of the group so that the leds are released
1245 * once we are done using them.
1247 error = devm_add_action_or_reset(&wacom->hdev->dev,
1248 wacom_led_groups_release_one,
1249 &wacom->led.groups[group_id]);
1256 devres_release_group(dev, &wacom->led.groups[group_id]);
1260 struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1263 struct wacom_group_leds *group;
1265 if (group_id >= wacom->led.count)
1268 group = &wacom->led.groups[group_id];
1275 return &group->leds[id];
1279 * wacom_led_next: gives the next available led with a wacom trigger.
1281 * returns the next available struct wacom_led which has its default trigger
1282 * or the current one if none is available.
1284 struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1286 struct wacom_led *next_led;
1296 next_led = wacom_led_find(wacom, group, ++next);
1297 if (!next_led || next_led == cur)
1299 } while (next_led->cdev.trigger != &next_led->trigger);
1304 static void wacom_led_groups_release(void *data)
1306 struct wacom *wacom = data;
1308 wacom->led.groups = NULL;
1309 wacom->led.count = 0;
1312 static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1314 struct device *dev = &wacom->hdev->dev;
1315 struct wacom_group_leds *groups;
1318 groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
1323 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1327 wacom->led.groups = groups;
1328 wacom->led.count = count;
1333 static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1334 int led_per_group, bool read_only)
1339 if (!wacom->wacom_wac.pad_input)
1342 dev = &wacom->wacom_wac.pad_input->dev;
1344 error = wacom_led_groups_allocate(wacom, group_count);
1348 for (i = 0; i < group_count; i++) {
1349 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1359 static int wacom_initialize_leds(struct wacom *wacom)
1363 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1366 /* Initialize default values */
1367 switch (wacom->wacom_wac.features.type) {
1372 wacom->led.llv = 10;
1373 wacom->led.hlv = 20;
1374 wacom->led.max_llv = 127;
1375 wacom->led.max_hlv = 127;
1376 wacom->led.img_lum = 10;
1378 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1380 hid_err(wacom->hdev,
1381 "cannot create leds err: %d\n", error);
1385 error = wacom_devm_sysfs_create_group(wacom,
1386 &intuos4_led_attr_group);
1393 wacom->led.img_lum = 0;
1395 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1397 hid_err(wacom->hdev,
1398 "cannot create leds err: %d\n", error);
1402 error = wacom_devm_sysfs_create_group(wacom,
1403 &cintiq_led_attr_group);
1412 wacom->led.llv = 32;
1413 wacom->led.max_llv = 96;
1415 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1417 hid_err(wacom->hdev,
1418 "cannot create leds err: %d\n", error);
1422 error = wacom_devm_sysfs_create_group(wacom,
1423 &intuos5_led_attr_group);
1427 wacom->led.llv = 50;
1428 wacom->led.max_llv = 100;
1429 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1431 hid_err(wacom->hdev,
1432 "cannot create leds err: %d\n", error);
1438 wacom->led.llv = 255;
1439 wacom->led.max_llv = 255;
1440 error = wacom_led_groups_allocate(wacom, 5);
1442 hid_err(wacom->hdev,
1443 "cannot create leds err: %d\n", error);
1453 hid_err(wacom->hdev,
1454 "cannot create sysfs group err: %d\n", error);
1461 static void wacom_init_work(struct work_struct *work)
1463 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1465 _wacom_query_tablet_data(wacom);
1466 wacom_led_control(wacom);
1469 static void wacom_query_tablet_data(struct wacom *wacom)
1471 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1474 static enum power_supply_property wacom_battery_props[] = {
1475 POWER_SUPPLY_PROP_MODEL_NAME,
1476 POWER_SUPPLY_PROP_PRESENT,
1477 POWER_SUPPLY_PROP_STATUS,
1478 POWER_SUPPLY_PROP_SCOPE,
1479 POWER_SUPPLY_PROP_CAPACITY
1482 static int wacom_battery_get_property(struct power_supply *psy,
1483 enum power_supply_property psp,
1484 union power_supply_propval *val)
1486 struct wacom_battery *battery = power_supply_get_drvdata(psy);
1490 case POWER_SUPPLY_PROP_MODEL_NAME:
1491 val->strval = battery->wacom->wacom_wac.name;
1493 case POWER_SUPPLY_PROP_PRESENT:
1494 val->intval = battery->bat_connected;
1496 case POWER_SUPPLY_PROP_SCOPE:
1497 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1499 case POWER_SUPPLY_PROP_CAPACITY:
1500 val->intval = battery->battery_capacity;
1502 case POWER_SUPPLY_PROP_STATUS:
1503 if (battery->bat_charging)
1504 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1505 else if (battery->battery_capacity == 100 &&
1506 battery->ps_connected)
1507 val->intval = POWER_SUPPLY_STATUS_FULL;
1508 else if (battery->ps_connected)
1509 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1511 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1521 static int __wacom_initialize_battery(struct wacom *wacom,
1522 struct wacom_battery *battery)
1524 static atomic_t battery_no = ATOMIC_INIT(0);
1525 struct device *dev = &wacom->hdev->dev;
1526 struct power_supply_config psy_cfg = { .drv_data = battery, };
1527 struct power_supply *ps_bat;
1528 struct power_supply_desc *bat_desc = &battery->bat_desc;
1532 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1535 battery->wacom = wacom;
1537 n = atomic_inc_return(&battery_no) - 1;
1539 bat_desc->properties = wacom_battery_props;
1540 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1541 bat_desc->get_property = wacom_battery_get_property;
1542 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1543 bat_desc->name = battery->bat_name;
1544 bat_desc->type = POWER_SUPPLY_TYPE_USB;
1545 bat_desc->use_for_apm = 0;
1547 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1548 if (IS_ERR(ps_bat)) {
1549 error = PTR_ERR(ps_bat);
1553 power_supply_powers(ps_bat, &wacom->hdev->dev);
1555 battery->battery = ps_bat;
1557 devres_close_group(dev, bat_desc);
1561 devres_release_group(dev, bat_desc);
1565 static int wacom_initialize_battery(struct wacom *wacom)
1567 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1568 return __wacom_initialize_battery(wacom, &wacom->battery);
1573 static void wacom_destroy_battery(struct wacom *wacom)
1575 if (wacom->battery.battery) {
1576 devres_release_group(&wacom->hdev->dev,
1577 &wacom->battery.bat_desc);
1578 wacom->battery.battery = NULL;
1582 static ssize_t wacom_show_speed(struct device *dev,
1583 struct device_attribute
1586 struct hid_device *hdev = to_hid_device(dev);
1587 struct wacom *wacom = hid_get_drvdata(hdev);
1589 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1592 static ssize_t wacom_store_speed(struct device *dev,
1593 struct device_attribute *attr,
1594 const char *buf, size_t count)
1596 struct hid_device *hdev = to_hid_device(dev);
1597 struct wacom *wacom = hid_get_drvdata(hdev);
1600 if (kstrtou8(buf, 0, &new_speed))
1603 if (new_speed != 0 && new_speed != 1)
1606 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1611 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1612 wacom_show_speed, wacom_store_speed);
1615 static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1616 struct kobj_attribute *kattr,
1617 char *buf, int index)
1619 struct device *dev = kobj_to_dev(kobj->parent);
1620 struct hid_device *hdev = to_hid_device(dev);
1621 struct wacom *wacom = hid_get_drvdata(hdev);
1624 mode = wacom->led.groups[index].select;
1625 if (mode >= 0 && mode < 3)
1626 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
1628 return snprintf(buf, PAGE_SIZE, "%d\n", -1);
1631 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1632 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1633 struct kobj_attribute *kattr, char *buf) \
1635 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1637 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1638 .attr = {.name = "remote_mode", \
1639 .mode = DEV_ATTR_RO_PERM}, \
1640 .show = wacom_show_remote##SET_ID##_mode, \
1642 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1643 &remote##SET_ID##_mode_attr.attr, \
1646 static struct attribute_group remote##SET_ID##_serial_group = { \
1648 .attrs = remote##SET_ID##_serial_attrs, \
1651 DEVICE_EKR_ATTR_GROUP(0);
1652 DEVICE_EKR_ATTR_GROUP(1);
1653 DEVICE_EKR_ATTR_GROUP(2);
1654 DEVICE_EKR_ATTR_GROUP(3);
1655 DEVICE_EKR_ATTR_GROUP(4);
1657 static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1661 struct wacom_remote *remote = wacom->remote;
1663 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1666 if (!remote->remotes[index].group.name)
1669 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1670 &remote->remotes[index].group);
1672 remote->remotes[index].group.name = NULL;
1673 hid_err(wacom->hdev,
1674 "cannot create sysfs group err: %d\n", error);
1681 static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1683 const size_t buf_size = 2;
1687 buf = kzalloc(buf_size, GFP_KERNEL);
1691 buf[0] = WAC_CMD_DELETE_PAIRING;
1694 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1695 buf_size, WAC_CMD_RETRIES);
1701 static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1702 struct kobj_attribute *attr,
1703 const char *buf, size_t count)
1705 unsigned char selector = 0;
1706 struct device *dev = kobj_to_dev(kobj->parent);
1707 struct hid_device *hdev = to_hid_device(dev);
1708 struct wacom *wacom = hid_get_drvdata(hdev);
1711 if (!strncmp(buf, "*\n", 2)) {
1712 selector = WAC_CMD_UNPAIR_ALL;
1714 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1719 mutex_lock(&wacom->lock);
1721 err = wacom_cmd_unpair_remote(wacom, selector);
1722 mutex_unlock(&wacom->lock);
1724 return err < 0 ? err : count;
1727 static struct kobj_attribute unpair_remote_attr = {
1728 .attr = {.name = "unpair_remote", .mode = 0200},
1729 .store = wacom_store_unpair_remote,
1732 static const struct attribute *remote_unpair_attrs[] = {
1733 &unpair_remote_attr.attr,
1737 static void wacom_remotes_destroy(void *data)
1739 struct wacom *wacom = data;
1740 struct wacom_remote *remote = wacom->remote;
1745 kobject_put(remote->remote_dir);
1746 kfifo_free(&remote->remote_fifo);
1747 wacom->remote = NULL;
1750 static int wacom_initialize_remotes(struct wacom *wacom)
1753 struct wacom_remote *remote;
1756 if (wacom->wacom_wac.features.type != REMOTE)
1759 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1764 wacom->remote = remote;
1766 spin_lock_init(&remote->remote_lock);
1768 error = kfifo_alloc(&remote->remote_fifo,
1769 5 * sizeof(struct wacom_remote_data),
1772 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1776 remote->remotes[0].group = remote0_serial_group;
1777 remote->remotes[1].group = remote1_serial_group;
1778 remote->remotes[2].group = remote2_serial_group;
1779 remote->remotes[3].group = remote3_serial_group;
1780 remote->remotes[4].group = remote4_serial_group;
1782 remote->remote_dir = kobject_create_and_add("wacom_remote",
1783 &wacom->hdev->dev.kobj);
1784 if (!remote->remote_dir)
1787 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
1790 hid_err(wacom->hdev,
1791 "cannot create sysfs group err: %d\n", error);
1795 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1796 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
1797 remote->remotes[i].serial = 0;
1800 error = devm_add_action_or_reset(&wacom->hdev->dev,
1801 wacom_remotes_destroy, wacom);
1808 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1810 struct input_dev *input_dev;
1811 struct hid_device *hdev = wacom->hdev;
1812 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1814 input_dev = devm_input_allocate_device(&hdev->dev);
1818 input_dev->name = wacom_wac->features.name;
1819 input_dev->phys = hdev->phys;
1820 input_dev->dev.parent = &hdev->dev;
1821 input_dev->open = wacom_open;
1822 input_dev->close = wacom_close;
1823 input_dev->uniq = hdev->uniq;
1824 input_dev->id.bustype = hdev->bus;
1825 input_dev->id.vendor = hdev->vendor;
1826 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1827 input_dev->id.version = hdev->version;
1828 input_set_drvdata(input_dev, wacom);
1833 static int wacom_allocate_inputs(struct wacom *wacom)
1835 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1837 wacom_wac->pen_input = wacom_allocate_input(wacom);
1838 wacom_wac->touch_input = wacom_allocate_input(wacom);
1839 wacom_wac->pad_input = wacom_allocate_input(wacom);
1840 if (!wacom_wac->pen_input ||
1841 !wacom_wac->touch_input ||
1842 !wacom_wac->pad_input)
1845 wacom_wac->pen_input->name = wacom_wac->pen_name;
1846 wacom_wac->touch_input->name = wacom_wac->touch_name;
1847 wacom_wac->pad_input->name = wacom_wac->pad_name;
1852 static int wacom_register_inputs(struct wacom *wacom)
1854 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
1855 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1858 pen_input_dev = wacom_wac->pen_input;
1859 touch_input_dev = wacom_wac->touch_input;
1860 pad_input_dev = wacom_wac->pad_input;
1862 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
1865 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1867 /* no pen in use on this interface */
1868 input_free_device(pen_input_dev);
1869 wacom_wac->pen_input = NULL;
1870 pen_input_dev = NULL;
1872 error = input_register_device(pen_input_dev);
1877 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1879 /* no touch in use on this interface */
1880 input_free_device(touch_input_dev);
1881 wacom_wac->touch_input = NULL;
1882 touch_input_dev = NULL;
1884 error = input_register_device(touch_input_dev);
1889 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1891 /* no pad in use on this interface */
1892 input_free_device(pad_input_dev);
1893 wacom_wac->pad_input = NULL;
1894 pad_input_dev = NULL;
1896 error = input_register_device(pad_input_dev);
1904 wacom_wac->pad_input = NULL;
1905 wacom_wac->touch_input = NULL;
1906 wacom_wac->pen_input = NULL;
1911 * Not all devices report physical dimensions from HID.
1912 * Compute the default from hardcoded logical dimension
1913 * and resolution before driver overwrites them.
1915 static void wacom_set_default_phy(struct wacom_features *features)
1917 if (features->x_resolution) {
1918 features->x_phy = (features->x_max * 100) /
1919 features->x_resolution;
1920 features->y_phy = (features->y_max * 100) /
1921 features->y_resolution;
1925 static void wacom_calculate_res(struct wacom_features *features)
1927 /* set unit to "100th of a mm" for devices not reported by HID */
1928 if (!features->unit) {
1929 features->unit = 0x11;
1930 features->unitExpo = -3;
1933 features->x_resolution = wacom_calc_hid_res(features->x_max,
1936 features->unitExpo);
1937 features->y_resolution = wacom_calc_hid_res(features->y_max,
1940 features->unitExpo);
1943 void wacom_battery_work(struct work_struct *work)
1945 struct wacom *wacom = container_of(work, struct wacom, battery_work);
1947 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1948 !wacom->battery.battery) {
1949 wacom_initialize_battery(wacom);
1951 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1952 wacom->battery.battery) {
1953 wacom_destroy_battery(wacom);
1957 static size_t wacom_compute_pktlen(struct hid_device *hdev)
1959 struct hid_report_enum *report_enum;
1960 struct hid_report *report;
1963 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1965 list_for_each_entry(report, &report_enum->report_list, list) {
1966 size_t report_size = hid_report_len(report);
1967 if (report_size > size)
1974 static void wacom_update_name(struct wacom *wacom, const char *suffix)
1976 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1977 struct wacom_features *features = &wacom_wac->features;
1978 char name[WACOM_NAME_MAX];
1980 /* Generic devices name unspecified */
1981 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1982 if (strstr(wacom->hdev->name, "Wacom") ||
1983 strstr(wacom->hdev->name, "wacom") ||
1984 strstr(wacom->hdev->name, "WACOM")) {
1985 /* name is in HID descriptor, use it */
1986 strlcpy(name, wacom->hdev->name, sizeof(name));
1988 /* strip out excess whitespaces */
1990 char *gap = strstr(name, " ");
1993 /* shift everything including the terminator */
1994 memmove(gap, gap+1, strlen(gap));
1997 /* strip off excessive prefixing */
1998 if (strstr(name, "Wacom Co.,Ltd. Wacom ") == name) {
1999 int n = strlen(name);
2000 int x = strlen("Wacom Co.,Ltd. ");
2001 memmove(name, name+x, n-x+1);
2003 if (strstr(name, "Wacom Co., Ltd. Wacom ") == name) {
2004 int n = strlen(name);
2005 int x = strlen("Wacom Co., Ltd. ");
2006 memmove(name, name+x, n-x+1);
2009 /* get rid of trailing whitespace */
2010 if (name[strlen(name)-1] == ' ')
2011 name[strlen(name)-1] = '\0';
2013 /* no meaningful name retrieved. use product ID */
2014 snprintf(name, sizeof(name),
2015 "%s %X", features->name, wacom->hdev->product);
2018 strlcpy(name, features->name, sizeof(name));
2021 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2024 /* Append the device type to the name */
2025 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
2026 "%s%s Pen", name, suffix);
2027 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
2028 "%s%s Finger", name, suffix);
2029 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
2030 "%s%s Pad", name, suffix);
2033 static void wacom_release_resources(struct wacom *wacom)
2035 struct hid_device *hdev = wacom->hdev;
2037 if (!wacom->resources)
2040 devres_release_group(&hdev->dev, wacom);
2042 wacom->resources = false;
2044 wacom->wacom_wac.pen_input = NULL;
2045 wacom->wacom_wac.touch_input = NULL;
2046 wacom->wacom_wac.pad_input = NULL;
2049 static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
2051 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2052 struct wacom_features *features = &wacom_wac->features;
2053 struct hid_device *hdev = wacom->hdev;
2055 unsigned int connect_mask = HID_CONNECT_HIDRAW;
2057 features->pktlen = wacom_compute_pktlen(hdev);
2058 if (features->pktlen > WACOM_PKGLEN_MAX)
2061 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2064 wacom->resources = true;
2066 error = wacom_allocate_inputs(wacom);
2071 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2072 * into debug mode for the touch part.
2073 * We ignore the other interfaces.
2075 if (features->type == BAMBOO_PAD) {
2076 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2077 features->type = HID_GENERIC;
2078 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2079 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2085 /* set the default size in case we do not get them from hid */
2086 wacom_set_default_phy(features);
2088 /* Retrieve the physical and logical size for touch devices */
2089 wacom_retrieve_hid_descriptor(hdev, features);
2090 wacom_setup_device_quirks(wacom);
2092 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2093 features->type != WIRELESS) {
2094 error = features->type == HID_GENERIC ? -ENODEV : 0;
2096 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
2098 error ? "Ignoring" : "Assuming pen");
2103 features->device_type |= WACOM_DEVICETYPE_PEN;
2106 wacom_calculate_res(features);
2108 wacom_update_name(wacom, wireless ? " (WL)" : "");
2110 error = wacom_add_shared_data(hdev);
2114 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2115 (features->quirks & WACOM_QUIRK_BATTERY)) {
2116 error = wacom_initialize_battery(wacom);
2121 error = wacom_register_inputs(wacom);
2125 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
2126 error = wacom_initialize_leds(wacom);
2130 error = wacom_initialize_remotes(wacom);
2135 if (features->type == HID_GENERIC)
2136 connect_mask |= HID_CONNECT_DRIVER;
2138 /* Regular HID work starts now */
2139 error = hid_hw_start(hdev, connect_mask);
2141 hid_err(hdev, "hw start failed\n");
2146 /* Note that if query fails it is not a hard failure */
2147 wacom_query_tablet_data(wacom);
2150 /* touch only Bamboo doesn't support pen */
2151 if ((features->type == BAMBOO_TOUCH) &&
2152 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2157 /* pen only Bamboo neither support touch nor pad */
2158 if ((features->type == BAMBOO_PEN) &&
2159 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2160 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2165 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2166 error = hid_hw_open(hdev);
2168 if ((wacom_wac->features.type == INTUOSHT ||
2169 wacom_wac->features.type == INTUOSHT2) &&
2170 (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
2171 wacom_wac->shared->type = wacom_wac->features.type;
2172 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2175 devres_close_group(&hdev->dev, wacom);
2182 wacom_release_resources(wacom);
2186 static void wacom_wireless_work(struct work_struct *work)
2188 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2189 struct usb_device *usbdev = wacom->usbdev;
2190 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2191 struct hid_device *hdev1, *hdev2;
2192 struct wacom *wacom1, *wacom2;
2193 struct wacom_wac *wacom_wac1, *wacom_wac2;
2197 * Regardless if this is a disconnect or a new tablet,
2198 * remove any existing input and battery devices.
2201 wacom_destroy_battery(wacom);
2203 /* Stylus interface */
2204 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2205 wacom1 = hid_get_drvdata(hdev1);
2206 wacom_wac1 = &(wacom1->wacom_wac);
2207 wacom_release_resources(wacom1);
2209 /* Touch interface */
2210 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2211 wacom2 = hid_get_drvdata(hdev2);
2212 wacom_wac2 = &(wacom2->wacom_wac);
2213 wacom_release_resources(wacom2);
2215 if (wacom_wac->pid == 0) {
2216 hid_info(wacom->hdev, "wireless tablet disconnected\n");
2218 const struct hid_device_id *id = wacom_ids;
2220 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2224 if (id->vendor == USB_VENDOR_ID_WACOM &&
2225 id->product == wacom_wac->pid)
2231 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2235 /* Stylus interface */
2236 wacom_wac1->features =
2237 *((struct wacom_features *)id->driver_data);
2239 wacom_wac1->pid = wacom_wac->pid;
2241 error = wacom_parse_and_register(wacom1, true);
2245 /* Touch interface */
2246 if (wacom_wac1->features.touch_max ||
2247 (wacom_wac1->features.type >= INTUOSHT &&
2248 wacom_wac1->features.type <= BAMBOO_PT)) {
2249 wacom_wac2->features =
2250 *((struct wacom_features *)id->driver_data);
2251 wacom_wac2->pid = wacom_wac->pid;
2253 error = wacom_parse_and_register(wacom2, true);
2258 strlcpy(wacom_wac->name, wacom_wac1->name,
2259 sizeof(wacom_wac->name));
2260 error = wacom_initialize_battery(wacom);
2268 wacom_release_resources(wacom1);
2269 wacom_release_resources(wacom2);
2273 static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2275 struct wacom_remote *remote = wacom->remote;
2276 u32 serial = remote->remotes[index].serial;
2278 unsigned long flags;
2280 spin_lock_irqsave(&remote->remote_lock, flags);
2281 remote->remotes[index].registered = false;
2282 spin_unlock_irqrestore(&remote->remote_lock, flags);
2284 if (remote->remotes[index].battery.battery)
2285 devres_release_group(&wacom->hdev->dev,
2286 &remote->remotes[index].battery.bat_desc);
2288 if (remote->remotes[index].group.name)
2289 devres_release_group(&wacom->hdev->dev,
2290 &remote->remotes[index]);
2292 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2293 if (remote->remotes[i].serial == serial) {
2294 remote->remotes[i].serial = 0;
2295 remote->remotes[i].group.name = NULL;
2296 remote->remotes[i].registered = false;
2297 remote->remotes[i].battery.battery = NULL;
2298 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2303 static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2306 struct wacom_remote *remote = wacom->remote;
2307 struct device *dev = &wacom->hdev->dev;
2310 /* A remote can pair more than once with an EKR,
2311 * check to make sure this serial isn't already paired.
2313 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2314 if (remote->remotes[k].serial == serial)
2318 if (k < WACOM_MAX_REMOTES) {
2319 remote->remotes[index].serial = serial;
2323 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2326 error = wacom_remote_create_attr_group(wacom, serial, index);
2330 remote->remotes[index].input = wacom_allocate_input(wacom);
2331 if (!remote->remotes[index].input) {
2335 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2336 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2338 if (!remote->remotes[index].input->name) {
2343 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2348 remote->remotes[index].serial = serial;
2350 error = input_register_device(remote->remotes[index].input);
2354 error = wacom_led_groups_alloc_and_register_one(
2355 &remote->remotes[index].input->dev,
2356 wacom, index, 3, true);
2360 remote->remotes[index].registered = true;
2362 devres_close_group(dev, &remote->remotes[index]);
2366 devres_release_group(dev, &remote->remotes[index]);
2367 remote->remotes[index].serial = 0;
2371 static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2373 struct wacom_remote *remote = wacom->remote;
2376 if (!remote->remotes[index].registered)
2379 if (remote->remotes[index].battery.battery)
2382 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2385 error = __wacom_initialize_battery(wacom,
2386 &wacom->remote->remotes[index].battery);
2393 static void wacom_remote_work(struct work_struct *work)
2395 struct wacom *wacom = container_of(work, struct wacom, remote_work);
2396 struct wacom_remote *remote = wacom->remote;
2397 struct wacom_remote_data data;
2398 unsigned long flags;
2403 spin_lock_irqsave(&remote->remote_lock, flags);
2405 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2407 if (count != sizeof(data)) {
2408 hid_err(wacom->hdev,
2409 "workitem triggered without status available\n");
2410 spin_unlock_irqrestore(&remote->remote_lock, flags);
2414 if (!kfifo_is_empty(&remote->remote_fifo))
2415 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2417 spin_unlock_irqrestore(&remote->remote_lock, flags);
2419 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2420 serial = data.remote[i].serial;
2421 if (data.remote[i].connected) {
2423 if (remote->remotes[i].serial == serial) {
2424 wacom_remote_attach_battery(wacom, i);
2428 if (remote->remotes[i].serial)
2429 wacom_remote_destroy_one(wacom, i);
2431 wacom_remote_create_one(wacom, serial, i);
2433 } else if (remote->remotes[i].serial) {
2434 wacom_remote_destroy_one(wacom, i);
2439 static int wacom_probe(struct hid_device *hdev,
2440 const struct hid_device_id *id)
2442 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2443 struct usb_device *dev = interface_to_usbdev(intf);
2444 struct wacom *wacom;
2445 struct wacom_wac *wacom_wac;
2446 struct wacom_features *features;
2449 if (!id->driver_data)
2452 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2454 /* hid-core sets this quirk for the boot interface */
2455 hdev->quirks &= ~HID_QUIRK_NOGET;
2457 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2461 hid_set_drvdata(hdev, wacom);
2464 wacom_wac = &wacom->wacom_wac;
2465 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2466 features = &wacom_wac->features;
2468 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2473 wacom_wac->hid_data.inputmode = -1;
2474 wacom_wac->mode_report = -1;
2476 wacom->usbdev = dev;
2478 mutex_init(&wacom->lock);
2479 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
2480 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2481 INIT_WORK(&wacom->battery_work, wacom_battery_work);
2482 INIT_WORK(&wacom->remote_work, wacom_remote_work);
2484 /* ask for the report descriptor to be loaded by HID */
2485 error = hid_parse(hdev);
2487 hid_err(hdev, "parse failed\n");
2491 error = wacom_parse_and_register(wacom, false);
2495 if (hdev->bus == BUS_BLUETOOTH) {
2496 error = device_create_file(&hdev->dev, &dev_attr_speed);
2499 "can't create sysfs speed attribute err: %d\n",
2506 hid_set_drvdata(hdev, NULL);
2510 static void wacom_remove(struct hid_device *hdev)
2512 struct wacom *wacom = hid_get_drvdata(hdev);
2513 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2514 struct wacom_features *features = &wacom_wac->features;
2516 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2521 cancel_delayed_work_sync(&wacom->init_work);
2522 cancel_work_sync(&wacom->wireless_work);
2523 cancel_work_sync(&wacom->battery_work);
2524 cancel_work_sync(&wacom->remote_work);
2525 if (hdev->bus == BUS_BLUETOOTH)
2526 device_remove_file(&hdev->dev, &dev_attr_speed);
2528 /* make sure we don't trigger the LEDs */
2529 wacom_led_groups_release(wacom);
2530 wacom_release_resources(wacom);
2532 hid_set_drvdata(hdev, NULL);
2536 static int wacom_resume(struct hid_device *hdev)
2538 struct wacom *wacom = hid_get_drvdata(hdev);
2540 mutex_lock(&wacom->lock);
2542 /* switch to wacom mode first */
2543 _wacom_query_tablet_data(wacom);
2544 wacom_led_control(wacom);
2546 mutex_unlock(&wacom->lock);
2551 static int wacom_reset_resume(struct hid_device *hdev)
2553 return wacom_resume(hdev);
2555 #endif /* CONFIG_PM */
2557 static struct hid_driver wacom_driver = {
2559 .id_table = wacom_ids,
2560 .probe = wacom_probe,
2561 .remove = wacom_remove,
2562 .report = wacom_wac_report,
2564 .resume = wacom_resume,
2565 .reset_resume = wacom_reset_resume,
2567 .raw_event = wacom_raw_event,
2569 module_hid_driver(wacom_driver);
2571 MODULE_VERSION(DRIVER_VERSION);
2572 MODULE_AUTHOR(DRIVER_AUTHOR);
2573 MODULE_DESCRIPTION(DRIVER_DESC);
2574 MODULE_LICENSE(DRIVER_LICENSE);