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/hid.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
27 #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
28 #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
30 static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
31 size_t size, unsigned int retries)
36 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
38 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
43 static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
44 size_t size, unsigned int retries)
49 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
51 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
56 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
57 u8 *raw_data, int size)
59 struct wacom *wacom = hid_get_drvdata(hdev);
61 if (size > WACOM_PKGLEN_MAX)
64 memcpy(wacom->wacom_wac.data, raw_data, size);
66 wacom_wac_irq(&wacom->wacom_wac, size);
71 static int wacom_open(struct input_dev *dev)
73 struct wacom *wacom = input_get_drvdata(dev);
76 mutex_lock(&wacom->lock);
77 retval = hid_hw_open(wacom->hdev);
78 mutex_unlock(&wacom->lock);
83 static void wacom_close(struct input_dev *dev)
85 struct wacom *wacom = input_get_drvdata(dev);
87 mutex_lock(&wacom->lock);
88 hid_hw_close(wacom->hdev);
89 mutex_unlock(&wacom->lock);
93 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
95 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
96 unsigned unit, int exponent)
98 struct hid_field field = {
99 .logical_maximum = logical_extents,
100 .physical_maximum = physical_extents,
102 .unit_exponent = exponent,
105 return hidinput_calc_abs_res(&field, ABS_X);
108 static void wacom_feature_mapping(struct hid_device *hdev,
109 struct hid_field *field, struct hid_usage *usage)
111 struct wacom *wacom = hid_get_drvdata(hdev);
112 struct wacom_features *features = &wacom->wacom_wac.features;
114 switch (usage->hid) {
115 case HID_DG_CONTACTMAX:
116 /* leave touch_max as is if predefined */
117 if (!features->touch_max)
118 features->touch_max = field->value[0];
124 * Interface Descriptor of wacom devices can be incomplete and
125 * inconsistent so wacom_features table is used to store stylus
126 * device's packet lengths, various maximum values, and tablet
127 * resolution based on product ID's.
129 * For devices that contain 2 interfaces, wacom_features table is
130 * inaccurate for the touch interface. Since the Interface Descriptor
131 * for touch interfaces has pretty complete data, this function exists
132 * to query tablet for this missing information instead of hard coding in
133 * an additional table.
135 * A typical Interface Descriptor for a stylus will contain a
136 * boot mouse application collection that is not of interest and this
137 * function will ignore it.
139 * It also contains a digitizer application collection that also is not
140 * of interest since any information it contains would be duplicate
141 * of what is in wacom_features. Usually it defines a report of an array
142 * of bytes that could be used as max length of the stylus packet returned.
143 * If it happens to define a Digitizer-Stylus Physical Collection then
144 * the X and Y logical values contain valid data but it is ignored.
146 * A typical Interface Descriptor for a touch interface will contain a
147 * Digitizer-Finger Physical Collection which will define both logical
148 * X/Y maximum as well as the physical size of tablet. Since touch
149 * interfaces haven't supported pressure or distance, this is enough
150 * information to override invalid values in the wacom_features table.
152 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
153 * data. We deal with them after returning from this function.
155 static void wacom_usage_mapping(struct hid_device *hdev,
156 struct hid_field *field, struct hid_usage *usage)
158 struct wacom *wacom = hid_get_drvdata(hdev);
159 struct wacom_features *features = &wacom->wacom_wac.features;
160 bool finger = (field->logical == HID_DG_FINGER) ||
161 (field->physical == HID_DG_FINGER);
162 bool pen = (field->logical == HID_DG_STYLUS) ||
163 (field->physical == HID_DG_STYLUS);
166 * Requiring Stylus Usage will ignore boot mouse
167 * X/Y values and some cases of invalid Digitizer X/Y
168 * values commonly reported.
173 if (finger && !features->touch_max)
174 /* touch device at least supports one touch point */
175 features->touch_max = 1;
177 switch (usage->hid) {
179 features->x_max = field->logical_maximum;
181 features->device_type = BTN_TOOL_FINGER;
182 features->x_phy = field->physical_maximum;
183 if (features->type != BAMBOO_PT) {
184 features->unit = field->unit;
185 features->unitExpo = field->unit_exponent;
188 features->device_type = BTN_TOOL_PEN;
192 features->y_max = field->logical_maximum;
194 features->y_phy = field->physical_maximum;
195 if (features->type != BAMBOO_PT) {
196 features->unit = field->unit;
197 features->unitExpo = field->unit_exponent;
201 case HID_DG_TIPPRESSURE:
203 features->pressure_max = field->logical_maximum;
208 static void wacom_parse_hid(struct hid_device *hdev,
209 struct wacom_features *features)
211 struct hid_report_enum *rep_enum;
212 struct hid_report *hreport;
215 /* check features first */
216 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
217 list_for_each_entry(hreport, &rep_enum->report_list, list) {
218 for (i = 0; i < hreport->maxfield; i++) {
219 /* Ignore if report count is out of bounds. */
220 if (hreport->field[i]->report_count < 1)
223 for (j = 0; j < hreport->field[i]->maxusage; j++) {
224 wacom_feature_mapping(hdev, hreport->field[i],
225 hreport->field[i]->usage + j);
230 /* now check the input usages */
231 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
232 list_for_each_entry(hreport, &rep_enum->report_list, list) {
234 if (!hreport->maxfield)
237 for (i = 0; i < hreport->maxfield; i++)
238 for (j = 0; j < hreport->field[i]->maxusage; j++)
239 wacom_usage_mapping(hdev, hreport->field[i],
240 hreport->field[i]->usage + j);
244 static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
245 int length, int mode)
247 unsigned char *rep_data;
248 int error = -ENOMEM, limit = 0;
250 rep_data = kzalloc(length, GFP_KERNEL);
255 rep_data[0] = report_id;
258 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
261 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
262 rep_data, length, 1);
263 } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
267 return error < 0 ? error : 0;
270 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
271 struct wacom_features *features)
273 struct wacom *wacom = hid_get_drvdata(hdev);
277 switch (features->type) {
281 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
285 rep_data[0] = speed == 0 ? 0x05 : 0x06;
288 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
292 wacom->wacom_wac.bt_high_speed = speed;
298 * Note that if the raw queries fail, it's not a hard failure
299 * and it is safe to continue
301 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
306 wacom->wacom_wac.bt_features &= ~0x20;
308 wacom->wacom_wac.bt_features |= 0x20;
311 rep_data[1] = wacom->wacom_wac.bt_features;
313 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
316 wacom->wacom_wac.bt_high_speed = speed;
324 * Switch the tablet into its most-capable mode. Wacom tablets are
325 * typically configured to power-up in a mode which sends mouse-like
326 * reports to the OS. To get absolute position, pressure data, etc.
327 * from the tablet, it is necessary to switch the tablet out of this
328 * mode and into one which sends the full range of tablet data.
330 static int wacom_query_tablet_data(struct hid_device *hdev,
331 struct wacom_features *features)
333 if (hdev->bus == BUS_BLUETOOTH)
334 return wacom_bt_query_tablet_data(hdev, 1, features);
336 if (features->device_type == BTN_TOOL_FINGER) {
337 if (features->type > TABLETPC) {
338 /* MT Tablet PC touch */
339 return wacom_set_device_mode(hdev, 3, 4, 4);
341 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
342 return wacom_set_device_mode(hdev, 18, 3, 2);
344 } else if (features->device_type == BTN_TOOL_PEN) {
345 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
346 return wacom_set_device_mode(hdev, 2, 2, 2);
353 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
354 struct wacom_features *features)
356 struct wacom *wacom = hid_get_drvdata(hdev);
357 struct usb_interface *intf = wacom->intf;
359 /* default features */
360 features->device_type = BTN_TOOL_PEN;
361 features->x_fuzz = 4;
362 features->y_fuzz = 4;
363 features->pressure_fuzz = 0;
364 features->distance_fuzz = 0;
367 * The wireless device HID is basic and layout conflicts with
368 * other tablets (monitor and touch interface can look like pen).
369 * Skip the query for this type and modify defaults based on
372 if (features->type == WIRELESS) {
373 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
374 features->device_type = 0;
375 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
376 features->device_type = BTN_TOOL_FINGER;
377 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
381 /* only devices that support touch need to retrieve the info */
382 if (features->type < BAMBOO_PT)
385 wacom_parse_hid(hdev, features);
388 struct wacom_hdev_data {
389 struct list_head list;
391 struct hid_device *dev;
392 struct wacom_shared shared;
395 static LIST_HEAD(wacom_udev_list);
396 static DEFINE_MUTEX(wacom_udev_list_lock);
398 static bool wacom_are_sibling(struct hid_device *hdev,
399 struct hid_device *sibling)
401 struct wacom *wacom = hid_get_drvdata(hdev);
402 struct wacom_features *features = &wacom->wacom_wac.features;
403 int vid = features->oVid;
404 int pid = features->oPid;
407 if (vid == 0 && pid == 0) {
412 if (vid != sibling->vendor || pid != sibling->product)
415 /* Compare the physical path. */
416 n1 = strrchr(hdev->phys, '.') - hdev->phys;
417 n2 = strrchr(sibling->phys, '.') - sibling->phys;
418 if (n1 != n2 || n1 <= 0 || n2 <= 0)
421 return !strncmp(hdev->phys, sibling->phys, n1);
424 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
426 struct wacom_hdev_data *data;
428 list_for_each_entry(data, &wacom_udev_list, list) {
429 if (wacom_are_sibling(hdev, data->dev)) {
430 kref_get(&data->kref);
438 static int wacom_add_shared_data(struct hid_device *hdev)
440 struct wacom *wacom = hid_get_drvdata(hdev);
441 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
442 struct wacom_hdev_data *data;
445 mutex_lock(&wacom_udev_list_lock);
447 data = wacom_get_hdev_data(hdev);
449 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
455 kref_init(&data->kref);
457 list_add_tail(&data->list, &wacom_udev_list);
460 wacom_wac->shared = &data->shared;
463 mutex_unlock(&wacom_udev_list_lock);
467 static void wacom_release_shared_data(struct kref *kref)
469 struct wacom_hdev_data *data =
470 container_of(kref, struct wacom_hdev_data, kref);
472 mutex_lock(&wacom_udev_list_lock);
473 list_del(&data->list);
474 mutex_unlock(&wacom_udev_list_lock);
479 static void wacom_remove_shared_data(struct wacom_wac *wacom)
481 struct wacom_hdev_data *data;
484 data = container_of(wacom->shared, struct wacom_hdev_data, shared);
485 kref_put(&data->kref, wacom_release_shared_data);
486 wacom->shared = NULL;
490 static int wacom_led_control(struct wacom *wacom)
494 unsigned char report_id = WAC_CMD_LED_CONTROL;
497 if (wacom->wacom_wac.pid) { /* wireless connected */
498 report_id = WAC_CMD_WL_LED_CONTROL;
501 buf = kzalloc(buf_size, GFP_KERNEL);
505 if (wacom->wacom_wac.features.type >= INTUOS5S &&
506 wacom->wacom_wac.features.type <= INTUOSPL) {
508 * Touch Ring and crop mark LED luminance may take on
509 * one of four values:
510 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
512 int ring_led = wacom->led.select[0] & 0x03;
513 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
515 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
518 if (wacom->wacom_wac.pid) {
519 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
520 buf, buf_size, WAC_CMD_RETRIES);
527 int led = wacom->led.select[0] | 0x4;
529 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
530 wacom->wacom_wac.features.type == WACOM_24HD)
531 led |= (wacom->led.select[1] << 4) | 0x40;
535 buf[2] = wacom->led.llv;
536 buf[3] = wacom->led.hlv;
537 buf[4] = wacom->led.img_lum;
540 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
547 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
548 const unsigned len, const void *img)
552 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
554 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
558 /* Send 'start' command */
559 buf[0] = WAC_CMD_ICON_START;
561 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
567 buf[1] = button_id & 0x07;
568 for (i = 0; i < 4; i++) {
570 memcpy(buf + 3, img + i * chunk_len, chunk_len);
572 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
573 buf, chunk_len + 3, WAC_CMD_RETRIES);
579 buf[0] = WAC_CMD_ICON_START;
581 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
589 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
590 const char *buf, size_t count)
592 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
593 struct wacom *wacom = hid_get_drvdata(hdev);
597 err = kstrtouint(buf, 10, &id);
601 mutex_lock(&wacom->lock);
603 wacom->led.select[set_id] = id & 0x3;
604 err = wacom_led_control(wacom);
606 mutex_unlock(&wacom->lock);
608 return err < 0 ? err : count;
611 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
612 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
613 struct device_attribute *attr, const char *buf, size_t count) \
615 return wacom_led_select_store(dev, SET_ID, buf, count); \
617 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
618 struct device_attribute *attr, char *buf) \
620 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
621 struct wacom *wacom = hid_get_drvdata(hdev); \
622 return scnprintf(buf, PAGE_SIZE, "%d\n", \
623 wacom->led.select[SET_ID]); \
625 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
626 wacom_led##SET_ID##_select_show, \
627 wacom_led##SET_ID##_select_store)
629 DEVICE_LED_SELECT_ATTR(0);
630 DEVICE_LED_SELECT_ATTR(1);
632 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
633 const char *buf, size_t count)
638 err = kstrtouint(buf, 10, &value);
642 mutex_lock(&wacom->lock);
644 *dest = value & 0x7f;
645 err = wacom_led_control(wacom);
647 mutex_unlock(&wacom->lock);
649 return err < 0 ? err : count;
652 #define DEVICE_LUMINANCE_ATTR(name, field) \
653 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
654 struct device_attribute *attr, const char *buf, size_t count) \
656 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
657 struct wacom *wacom = hid_get_drvdata(hdev); \
659 return wacom_luminance_store(wacom, &wacom->led.field, \
662 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
663 struct device_attribute *attr, char *buf) \
665 struct wacom *wacom = dev_get_drvdata(dev); \
666 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
668 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
669 wacom_##name##_luminance_show, \
670 wacom_##name##_luminance_store)
672 DEVICE_LUMINANCE_ATTR(status0, llv);
673 DEVICE_LUMINANCE_ATTR(status1, hlv);
674 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
676 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
677 const char *buf, size_t count)
679 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
680 struct wacom *wacom = hid_get_drvdata(hdev);
685 if (hdev->bus == BUS_BLUETOOTH) {
687 xfer_id = WAC_CMD_ICON_BT_XFER;
690 xfer_id = WAC_CMD_ICON_XFER;
696 mutex_lock(&wacom->lock);
698 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
700 mutex_unlock(&wacom->lock);
702 return err < 0 ? err : count;
705 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
706 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
707 struct device_attribute *attr, const char *buf, size_t count) \
709 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
711 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
712 NULL, wacom_btnimg##BUTTON_ID##_store)
714 DEVICE_BTNIMG_ATTR(0);
715 DEVICE_BTNIMG_ATTR(1);
716 DEVICE_BTNIMG_ATTR(2);
717 DEVICE_BTNIMG_ATTR(3);
718 DEVICE_BTNIMG_ATTR(4);
719 DEVICE_BTNIMG_ATTR(5);
720 DEVICE_BTNIMG_ATTR(6);
721 DEVICE_BTNIMG_ATTR(7);
723 static struct attribute *cintiq_led_attrs[] = {
724 &dev_attr_status_led0_select.attr,
725 &dev_attr_status_led1_select.attr,
729 static struct attribute_group cintiq_led_attr_group = {
731 .attrs = cintiq_led_attrs,
734 static struct attribute *intuos4_led_attrs[] = {
735 &dev_attr_status0_luminance.attr,
736 &dev_attr_status1_luminance.attr,
737 &dev_attr_status_led0_select.attr,
738 &dev_attr_buttons_luminance.attr,
739 &dev_attr_button0_rawimg.attr,
740 &dev_attr_button1_rawimg.attr,
741 &dev_attr_button2_rawimg.attr,
742 &dev_attr_button3_rawimg.attr,
743 &dev_attr_button4_rawimg.attr,
744 &dev_attr_button5_rawimg.attr,
745 &dev_attr_button6_rawimg.attr,
746 &dev_attr_button7_rawimg.attr,
750 static struct attribute_group intuos4_led_attr_group = {
752 .attrs = intuos4_led_attrs,
755 static struct attribute *intuos5_led_attrs[] = {
756 &dev_attr_status0_luminance.attr,
757 &dev_attr_status_led0_select.attr,
761 static struct attribute_group intuos5_led_attr_group = {
763 .attrs = intuos5_led_attrs,
766 static int wacom_initialize_leds(struct wacom *wacom)
770 /* Initialize default values */
771 switch (wacom->wacom_wac.features.type) {
776 wacom->led.select[0] = 0;
777 wacom->led.select[1] = 0;
780 wacom->led.img_lum = 10;
781 error = sysfs_create_group(&wacom->hdev->dev.kobj,
782 &intuos4_led_attr_group);
787 wacom->led.select[0] = 0;
788 wacom->led.select[1] = 0;
791 wacom->led.img_lum = 0;
793 error = sysfs_create_group(&wacom->hdev->dev.kobj,
794 &cintiq_led_attr_group);
803 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
804 wacom->led.select[0] = 0;
805 wacom->led.select[1] = 0;
808 wacom->led.img_lum = 0;
810 error = sysfs_create_group(&wacom->hdev->dev.kobj,
811 &intuos5_led_attr_group);
822 "cannot create sysfs group err: %d\n", error);
825 wacom_led_control(wacom);
826 wacom->led_initialized = true;
831 static void wacom_destroy_leds(struct wacom *wacom)
833 if (!wacom->led_initialized)
836 wacom->led_initialized = false;
838 switch (wacom->wacom_wac.features.type) {
843 sysfs_remove_group(&wacom->hdev->dev.kobj,
844 &intuos4_led_attr_group);
849 sysfs_remove_group(&wacom->hdev->dev.kobj,
850 &cintiq_led_attr_group);
859 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
860 sysfs_remove_group(&wacom->hdev->dev.kobj,
861 &intuos5_led_attr_group);
866 static enum power_supply_property wacom_battery_props[] = {
867 POWER_SUPPLY_PROP_STATUS,
868 POWER_SUPPLY_PROP_SCOPE,
869 POWER_SUPPLY_PROP_CAPACITY
872 static enum power_supply_property wacom_ac_props[] = {
873 POWER_SUPPLY_PROP_PRESENT,
874 POWER_SUPPLY_PROP_ONLINE,
875 POWER_SUPPLY_PROP_SCOPE,
878 static int wacom_battery_get_property(struct power_supply *psy,
879 enum power_supply_property psp,
880 union power_supply_propval *val)
882 struct wacom *wacom = container_of(psy, struct wacom, battery);
886 case POWER_SUPPLY_PROP_SCOPE:
887 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
889 case POWER_SUPPLY_PROP_CAPACITY:
891 wacom->wacom_wac.battery_capacity;
893 case POWER_SUPPLY_PROP_STATUS:
894 if (wacom->wacom_wac.bat_charging)
895 val->intval = POWER_SUPPLY_STATUS_CHARGING;
896 else if (wacom->wacom_wac.battery_capacity == 100 &&
897 wacom->wacom_wac.ps_connected)
898 val->intval = POWER_SUPPLY_STATUS_FULL;
900 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
910 static int wacom_ac_get_property(struct power_supply *psy,
911 enum power_supply_property psp,
912 union power_supply_propval *val)
914 struct wacom *wacom = container_of(psy, struct wacom, ac);
918 case POWER_SUPPLY_PROP_PRESENT:
920 case POWER_SUPPLY_PROP_ONLINE:
921 val->intval = wacom->wacom_wac.ps_connected;
923 case POWER_SUPPLY_PROP_SCOPE:
924 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
933 static int wacom_initialize_battery(struct wacom *wacom)
935 static atomic_t battery_no = ATOMIC_INIT(0);
939 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
940 n = atomic_inc_return(&battery_no) - 1;
942 wacom->battery.properties = wacom_battery_props;
943 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
944 wacom->battery.get_property = wacom_battery_get_property;
945 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
946 wacom->battery.name = wacom->wacom_wac.bat_name;
947 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
948 wacom->battery.use_for_apm = 0;
950 wacom->ac.properties = wacom_ac_props;
951 wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
952 wacom->ac.get_property = wacom_ac_get_property;
953 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
954 wacom->ac.name = wacom->wacom_wac.ac_name;
955 wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
956 wacom->ac.use_for_apm = 0;
958 error = power_supply_register(&wacom->hdev->dev,
963 power_supply_powers(&wacom->battery, &wacom->hdev->dev);
965 error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
967 power_supply_unregister(&wacom->battery);
971 power_supply_powers(&wacom->ac, &wacom->hdev->dev);
977 static void wacom_destroy_battery(struct wacom *wacom)
979 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
980 wacom->battery.dev) {
981 power_supply_unregister(&wacom->battery);
982 wacom->battery.dev = NULL;
983 power_supply_unregister(&wacom->ac);
984 wacom->ac.dev = NULL;
988 static ssize_t wacom_show_speed(struct device *dev,
989 struct device_attribute
992 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
993 struct wacom *wacom = hid_get_drvdata(hdev);
995 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
998 static ssize_t wacom_store_speed(struct device *dev,
999 struct device_attribute *attr,
1000 const char *buf, size_t count)
1002 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1003 struct wacom *wacom = hid_get_drvdata(hdev);
1006 if (kstrtou8(buf, 0, &new_speed))
1009 if (new_speed != 0 && new_speed != 1)
1012 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1017 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1018 wacom_show_speed, wacom_store_speed);
1020 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1022 struct input_dev *input_dev;
1023 struct hid_device *hdev = wacom->hdev;
1024 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1026 input_dev = input_allocate_device();
1030 input_dev->name = wacom_wac->name;
1031 input_dev->phys = hdev->phys;
1032 input_dev->dev.parent = &hdev->dev;
1033 input_dev->open = wacom_open;
1034 input_dev->close = wacom_close;
1035 input_dev->uniq = hdev->uniq;
1036 input_dev->id.bustype = hdev->bus;
1037 input_dev->id.vendor = hdev->vendor;
1038 input_dev->id.product = hdev->product;
1039 input_dev->id.version = hdev->version;
1040 input_set_drvdata(input_dev, wacom);
1045 static void wacom_unregister_inputs(struct wacom *wacom)
1047 if (wacom->wacom_wac.input)
1048 input_unregister_device(wacom->wacom_wac.input);
1049 if (wacom->wacom_wac.pad_input)
1050 input_unregister_device(wacom->wacom_wac.pad_input);
1051 wacom->wacom_wac.input = NULL;
1052 wacom->wacom_wac.pad_input = NULL;
1053 wacom_destroy_leds(wacom);
1056 static int wacom_register_inputs(struct wacom *wacom)
1058 struct input_dev *input_dev, *pad_input_dev;
1059 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1062 input_dev = wacom_allocate_input(wacom);
1063 pad_input_dev = wacom_allocate_input(wacom);
1064 if (!input_dev || !pad_input_dev) {
1069 wacom_wac->input = input_dev;
1070 wacom_wac->pad_input = pad_input_dev;
1071 wacom_wac->pad_input->name = wacom_wac->pad_name;
1073 error = wacom_setup_input_capabilities(input_dev, wacom_wac);
1077 error = input_register_device(input_dev);
1081 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1083 /* no pad in use on this interface */
1084 input_free_device(pad_input_dev);
1085 wacom_wac->pad_input = NULL;
1086 pad_input_dev = NULL;
1088 error = input_register_device(pad_input_dev);
1092 error = wacom_initialize_leds(wacom);
1100 input_unregister_device(pad_input_dev);
1101 pad_input_dev = NULL;
1103 input_unregister_device(input_dev);
1106 wacom_wac->input = NULL;
1107 wacom_wac->pad_input = NULL;
1110 input_free_device(input_dev);
1112 input_free_device(pad_input_dev);
1116 static void wacom_wireless_work(struct work_struct *work)
1118 struct wacom *wacom = container_of(work, struct wacom, work);
1119 struct usb_device *usbdev = wacom->usbdev;
1120 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1121 struct hid_device *hdev1, *hdev2;
1122 struct wacom *wacom1, *wacom2;
1123 struct wacom_wac *wacom_wac1, *wacom_wac2;
1127 * Regardless if this is a disconnect or a new tablet,
1128 * remove any existing input and battery devices.
1131 wacom_destroy_battery(wacom);
1133 /* Stylus interface */
1134 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1135 wacom1 = hid_get_drvdata(hdev1);
1136 wacom_wac1 = &(wacom1->wacom_wac);
1137 wacom_unregister_inputs(wacom1);
1139 /* Touch interface */
1140 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1141 wacom2 = hid_get_drvdata(hdev2);
1142 wacom_wac2 = &(wacom2->wacom_wac);
1143 wacom_unregister_inputs(wacom2);
1145 if (wacom_wac->pid == 0) {
1146 hid_info(wacom->hdev, "wireless tablet disconnected\n");
1147 wacom_wac1->shared->type = 0;
1149 const struct hid_device_id *id = wacom_ids;
1151 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1155 if (id->vendor == USB_VENDOR_ID_WACOM &&
1156 id->product == wacom_wac->pid)
1162 hid_info(wacom->hdev, "ignoring unknown PID.\n");
1166 /* Stylus interface */
1167 wacom_wac1->features =
1168 *((struct wacom_features *)id->driver_data);
1169 wacom_wac1->features.device_type = BTN_TOOL_PEN;
1170 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1171 wacom_wac1->features.name);
1172 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1173 wacom_wac1->features.name);
1174 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1175 wacom_wac1->shared->type = wacom_wac1->features.type;
1176 wacom_wac1->pid = wacom_wac->pid;
1177 error = wacom_register_inputs(wacom1);
1181 /* Touch interface */
1182 if (wacom_wac1->features.touch_max ||
1183 wacom_wac1->features.type == INTUOSHT) {
1184 wacom_wac2->features =
1185 *((struct wacom_features *)id->driver_data);
1186 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1187 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1188 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1189 if (wacom_wac2->features.touch_max)
1190 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1191 "%s (WL) Finger",wacom_wac2->features.name);
1193 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1194 "%s (WL) Pad",wacom_wac2->features.name);
1195 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1196 "%s (WL) Pad", wacom_wac2->features.name);
1197 wacom_wac2->pid = wacom_wac->pid;
1198 error = wacom_register_inputs(wacom2);
1202 if (wacom_wac1->features.type == INTUOSHT &&
1203 wacom_wac1->features.touch_max)
1204 wacom_wac->shared->touch_input = wacom_wac2->input;
1207 error = wacom_initialize_battery(wacom);
1215 wacom_unregister_inputs(wacom1);
1216 wacom_unregister_inputs(wacom2);
1221 * Not all devices report physical dimensions from HID.
1222 * Compute the default from hardcoded logical dimension
1223 * and resolution before driver overwrites them.
1225 static void wacom_set_default_phy(struct wacom_features *features)
1227 if (features->x_resolution) {
1228 features->x_phy = (features->x_max * 100) /
1229 features->x_resolution;
1230 features->y_phy = (features->y_max * 100) /
1231 features->y_resolution;
1235 static void wacom_calculate_res(struct wacom_features *features)
1237 features->x_resolution = wacom_calc_hid_res(features->x_max,
1240 features->unitExpo);
1241 features->y_resolution = wacom_calc_hid_res(features->y_max,
1244 features->unitExpo);
1247 static int wacom_hid_report_len(struct hid_report *report)
1249 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1250 return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1253 static size_t wacom_compute_pktlen(struct hid_device *hdev)
1255 struct hid_report_enum *report_enum;
1256 struct hid_report *report;
1259 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1261 list_for_each_entry(report, &report_enum->report_list, list) {
1262 size_t report_size = wacom_hid_report_len(report);
1263 if (report_size > size)
1270 static int wacom_probe(struct hid_device *hdev,
1271 const struct hid_device_id *id)
1273 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1274 struct usb_device *dev = interface_to_usbdev(intf);
1275 struct wacom *wacom;
1276 struct wacom_wac *wacom_wac;
1277 struct wacom_features *features;
1280 if (!id->driver_data)
1283 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1287 hid_set_drvdata(hdev, wacom);
1290 /* ask for the report descriptor to be loaded by HID */
1291 error = hid_parse(hdev);
1293 hid_err(hdev, "parse failed\n");
1297 wacom_wac = &wacom->wacom_wac;
1298 wacom_wac->features = *((struct wacom_features *)id->driver_data);
1299 features = &wacom_wac->features;
1300 features->pktlen = wacom_compute_pktlen(hdev);
1301 if (features->pktlen > WACOM_PKGLEN_MAX) {
1306 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1311 wacom->usbdev = dev;
1313 mutex_init(&wacom->lock);
1314 INIT_WORK(&wacom->work, wacom_wireless_work);
1316 /* set the default size in case we do not get them from hid */
1317 wacom_set_default_phy(features);
1319 /* Retrieve the physical and logical size for touch devices */
1320 wacom_retrieve_hid_descriptor(hdev, features);
1323 * Intuos5 has no useful data about its touch interface in its
1324 * HID descriptor. If this is the touch interface (PacketSize
1325 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1327 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
1328 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
1329 features->device_type = BTN_TOOL_FINGER;
1331 features->x_max = 4096;
1332 features->y_max = 4096;
1334 features->device_type = BTN_TOOL_PEN;
1339 * Same thing for Bamboo 3rd gen.
1341 if ((features->type == BAMBOO_PT) &&
1342 (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1343 (features->device_type == BTN_TOOL_PEN)) {
1344 features->device_type = BTN_TOOL_FINGER;
1346 features->x_max = 4096;
1347 features->y_max = 4096;
1350 if (hdev->bus == BUS_BLUETOOTH)
1351 features->quirks |= WACOM_QUIRK_BATTERY;
1353 wacom_setup_device_quirks(features);
1355 /* set unit to "100th of a mm" for devices not reported by HID */
1356 if (!features->unit) {
1357 features->unit = 0x11;
1358 features->unitExpo = -3;
1360 wacom_calculate_res(features);
1362 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1363 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1364 "%s Pad", features->name);
1366 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
1367 /* Append the device type to the name */
1368 if (features->device_type != BTN_TOOL_FINGER)
1369 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1370 else if (features->touch_max)
1371 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1373 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
1375 error = wacom_add_shared_data(hdev);
1380 if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1381 (features->quirks & WACOM_QUIRK_BATTERY)) {
1382 error = wacom_initialize_battery(wacom);
1387 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1388 error = wacom_register_inputs(wacom);
1393 if (hdev->bus == BUS_BLUETOOTH) {
1394 error = device_create_file(&hdev->dev, &dev_attr_speed);
1397 "can't create sysfs speed attribute err: %d\n",
1401 /* Note that if query fails it is not a hard failure */
1402 wacom_query_tablet_data(hdev, features);
1404 /* Regular HID work starts now */
1405 error = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1407 hid_err(hdev, "hw start failed\n");
1411 if (features->quirks & WACOM_QUIRK_MONITOR)
1412 error = hid_hw_open(hdev);
1414 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1415 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1416 wacom_wac->shared->touch_input = wacom_wac->input;
1421 fail4: if (hdev->bus == BUS_BLUETOOTH)
1422 device_remove_file(&hdev->dev, &dev_attr_speed);
1423 wacom_unregister_inputs(wacom);
1424 fail3: wacom_destroy_battery(wacom);
1425 fail2: wacom_remove_shared_data(wacom_wac);
1426 fail1: kfree(wacom);
1427 hid_set_drvdata(hdev, NULL);
1431 static void wacom_remove(struct hid_device *hdev)
1433 struct wacom *wacom = hid_get_drvdata(hdev);
1437 cancel_work_sync(&wacom->work);
1438 wacom_unregister_inputs(wacom);
1439 if (hdev->bus == BUS_BLUETOOTH)
1440 device_remove_file(&hdev->dev, &dev_attr_speed);
1441 wacom_destroy_battery(wacom);
1442 wacom_remove_shared_data(&wacom->wacom_wac);
1444 hid_set_drvdata(hdev, NULL);
1449 static int wacom_resume(struct hid_device *hdev)
1451 struct wacom *wacom = hid_get_drvdata(hdev);
1452 struct wacom_features *features = &wacom->wacom_wac.features;
1454 mutex_lock(&wacom->lock);
1456 /* switch to wacom mode first */
1457 wacom_query_tablet_data(hdev, features);
1458 wacom_led_control(wacom);
1460 mutex_unlock(&wacom->lock);
1465 static int wacom_reset_resume(struct hid_device *hdev)
1467 return wacom_resume(hdev);
1469 #endif /* CONFIG_PM */
1471 static struct hid_driver wacom_driver = {
1473 .id_table = wacom_ids,
1474 .probe = wacom_probe,
1475 .remove = wacom_remove,
1477 .resume = wacom_resume,
1478 .reset_resume = wacom_reset_resume,
1480 .raw_event = wacom_raw_event,
1482 module_hid_driver(wacom_driver);
1484 MODULE_VERSION(DRIVER_VERSION);
1485 MODULE_AUTHOR(DRIVER_AUTHOR);
1486 MODULE_DESCRIPTION(DRIVER_DESC);
1487 MODULE_LICENSE(DRIVER_LICENSE);