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
20 #define WAC_CMD_WL_LED_CONTROL 0x03
21 #define WAC_CMD_LED_CONTROL 0x20
22 #define WAC_CMD_ICON_START 0x21
23 #define WAC_CMD_ICON_XFER 0x23
24 #define WAC_CMD_ICON_BT_XFER 0x26
25 #define WAC_CMD_RETRIES 10
26 #define WAC_CMD_DELETE_PAIRING 0x20
27 #define WAC_CMD_UNPAIR_ALL 0xFF
29 #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
30 #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
31 #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
33 static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
34 size_t size, unsigned int retries)
39 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
41 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
44 hid_err(hdev, "wacom_get_report: ran out of retries "
45 "(last error = %d)\n", retval);
50 static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
51 size_t size, unsigned int retries)
56 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
58 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
61 hid_err(hdev, "wacom_set_report: ran out of retries "
62 "(last error = %d)\n", retval);
67 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
68 u8 *raw_data, int size)
70 struct wacom *wacom = hid_get_drvdata(hdev);
72 if (size > WACOM_PKGLEN_MAX)
75 memcpy(wacom->wacom_wac.data, raw_data, size);
77 wacom_wac_irq(&wacom->wacom_wac, size);
82 static int wacom_open(struct input_dev *dev)
84 struct wacom *wacom = input_get_drvdata(dev);
86 return hid_hw_open(wacom->hdev);
89 static void wacom_close(struct input_dev *dev)
91 struct wacom *wacom = input_get_drvdata(dev);
94 * wacom->hdev should never be null, but surprisingly, I had the case
95 * once while unplugging the Wacom Wireless Receiver.
98 hid_hw_close(wacom->hdev);
102 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
104 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
105 unsigned unit, int exponent)
107 struct hid_field field = {
108 .logical_maximum = logical_extents,
109 .physical_maximum = physical_extents,
111 .unit_exponent = exponent,
114 return hidinput_calc_abs_res(&field, ABS_X);
117 static void wacom_feature_mapping(struct hid_device *hdev,
118 struct hid_field *field, struct hid_usage *usage)
120 struct wacom *wacom = hid_get_drvdata(hdev);
121 struct wacom_features *features = &wacom->wacom_wac.features;
122 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
126 switch (usage->hid) {
127 case HID_DG_CONTACTMAX:
128 /* leave touch_max as is if predefined */
129 if (!features->touch_max) {
131 data = kzalloc(2, GFP_KERNEL);
134 data[0] = field->report->id;
135 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
136 data, 2, WAC_CMD_RETRIES);
138 features->touch_max = data[1];
140 features->touch_max = 16;
141 hid_warn(hdev, "wacom_feature_mapping: "
142 "could not get HID_DG_CONTACTMAX, "
143 "defaulting to %d\n",
144 features->touch_max);
149 case HID_DG_INPUTMODE:
150 /* Ignore if value index is out of bounds. */
151 if (usage->usage_index >= field->report_count) {
152 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
156 hid_data->inputmode = field->report->id;
157 hid_data->inputmode_index = usage->usage_index;
160 case HID_UP_DIGITIZER:
161 if (field->report->id == 0x0B &&
162 (field->application == WACOM_HID_G9_PEN ||
163 field->application == WACOM_HID_G11_PEN)) {
164 wacom->wacom_wac.mode_report = field->report->id;
165 wacom->wacom_wac.mode_value = 0;
169 case WACOM_HID_WD_DATAMODE:
170 wacom->wacom_wac.mode_report = field->report->id;
171 wacom->wacom_wac.mode_value = 2;
174 case WACOM_HID_UP_G9:
175 case WACOM_HID_UP_G11:
176 if (field->report->id == 0x03 &&
177 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
178 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
179 wacom->wacom_wac.mode_report = field->report->id;
180 wacom->wacom_wac.mode_value = 0;
187 * Interface Descriptor of wacom devices can be incomplete and
188 * inconsistent so wacom_features table is used to store stylus
189 * device's packet lengths, various maximum values, and tablet
190 * resolution based on product ID's.
192 * For devices that contain 2 interfaces, wacom_features table is
193 * inaccurate for the touch interface. Since the Interface Descriptor
194 * for touch interfaces has pretty complete data, this function exists
195 * to query tablet for this missing information instead of hard coding in
196 * an additional table.
198 * A typical Interface Descriptor for a stylus will contain a
199 * boot mouse application collection that is not of interest and this
200 * function will ignore it.
202 * It also contains a digitizer application collection that also is not
203 * of interest since any information it contains would be duplicate
204 * of what is in wacom_features. Usually it defines a report of an array
205 * of bytes that could be used as max length of the stylus packet returned.
206 * If it happens to define a Digitizer-Stylus Physical Collection then
207 * the X and Y logical values contain valid data but it is ignored.
209 * A typical Interface Descriptor for a touch interface will contain a
210 * Digitizer-Finger Physical Collection which will define both logical
211 * X/Y maximum as well as the physical size of tablet. Since touch
212 * interfaces haven't supported pressure or distance, this is enough
213 * information to override invalid values in the wacom_features table.
215 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
216 * data. We deal with them after returning from this function.
218 static void wacom_usage_mapping(struct hid_device *hdev,
219 struct hid_field *field, struct hid_usage *usage)
221 struct wacom *wacom = hid_get_drvdata(hdev);
222 struct wacom_features *features = &wacom->wacom_wac.features;
223 bool finger = WACOM_FINGER_FIELD(field);
224 bool pen = WACOM_PEN_FIELD(field);
227 * Requiring Stylus Usage will ignore boot mouse
228 * X/Y values and some cases of invalid Digitizer X/Y
229 * values commonly reported.
232 features->device_type |= WACOM_DEVICETYPE_PEN;
234 features->device_type |= WACOM_DEVICETYPE_TOUCH;
239 * Bamboo models do not support HID_DG_CONTACTMAX.
240 * And, Bamboo Pen only descriptor contains touch.
242 if (features->type > BAMBOO_PT) {
243 /* ISDv4 touch devices at least supports one touch point */
244 if (finger && !features->touch_max)
245 features->touch_max = 1;
249 * ISDv4 devices which predate HID's adoption of the
250 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
251 * position instead. We can accurately detect if a
252 * usage with that value should be HID_DG_BARRELSWITCH2
253 * based on the surrounding usages, which have remained
254 * constant across generations.
256 if (features->type == HID_GENERIC &&
257 usage->hid == 0x000D0000 &&
258 field->application == HID_DG_PEN &&
259 field->physical == HID_DG_STYLUS) {
260 int i = usage->usage_index;
262 if (i-4 >= 0 && i+1 < field->maxusage &&
263 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
264 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
265 field->usage[i-2].hid == HID_DG_ERASER &&
266 field->usage[i-1].hid == HID_DG_INVERT &&
267 field->usage[i+1].hid == HID_DG_INRANGE) {
268 usage->hid = HID_DG_BARRELSWITCH2;
272 switch (usage->hid) {
274 features->x_max = field->logical_maximum;
276 features->x_phy = field->physical_maximum;
277 if ((features->type != BAMBOO_PT) &&
278 (features->type != BAMBOO_TOUCH)) {
279 features->unit = field->unit;
280 features->unitExpo = field->unit_exponent;
285 features->y_max = field->logical_maximum;
287 features->y_phy = field->physical_maximum;
288 if ((features->type != BAMBOO_PT) &&
289 (features->type != BAMBOO_TOUCH)) {
290 features->unit = field->unit;
291 features->unitExpo = field->unit_exponent;
295 case HID_DG_TIPPRESSURE:
297 features->pressure_max = field->logical_maximum;
301 if (features->type == HID_GENERIC)
302 wacom_wac_usage_mapping(hdev, field, usage);
305 static void wacom_post_parse_hid(struct hid_device *hdev,
306 struct wacom_features *features)
308 struct wacom *wacom = hid_get_drvdata(hdev);
309 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
311 if (features->type == HID_GENERIC) {
312 /* Any last-minute generic device setup */
313 if (features->touch_max > 1) {
314 input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
320 static void wacom_parse_hid(struct hid_device *hdev,
321 struct wacom_features *features)
323 struct hid_report_enum *rep_enum;
324 struct hid_report *hreport;
327 /* check features first */
328 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
329 list_for_each_entry(hreport, &rep_enum->report_list, list) {
330 for (i = 0; i < hreport->maxfield; i++) {
331 /* Ignore if report count is out of bounds. */
332 if (hreport->field[i]->report_count < 1)
335 for (j = 0; j < hreport->field[i]->maxusage; j++) {
336 wacom_feature_mapping(hdev, hreport->field[i],
337 hreport->field[i]->usage + j);
342 /* now check the input usages */
343 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
344 list_for_each_entry(hreport, &rep_enum->report_list, list) {
346 if (!hreport->maxfield)
349 for (i = 0; i < hreport->maxfield; i++)
350 for (j = 0; j < hreport->field[i]->maxusage; j++)
351 wacom_usage_mapping(hdev, hreport->field[i],
352 hreport->field[i]->usage + j);
355 wacom_post_parse_hid(hdev, features);
358 static int wacom_hid_set_device_mode(struct hid_device *hdev)
360 struct wacom *wacom = hid_get_drvdata(hdev);
361 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
362 struct hid_report *r;
363 struct hid_report_enum *re;
365 if (hid_data->inputmode < 0)
368 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
369 r = re->report_id_hash[hid_data->inputmode];
371 r->field[0]->value[hid_data->inputmode_index] = 2;
372 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
377 static int wacom_set_device_mode(struct hid_device *hdev,
378 struct wacom_wac *wacom_wac)
381 struct hid_report *r;
382 struct hid_report_enum *re;
384 int error = -ENOMEM, limit = 0;
386 if (wacom_wac->mode_report < 0)
389 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
390 r = re->report_id_hash[wacom_wac->mode_report];
394 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
398 length = hid_report_len(r);
401 rep_data[0] = wacom_wac->mode_report;
402 rep_data[1] = wacom_wac->mode_value;
404 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
407 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
408 rep_data, length, 1);
409 } while (error >= 0 &&
410 rep_data[1] != wacom_wac->mode_report &&
411 limit++ < WAC_MSG_RETRIES);
415 return error < 0 ? error : 0;
418 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
419 struct wacom_features *features)
421 struct wacom *wacom = hid_get_drvdata(hdev);
425 switch (features->type) {
429 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
433 rep_data[0] = speed == 0 ? 0x05 : 0x06;
436 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
440 wacom->wacom_wac.bt_high_speed = speed;
446 * Note that if the raw queries fail, it's not a hard failure
447 * and it is safe to continue
449 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
454 wacom->wacom_wac.bt_features &= ~0x20;
456 wacom->wacom_wac.bt_features |= 0x20;
459 rep_data[1] = wacom->wacom_wac.bt_features;
461 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
464 wacom->wacom_wac.bt_high_speed = speed;
472 * Switch the tablet into its most-capable mode. Wacom tablets are
473 * typically configured to power-up in a mode which sends mouse-like
474 * reports to the OS. To get absolute position, pressure data, etc.
475 * from the tablet, it is necessary to switch the tablet out of this
476 * mode and into one which sends the full range of tablet data.
478 static int wacom_query_tablet_data(struct hid_device *hdev,
479 struct wacom_features *features)
481 struct wacom *wacom = hid_get_drvdata(hdev);
482 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
484 if (hdev->bus == BUS_BLUETOOTH)
485 return wacom_bt_query_tablet_data(hdev, 1, features);
487 if (features->type != HID_GENERIC) {
488 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
489 if (features->type > TABLETPC) {
490 /* MT Tablet PC touch */
491 wacom_wac->mode_report = 3;
492 wacom_wac->mode_value = 4;
493 } else if (features->type == WACOM_24HDT) {
494 wacom_wac->mode_report = 18;
495 wacom_wac->mode_value = 2;
496 } else if (features->type == WACOM_27QHDT) {
497 wacom_wac->mode_report = 131;
498 wacom_wac->mode_value = 2;
499 } else if (features->type == BAMBOO_PAD) {
500 wacom_wac->mode_report = 2;
501 wacom_wac->mode_value = 2;
503 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
504 if (features->type <= BAMBOO_PT) {
505 wacom_wac->mode_report = 2;
506 wacom_wac->mode_value = 2;
511 wacom_set_device_mode(hdev, wacom_wac);
513 if (features->type == HID_GENERIC)
514 return wacom_hid_set_device_mode(hdev);
519 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
520 struct wacom_features *features)
522 struct wacom *wacom = hid_get_drvdata(hdev);
523 struct usb_interface *intf = wacom->intf;
525 /* default features */
526 features->x_fuzz = 4;
527 features->y_fuzz = 4;
528 features->pressure_fuzz = 0;
529 features->distance_fuzz = 1;
530 features->tilt_fuzz = 1;
533 * The wireless device HID is basic and layout conflicts with
534 * other tablets (monitor and touch interface can look like pen).
535 * Skip the query for this type and modify defaults based on
538 if (features->type == WIRELESS) {
539 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
540 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
542 features->device_type = WACOM_DEVICETYPE_NONE;
546 wacom_parse_hid(hdev, features);
549 struct wacom_hdev_data {
550 struct list_head list;
552 struct hid_device *dev;
553 struct wacom_shared shared;
556 static LIST_HEAD(wacom_udev_list);
557 static DEFINE_MUTEX(wacom_udev_list_lock);
559 static bool compare_device_paths(struct hid_device *hdev_a,
560 struct hid_device *hdev_b, char separator)
562 int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
563 int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
565 if (n1 != n2 || n1 <= 0 || n2 <= 0)
568 return !strncmp(hdev_a->phys, hdev_b->phys, n1);
571 static bool wacom_are_sibling(struct hid_device *hdev,
572 struct hid_device *sibling)
574 struct wacom *wacom = hid_get_drvdata(hdev);
575 struct wacom_features *features = &wacom->wacom_wac.features;
576 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
577 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
578 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
579 __u32 oPid = features->oPid ? features->oPid : hdev->product;
581 /* The defined oVid/oPid must match that of the sibling */
582 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
584 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
588 * Devices with the same VID/PID must share the same physical
589 * device path, while those with different VID/PID must share
590 * the same physical parent device path.
592 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
593 if (!compare_device_paths(hdev, sibling, '/'))
596 if (!compare_device_paths(hdev, sibling, '.'))
600 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
601 if (features->type != HID_GENERIC)
605 * Direct-input devices may not be siblings of indirect-input
608 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
609 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
613 * Indirect-input devices may not be siblings of direct-input
616 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
617 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
620 /* Pen devices may only be siblings of touch devices */
621 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
622 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
625 /* Touch devices may only be siblings of pen devices */
626 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
627 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
631 * No reason could be found for these two devices to NOT be
632 * siblings, so there's a good chance they ARE siblings
637 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
639 struct wacom_hdev_data *data;
641 /* Try to find an already-probed interface from the same device */
642 list_for_each_entry(data, &wacom_udev_list, list) {
643 if (compare_device_paths(hdev, data->dev, '/'))
647 /* Fallback to finding devices that appear to be "siblings" */
648 list_for_each_entry(data, &wacom_udev_list, list) {
649 if (wacom_are_sibling(hdev, data->dev)) {
650 kref_get(&data->kref);
658 static void wacom_release_shared_data(struct kref *kref)
660 struct wacom_hdev_data *data =
661 container_of(kref, struct wacom_hdev_data, kref);
663 mutex_lock(&wacom_udev_list_lock);
664 list_del(&data->list);
665 mutex_unlock(&wacom_udev_list_lock);
670 static void wacom_remove_shared_data(void *res)
672 struct wacom *wacom = res;
673 struct wacom_hdev_data *data;
674 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
676 if (wacom_wac->shared) {
677 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
680 if (wacom_wac->shared->touch == wacom->hdev)
681 wacom_wac->shared->touch = NULL;
682 else if (wacom_wac->shared->pen == wacom->hdev)
683 wacom_wac->shared->pen = NULL;
685 kref_put(&data->kref, wacom_release_shared_data);
686 wacom_wac->shared = NULL;
690 static int wacom_add_shared_data(struct hid_device *hdev)
692 struct wacom *wacom = hid_get_drvdata(hdev);
693 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
694 struct wacom_hdev_data *data;
697 mutex_lock(&wacom_udev_list_lock);
699 data = wacom_get_hdev_data(hdev);
701 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
707 kref_init(&data->kref);
709 list_add_tail(&data->list, &wacom_udev_list);
712 wacom_wac->shared = &data->shared;
714 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
716 mutex_unlock(&wacom_udev_list_lock);
717 wacom_remove_shared_data(wacom);
721 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
722 wacom_wac->shared->touch = hdev;
723 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
724 wacom_wac->shared->pen = hdev;
727 mutex_unlock(&wacom_udev_list_lock);
731 static int wacom_led_control(struct wacom *wacom)
735 unsigned char report_id = WAC_CMD_LED_CONTROL;
738 if (!hid_get_drvdata(wacom->hdev))
741 if (!wacom->led.groups)
744 if (wacom->wacom_wac.pid) { /* wireless connected */
745 report_id = WAC_CMD_WL_LED_CONTROL;
748 buf = kzalloc(buf_size, GFP_KERNEL);
752 if (wacom->wacom_wac.features.type >= INTUOS5S &&
753 wacom->wacom_wac.features.type <= INTUOSPL) {
755 * Touch Ring and crop mark LED luminance may take on
756 * one of four values:
757 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
759 int ring_led = wacom->led.groups[0].select & 0x03;
760 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
762 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
765 if (wacom->wacom_wac.pid) {
766 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
767 buf, buf_size, WAC_CMD_RETRIES);
774 int led = wacom->led.groups[0].select | 0x4;
776 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
777 wacom->wacom_wac.features.type == WACOM_24HD)
778 led |= (wacom->led.groups[1].select << 4) | 0x40;
782 buf[2] = wacom->led.llv;
783 buf[3] = wacom->led.hlv;
784 buf[4] = wacom->led.img_lum;
787 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
794 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
795 const unsigned len, const void *img)
799 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
801 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
805 /* Send 'start' command */
806 buf[0] = WAC_CMD_ICON_START;
808 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
814 buf[1] = button_id & 0x07;
815 for (i = 0; i < 4; i++) {
817 memcpy(buf + 3, img + i * chunk_len, chunk_len);
819 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
820 buf, chunk_len + 3, WAC_CMD_RETRIES);
826 buf[0] = WAC_CMD_ICON_START;
828 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
836 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
837 const char *buf, size_t count)
839 struct hid_device *hdev = to_hid_device(dev);
840 struct wacom *wacom = hid_get_drvdata(hdev);
844 err = kstrtouint(buf, 10, &id);
848 mutex_lock(&wacom->lock);
850 wacom->led.groups[set_id].select = id & 0x3;
851 err = wacom_led_control(wacom);
853 mutex_unlock(&wacom->lock);
855 return err < 0 ? err : count;
858 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
859 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
860 struct device_attribute *attr, const char *buf, size_t count) \
862 return wacom_led_select_store(dev, SET_ID, buf, count); \
864 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
865 struct device_attribute *attr, char *buf) \
867 struct hid_device *hdev = to_hid_device(dev);\
868 struct wacom *wacom = hid_get_drvdata(hdev); \
869 return scnprintf(buf, PAGE_SIZE, "%d\n", \
870 wacom->led.groups[SET_ID].select); \
872 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
873 wacom_led##SET_ID##_select_show, \
874 wacom_led##SET_ID##_select_store)
876 DEVICE_LED_SELECT_ATTR(0);
877 DEVICE_LED_SELECT_ATTR(1);
879 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
880 const char *buf, size_t count)
885 err = kstrtouint(buf, 10, &value);
889 mutex_lock(&wacom->lock);
891 *dest = value & 0x7f;
892 err = wacom_led_control(wacom);
894 mutex_unlock(&wacom->lock);
896 return err < 0 ? err : count;
899 #define DEVICE_LUMINANCE_ATTR(name, field) \
900 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
901 struct device_attribute *attr, const char *buf, size_t count) \
903 struct hid_device *hdev = to_hid_device(dev);\
904 struct wacom *wacom = hid_get_drvdata(hdev); \
906 return wacom_luminance_store(wacom, &wacom->led.field, \
909 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
910 struct device_attribute *attr, char *buf) \
912 struct wacom *wacom = dev_get_drvdata(dev); \
913 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
915 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
916 wacom_##name##_luminance_show, \
917 wacom_##name##_luminance_store)
919 DEVICE_LUMINANCE_ATTR(status0, llv);
920 DEVICE_LUMINANCE_ATTR(status1, hlv);
921 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
923 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
924 const char *buf, size_t count)
926 struct hid_device *hdev = to_hid_device(dev);
927 struct wacom *wacom = hid_get_drvdata(hdev);
932 if (hdev->bus == BUS_BLUETOOTH) {
934 xfer_id = WAC_CMD_ICON_BT_XFER;
937 xfer_id = WAC_CMD_ICON_XFER;
943 mutex_lock(&wacom->lock);
945 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
947 mutex_unlock(&wacom->lock);
949 return err < 0 ? err : count;
952 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
953 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
954 struct device_attribute *attr, const char *buf, size_t count) \
956 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
958 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
959 NULL, wacom_btnimg##BUTTON_ID##_store)
961 DEVICE_BTNIMG_ATTR(0);
962 DEVICE_BTNIMG_ATTR(1);
963 DEVICE_BTNIMG_ATTR(2);
964 DEVICE_BTNIMG_ATTR(3);
965 DEVICE_BTNIMG_ATTR(4);
966 DEVICE_BTNIMG_ATTR(5);
967 DEVICE_BTNIMG_ATTR(6);
968 DEVICE_BTNIMG_ATTR(7);
970 static struct attribute *cintiq_led_attrs[] = {
971 &dev_attr_status_led0_select.attr,
972 &dev_attr_status_led1_select.attr,
976 static struct attribute_group cintiq_led_attr_group = {
978 .attrs = cintiq_led_attrs,
981 static struct attribute *intuos4_led_attrs[] = {
982 &dev_attr_status0_luminance.attr,
983 &dev_attr_status1_luminance.attr,
984 &dev_attr_status_led0_select.attr,
985 &dev_attr_buttons_luminance.attr,
986 &dev_attr_button0_rawimg.attr,
987 &dev_attr_button1_rawimg.attr,
988 &dev_attr_button2_rawimg.attr,
989 &dev_attr_button3_rawimg.attr,
990 &dev_attr_button4_rawimg.attr,
991 &dev_attr_button5_rawimg.attr,
992 &dev_attr_button6_rawimg.attr,
993 &dev_attr_button7_rawimg.attr,
997 static struct attribute_group intuos4_led_attr_group = {
999 .attrs = intuos4_led_attrs,
1002 static struct attribute *intuos5_led_attrs[] = {
1003 &dev_attr_status0_luminance.attr,
1004 &dev_attr_status_led0_select.attr,
1008 static struct attribute_group intuos5_led_attr_group = {
1009 .name = "wacom_led",
1010 .attrs = intuos5_led_attrs,
1013 struct wacom_sysfs_group_devres {
1014 struct attribute_group *group;
1015 struct kobject *root;
1018 static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1020 struct wacom_sysfs_group_devres *devres = res;
1021 struct kobject *kobj = devres->root;
1023 dev_dbg(dev, "%s: dropping reference to %s\n",
1024 __func__, devres->group->name);
1025 sysfs_remove_group(kobj, devres->group);
1028 static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1029 struct kobject *root,
1030 struct attribute_group *group)
1032 struct wacom_sysfs_group_devres *devres;
1035 devres = devres_alloc(wacom_devm_sysfs_group_release,
1036 sizeof(struct wacom_sysfs_group_devres),
1041 devres->group = group;
1042 devres->root = root;
1044 error = sysfs_create_group(devres->root, group);
1048 devres_add(&wacom->hdev->dev, devres);
1053 static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1054 struct attribute_group *group)
1056 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1060 enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
1062 struct wacom *wacom = led->wacom;
1064 if (wacom->led.max_hlv)
1065 return led->hlv * LED_FULL / wacom->led.max_hlv;
1067 if (wacom->led.max_llv)
1068 return led->llv * LED_FULL / wacom->led.max_llv;
1070 /* device doesn't support brightness tuning */
1074 static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1076 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1077 struct wacom *wacom = led->wacom;
1079 if (wacom->led.groups[led->group].select != led->id)
1082 return wacom_leds_brightness_get(led);
1085 static int wacom_led_brightness_set(struct led_classdev *cdev,
1086 enum led_brightness brightness)
1088 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1089 struct wacom *wacom = led->wacom;
1092 mutex_lock(&wacom->lock);
1094 if (!wacom->led.groups || (brightness == LED_OFF &&
1095 wacom->led.groups[led->group].select != led->id)) {
1100 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1101 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1103 wacom->led.groups[led->group].select = led->id;
1105 error = wacom_led_control(wacom);
1108 mutex_unlock(&wacom->lock);
1113 static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1114 enum led_brightness brightness)
1118 static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1119 struct wacom_led *led, unsigned int group,
1120 unsigned int id, bool read_only)
1125 name = devm_kasprintf(dev, GFP_KERNEL,
1134 led->trigger.name = name;
1135 error = devm_led_trigger_register(dev, &led->trigger);
1137 hid_err(wacom->hdev,
1138 "failed to register LED trigger %s: %d\n",
1139 led->cdev.name, error);
1147 led->llv = wacom->led.llv;
1148 led->hlv = wacom->led.hlv;
1149 led->cdev.name = name;
1150 led->cdev.max_brightness = LED_FULL;
1151 led->cdev.flags = LED_HW_PLUGGABLE;
1152 led->cdev.brightness_get = __wacom_led_brightness_get;
1154 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
1155 led->cdev.default_trigger = led->cdev.name;
1157 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
1160 error = devm_led_classdev_register(dev, &led->cdev);
1162 hid_err(wacom->hdev,
1163 "failed to register LED %s: %d\n",
1164 led->cdev.name, error);
1165 led->cdev.name = NULL;
1172 static void wacom_led_groups_release_one(void *data)
1174 struct wacom_group_leds *group = data;
1176 devres_release_group(group->dev, group);
1179 static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1180 struct wacom *wacom,
1181 int group_id, int count,
1184 struct wacom_led *leds;
1187 if (group_id >= wacom->led.count || count <= 0)
1190 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1193 leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
1199 wacom->led.groups[group_id].leds = leds;
1200 wacom->led.groups[group_id].count = count;
1202 for (i = 0; i < count; i++) {
1203 error = wacom_led_register_one(dev, wacom, &leds[i],
1204 group_id, i, read_only);
1209 wacom->led.groups[group_id].dev = dev;
1211 devres_close_group(dev, &wacom->led.groups[group_id]);
1214 * There is a bug (?) in devm_led_classdev_register() in which its
1215 * increments the refcount of the parent. If the parent is an input
1216 * device, that means the ref count never reaches 0 when
1217 * devm_input_device_release() gets called.
1218 * This means that the LEDs are still there after disconnect.
1219 * Manually force the release of the group so that the leds are released
1220 * once we are done using them.
1222 error = devm_add_action_or_reset(&wacom->hdev->dev,
1223 wacom_led_groups_release_one,
1224 &wacom->led.groups[group_id]);
1231 devres_release_group(dev, &wacom->led.groups[group_id]);
1235 struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1238 struct wacom_group_leds *group;
1240 if (group_id >= wacom->led.count)
1243 group = &wacom->led.groups[group_id];
1250 return &group->leds[id];
1254 * wacom_led_next: gives the next available led with a wacom trigger.
1256 * returns the next available struct wacom_led which has its default trigger
1257 * or the current one if none is available.
1259 struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1261 struct wacom_led *next_led;
1271 next_led = wacom_led_find(wacom, group, ++next);
1272 if (!next_led || next_led == cur)
1274 } while (next_led->cdev.trigger != &next_led->trigger);
1279 static void wacom_led_groups_release(void *data)
1281 struct wacom *wacom = data;
1283 wacom->led.groups = NULL;
1284 wacom->led.count = 0;
1287 static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1289 struct device *dev = &wacom->hdev->dev;
1290 struct wacom_group_leds *groups;
1293 groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
1298 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1302 wacom->led.groups = groups;
1303 wacom->led.count = count;
1308 static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1309 int led_per_group, bool read_only)
1314 if (!wacom->wacom_wac.pad_input)
1317 dev = &wacom->wacom_wac.pad_input->dev;
1319 error = wacom_led_groups_allocate(wacom, group_count);
1323 for (i = 0; i < group_count; i++) {
1324 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1334 static int wacom_initialize_leds(struct wacom *wacom)
1338 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1341 /* Initialize default values */
1342 switch (wacom->wacom_wac.features.type) {
1347 wacom->led.llv = 10;
1348 wacom->led.hlv = 20;
1349 wacom->led.max_llv = 127;
1350 wacom->led.max_hlv = 127;
1351 wacom->led.img_lum = 10;
1353 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1355 hid_err(wacom->hdev,
1356 "cannot create leds err: %d\n", error);
1360 error = wacom_devm_sysfs_create_group(wacom,
1361 &intuos4_led_attr_group);
1368 wacom->led.img_lum = 0;
1370 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1372 hid_err(wacom->hdev,
1373 "cannot create leds err: %d\n", error);
1377 error = wacom_devm_sysfs_create_group(wacom,
1378 &cintiq_led_attr_group);
1387 wacom->led.llv = 32;
1388 wacom->led.max_llv = 96;
1390 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1392 hid_err(wacom->hdev,
1393 "cannot create leds err: %d\n", error);
1397 error = wacom_devm_sysfs_create_group(wacom,
1398 &intuos5_led_attr_group);
1402 wacom->led.llv = 255;
1403 wacom->led.max_llv = 255;
1404 error = wacom_led_groups_allocate(wacom, 5);
1406 hid_err(wacom->hdev,
1407 "cannot create leds err: %d\n", error);
1417 hid_err(wacom->hdev,
1418 "cannot create sysfs group err: %d\n", error);
1421 wacom_led_control(wacom);
1426 static enum power_supply_property wacom_battery_props[] = {
1427 POWER_SUPPLY_PROP_MODEL_NAME,
1428 POWER_SUPPLY_PROP_PRESENT,
1429 POWER_SUPPLY_PROP_STATUS,
1430 POWER_SUPPLY_PROP_SCOPE,
1431 POWER_SUPPLY_PROP_CAPACITY
1434 static int wacom_battery_get_property(struct power_supply *psy,
1435 enum power_supply_property psp,
1436 union power_supply_propval *val)
1438 struct wacom_battery *battery = power_supply_get_drvdata(psy);
1442 case POWER_SUPPLY_PROP_MODEL_NAME:
1443 val->strval = battery->wacom->wacom_wac.name;
1445 case POWER_SUPPLY_PROP_PRESENT:
1446 val->intval = battery->bat_connected;
1448 case POWER_SUPPLY_PROP_SCOPE:
1449 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1451 case POWER_SUPPLY_PROP_CAPACITY:
1452 val->intval = battery->battery_capacity;
1454 case POWER_SUPPLY_PROP_STATUS:
1455 if (battery->bat_charging)
1456 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1457 else if (battery->battery_capacity == 100 &&
1458 battery->ps_connected)
1459 val->intval = POWER_SUPPLY_STATUS_FULL;
1460 else if (battery->ps_connected)
1461 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1463 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1473 static int __wacom_initialize_battery(struct wacom *wacom,
1474 struct wacom_battery *battery)
1476 static atomic_t battery_no = ATOMIC_INIT(0);
1477 struct device *dev = &wacom->hdev->dev;
1478 struct power_supply_config psy_cfg = { .drv_data = battery, };
1479 struct power_supply *ps_bat;
1480 struct power_supply_desc *bat_desc = &battery->bat_desc;
1484 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1487 battery->wacom = wacom;
1489 n = atomic_inc_return(&battery_no) - 1;
1491 bat_desc->properties = wacom_battery_props;
1492 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1493 bat_desc->get_property = wacom_battery_get_property;
1494 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1495 bat_desc->name = battery->bat_name;
1496 bat_desc->type = POWER_SUPPLY_TYPE_USB;
1497 bat_desc->use_for_apm = 0;
1499 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1500 if (IS_ERR(ps_bat)) {
1501 error = PTR_ERR(ps_bat);
1505 power_supply_powers(ps_bat, &wacom->hdev->dev);
1507 battery->battery = ps_bat;
1509 devres_close_group(dev, bat_desc);
1513 devres_release_group(dev, bat_desc);
1517 static int wacom_initialize_battery(struct wacom *wacom)
1519 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1520 return __wacom_initialize_battery(wacom, &wacom->battery);
1525 static void wacom_destroy_battery(struct wacom *wacom)
1527 if (wacom->battery.battery) {
1528 devres_release_group(&wacom->hdev->dev,
1529 &wacom->battery.bat_desc);
1530 wacom->battery.battery = NULL;
1534 static ssize_t wacom_show_speed(struct device *dev,
1535 struct device_attribute
1538 struct hid_device *hdev = to_hid_device(dev);
1539 struct wacom *wacom = hid_get_drvdata(hdev);
1541 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1544 static ssize_t wacom_store_speed(struct device *dev,
1545 struct device_attribute *attr,
1546 const char *buf, size_t count)
1548 struct hid_device *hdev = to_hid_device(dev);
1549 struct wacom *wacom = hid_get_drvdata(hdev);
1552 if (kstrtou8(buf, 0, &new_speed))
1555 if (new_speed != 0 && new_speed != 1)
1558 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1563 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1564 wacom_show_speed, wacom_store_speed);
1567 static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1568 struct kobj_attribute *kattr,
1569 char *buf, int index)
1571 struct device *dev = kobj_to_dev(kobj->parent);
1572 struct hid_device *hdev = to_hid_device(dev);
1573 struct wacom *wacom = hid_get_drvdata(hdev);
1576 mode = wacom->led.groups[index].select;
1577 if (mode >= 0 && mode < 3)
1578 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
1580 return snprintf(buf, PAGE_SIZE, "%d\n", -1);
1583 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1584 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1585 struct kobj_attribute *kattr, char *buf) \
1587 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1589 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1590 .attr = {.name = "remote_mode", \
1591 .mode = DEV_ATTR_RO_PERM}, \
1592 .show = wacom_show_remote##SET_ID##_mode, \
1594 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1595 &remote##SET_ID##_mode_attr.attr, \
1598 static struct attribute_group remote##SET_ID##_serial_group = { \
1600 .attrs = remote##SET_ID##_serial_attrs, \
1603 DEVICE_EKR_ATTR_GROUP(0);
1604 DEVICE_EKR_ATTR_GROUP(1);
1605 DEVICE_EKR_ATTR_GROUP(2);
1606 DEVICE_EKR_ATTR_GROUP(3);
1607 DEVICE_EKR_ATTR_GROUP(4);
1609 static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1613 struct wacom_remote *remote = wacom->remote;
1615 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1618 if (!remote->remotes[index].group.name)
1621 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1622 &remote->remotes[index].group);
1624 remote->remotes[index].group.name = NULL;
1625 hid_err(wacom->hdev,
1626 "cannot create sysfs group err: %d\n", error);
1633 static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1635 const size_t buf_size = 2;
1639 buf = kzalloc(buf_size, GFP_KERNEL);
1643 buf[0] = WAC_CMD_DELETE_PAIRING;
1646 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1647 buf_size, WAC_CMD_RETRIES);
1653 static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1654 struct kobj_attribute *attr,
1655 const char *buf, size_t count)
1657 unsigned char selector = 0;
1658 struct device *dev = kobj_to_dev(kobj->parent);
1659 struct hid_device *hdev = to_hid_device(dev);
1660 struct wacom *wacom = hid_get_drvdata(hdev);
1663 if (!strncmp(buf, "*\n", 2)) {
1664 selector = WAC_CMD_UNPAIR_ALL;
1666 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1671 mutex_lock(&wacom->lock);
1673 err = wacom_cmd_unpair_remote(wacom, selector);
1674 mutex_unlock(&wacom->lock);
1676 return err < 0 ? err : count;
1679 static struct kobj_attribute unpair_remote_attr = {
1680 .attr = {.name = "unpair_remote", .mode = 0200},
1681 .store = wacom_store_unpair_remote,
1684 static const struct attribute *remote_unpair_attrs[] = {
1685 &unpair_remote_attr.attr,
1689 static void wacom_remotes_destroy(void *data)
1691 struct wacom *wacom = data;
1692 struct wacom_remote *remote = wacom->remote;
1697 kobject_put(remote->remote_dir);
1698 kfifo_free(&remote->remote_fifo);
1699 wacom->remote = NULL;
1702 static int wacom_initialize_remotes(struct wacom *wacom)
1705 struct wacom_remote *remote;
1708 if (wacom->wacom_wac.features.type != REMOTE)
1711 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1716 wacom->remote = remote;
1718 spin_lock_init(&remote->remote_lock);
1720 error = kfifo_alloc(&remote->remote_fifo,
1721 5 * sizeof(struct wacom_remote_data),
1724 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1728 remote->remotes[0].group = remote0_serial_group;
1729 remote->remotes[1].group = remote1_serial_group;
1730 remote->remotes[2].group = remote2_serial_group;
1731 remote->remotes[3].group = remote3_serial_group;
1732 remote->remotes[4].group = remote4_serial_group;
1734 remote->remote_dir = kobject_create_and_add("wacom_remote",
1735 &wacom->hdev->dev.kobj);
1736 if (!remote->remote_dir)
1739 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
1742 hid_err(wacom->hdev,
1743 "cannot create sysfs group err: %d\n", error);
1747 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1748 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
1749 remote->remotes[i].serial = 0;
1752 error = devm_add_action_or_reset(&wacom->hdev->dev,
1753 wacom_remotes_destroy, wacom);
1760 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1762 struct input_dev *input_dev;
1763 struct hid_device *hdev = wacom->hdev;
1764 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1766 input_dev = devm_input_allocate_device(&hdev->dev);
1770 input_dev->name = wacom_wac->features.name;
1771 input_dev->phys = hdev->phys;
1772 input_dev->dev.parent = &hdev->dev;
1773 input_dev->open = wacom_open;
1774 input_dev->close = wacom_close;
1775 input_dev->uniq = hdev->uniq;
1776 input_dev->id.bustype = hdev->bus;
1777 input_dev->id.vendor = hdev->vendor;
1778 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1779 input_dev->id.version = hdev->version;
1780 input_set_drvdata(input_dev, wacom);
1785 static int wacom_allocate_inputs(struct wacom *wacom)
1787 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1789 wacom_wac->pen_input = wacom_allocate_input(wacom);
1790 wacom_wac->touch_input = wacom_allocate_input(wacom);
1791 wacom_wac->pad_input = wacom_allocate_input(wacom);
1792 if (!wacom_wac->pen_input ||
1793 !wacom_wac->touch_input ||
1794 !wacom_wac->pad_input)
1797 wacom_wac->pen_input->name = wacom_wac->pen_name;
1798 wacom_wac->touch_input->name = wacom_wac->touch_name;
1799 wacom_wac->pad_input->name = wacom_wac->pad_name;
1804 static int wacom_register_inputs(struct wacom *wacom)
1806 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
1807 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1810 pen_input_dev = wacom_wac->pen_input;
1811 touch_input_dev = wacom_wac->touch_input;
1812 pad_input_dev = wacom_wac->pad_input;
1814 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
1817 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1819 /* no pen in use on this interface */
1820 input_free_device(pen_input_dev);
1821 wacom_wac->pen_input = NULL;
1822 pen_input_dev = NULL;
1824 error = input_register_device(pen_input_dev);
1829 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1831 /* no touch in use on this interface */
1832 input_free_device(touch_input_dev);
1833 wacom_wac->touch_input = NULL;
1834 touch_input_dev = NULL;
1836 error = input_register_device(touch_input_dev);
1841 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1843 /* no pad in use on this interface */
1844 input_free_device(pad_input_dev);
1845 wacom_wac->pad_input = NULL;
1846 pad_input_dev = NULL;
1848 error = input_register_device(pad_input_dev);
1856 wacom_wac->pad_input = NULL;
1857 wacom_wac->touch_input = NULL;
1858 wacom_wac->pen_input = NULL;
1863 * Not all devices report physical dimensions from HID.
1864 * Compute the default from hardcoded logical dimension
1865 * and resolution before driver overwrites them.
1867 static void wacom_set_default_phy(struct wacom_features *features)
1869 if (features->x_resolution) {
1870 features->x_phy = (features->x_max * 100) /
1871 features->x_resolution;
1872 features->y_phy = (features->y_max * 100) /
1873 features->y_resolution;
1877 static void wacom_calculate_res(struct wacom_features *features)
1879 /* set unit to "100th of a mm" for devices not reported by HID */
1880 if (!features->unit) {
1881 features->unit = 0x11;
1882 features->unitExpo = -3;
1885 features->x_resolution = wacom_calc_hid_res(features->x_max,
1888 features->unitExpo);
1889 features->y_resolution = wacom_calc_hid_res(features->y_max,
1892 features->unitExpo);
1895 void wacom_battery_work(struct work_struct *work)
1897 struct wacom *wacom = container_of(work, struct wacom, battery_work);
1899 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1900 !wacom->battery.battery) {
1901 wacom_initialize_battery(wacom);
1903 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1904 wacom->battery.battery) {
1905 wacom_destroy_battery(wacom);
1909 static size_t wacom_compute_pktlen(struct hid_device *hdev)
1911 struct hid_report_enum *report_enum;
1912 struct hid_report *report;
1915 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1917 list_for_each_entry(report, &report_enum->report_list, list) {
1918 size_t report_size = hid_report_len(report);
1919 if (report_size > size)
1926 static void wacom_update_name(struct wacom *wacom, const char *suffix)
1928 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1929 struct wacom_features *features = &wacom_wac->features;
1930 char name[WACOM_NAME_MAX];
1932 /* Generic devices name unspecified */
1933 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1934 if (strstr(wacom->hdev->name, "Wacom") ||
1935 strstr(wacom->hdev->name, "wacom") ||
1936 strstr(wacom->hdev->name, "WACOM")) {
1937 /* name is in HID descriptor, use it */
1938 strlcpy(name, wacom->hdev->name, sizeof(name));
1940 /* strip out excess whitespaces */
1942 char *gap = strstr(name, " ");
1945 /* shift everything including the terminator */
1946 memmove(gap, gap+1, strlen(gap));
1949 /* strip off excessive prefixing */
1950 if (strstr(name, "Wacom Co.,Ltd. Wacom ") == name) {
1951 int n = strlen(name);
1952 int x = strlen("Wacom Co.,Ltd. ");
1953 memmove(name, name+x, n-x+1);
1955 if (strstr(name, "Wacom Co., Ltd. Wacom ") == name) {
1956 int n = strlen(name);
1957 int x = strlen("Wacom Co., Ltd. ");
1958 memmove(name, name+x, n-x+1);
1961 /* get rid of trailing whitespace */
1962 if (name[strlen(name)-1] == ' ')
1963 name[strlen(name)-1] = '\0';
1965 /* no meaningful name retrieved. use product ID */
1966 snprintf(name, sizeof(name),
1967 "%s %X", features->name, wacom->hdev->product);
1970 strlcpy(name, features->name, sizeof(name));
1973 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
1976 /* Append the device type to the name */
1977 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
1978 "%s%s Pen", name, suffix);
1979 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
1980 "%s%s Finger", name, suffix);
1981 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1982 "%s%s Pad", name, suffix);
1985 static void wacom_release_resources(struct wacom *wacom)
1987 struct hid_device *hdev = wacom->hdev;
1989 if (!wacom->resources)
1992 devres_release_group(&hdev->dev, wacom);
1994 wacom->resources = false;
1996 wacom->wacom_wac.pen_input = NULL;
1997 wacom->wacom_wac.touch_input = NULL;
1998 wacom->wacom_wac.pad_input = NULL;
2001 static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
2003 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2004 struct wacom_features *features = &wacom_wac->features;
2005 struct hid_device *hdev = wacom->hdev;
2007 unsigned int connect_mask = HID_CONNECT_HIDRAW;
2009 features->pktlen = wacom_compute_pktlen(hdev);
2010 if (features->pktlen > WACOM_PKGLEN_MAX)
2013 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2016 wacom->resources = true;
2018 error = wacom_allocate_inputs(wacom);
2023 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2024 * into debug mode for the touch part.
2025 * We ignore the other interfaces.
2027 if (features->type == BAMBOO_PAD) {
2028 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2029 features->type = HID_GENERIC;
2030 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2031 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2037 /* set the default size in case we do not get them from hid */
2038 wacom_set_default_phy(features);
2040 /* Retrieve the physical and logical size for touch devices */
2041 wacom_retrieve_hid_descriptor(hdev, features);
2042 wacom_setup_device_quirks(wacom);
2044 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2045 features->type != WIRELESS) {
2046 error = features->type == HID_GENERIC ? -ENODEV : 0;
2048 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
2050 error ? "Ignoring" : "Assuming pen");
2055 features->device_type |= WACOM_DEVICETYPE_PEN;
2058 wacom_calculate_res(features);
2060 wacom_update_name(wacom, wireless ? " (WL)" : "");
2062 error = wacom_add_shared_data(hdev);
2066 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2067 (features->quirks & WACOM_QUIRK_BATTERY)) {
2068 error = wacom_initialize_battery(wacom);
2073 error = wacom_register_inputs(wacom);
2077 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
2078 error = wacom_initialize_leds(wacom);
2082 error = wacom_initialize_remotes(wacom);
2087 if (features->type == HID_GENERIC)
2088 connect_mask |= HID_CONNECT_DRIVER;
2090 /* Regular HID work starts now */
2091 error = hid_hw_start(hdev, connect_mask);
2093 hid_err(hdev, "hw start failed\n");
2098 /* Note that if query fails it is not a hard failure */
2099 wacom_query_tablet_data(hdev, features);
2102 /* touch only Bamboo doesn't support pen */
2103 if ((features->type == BAMBOO_TOUCH) &&
2104 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2109 /* pen only Bamboo neither support touch nor pad */
2110 if ((features->type == BAMBOO_PEN) &&
2111 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2112 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2117 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2118 error = hid_hw_open(hdev);
2120 if ((wacom_wac->features.type == INTUOSHT ||
2121 wacom_wac->features.type == INTUOSHT2) &&
2122 (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
2123 wacom_wac->shared->type = wacom_wac->features.type;
2124 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2127 devres_close_group(&hdev->dev, wacom);
2134 wacom_release_resources(wacom);
2138 static void wacom_wireless_work(struct work_struct *work)
2140 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2141 struct usb_device *usbdev = wacom->usbdev;
2142 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2143 struct hid_device *hdev1, *hdev2;
2144 struct wacom *wacom1, *wacom2;
2145 struct wacom_wac *wacom_wac1, *wacom_wac2;
2149 * Regardless if this is a disconnect or a new tablet,
2150 * remove any existing input and battery devices.
2153 wacom_destroy_battery(wacom);
2155 /* Stylus interface */
2156 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2157 wacom1 = hid_get_drvdata(hdev1);
2158 wacom_wac1 = &(wacom1->wacom_wac);
2159 wacom_release_resources(wacom1);
2161 /* Touch interface */
2162 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2163 wacom2 = hid_get_drvdata(hdev2);
2164 wacom_wac2 = &(wacom2->wacom_wac);
2165 wacom_release_resources(wacom2);
2167 if (wacom_wac->pid == 0) {
2168 hid_info(wacom->hdev, "wireless tablet disconnected\n");
2170 const struct hid_device_id *id = wacom_ids;
2172 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2176 if (id->vendor == USB_VENDOR_ID_WACOM &&
2177 id->product == wacom_wac->pid)
2183 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2187 /* Stylus interface */
2188 wacom_wac1->features =
2189 *((struct wacom_features *)id->driver_data);
2191 wacom_wac1->pid = wacom_wac->pid;
2193 error = wacom_parse_and_register(wacom1, true);
2197 /* Touch interface */
2198 if (wacom_wac1->features.touch_max ||
2199 (wacom_wac1->features.type >= INTUOSHT &&
2200 wacom_wac1->features.type <= BAMBOO_PT)) {
2201 wacom_wac2->features =
2202 *((struct wacom_features *)id->driver_data);
2203 wacom_wac2->pid = wacom_wac->pid;
2205 error = wacom_parse_and_register(wacom2, true);
2210 strlcpy(wacom_wac->name, wacom_wac1->name,
2211 sizeof(wacom_wac->name));
2212 error = wacom_initialize_battery(wacom);
2220 wacom_release_resources(wacom1);
2221 wacom_release_resources(wacom2);
2225 static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2227 struct wacom_remote *remote = wacom->remote;
2228 u32 serial = remote->remotes[index].serial;
2230 unsigned long flags;
2232 spin_lock_irqsave(&remote->remote_lock, flags);
2233 remote->remotes[index].registered = false;
2234 spin_unlock_irqrestore(&remote->remote_lock, flags);
2236 if (remote->remotes[index].battery.battery)
2237 devres_release_group(&wacom->hdev->dev,
2238 &remote->remotes[index].battery.bat_desc);
2240 if (remote->remotes[index].group.name)
2241 devres_release_group(&wacom->hdev->dev,
2242 &remote->remotes[index]);
2244 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2245 if (remote->remotes[i].serial == serial) {
2246 remote->remotes[i].serial = 0;
2247 remote->remotes[i].group.name = NULL;
2248 remote->remotes[i].registered = false;
2249 remote->remotes[i].battery.battery = NULL;
2250 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2255 static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2258 struct wacom_remote *remote = wacom->remote;
2259 struct device *dev = &wacom->hdev->dev;
2262 /* A remote can pair more than once with an EKR,
2263 * check to make sure this serial isn't already paired.
2265 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2266 if (remote->remotes[k].serial == serial)
2270 if (k < WACOM_MAX_REMOTES) {
2271 remote->remotes[index].serial = serial;
2275 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2278 error = wacom_remote_create_attr_group(wacom, serial, index);
2282 remote->remotes[index].input = wacom_allocate_input(wacom);
2283 if (!remote->remotes[index].input) {
2287 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2288 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2290 if (!remote->remotes[index].input->name) {
2295 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2300 remote->remotes[index].serial = serial;
2302 error = input_register_device(remote->remotes[index].input);
2306 error = wacom_led_groups_alloc_and_register_one(
2307 &remote->remotes[index].input->dev,
2308 wacom, index, 3, true);
2312 remote->remotes[index].registered = true;
2314 devres_close_group(dev, &remote->remotes[index]);
2318 devres_release_group(dev, &remote->remotes[index]);
2319 remote->remotes[index].serial = 0;
2323 static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2325 struct wacom_remote *remote = wacom->remote;
2328 if (!remote->remotes[index].registered)
2331 if (remote->remotes[index].battery.battery)
2334 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2337 error = __wacom_initialize_battery(wacom,
2338 &wacom->remote->remotes[index].battery);
2345 static void wacom_remote_work(struct work_struct *work)
2347 struct wacom *wacom = container_of(work, struct wacom, remote_work);
2348 struct wacom_remote *remote = wacom->remote;
2349 struct wacom_remote_data data;
2350 unsigned long flags;
2355 spin_lock_irqsave(&remote->remote_lock, flags);
2357 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2359 if (count != sizeof(data)) {
2360 hid_err(wacom->hdev,
2361 "workitem triggered without status available\n");
2362 spin_unlock_irqrestore(&remote->remote_lock, flags);
2366 if (!kfifo_is_empty(&remote->remote_fifo))
2367 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2369 spin_unlock_irqrestore(&remote->remote_lock, flags);
2371 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2372 serial = data.remote[i].serial;
2373 if (data.remote[i].connected) {
2375 if (remote->remotes[i].serial == serial) {
2376 wacom_remote_attach_battery(wacom, i);
2380 if (remote->remotes[i].serial)
2381 wacom_remote_destroy_one(wacom, i);
2383 wacom_remote_create_one(wacom, serial, i);
2385 } else if (remote->remotes[i].serial) {
2386 wacom_remote_destroy_one(wacom, i);
2391 static int wacom_probe(struct hid_device *hdev,
2392 const struct hid_device_id *id)
2394 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2395 struct usb_device *dev = interface_to_usbdev(intf);
2396 struct wacom *wacom;
2397 struct wacom_wac *wacom_wac;
2398 struct wacom_features *features;
2401 if (!id->driver_data)
2404 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2406 /* hid-core sets this quirk for the boot interface */
2407 hdev->quirks &= ~HID_QUIRK_NOGET;
2409 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2413 hid_set_drvdata(hdev, wacom);
2416 wacom_wac = &wacom->wacom_wac;
2417 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2418 features = &wacom_wac->features;
2420 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2425 wacom_wac->hid_data.inputmode = -1;
2426 wacom_wac->mode_report = -1;
2428 wacom->usbdev = dev;
2430 mutex_init(&wacom->lock);
2431 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2432 INIT_WORK(&wacom->battery_work, wacom_battery_work);
2433 INIT_WORK(&wacom->remote_work, wacom_remote_work);
2435 /* ask for the report descriptor to be loaded by HID */
2436 error = hid_parse(hdev);
2438 hid_err(hdev, "parse failed\n");
2442 error = wacom_parse_and_register(wacom, false);
2446 if (hdev->bus == BUS_BLUETOOTH) {
2447 error = device_create_file(&hdev->dev, &dev_attr_speed);
2450 "can't create sysfs speed attribute err: %d\n",
2457 hid_set_drvdata(hdev, NULL);
2461 static void wacom_remove(struct hid_device *hdev)
2463 struct wacom *wacom = hid_get_drvdata(hdev);
2464 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2465 struct wacom_features *features = &wacom_wac->features;
2467 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2472 cancel_work_sync(&wacom->wireless_work);
2473 cancel_work_sync(&wacom->battery_work);
2474 cancel_work_sync(&wacom->remote_work);
2475 if (hdev->bus == BUS_BLUETOOTH)
2476 device_remove_file(&hdev->dev, &dev_attr_speed);
2478 hid_set_drvdata(hdev, NULL);
2482 static int wacom_resume(struct hid_device *hdev)
2484 struct wacom *wacom = hid_get_drvdata(hdev);
2485 struct wacom_features *features = &wacom->wacom_wac.features;
2487 mutex_lock(&wacom->lock);
2489 /* switch to wacom mode first */
2490 wacom_query_tablet_data(hdev, features);
2491 wacom_led_control(wacom);
2493 mutex_unlock(&wacom->lock);
2498 static int wacom_reset_resume(struct hid_device *hdev)
2500 return wacom_resume(hdev);
2502 #endif /* CONFIG_PM */
2504 static struct hid_driver wacom_driver = {
2506 .id_table = wacom_ids,
2507 .probe = wacom_probe,
2508 .remove = wacom_remove,
2509 .report = wacom_wac_report,
2511 .resume = wacom_resume,
2512 .reset_resume = wacom_reset_resume,
2514 .raw_event = wacom_raw_event,
2516 module_hid_driver(wacom_driver);
2518 MODULE_VERSION(DRIVER_VERSION);
2519 MODULE_AUTHOR(DRIVER_AUTHOR);
2520 MODULE_DESCRIPTION(DRIVER_DESC);
2521 MODULE_LICENSE(DRIVER_LICENSE);