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 buf = kzalloc(buf_size, GFP_KERNEL);
763 if (wacom->wacom_wac.features.type >= INTUOS5S &&
764 wacom->wacom_wac.features.type <= INTUOSPL) {
766 * Touch Ring and crop mark LED luminance may take on
767 * one of four values:
768 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
770 int ring_led = wacom->led.groups[0].select & 0x03;
771 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
773 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
776 if (wacom->wacom_wac.pid) {
777 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
778 buf, buf_size, WAC_CMD_RETRIES);
785 int led = wacom->led.groups[0].select | 0x4;
787 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
788 wacom->wacom_wac.features.type == WACOM_24HD)
789 led |= (wacom->led.groups[1].select << 4) | 0x40;
793 buf[2] = wacom->led.llv;
794 buf[3] = wacom->led.hlv;
795 buf[4] = wacom->led.img_lum;
798 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
805 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
806 const unsigned len, const void *img)
810 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
812 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
816 /* Send 'start' command */
817 buf[0] = WAC_CMD_ICON_START;
819 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
825 buf[1] = button_id & 0x07;
826 for (i = 0; i < 4; i++) {
828 memcpy(buf + 3, img + i * chunk_len, chunk_len);
830 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
831 buf, chunk_len + 3, WAC_CMD_RETRIES);
837 buf[0] = WAC_CMD_ICON_START;
839 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
847 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
848 const char *buf, size_t count)
850 struct hid_device *hdev = to_hid_device(dev);
851 struct wacom *wacom = hid_get_drvdata(hdev);
855 err = kstrtouint(buf, 10, &id);
859 mutex_lock(&wacom->lock);
861 wacom->led.groups[set_id].select = id & 0x3;
862 err = wacom_led_control(wacom);
864 mutex_unlock(&wacom->lock);
866 return err < 0 ? err : count;
869 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
870 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
871 struct device_attribute *attr, const char *buf, size_t count) \
873 return wacom_led_select_store(dev, SET_ID, buf, count); \
875 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
876 struct device_attribute *attr, char *buf) \
878 struct hid_device *hdev = to_hid_device(dev);\
879 struct wacom *wacom = hid_get_drvdata(hdev); \
880 return scnprintf(buf, PAGE_SIZE, "%d\n", \
881 wacom->led.groups[SET_ID].select); \
883 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
884 wacom_led##SET_ID##_select_show, \
885 wacom_led##SET_ID##_select_store)
887 DEVICE_LED_SELECT_ATTR(0);
888 DEVICE_LED_SELECT_ATTR(1);
890 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
891 const char *buf, size_t count)
896 err = kstrtouint(buf, 10, &value);
900 mutex_lock(&wacom->lock);
902 *dest = value & 0x7f;
903 err = wacom_led_control(wacom);
905 mutex_unlock(&wacom->lock);
907 return err < 0 ? err : count;
910 #define DEVICE_LUMINANCE_ATTR(name, field) \
911 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
912 struct device_attribute *attr, const char *buf, size_t count) \
914 struct hid_device *hdev = to_hid_device(dev);\
915 struct wacom *wacom = hid_get_drvdata(hdev); \
917 return wacom_luminance_store(wacom, &wacom->led.field, \
920 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
921 struct device_attribute *attr, char *buf) \
923 struct wacom *wacom = dev_get_drvdata(dev); \
924 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
926 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
927 wacom_##name##_luminance_show, \
928 wacom_##name##_luminance_store)
930 DEVICE_LUMINANCE_ATTR(status0, llv);
931 DEVICE_LUMINANCE_ATTR(status1, hlv);
932 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
934 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
935 const char *buf, size_t count)
937 struct hid_device *hdev = to_hid_device(dev);
938 struct wacom *wacom = hid_get_drvdata(hdev);
943 if (hdev->bus == BUS_BLUETOOTH) {
945 xfer_id = WAC_CMD_ICON_BT_XFER;
948 xfer_id = WAC_CMD_ICON_XFER;
954 mutex_lock(&wacom->lock);
956 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
958 mutex_unlock(&wacom->lock);
960 return err < 0 ? err : count;
963 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
964 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
965 struct device_attribute *attr, const char *buf, size_t count) \
967 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
969 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
970 NULL, wacom_btnimg##BUTTON_ID##_store)
972 DEVICE_BTNIMG_ATTR(0);
973 DEVICE_BTNIMG_ATTR(1);
974 DEVICE_BTNIMG_ATTR(2);
975 DEVICE_BTNIMG_ATTR(3);
976 DEVICE_BTNIMG_ATTR(4);
977 DEVICE_BTNIMG_ATTR(5);
978 DEVICE_BTNIMG_ATTR(6);
979 DEVICE_BTNIMG_ATTR(7);
981 static struct attribute *cintiq_led_attrs[] = {
982 &dev_attr_status_led0_select.attr,
983 &dev_attr_status_led1_select.attr,
987 static struct attribute_group cintiq_led_attr_group = {
989 .attrs = cintiq_led_attrs,
992 static struct attribute *intuos4_led_attrs[] = {
993 &dev_attr_status0_luminance.attr,
994 &dev_attr_status1_luminance.attr,
995 &dev_attr_status_led0_select.attr,
996 &dev_attr_buttons_luminance.attr,
997 &dev_attr_button0_rawimg.attr,
998 &dev_attr_button1_rawimg.attr,
999 &dev_attr_button2_rawimg.attr,
1000 &dev_attr_button3_rawimg.attr,
1001 &dev_attr_button4_rawimg.attr,
1002 &dev_attr_button5_rawimg.attr,
1003 &dev_attr_button6_rawimg.attr,
1004 &dev_attr_button7_rawimg.attr,
1008 static struct attribute_group intuos4_led_attr_group = {
1009 .name = "wacom_led",
1010 .attrs = intuos4_led_attrs,
1013 static struct attribute *intuos5_led_attrs[] = {
1014 &dev_attr_status0_luminance.attr,
1015 &dev_attr_status_led0_select.attr,
1019 static struct attribute_group intuos5_led_attr_group = {
1020 .name = "wacom_led",
1021 .attrs = intuos5_led_attrs,
1024 struct wacom_sysfs_group_devres {
1025 struct attribute_group *group;
1026 struct kobject *root;
1029 static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1031 struct wacom_sysfs_group_devres *devres = res;
1032 struct kobject *kobj = devres->root;
1034 dev_dbg(dev, "%s: dropping reference to %s\n",
1035 __func__, devres->group->name);
1036 sysfs_remove_group(kobj, devres->group);
1039 static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1040 struct kobject *root,
1041 struct attribute_group *group)
1043 struct wacom_sysfs_group_devres *devres;
1046 devres = devres_alloc(wacom_devm_sysfs_group_release,
1047 sizeof(struct wacom_sysfs_group_devres),
1052 devres->group = group;
1053 devres->root = root;
1055 error = sysfs_create_group(devres->root, group);
1059 devres_add(&wacom->hdev->dev, devres);
1064 static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1065 struct attribute_group *group)
1067 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1071 enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
1073 struct wacom *wacom = led->wacom;
1075 if (wacom->led.max_hlv)
1076 return led->hlv * LED_FULL / wacom->led.max_hlv;
1078 if (wacom->led.max_llv)
1079 return led->llv * LED_FULL / wacom->led.max_llv;
1081 /* device doesn't support brightness tuning */
1085 static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1087 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1088 struct wacom *wacom = led->wacom;
1090 if (wacom->led.groups[led->group].select != led->id)
1093 return wacom_leds_brightness_get(led);
1096 static int wacom_led_brightness_set(struct led_classdev *cdev,
1097 enum led_brightness brightness)
1099 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1100 struct wacom *wacom = led->wacom;
1103 mutex_lock(&wacom->lock);
1105 if (!wacom->led.groups || (brightness == LED_OFF &&
1106 wacom->led.groups[led->group].select != led->id)) {
1111 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1112 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1114 wacom->led.groups[led->group].select = led->id;
1116 error = wacom_led_control(wacom);
1119 mutex_unlock(&wacom->lock);
1124 static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1125 enum led_brightness brightness)
1129 static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1130 struct wacom_led *led, unsigned int group,
1131 unsigned int id, bool read_only)
1136 name = devm_kasprintf(dev, GFP_KERNEL,
1145 led->trigger.name = name;
1146 error = devm_led_trigger_register(dev, &led->trigger);
1148 hid_err(wacom->hdev,
1149 "failed to register LED trigger %s: %d\n",
1150 led->cdev.name, error);
1158 led->llv = wacom->led.llv;
1159 led->hlv = wacom->led.hlv;
1160 led->cdev.name = name;
1161 led->cdev.max_brightness = LED_FULL;
1162 led->cdev.flags = LED_HW_PLUGGABLE;
1163 led->cdev.brightness_get = __wacom_led_brightness_get;
1165 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
1166 led->cdev.default_trigger = led->cdev.name;
1168 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
1171 error = devm_led_classdev_register(dev, &led->cdev);
1173 hid_err(wacom->hdev,
1174 "failed to register LED %s: %d\n",
1175 led->cdev.name, error);
1176 led->cdev.name = NULL;
1183 static void wacom_led_groups_release_one(void *data)
1185 struct wacom_group_leds *group = data;
1187 devres_release_group(group->dev, group);
1190 static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1191 struct wacom *wacom,
1192 int group_id, int count,
1195 struct wacom_led *leds;
1198 if (group_id >= wacom->led.count || count <= 0)
1201 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1204 leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
1210 wacom->led.groups[group_id].leds = leds;
1211 wacom->led.groups[group_id].count = count;
1213 for (i = 0; i < count; i++) {
1214 error = wacom_led_register_one(dev, wacom, &leds[i],
1215 group_id, i, read_only);
1220 wacom->led.groups[group_id].dev = dev;
1222 devres_close_group(dev, &wacom->led.groups[group_id]);
1225 * There is a bug (?) in devm_led_classdev_register() in which its
1226 * increments the refcount of the parent. If the parent is an input
1227 * device, that means the ref count never reaches 0 when
1228 * devm_input_device_release() gets called.
1229 * This means that the LEDs are still there after disconnect.
1230 * Manually force the release of the group so that the leds are released
1231 * once we are done using them.
1233 error = devm_add_action_or_reset(&wacom->hdev->dev,
1234 wacom_led_groups_release_one,
1235 &wacom->led.groups[group_id]);
1242 devres_release_group(dev, &wacom->led.groups[group_id]);
1246 struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1249 struct wacom_group_leds *group;
1251 if (group_id >= wacom->led.count)
1254 group = &wacom->led.groups[group_id];
1261 return &group->leds[id];
1265 * wacom_led_next: gives the next available led with a wacom trigger.
1267 * returns the next available struct wacom_led which has its default trigger
1268 * or the current one if none is available.
1270 struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1272 struct wacom_led *next_led;
1282 next_led = wacom_led_find(wacom, group, ++next);
1283 if (!next_led || next_led == cur)
1285 } while (next_led->cdev.trigger != &next_led->trigger);
1290 static void wacom_led_groups_release(void *data)
1292 struct wacom *wacom = data;
1294 wacom->led.groups = NULL;
1295 wacom->led.count = 0;
1298 static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1300 struct device *dev = &wacom->hdev->dev;
1301 struct wacom_group_leds *groups;
1304 groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
1309 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1313 wacom->led.groups = groups;
1314 wacom->led.count = count;
1319 static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1320 int led_per_group, bool read_only)
1325 if (!wacom->wacom_wac.pad_input)
1328 dev = &wacom->wacom_wac.pad_input->dev;
1330 error = wacom_led_groups_allocate(wacom, group_count);
1334 for (i = 0; i < group_count; i++) {
1335 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1345 static int wacom_initialize_leds(struct wacom *wacom)
1349 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1352 /* Initialize default values */
1353 switch (wacom->wacom_wac.features.type) {
1358 wacom->led.llv = 10;
1359 wacom->led.hlv = 20;
1360 wacom->led.max_llv = 127;
1361 wacom->led.max_hlv = 127;
1362 wacom->led.img_lum = 10;
1364 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1366 hid_err(wacom->hdev,
1367 "cannot create leds err: %d\n", error);
1371 error = wacom_devm_sysfs_create_group(wacom,
1372 &intuos4_led_attr_group);
1379 wacom->led.img_lum = 0;
1381 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1383 hid_err(wacom->hdev,
1384 "cannot create leds err: %d\n", error);
1388 error = wacom_devm_sysfs_create_group(wacom,
1389 &cintiq_led_attr_group);
1398 wacom->led.llv = 32;
1399 wacom->led.max_llv = 96;
1401 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1403 hid_err(wacom->hdev,
1404 "cannot create leds err: %d\n", error);
1408 error = wacom_devm_sysfs_create_group(wacom,
1409 &intuos5_led_attr_group);
1413 wacom->led.llv = 255;
1414 wacom->led.max_llv = 255;
1415 error = wacom_led_groups_allocate(wacom, 5);
1417 hid_err(wacom->hdev,
1418 "cannot create leds err: %d\n", error);
1428 hid_err(wacom->hdev,
1429 "cannot create sysfs group err: %d\n", error);
1436 static void wacom_init_work(struct work_struct *work)
1438 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1440 _wacom_query_tablet_data(wacom);
1441 wacom_led_control(wacom);
1444 static void wacom_query_tablet_data(struct wacom *wacom)
1446 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1449 static enum power_supply_property wacom_battery_props[] = {
1450 POWER_SUPPLY_PROP_MODEL_NAME,
1451 POWER_SUPPLY_PROP_PRESENT,
1452 POWER_SUPPLY_PROP_STATUS,
1453 POWER_SUPPLY_PROP_SCOPE,
1454 POWER_SUPPLY_PROP_CAPACITY
1457 static int wacom_battery_get_property(struct power_supply *psy,
1458 enum power_supply_property psp,
1459 union power_supply_propval *val)
1461 struct wacom_battery *battery = power_supply_get_drvdata(psy);
1465 case POWER_SUPPLY_PROP_MODEL_NAME:
1466 val->strval = battery->wacom->wacom_wac.name;
1468 case POWER_SUPPLY_PROP_PRESENT:
1469 val->intval = battery->bat_connected;
1471 case POWER_SUPPLY_PROP_SCOPE:
1472 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1474 case POWER_SUPPLY_PROP_CAPACITY:
1475 val->intval = battery->battery_capacity;
1477 case POWER_SUPPLY_PROP_STATUS:
1478 if (battery->bat_charging)
1479 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1480 else if (battery->battery_capacity == 100 &&
1481 battery->ps_connected)
1482 val->intval = POWER_SUPPLY_STATUS_FULL;
1483 else if (battery->ps_connected)
1484 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1486 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1496 static int __wacom_initialize_battery(struct wacom *wacom,
1497 struct wacom_battery *battery)
1499 static atomic_t battery_no = ATOMIC_INIT(0);
1500 struct device *dev = &wacom->hdev->dev;
1501 struct power_supply_config psy_cfg = { .drv_data = battery, };
1502 struct power_supply *ps_bat;
1503 struct power_supply_desc *bat_desc = &battery->bat_desc;
1507 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1510 battery->wacom = wacom;
1512 n = atomic_inc_return(&battery_no) - 1;
1514 bat_desc->properties = wacom_battery_props;
1515 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1516 bat_desc->get_property = wacom_battery_get_property;
1517 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1518 bat_desc->name = battery->bat_name;
1519 bat_desc->type = POWER_SUPPLY_TYPE_USB;
1520 bat_desc->use_for_apm = 0;
1522 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1523 if (IS_ERR(ps_bat)) {
1524 error = PTR_ERR(ps_bat);
1528 power_supply_powers(ps_bat, &wacom->hdev->dev);
1530 battery->battery = ps_bat;
1532 devres_close_group(dev, bat_desc);
1536 devres_release_group(dev, bat_desc);
1540 static int wacom_initialize_battery(struct wacom *wacom)
1542 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1543 return __wacom_initialize_battery(wacom, &wacom->battery);
1548 static void wacom_destroy_battery(struct wacom *wacom)
1550 if (wacom->battery.battery) {
1551 devres_release_group(&wacom->hdev->dev,
1552 &wacom->battery.bat_desc);
1553 wacom->battery.battery = NULL;
1557 static ssize_t wacom_show_speed(struct device *dev,
1558 struct device_attribute
1561 struct hid_device *hdev = to_hid_device(dev);
1562 struct wacom *wacom = hid_get_drvdata(hdev);
1564 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1567 static ssize_t wacom_store_speed(struct device *dev,
1568 struct device_attribute *attr,
1569 const char *buf, size_t count)
1571 struct hid_device *hdev = to_hid_device(dev);
1572 struct wacom *wacom = hid_get_drvdata(hdev);
1575 if (kstrtou8(buf, 0, &new_speed))
1578 if (new_speed != 0 && new_speed != 1)
1581 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1586 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1587 wacom_show_speed, wacom_store_speed);
1590 static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1591 struct kobj_attribute *kattr,
1592 char *buf, int index)
1594 struct device *dev = kobj_to_dev(kobj->parent);
1595 struct hid_device *hdev = to_hid_device(dev);
1596 struct wacom *wacom = hid_get_drvdata(hdev);
1599 mode = wacom->led.groups[index].select;
1600 if (mode >= 0 && mode < 3)
1601 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
1603 return snprintf(buf, PAGE_SIZE, "%d\n", -1);
1606 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1607 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1608 struct kobj_attribute *kattr, char *buf) \
1610 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1612 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1613 .attr = {.name = "remote_mode", \
1614 .mode = DEV_ATTR_RO_PERM}, \
1615 .show = wacom_show_remote##SET_ID##_mode, \
1617 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1618 &remote##SET_ID##_mode_attr.attr, \
1621 static struct attribute_group remote##SET_ID##_serial_group = { \
1623 .attrs = remote##SET_ID##_serial_attrs, \
1626 DEVICE_EKR_ATTR_GROUP(0);
1627 DEVICE_EKR_ATTR_GROUP(1);
1628 DEVICE_EKR_ATTR_GROUP(2);
1629 DEVICE_EKR_ATTR_GROUP(3);
1630 DEVICE_EKR_ATTR_GROUP(4);
1632 static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1636 struct wacom_remote *remote = wacom->remote;
1638 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1641 if (!remote->remotes[index].group.name)
1644 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1645 &remote->remotes[index].group);
1647 remote->remotes[index].group.name = NULL;
1648 hid_err(wacom->hdev,
1649 "cannot create sysfs group err: %d\n", error);
1656 static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1658 const size_t buf_size = 2;
1662 buf = kzalloc(buf_size, GFP_KERNEL);
1666 buf[0] = WAC_CMD_DELETE_PAIRING;
1669 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1670 buf_size, WAC_CMD_RETRIES);
1676 static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1677 struct kobj_attribute *attr,
1678 const char *buf, size_t count)
1680 unsigned char selector = 0;
1681 struct device *dev = kobj_to_dev(kobj->parent);
1682 struct hid_device *hdev = to_hid_device(dev);
1683 struct wacom *wacom = hid_get_drvdata(hdev);
1686 if (!strncmp(buf, "*\n", 2)) {
1687 selector = WAC_CMD_UNPAIR_ALL;
1689 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1694 mutex_lock(&wacom->lock);
1696 err = wacom_cmd_unpair_remote(wacom, selector);
1697 mutex_unlock(&wacom->lock);
1699 return err < 0 ? err : count;
1702 static struct kobj_attribute unpair_remote_attr = {
1703 .attr = {.name = "unpair_remote", .mode = 0200},
1704 .store = wacom_store_unpair_remote,
1707 static const struct attribute *remote_unpair_attrs[] = {
1708 &unpair_remote_attr.attr,
1712 static void wacom_remotes_destroy(void *data)
1714 struct wacom *wacom = data;
1715 struct wacom_remote *remote = wacom->remote;
1720 kobject_put(remote->remote_dir);
1721 kfifo_free(&remote->remote_fifo);
1722 wacom->remote = NULL;
1725 static int wacom_initialize_remotes(struct wacom *wacom)
1728 struct wacom_remote *remote;
1731 if (wacom->wacom_wac.features.type != REMOTE)
1734 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1739 wacom->remote = remote;
1741 spin_lock_init(&remote->remote_lock);
1743 error = kfifo_alloc(&remote->remote_fifo,
1744 5 * sizeof(struct wacom_remote_data),
1747 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1751 remote->remotes[0].group = remote0_serial_group;
1752 remote->remotes[1].group = remote1_serial_group;
1753 remote->remotes[2].group = remote2_serial_group;
1754 remote->remotes[3].group = remote3_serial_group;
1755 remote->remotes[4].group = remote4_serial_group;
1757 remote->remote_dir = kobject_create_and_add("wacom_remote",
1758 &wacom->hdev->dev.kobj);
1759 if (!remote->remote_dir)
1762 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
1765 hid_err(wacom->hdev,
1766 "cannot create sysfs group err: %d\n", error);
1770 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1771 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
1772 remote->remotes[i].serial = 0;
1775 error = devm_add_action_or_reset(&wacom->hdev->dev,
1776 wacom_remotes_destroy, wacom);
1783 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1785 struct input_dev *input_dev;
1786 struct hid_device *hdev = wacom->hdev;
1787 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1789 input_dev = devm_input_allocate_device(&hdev->dev);
1793 input_dev->name = wacom_wac->features.name;
1794 input_dev->phys = hdev->phys;
1795 input_dev->dev.parent = &hdev->dev;
1796 input_dev->open = wacom_open;
1797 input_dev->close = wacom_close;
1798 input_dev->uniq = hdev->uniq;
1799 input_dev->id.bustype = hdev->bus;
1800 input_dev->id.vendor = hdev->vendor;
1801 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1802 input_dev->id.version = hdev->version;
1803 input_set_drvdata(input_dev, wacom);
1808 static int wacom_allocate_inputs(struct wacom *wacom)
1810 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1812 wacom_wac->pen_input = wacom_allocate_input(wacom);
1813 wacom_wac->touch_input = wacom_allocate_input(wacom);
1814 wacom_wac->pad_input = wacom_allocate_input(wacom);
1815 if (!wacom_wac->pen_input ||
1816 !wacom_wac->touch_input ||
1817 !wacom_wac->pad_input)
1820 wacom_wac->pen_input->name = wacom_wac->pen_name;
1821 wacom_wac->touch_input->name = wacom_wac->touch_name;
1822 wacom_wac->pad_input->name = wacom_wac->pad_name;
1827 static int wacom_register_inputs(struct wacom *wacom)
1829 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
1830 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1833 pen_input_dev = wacom_wac->pen_input;
1834 touch_input_dev = wacom_wac->touch_input;
1835 pad_input_dev = wacom_wac->pad_input;
1837 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
1840 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1842 /* no pen in use on this interface */
1843 input_free_device(pen_input_dev);
1844 wacom_wac->pen_input = NULL;
1845 pen_input_dev = NULL;
1847 error = input_register_device(pen_input_dev);
1852 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1854 /* no touch in use on this interface */
1855 input_free_device(touch_input_dev);
1856 wacom_wac->touch_input = NULL;
1857 touch_input_dev = NULL;
1859 error = input_register_device(touch_input_dev);
1864 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1866 /* no pad in use on this interface */
1867 input_free_device(pad_input_dev);
1868 wacom_wac->pad_input = NULL;
1869 pad_input_dev = NULL;
1871 error = input_register_device(pad_input_dev);
1879 wacom_wac->pad_input = NULL;
1880 wacom_wac->touch_input = NULL;
1881 wacom_wac->pen_input = NULL;
1886 * Not all devices report physical dimensions from HID.
1887 * Compute the default from hardcoded logical dimension
1888 * and resolution before driver overwrites them.
1890 static void wacom_set_default_phy(struct wacom_features *features)
1892 if (features->x_resolution) {
1893 features->x_phy = (features->x_max * 100) /
1894 features->x_resolution;
1895 features->y_phy = (features->y_max * 100) /
1896 features->y_resolution;
1900 static void wacom_calculate_res(struct wacom_features *features)
1902 /* set unit to "100th of a mm" for devices not reported by HID */
1903 if (!features->unit) {
1904 features->unit = 0x11;
1905 features->unitExpo = -3;
1908 features->x_resolution = wacom_calc_hid_res(features->x_max,
1911 features->unitExpo);
1912 features->y_resolution = wacom_calc_hid_res(features->y_max,
1915 features->unitExpo);
1918 void wacom_battery_work(struct work_struct *work)
1920 struct wacom *wacom = container_of(work, struct wacom, battery_work);
1922 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1923 !wacom->battery.battery) {
1924 wacom_initialize_battery(wacom);
1926 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1927 wacom->battery.battery) {
1928 wacom_destroy_battery(wacom);
1932 static size_t wacom_compute_pktlen(struct hid_device *hdev)
1934 struct hid_report_enum *report_enum;
1935 struct hid_report *report;
1938 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1940 list_for_each_entry(report, &report_enum->report_list, list) {
1941 size_t report_size = hid_report_len(report);
1942 if (report_size > size)
1949 static void wacom_update_name(struct wacom *wacom, const char *suffix)
1951 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1952 struct wacom_features *features = &wacom_wac->features;
1953 char name[WACOM_NAME_MAX];
1955 /* Generic devices name unspecified */
1956 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1957 if (strstr(wacom->hdev->name, "Wacom") ||
1958 strstr(wacom->hdev->name, "wacom") ||
1959 strstr(wacom->hdev->name, "WACOM")) {
1960 /* name is in HID descriptor, use it */
1961 strlcpy(name, wacom->hdev->name, sizeof(name));
1963 /* strip out excess whitespaces */
1965 char *gap = strstr(name, " ");
1968 /* shift everything including the terminator */
1969 memmove(gap, gap+1, strlen(gap));
1972 /* strip off excessive prefixing */
1973 if (strstr(name, "Wacom Co.,Ltd. Wacom ") == name) {
1974 int n = strlen(name);
1975 int x = strlen("Wacom Co.,Ltd. ");
1976 memmove(name, name+x, n-x+1);
1978 if (strstr(name, "Wacom Co., Ltd. Wacom ") == name) {
1979 int n = strlen(name);
1980 int x = strlen("Wacom Co., Ltd. ");
1981 memmove(name, name+x, n-x+1);
1984 /* get rid of trailing whitespace */
1985 if (name[strlen(name)-1] == ' ')
1986 name[strlen(name)-1] = '\0';
1988 /* no meaningful name retrieved. use product ID */
1989 snprintf(name, sizeof(name),
1990 "%s %X", features->name, wacom->hdev->product);
1993 strlcpy(name, features->name, sizeof(name));
1996 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
1999 /* Append the device type to the name */
2000 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
2001 "%s%s Pen", name, suffix);
2002 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
2003 "%s%s Finger", name, suffix);
2004 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
2005 "%s%s Pad", name, suffix);
2008 static void wacom_release_resources(struct wacom *wacom)
2010 struct hid_device *hdev = wacom->hdev;
2012 if (!wacom->resources)
2015 devres_release_group(&hdev->dev, wacom);
2017 wacom->resources = false;
2019 wacom->wacom_wac.pen_input = NULL;
2020 wacom->wacom_wac.touch_input = NULL;
2021 wacom->wacom_wac.pad_input = NULL;
2024 static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
2026 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2027 struct wacom_features *features = &wacom_wac->features;
2028 struct hid_device *hdev = wacom->hdev;
2030 unsigned int connect_mask = HID_CONNECT_HIDRAW;
2032 features->pktlen = wacom_compute_pktlen(hdev);
2033 if (features->pktlen > WACOM_PKGLEN_MAX)
2036 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2039 wacom->resources = true;
2041 error = wacom_allocate_inputs(wacom);
2046 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2047 * into debug mode for the touch part.
2048 * We ignore the other interfaces.
2050 if (features->type == BAMBOO_PAD) {
2051 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2052 features->type = HID_GENERIC;
2053 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2054 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2060 /* set the default size in case we do not get them from hid */
2061 wacom_set_default_phy(features);
2063 /* Retrieve the physical and logical size for touch devices */
2064 wacom_retrieve_hid_descriptor(hdev, features);
2065 wacom_setup_device_quirks(wacom);
2067 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2068 features->type != WIRELESS) {
2069 error = features->type == HID_GENERIC ? -ENODEV : 0;
2071 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
2073 error ? "Ignoring" : "Assuming pen");
2078 features->device_type |= WACOM_DEVICETYPE_PEN;
2081 wacom_calculate_res(features);
2083 wacom_update_name(wacom, wireless ? " (WL)" : "");
2085 error = wacom_add_shared_data(hdev);
2089 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2090 (features->quirks & WACOM_QUIRK_BATTERY)) {
2091 error = wacom_initialize_battery(wacom);
2096 error = wacom_register_inputs(wacom);
2100 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
2101 error = wacom_initialize_leds(wacom);
2105 error = wacom_initialize_remotes(wacom);
2110 if (features->type == HID_GENERIC)
2111 connect_mask |= HID_CONNECT_DRIVER;
2113 /* Regular HID work starts now */
2114 error = hid_hw_start(hdev, connect_mask);
2116 hid_err(hdev, "hw start failed\n");
2121 /* Note that if query fails it is not a hard failure */
2122 wacom_query_tablet_data(wacom);
2125 /* touch only Bamboo doesn't support pen */
2126 if ((features->type == BAMBOO_TOUCH) &&
2127 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2132 /* pen only Bamboo neither support touch nor pad */
2133 if ((features->type == BAMBOO_PEN) &&
2134 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2135 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2140 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2141 error = hid_hw_open(hdev);
2143 if ((wacom_wac->features.type == INTUOSHT ||
2144 wacom_wac->features.type == INTUOSHT2) &&
2145 (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
2146 wacom_wac->shared->type = wacom_wac->features.type;
2147 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2150 devres_close_group(&hdev->dev, wacom);
2157 wacom_release_resources(wacom);
2161 static void wacom_wireless_work(struct work_struct *work)
2163 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2164 struct usb_device *usbdev = wacom->usbdev;
2165 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2166 struct hid_device *hdev1, *hdev2;
2167 struct wacom *wacom1, *wacom2;
2168 struct wacom_wac *wacom_wac1, *wacom_wac2;
2172 * Regardless if this is a disconnect or a new tablet,
2173 * remove any existing input and battery devices.
2176 wacom_destroy_battery(wacom);
2178 /* Stylus interface */
2179 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2180 wacom1 = hid_get_drvdata(hdev1);
2181 wacom_wac1 = &(wacom1->wacom_wac);
2182 wacom_release_resources(wacom1);
2184 /* Touch interface */
2185 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2186 wacom2 = hid_get_drvdata(hdev2);
2187 wacom_wac2 = &(wacom2->wacom_wac);
2188 wacom_release_resources(wacom2);
2190 if (wacom_wac->pid == 0) {
2191 hid_info(wacom->hdev, "wireless tablet disconnected\n");
2193 const struct hid_device_id *id = wacom_ids;
2195 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2199 if (id->vendor == USB_VENDOR_ID_WACOM &&
2200 id->product == wacom_wac->pid)
2206 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2210 /* Stylus interface */
2211 wacom_wac1->features =
2212 *((struct wacom_features *)id->driver_data);
2214 wacom_wac1->pid = wacom_wac->pid;
2216 error = wacom_parse_and_register(wacom1, true);
2220 /* Touch interface */
2221 if (wacom_wac1->features.touch_max ||
2222 (wacom_wac1->features.type >= INTUOSHT &&
2223 wacom_wac1->features.type <= BAMBOO_PT)) {
2224 wacom_wac2->features =
2225 *((struct wacom_features *)id->driver_data);
2226 wacom_wac2->pid = wacom_wac->pid;
2228 error = wacom_parse_and_register(wacom2, true);
2233 strlcpy(wacom_wac->name, wacom_wac1->name,
2234 sizeof(wacom_wac->name));
2235 error = wacom_initialize_battery(wacom);
2243 wacom_release_resources(wacom1);
2244 wacom_release_resources(wacom2);
2248 static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2250 struct wacom_remote *remote = wacom->remote;
2251 u32 serial = remote->remotes[index].serial;
2253 unsigned long flags;
2255 spin_lock_irqsave(&remote->remote_lock, flags);
2256 remote->remotes[index].registered = false;
2257 spin_unlock_irqrestore(&remote->remote_lock, flags);
2259 if (remote->remotes[index].battery.battery)
2260 devres_release_group(&wacom->hdev->dev,
2261 &remote->remotes[index].battery.bat_desc);
2263 if (remote->remotes[index].group.name)
2264 devres_release_group(&wacom->hdev->dev,
2265 &remote->remotes[index]);
2267 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2268 if (remote->remotes[i].serial == serial) {
2269 remote->remotes[i].serial = 0;
2270 remote->remotes[i].group.name = NULL;
2271 remote->remotes[i].registered = false;
2272 remote->remotes[i].battery.battery = NULL;
2273 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2278 static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2281 struct wacom_remote *remote = wacom->remote;
2282 struct device *dev = &wacom->hdev->dev;
2285 /* A remote can pair more than once with an EKR,
2286 * check to make sure this serial isn't already paired.
2288 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2289 if (remote->remotes[k].serial == serial)
2293 if (k < WACOM_MAX_REMOTES) {
2294 remote->remotes[index].serial = serial;
2298 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2301 error = wacom_remote_create_attr_group(wacom, serial, index);
2305 remote->remotes[index].input = wacom_allocate_input(wacom);
2306 if (!remote->remotes[index].input) {
2310 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2311 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2313 if (!remote->remotes[index].input->name) {
2318 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2323 remote->remotes[index].serial = serial;
2325 error = input_register_device(remote->remotes[index].input);
2329 error = wacom_led_groups_alloc_and_register_one(
2330 &remote->remotes[index].input->dev,
2331 wacom, index, 3, true);
2335 remote->remotes[index].registered = true;
2337 devres_close_group(dev, &remote->remotes[index]);
2341 devres_release_group(dev, &remote->remotes[index]);
2342 remote->remotes[index].serial = 0;
2346 static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2348 struct wacom_remote *remote = wacom->remote;
2351 if (!remote->remotes[index].registered)
2354 if (remote->remotes[index].battery.battery)
2357 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2360 error = __wacom_initialize_battery(wacom,
2361 &wacom->remote->remotes[index].battery);
2368 static void wacom_remote_work(struct work_struct *work)
2370 struct wacom *wacom = container_of(work, struct wacom, remote_work);
2371 struct wacom_remote *remote = wacom->remote;
2372 struct wacom_remote_data data;
2373 unsigned long flags;
2378 spin_lock_irqsave(&remote->remote_lock, flags);
2380 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2382 if (count != sizeof(data)) {
2383 hid_err(wacom->hdev,
2384 "workitem triggered without status available\n");
2385 spin_unlock_irqrestore(&remote->remote_lock, flags);
2389 if (!kfifo_is_empty(&remote->remote_fifo))
2390 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2392 spin_unlock_irqrestore(&remote->remote_lock, flags);
2394 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2395 serial = data.remote[i].serial;
2396 if (data.remote[i].connected) {
2398 if (remote->remotes[i].serial == serial) {
2399 wacom_remote_attach_battery(wacom, i);
2403 if (remote->remotes[i].serial)
2404 wacom_remote_destroy_one(wacom, i);
2406 wacom_remote_create_one(wacom, serial, i);
2408 } else if (remote->remotes[i].serial) {
2409 wacom_remote_destroy_one(wacom, i);
2414 static int wacom_probe(struct hid_device *hdev,
2415 const struct hid_device_id *id)
2417 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2418 struct usb_device *dev = interface_to_usbdev(intf);
2419 struct wacom *wacom;
2420 struct wacom_wac *wacom_wac;
2421 struct wacom_features *features;
2424 if (!id->driver_data)
2427 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2429 /* hid-core sets this quirk for the boot interface */
2430 hdev->quirks &= ~HID_QUIRK_NOGET;
2432 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2436 hid_set_drvdata(hdev, wacom);
2439 wacom_wac = &wacom->wacom_wac;
2440 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2441 features = &wacom_wac->features;
2443 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2448 wacom_wac->hid_data.inputmode = -1;
2449 wacom_wac->mode_report = -1;
2451 wacom->usbdev = dev;
2453 mutex_init(&wacom->lock);
2454 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
2455 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2456 INIT_WORK(&wacom->battery_work, wacom_battery_work);
2457 INIT_WORK(&wacom->remote_work, wacom_remote_work);
2459 /* ask for the report descriptor to be loaded by HID */
2460 error = hid_parse(hdev);
2462 hid_err(hdev, "parse failed\n");
2466 error = wacom_parse_and_register(wacom, false);
2470 if (hdev->bus == BUS_BLUETOOTH) {
2471 error = device_create_file(&hdev->dev, &dev_attr_speed);
2474 "can't create sysfs speed attribute err: %d\n",
2481 hid_set_drvdata(hdev, NULL);
2485 static void wacom_remove(struct hid_device *hdev)
2487 struct wacom *wacom = hid_get_drvdata(hdev);
2488 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2489 struct wacom_features *features = &wacom_wac->features;
2491 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2496 cancel_delayed_work_sync(&wacom->init_work);
2497 cancel_work_sync(&wacom->wireless_work);
2498 cancel_work_sync(&wacom->battery_work);
2499 cancel_work_sync(&wacom->remote_work);
2500 if (hdev->bus == BUS_BLUETOOTH)
2501 device_remove_file(&hdev->dev, &dev_attr_speed);
2503 /* make sure we don't trigger the LEDs */
2504 wacom_led_groups_release(wacom);
2505 wacom_release_resources(wacom);
2507 hid_set_drvdata(hdev, NULL);
2511 static int wacom_resume(struct hid_device *hdev)
2513 struct wacom *wacom = hid_get_drvdata(hdev);
2515 mutex_lock(&wacom->lock);
2517 /* switch to wacom mode first */
2518 _wacom_query_tablet_data(wacom);
2519 wacom_led_control(wacom);
2521 mutex_unlock(&wacom->lock);
2526 static int wacom_reset_resume(struct hid_device *hdev)
2528 return wacom_resume(hdev);
2530 #endif /* CONFIG_PM */
2532 static struct hid_driver wacom_driver = {
2534 .id_table = wacom_ids,
2535 .probe = wacom_probe,
2536 .remove = wacom_remove,
2537 .report = wacom_wac_report,
2539 .resume = wacom_resume,
2540 .reset_resume = wacom_reset_resume,
2542 .raw_event = wacom_raw_event,
2544 module_hid_driver(wacom_driver);
2546 MODULE_VERSION(DRIVER_VERSION);
2547 MODULE_AUTHOR(DRIVER_AUTHOR);
2548 MODULE_DESCRIPTION(DRIVER_DESC);
2549 MODULE_LICENSE(DRIVER_LICENSE);