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
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;
113 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
117 switch (usage->hid) {
118 case HID_DG_CONTACTMAX:
119 /* leave touch_max as is if predefined */
120 if (!features->touch_max) {
122 data = kzalloc(2, GFP_KERNEL);
125 data[0] = field->report->id;
126 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
129 features->touch_max = data[1];
133 case HID_DG_INPUTMODE:
134 /* Ignore if value index is out of bounds. */
135 if (usage->usage_index >= field->report_count) {
136 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
140 hid_data->inputmode = field->report->id;
141 hid_data->inputmode_index = usage->usage_index;
147 * Interface Descriptor of wacom devices can be incomplete and
148 * inconsistent so wacom_features table is used to store stylus
149 * device's packet lengths, various maximum values, and tablet
150 * resolution based on product ID's.
152 * For devices that contain 2 interfaces, wacom_features table is
153 * inaccurate for the touch interface. Since the Interface Descriptor
154 * for touch interfaces has pretty complete data, this function exists
155 * to query tablet for this missing information instead of hard coding in
156 * an additional table.
158 * A typical Interface Descriptor for a stylus will contain a
159 * boot mouse application collection that is not of interest and this
160 * function will ignore it.
162 * It also contains a digitizer application collection that also is not
163 * of interest since any information it contains would be duplicate
164 * of what is in wacom_features. Usually it defines a report of an array
165 * of bytes that could be used as max length of the stylus packet returned.
166 * If it happens to define a Digitizer-Stylus Physical Collection then
167 * the X and Y logical values contain valid data but it is ignored.
169 * A typical Interface Descriptor for a touch interface will contain a
170 * Digitizer-Finger Physical Collection which will define both logical
171 * X/Y maximum as well as the physical size of tablet. Since touch
172 * interfaces haven't supported pressure or distance, this is enough
173 * information to override invalid values in the wacom_features table.
175 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
176 * data. We deal with them after returning from this function.
178 static void wacom_usage_mapping(struct hid_device *hdev,
179 struct hid_field *field, struct hid_usage *usage)
181 struct wacom *wacom = hid_get_drvdata(hdev);
182 struct wacom_features *features = &wacom->wacom_wac.features;
183 bool finger = (field->logical == HID_DG_FINGER) ||
184 (field->physical == HID_DG_FINGER);
185 bool pen = (field->logical == HID_DG_STYLUS) ||
186 (field->physical == HID_DG_STYLUS);
189 * Requiring Stylus Usage will ignore boot mouse
190 * X/Y values and some cases of invalid Digitizer X/Y
191 * values commonly reported.
197 * Bamboo models do not support HID_DG_CONTACTMAX.
198 * And, Bamboo Pen only descriptor contains touch.
200 if (features->type != BAMBOO_PT) {
201 /* ISDv4 touch devices at least supports one touch point */
202 if (finger && !features->touch_max)
203 features->touch_max = 1;
206 switch (usage->hid) {
208 features->x_max = field->logical_maximum;
210 features->device_type = BTN_TOOL_FINGER;
211 features->x_phy = field->physical_maximum;
212 if (features->type != BAMBOO_PT) {
213 features->unit = field->unit;
214 features->unitExpo = field->unit_exponent;
217 features->device_type = BTN_TOOL_PEN;
221 features->y_max = field->logical_maximum;
223 features->y_phy = field->physical_maximum;
224 if (features->type != BAMBOO_PT) {
225 features->unit = field->unit;
226 features->unitExpo = field->unit_exponent;
230 case HID_DG_TIPPRESSURE:
232 features->pressure_max = field->logical_maximum;
236 if (features->type == HID_GENERIC)
237 wacom_wac_usage_mapping(hdev, field, usage);
240 static void wacom_post_parse_hid(struct hid_device *hdev,
241 struct wacom_features *features)
243 struct wacom *wacom = hid_get_drvdata(hdev);
244 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
246 if (features->type == HID_GENERIC) {
247 /* Any last-minute generic device setup */
248 if (features->touch_max > 1) {
249 input_mt_init_slots(wacom_wac->input, wacom_wac->features.touch_max,
255 static void wacom_parse_hid(struct hid_device *hdev,
256 struct wacom_features *features)
258 struct hid_report_enum *rep_enum;
259 struct hid_report *hreport;
262 /* check features first */
263 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
264 list_for_each_entry(hreport, &rep_enum->report_list, list) {
265 for (i = 0; i < hreport->maxfield; i++) {
266 /* Ignore if report count is out of bounds. */
267 if (hreport->field[i]->report_count < 1)
270 for (j = 0; j < hreport->field[i]->maxusage; j++) {
271 wacom_feature_mapping(hdev, hreport->field[i],
272 hreport->field[i]->usage + j);
277 /* now check the input usages */
278 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
279 list_for_each_entry(hreport, &rep_enum->report_list, list) {
281 if (!hreport->maxfield)
284 for (i = 0; i < hreport->maxfield; i++)
285 for (j = 0; j < hreport->field[i]->maxusage; j++)
286 wacom_usage_mapping(hdev, hreport->field[i],
287 hreport->field[i]->usage + j);
290 wacom_post_parse_hid(hdev, features);
293 static int wacom_hid_set_device_mode(struct hid_device *hdev)
295 struct wacom *wacom = hid_get_drvdata(hdev);
296 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
297 struct hid_report *r;
298 struct hid_report_enum *re;
300 if (hid_data->inputmode < 0)
303 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
304 r = re->report_id_hash[hid_data->inputmode];
306 r->field[0]->value[hid_data->inputmode_index] = 2;
307 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
312 static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
313 int length, int mode)
315 unsigned char *rep_data;
316 int error = -ENOMEM, limit = 0;
318 rep_data = kzalloc(length, GFP_KERNEL);
323 rep_data[0] = report_id;
326 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
329 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
330 rep_data, length, 1);
331 } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
335 return error < 0 ? error : 0;
338 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
339 struct wacom_features *features)
341 struct wacom *wacom = hid_get_drvdata(hdev);
345 switch (features->type) {
349 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
353 rep_data[0] = speed == 0 ? 0x05 : 0x06;
356 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
360 wacom->wacom_wac.bt_high_speed = speed;
366 * Note that if the raw queries fail, it's not a hard failure
367 * and it is safe to continue
369 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
374 wacom->wacom_wac.bt_features &= ~0x20;
376 wacom->wacom_wac.bt_features |= 0x20;
379 rep_data[1] = wacom->wacom_wac.bt_features;
381 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
384 wacom->wacom_wac.bt_high_speed = speed;
392 * Switch the tablet into its most-capable mode. Wacom tablets are
393 * typically configured to power-up in a mode which sends mouse-like
394 * reports to the OS. To get absolute position, pressure data, etc.
395 * from the tablet, it is necessary to switch the tablet out of this
396 * mode and into one which sends the full range of tablet data.
398 static int wacom_query_tablet_data(struct hid_device *hdev,
399 struct wacom_features *features)
401 if (hdev->bus == BUS_BLUETOOTH)
402 return wacom_bt_query_tablet_data(hdev, 1, features);
404 if (features->type == HID_GENERIC)
405 return wacom_hid_set_device_mode(hdev);
407 if (features->device_type == BTN_TOOL_FINGER) {
408 if (features->type > TABLETPC) {
409 /* MT Tablet PC touch */
410 return wacom_set_device_mode(hdev, 3, 4, 4);
412 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
413 return wacom_set_device_mode(hdev, 18, 3, 2);
415 } else if (features->device_type == BTN_TOOL_PEN) {
416 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
417 return wacom_set_device_mode(hdev, 2, 2, 2);
424 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
425 struct wacom_features *features)
427 struct wacom *wacom = hid_get_drvdata(hdev);
428 struct usb_interface *intf = wacom->intf;
430 /* default features */
431 features->device_type = BTN_TOOL_PEN;
432 features->x_fuzz = 4;
433 features->y_fuzz = 4;
434 features->pressure_fuzz = 0;
435 features->distance_fuzz = 0;
438 * The wireless device HID is basic and layout conflicts with
439 * other tablets (monitor and touch interface can look like pen).
440 * Skip the query for this type and modify defaults based on
443 if (features->type == WIRELESS) {
444 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
445 features->device_type = 0;
446 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
447 features->device_type = BTN_TOOL_FINGER;
448 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
452 /* only devices that support touch need to retrieve the info */
453 if (features->type < BAMBOO_PT)
456 wacom_parse_hid(hdev, features);
459 struct wacom_hdev_data {
460 struct list_head list;
462 struct hid_device *dev;
463 struct wacom_shared shared;
466 static LIST_HEAD(wacom_udev_list);
467 static DEFINE_MUTEX(wacom_udev_list_lock);
469 static bool wacom_are_sibling(struct hid_device *hdev,
470 struct hid_device *sibling)
472 struct wacom *wacom = hid_get_drvdata(hdev);
473 struct wacom_features *features = &wacom->wacom_wac.features;
474 int vid = features->oVid;
475 int pid = features->oPid;
478 if (vid == 0 && pid == 0) {
483 if (vid != sibling->vendor || pid != sibling->product)
486 /* Compare the physical path. */
487 n1 = strrchr(hdev->phys, '.') - hdev->phys;
488 n2 = strrchr(sibling->phys, '.') - sibling->phys;
489 if (n1 != n2 || n1 <= 0 || n2 <= 0)
492 return !strncmp(hdev->phys, sibling->phys, n1);
495 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
497 struct wacom_hdev_data *data;
499 list_for_each_entry(data, &wacom_udev_list, list) {
500 if (wacom_are_sibling(hdev, data->dev)) {
501 kref_get(&data->kref);
509 static int wacom_add_shared_data(struct hid_device *hdev)
511 struct wacom *wacom = hid_get_drvdata(hdev);
512 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
513 struct wacom_hdev_data *data;
516 mutex_lock(&wacom_udev_list_lock);
518 data = wacom_get_hdev_data(hdev);
520 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
526 kref_init(&data->kref);
528 list_add_tail(&data->list, &wacom_udev_list);
531 wacom_wac->shared = &data->shared;
534 mutex_unlock(&wacom_udev_list_lock);
538 static void wacom_release_shared_data(struct kref *kref)
540 struct wacom_hdev_data *data =
541 container_of(kref, struct wacom_hdev_data, kref);
543 mutex_lock(&wacom_udev_list_lock);
544 list_del(&data->list);
545 mutex_unlock(&wacom_udev_list_lock);
550 static void wacom_remove_shared_data(struct wacom_wac *wacom)
552 struct wacom_hdev_data *data;
555 data = container_of(wacom->shared, struct wacom_hdev_data, shared);
556 kref_put(&data->kref, wacom_release_shared_data);
557 wacom->shared = NULL;
561 static int wacom_led_control(struct wacom *wacom)
565 unsigned char report_id = WAC_CMD_LED_CONTROL;
568 if (wacom->wacom_wac.pid) { /* wireless connected */
569 report_id = WAC_CMD_WL_LED_CONTROL;
572 buf = kzalloc(buf_size, GFP_KERNEL);
576 if (wacom->wacom_wac.features.type >= INTUOS5S &&
577 wacom->wacom_wac.features.type <= INTUOSPL) {
579 * Touch Ring and crop mark LED luminance may take on
580 * one of four values:
581 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
583 int ring_led = wacom->led.select[0] & 0x03;
584 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
586 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
589 if (wacom->wacom_wac.pid) {
590 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
591 buf, buf_size, WAC_CMD_RETRIES);
598 int led = wacom->led.select[0] | 0x4;
600 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
601 wacom->wacom_wac.features.type == WACOM_24HD)
602 led |= (wacom->led.select[1] << 4) | 0x40;
606 buf[2] = wacom->led.llv;
607 buf[3] = wacom->led.hlv;
608 buf[4] = wacom->led.img_lum;
611 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
618 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
619 const unsigned len, const void *img)
623 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
625 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
629 /* Send 'start' command */
630 buf[0] = WAC_CMD_ICON_START;
632 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
638 buf[1] = button_id & 0x07;
639 for (i = 0; i < 4; i++) {
641 memcpy(buf + 3, img + i * chunk_len, chunk_len);
643 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
644 buf, chunk_len + 3, WAC_CMD_RETRIES);
650 buf[0] = WAC_CMD_ICON_START;
652 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
660 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
661 const char *buf, size_t count)
663 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
664 struct wacom *wacom = hid_get_drvdata(hdev);
668 err = kstrtouint(buf, 10, &id);
672 mutex_lock(&wacom->lock);
674 wacom->led.select[set_id] = id & 0x3;
675 err = wacom_led_control(wacom);
677 mutex_unlock(&wacom->lock);
679 return err < 0 ? err : count;
682 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
683 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
684 struct device_attribute *attr, const char *buf, size_t count) \
686 return wacom_led_select_store(dev, SET_ID, buf, count); \
688 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
689 struct device_attribute *attr, char *buf) \
691 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
692 struct wacom *wacom = hid_get_drvdata(hdev); \
693 return scnprintf(buf, PAGE_SIZE, "%d\n", \
694 wacom->led.select[SET_ID]); \
696 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
697 wacom_led##SET_ID##_select_show, \
698 wacom_led##SET_ID##_select_store)
700 DEVICE_LED_SELECT_ATTR(0);
701 DEVICE_LED_SELECT_ATTR(1);
703 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
704 const char *buf, size_t count)
709 err = kstrtouint(buf, 10, &value);
713 mutex_lock(&wacom->lock);
715 *dest = value & 0x7f;
716 err = wacom_led_control(wacom);
718 mutex_unlock(&wacom->lock);
720 return err < 0 ? err : count;
723 #define DEVICE_LUMINANCE_ATTR(name, field) \
724 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
725 struct device_attribute *attr, const char *buf, size_t count) \
727 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
728 struct wacom *wacom = hid_get_drvdata(hdev); \
730 return wacom_luminance_store(wacom, &wacom->led.field, \
733 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
734 struct device_attribute *attr, char *buf) \
736 struct wacom *wacom = dev_get_drvdata(dev); \
737 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
739 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
740 wacom_##name##_luminance_show, \
741 wacom_##name##_luminance_store)
743 DEVICE_LUMINANCE_ATTR(status0, llv);
744 DEVICE_LUMINANCE_ATTR(status1, hlv);
745 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
747 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
748 const char *buf, size_t count)
750 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
751 struct wacom *wacom = hid_get_drvdata(hdev);
756 if (hdev->bus == BUS_BLUETOOTH) {
758 xfer_id = WAC_CMD_ICON_BT_XFER;
761 xfer_id = WAC_CMD_ICON_XFER;
767 mutex_lock(&wacom->lock);
769 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
771 mutex_unlock(&wacom->lock);
773 return err < 0 ? err : count;
776 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
777 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
778 struct device_attribute *attr, const char *buf, size_t count) \
780 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
782 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
783 NULL, wacom_btnimg##BUTTON_ID##_store)
785 DEVICE_BTNIMG_ATTR(0);
786 DEVICE_BTNIMG_ATTR(1);
787 DEVICE_BTNIMG_ATTR(2);
788 DEVICE_BTNIMG_ATTR(3);
789 DEVICE_BTNIMG_ATTR(4);
790 DEVICE_BTNIMG_ATTR(5);
791 DEVICE_BTNIMG_ATTR(6);
792 DEVICE_BTNIMG_ATTR(7);
794 static struct attribute *cintiq_led_attrs[] = {
795 &dev_attr_status_led0_select.attr,
796 &dev_attr_status_led1_select.attr,
800 static struct attribute_group cintiq_led_attr_group = {
802 .attrs = cintiq_led_attrs,
805 static struct attribute *intuos4_led_attrs[] = {
806 &dev_attr_status0_luminance.attr,
807 &dev_attr_status1_luminance.attr,
808 &dev_attr_status_led0_select.attr,
809 &dev_attr_buttons_luminance.attr,
810 &dev_attr_button0_rawimg.attr,
811 &dev_attr_button1_rawimg.attr,
812 &dev_attr_button2_rawimg.attr,
813 &dev_attr_button3_rawimg.attr,
814 &dev_attr_button4_rawimg.attr,
815 &dev_attr_button5_rawimg.attr,
816 &dev_attr_button6_rawimg.attr,
817 &dev_attr_button7_rawimg.attr,
821 static struct attribute_group intuos4_led_attr_group = {
823 .attrs = intuos4_led_attrs,
826 static struct attribute *intuos5_led_attrs[] = {
827 &dev_attr_status0_luminance.attr,
828 &dev_attr_status_led0_select.attr,
832 static struct attribute_group intuos5_led_attr_group = {
834 .attrs = intuos5_led_attrs,
837 static int wacom_initialize_leds(struct wacom *wacom)
841 /* Initialize default values */
842 switch (wacom->wacom_wac.features.type) {
847 wacom->led.select[0] = 0;
848 wacom->led.select[1] = 0;
851 wacom->led.img_lum = 10;
852 error = sysfs_create_group(&wacom->hdev->dev.kobj,
853 &intuos4_led_attr_group);
858 wacom->led.select[0] = 0;
859 wacom->led.select[1] = 0;
862 wacom->led.img_lum = 0;
864 error = sysfs_create_group(&wacom->hdev->dev.kobj,
865 &cintiq_led_attr_group);
874 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
875 wacom->led.select[0] = 0;
876 wacom->led.select[1] = 0;
879 wacom->led.img_lum = 0;
881 error = sysfs_create_group(&wacom->hdev->dev.kobj,
882 &intuos5_led_attr_group);
893 "cannot create sysfs group err: %d\n", error);
896 wacom_led_control(wacom);
897 wacom->led_initialized = true;
902 static void wacom_destroy_leds(struct wacom *wacom)
904 if (!wacom->led_initialized)
907 wacom->led_initialized = false;
909 switch (wacom->wacom_wac.features.type) {
914 sysfs_remove_group(&wacom->hdev->dev.kobj,
915 &intuos4_led_attr_group);
920 sysfs_remove_group(&wacom->hdev->dev.kobj,
921 &cintiq_led_attr_group);
930 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
931 sysfs_remove_group(&wacom->hdev->dev.kobj,
932 &intuos5_led_attr_group);
937 static enum power_supply_property wacom_battery_props[] = {
938 POWER_SUPPLY_PROP_STATUS,
939 POWER_SUPPLY_PROP_SCOPE,
940 POWER_SUPPLY_PROP_CAPACITY
943 static enum power_supply_property wacom_ac_props[] = {
944 POWER_SUPPLY_PROP_PRESENT,
945 POWER_SUPPLY_PROP_ONLINE,
946 POWER_SUPPLY_PROP_SCOPE,
949 static int wacom_battery_get_property(struct power_supply *psy,
950 enum power_supply_property psp,
951 union power_supply_propval *val)
953 struct wacom *wacom = container_of(psy, struct wacom, battery);
957 case POWER_SUPPLY_PROP_SCOPE:
958 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
960 case POWER_SUPPLY_PROP_CAPACITY:
962 wacom->wacom_wac.battery_capacity;
964 case POWER_SUPPLY_PROP_STATUS:
965 if (wacom->wacom_wac.bat_charging)
966 val->intval = POWER_SUPPLY_STATUS_CHARGING;
967 else if (wacom->wacom_wac.battery_capacity == 100 &&
968 wacom->wacom_wac.ps_connected)
969 val->intval = POWER_SUPPLY_STATUS_FULL;
971 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
981 static int wacom_ac_get_property(struct power_supply *psy,
982 enum power_supply_property psp,
983 union power_supply_propval *val)
985 struct wacom *wacom = container_of(psy, struct wacom, ac);
989 case POWER_SUPPLY_PROP_PRESENT:
991 case POWER_SUPPLY_PROP_ONLINE:
992 val->intval = wacom->wacom_wac.ps_connected;
994 case POWER_SUPPLY_PROP_SCOPE:
995 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1004 static int wacom_initialize_battery(struct wacom *wacom)
1006 static atomic_t battery_no = ATOMIC_INIT(0);
1010 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
1011 n = atomic_inc_return(&battery_no) - 1;
1013 wacom->battery.properties = wacom_battery_props;
1014 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
1015 wacom->battery.get_property = wacom_battery_get_property;
1016 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
1017 wacom->battery.name = wacom->wacom_wac.bat_name;
1018 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1019 wacom->battery.use_for_apm = 0;
1021 wacom->ac.properties = wacom_ac_props;
1022 wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
1023 wacom->ac.get_property = wacom_ac_get_property;
1024 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
1025 wacom->ac.name = wacom->wacom_wac.ac_name;
1026 wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
1027 wacom->ac.use_for_apm = 0;
1029 error = power_supply_register(&wacom->hdev->dev,
1034 power_supply_powers(&wacom->battery, &wacom->hdev->dev);
1036 error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
1038 power_supply_unregister(&wacom->battery);
1042 power_supply_powers(&wacom->ac, &wacom->hdev->dev);
1048 static void wacom_destroy_battery(struct wacom *wacom)
1050 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1051 wacom->battery.dev) {
1052 power_supply_unregister(&wacom->battery);
1053 wacom->battery.dev = NULL;
1054 power_supply_unregister(&wacom->ac);
1055 wacom->ac.dev = NULL;
1059 static ssize_t wacom_show_speed(struct device *dev,
1060 struct device_attribute
1063 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1064 struct wacom *wacom = hid_get_drvdata(hdev);
1066 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1069 static ssize_t wacom_store_speed(struct device *dev,
1070 struct device_attribute *attr,
1071 const char *buf, size_t count)
1073 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1074 struct wacom *wacom = hid_get_drvdata(hdev);
1077 if (kstrtou8(buf, 0, &new_speed))
1080 if (new_speed != 0 && new_speed != 1)
1083 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1088 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1089 wacom_show_speed, wacom_store_speed);
1091 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1093 struct input_dev *input_dev;
1094 struct hid_device *hdev = wacom->hdev;
1095 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1097 input_dev = input_allocate_device();
1101 input_dev->name = wacom_wac->name;
1102 input_dev->phys = hdev->phys;
1103 input_dev->dev.parent = &hdev->dev;
1104 input_dev->open = wacom_open;
1105 input_dev->close = wacom_close;
1106 input_dev->uniq = hdev->uniq;
1107 input_dev->id.bustype = hdev->bus;
1108 input_dev->id.vendor = hdev->vendor;
1109 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1110 input_dev->id.version = hdev->version;
1111 input_set_drvdata(input_dev, wacom);
1116 static void wacom_free_inputs(struct wacom *wacom)
1118 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1120 if (wacom_wac->input)
1121 input_free_device(wacom_wac->input);
1122 if (wacom_wac->pad_input)
1123 input_free_device(wacom_wac->pad_input);
1124 wacom_wac->input = NULL;
1125 wacom_wac->pad_input = NULL;
1128 static int wacom_allocate_inputs(struct wacom *wacom)
1130 struct input_dev *input_dev, *pad_input_dev;
1131 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1133 input_dev = wacom_allocate_input(wacom);
1134 pad_input_dev = wacom_allocate_input(wacom);
1135 if (!input_dev || !pad_input_dev) {
1136 wacom_free_inputs(wacom);
1140 wacom_wac->input = input_dev;
1141 wacom_wac->pad_input = pad_input_dev;
1142 wacom_wac->pad_input->name = wacom_wac->pad_name;
1147 static void wacom_clean_inputs(struct wacom *wacom)
1149 if (wacom->wacom_wac.input) {
1150 if (wacom->wacom_wac.input_registered)
1151 input_unregister_device(wacom->wacom_wac.input);
1153 input_free_device(wacom->wacom_wac.input);
1155 if (wacom->wacom_wac.pad_input) {
1156 if (wacom->wacom_wac.pad_registered)
1157 input_unregister_device(wacom->wacom_wac.pad_input);
1159 input_free_device(wacom->wacom_wac.pad_input);
1161 wacom->wacom_wac.input = NULL;
1162 wacom->wacom_wac.pad_input = NULL;
1163 wacom_destroy_leds(wacom);
1166 static int wacom_register_inputs(struct wacom *wacom)
1168 struct input_dev *input_dev, *pad_input_dev;
1169 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1172 input_dev = wacom_wac->input;
1173 pad_input_dev = wacom_wac->pad_input;
1175 if (!input_dev || !pad_input_dev)
1178 error = wacom_setup_pentouch_input_capabilities(input_dev, wacom_wac);
1180 error = input_register_device(input_dev);
1183 wacom_wac->input_registered = true;
1186 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1188 /* no pad in use on this interface */
1189 input_free_device(pad_input_dev);
1190 wacom_wac->pad_input = NULL;
1191 pad_input_dev = NULL;
1193 error = input_register_device(pad_input_dev);
1195 goto fail_register_pad_input;
1196 wacom_wac->pad_registered = true;
1198 error = wacom_initialize_leds(wacom);
1206 input_unregister_device(pad_input_dev);
1207 pad_input_dev = NULL;
1208 wacom_wac->pad_registered = false;
1209 fail_register_pad_input:
1210 input_unregister_device(input_dev);
1211 wacom_wac->input = NULL;
1212 wacom_wac->input_registered = false;
1216 static void wacom_wireless_work(struct work_struct *work)
1218 struct wacom *wacom = container_of(work, struct wacom, work);
1219 struct usb_device *usbdev = wacom->usbdev;
1220 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1221 struct hid_device *hdev1, *hdev2;
1222 struct wacom *wacom1, *wacom2;
1223 struct wacom_wac *wacom_wac1, *wacom_wac2;
1227 * Regardless if this is a disconnect or a new tablet,
1228 * remove any existing input and battery devices.
1231 wacom_destroy_battery(wacom);
1233 /* Stylus interface */
1234 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1235 wacom1 = hid_get_drvdata(hdev1);
1236 wacom_wac1 = &(wacom1->wacom_wac);
1237 wacom_clean_inputs(wacom1);
1239 /* Touch interface */
1240 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1241 wacom2 = hid_get_drvdata(hdev2);
1242 wacom_wac2 = &(wacom2->wacom_wac);
1243 wacom_clean_inputs(wacom2);
1245 if (wacom_wac->pid == 0) {
1246 hid_info(wacom->hdev, "wireless tablet disconnected\n");
1247 wacom_wac1->shared->type = 0;
1249 const struct hid_device_id *id = wacom_ids;
1251 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1255 if (id->vendor == USB_VENDOR_ID_WACOM &&
1256 id->product == wacom_wac->pid)
1262 hid_info(wacom->hdev, "ignoring unknown PID.\n");
1266 /* Stylus interface */
1267 wacom_wac1->features =
1268 *((struct wacom_features *)id->driver_data);
1269 wacom_wac1->features.device_type = BTN_TOOL_PEN;
1270 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1271 wacom_wac1->features.name);
1272 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1273 wacom_wac1->features.name);
1274 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1275 wacom_wac1->shared->type = wacom_wac1->features.type;
1276 wacom_wac1->pid = wacom_wac->pid;
1277 error = wacom_allocate_inputs(wacom1) ||
1278 wacom_register_inputs(wacom1);
1282 /* Touch interface */
1283 if (wacom_wac1->features.touch_max ||
1284 wacom_wac1->features.type == INTUOSHT) {
1285 wacom_wac2->features =
1286 *((struct wacom_features *)id->driver_data);
1287 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1288 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1289 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1290 if (wacom_wac2->features.touch_max)
1291 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1292 "%s (WL) Finger",wacom_wac2->features.name);
1294 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1295 "%s (WL) Pad",wacom_wac2->features.name);
1296 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1297 "%s (WL) Pad", wacom_wac2->features.name);
1298 wacom_wac2->pid = wacom_wac->pid;
1299 error = wacom_allocate_inputs(wacom2) ||
1300 wacom_register_inputs(wacom2);
1304 if (wacom_wac1->features.type == INTUOSHT &&
1305 wacom_wac1->features.touch_max)
1306 wacom_wac->shared->touch_input = wacom_wac2->input;
1309 error = wacom_initialize_battery(wacom);
1317 wacom_clean_inputs(wacom1);
1318 wacom_clean_inputs(wacom2);
1323 * Not all devices report physical dimensions from HID.
1324 * Compute the default from hardcoded logical dimension
1325 * and resolution before driver overwrites them.
1327 static void wacom_set_default_phy(struct wacom_features *features)
1329 if (features->x_resolution) {
1330 features->x_phy = (features->x_max * 100) /
1331 features->x_resolution;
1332 features->y_phy = (features->y_max * 100) /
1333 features->y_resolution;
1337 static void wacom_calculate_res(struct wacom_features *features)
1339 features->x_resolution = wacom_calc_hid_res(features->x_max,
1342 features->unitExpo);
1343 features->y_resolution = wacom_calc_hid_res(features->y_max,
1346 features->unitExpo);
1349 static int wacom_hid_report_len(struct hid_report *report)
1351 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1352 return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1355 static size_t wacom_compute_pktlen(struct hid_device *hdev)
1357 struct hid_report_enum *report_enum;
1358 struct hid_report *report;
1361 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1363 list_for_each_entry(report, &report_enum->report_list, list) {
1364 size_t report_size = wacom_hid_report_len(report);
1365 if (report_size > size)
1372 static int wacom_probe(struct hid_device *hdev,
1373 const struct hid_device_id *id)
1375 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1376 struct usb_device *dev = interface_to_usbdev(intf);
1377 struct wacom *wacom;
1378 struct wacom_wac *wacom_wac;
1379 struct wacom_features *features;
1381 unsigned int connect_mask = HID_CONNECT_HIDRAW;
1383 if (!id->driver_data)
1386 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1388 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1392 hid_set_drvdata(hdev, wacom);
1395 /* ask for the report descriptor to be loaded by HID */
1396 error = hid_parse(hdev);
1398 hid_err(hdev, "parse failed\n");
1402 wacom_wac = &wacom->wacom_wac;
1403 wacom_wac->features = *((struct wacom_features *)id->driver_data);
1404 features = &wacom_wac->features;
1405 features->pktlen = wacom_compute_pktlen(hdev);
1406 if (features->pktlen > WACOM_PKGLEN_MAX) {
1411 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1416 wacom->usbdev = dev;
1418 mutex_init(&wacom->lock);
1419 INIT_WORK(&wacom->work, wacom_wireless_work);
1421 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1422 error = wacom_allocate_inputs(wacom);
1424 goto fail_allocate_inputs;
1427 /* set the default size in case we do not get them from hid */
1428 wacom_set_default_phy(features);
1430 /* Retrieve the physical and logical size for touch devices */
1431 wacom_retrieve_hid_descriptor(hdev, features);
1434 * Intuos5 has no useful data about its touch interface in its
1435 * HID descriptor. If this is the touch interface (PacketSize
1436 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1438 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
1439 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
1440 features->device_type = BTN_TOOL_FINGER;
1442 features->x_max = 4096;
1443 features->y_max = 4096;
1445 features->device_type = BTN_TOOL_PEN;
1450 * Same thing for Bamboo 3rd gen.
1452 if ((features->type == BAMBOO_PT) &&
1453 (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1454 (features->device_type == BTN_TOOL_PEN)) {
1455 features->device_type = BTN_TOOL_FINGER;
1457 features->x_max = 4096;
1458 features->y_max = 4096;
1461 if (hdev->bus == BUS_BLUETOOTH)
1462 features->quirks |= WACOM_QUIRK_BATTERY;
1464 wacom_setup_device_quirks(features);
1466 /* set unit to "100th of a mm" for devices not reported by HID */
1467 if (!features->unit) {
1468 features->unit = 0x11;
1469 features->unitExpo = -3;
1471 wacom_calculate_res(features);
1473 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1474 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1475 "%s Pad", features->name);
1477 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
1478 /* Append the device type to the name */
1479 if (features->device_type != BTN_TOOL_FINGER)
1480 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1481 else if (features->touch_max)
1482 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1484 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
1486 error = wacom_add_shared_data(hdev);
1488 goto fail_shared_data;
1491 if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1492 (features->quirks & WACOM_QUIRK_BATTERY)) {
1493 error = wacom_initialize_battery(wacom);
1498 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1499 error = wacom_register_inputs(wacom);
1501 goto fail_register_inputs;
1504 if (hdev->bus == BUS_BLUETOOTH) {
1505 error = device_create_file(&hdev->dev, &dev_attr_speed);
1508 "can't create sysfs speed attribute err: %d\n",
1512 if (features->type == HID_GENERIC)
1513 connect_mask |= HID_CONNECT_DRIVER;
1515 /* Regular HID work starts now */
1516 error = hid_hw_start(hdev, connect_mask);
1518 hid_err(hdev, "hw start failed\n");
1522 /* Note that if query fails it is not a hard failure */
1523 wacom_query_tablet_data(hdev, features);
1525 if (features->quirks & WACOM_QUIRK_MONITOR)
1526 error = hid_hw_open(hdev);
1528 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1529 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1530 wacom_wac->shared->touch_input = wacom_wac->input;
1536 if (hdev->bus == BUS_BLUETOOTH)
1537 device_remove_file(&hdev->dev, &dev_attr_speed);
1538 fail_register_inputs:
1539 wacom_clean_inputs(wacom);
1540 wacom_destroy_battery(wacom);
1542 wacom_remove_shared_data(wacom_wac);
1544 wacom_clean_inputs(wacom);
1545 fail_allocate_inputs:
1550 hid_set_drvdata(hdev, NULL);
1554 static void wacom_remove(struct hid_device *hdev)
1556 struct wacom *wacom = hid_get_drvdata(hdev);
1560 cancel_work_sync(&wacom->work);
1561 wacom_clean_inputs(wacom);
1562 if (hdev->bus == BUS_BLUETOOTH)
1563 device_remove_file(&hdev->dev, &dev_attr_speed);
1564 wacom_destroy_battery(wacom);
1565 wacom_remove_shared_data(&wacom->wacom_wac);
1567 hid_set_drvdata(hdev, NULL);
1572 static int wacom_resume(struct hid_device *hdev)
1574 struct wacom *wacom = hid_get_drvdata(hdev);
1575 struct wacom_features *features = &wacom->wacom_wac.features;
1577 mutex_lock(&wacom->lock);
1579 /* switch to wacom mode first */
1580 wacom_query_tablet_data(hdev, features);
1581 wacom_led_control(wacom);
1583 mutex_unlock(&wacom->lock);
1588 static int wacom_reset_resume(struct hid_device *hdev)
1590 return wacom_resume(hdev);
1592 #endif /* CONFIG_PM */
1594 static struct hid_driver wacom_driver = {
1596 .id_table = wacom_ids,
1597 .probe = wacom_probe,
1598 .remove = wacom_remove,
1599 .event = wacom_wac_event,
1600 .report = wacom_wac_report,
1602 .resume = wacom_resume,
1603 .reset_resume = wacom_reset_resume,
1605 .raw_event = wacom_raw_event,
1607 module_hid_driver(wacom_driver);
1609 MODULE_VERSION(DRIVER_VERSION);
1610 MODULE_AUTHOR(DRIVER_AUTHOR);
1611 MODULE_DESCRIPTION(DRIVER_DESC);
1612 MODULE_LICENSE(DRIVER_LICENSE);