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"
17 #define WAC_MSG_RETRIES 5
19 #define WAC_CMD_WL_LED_CONTROL 0x03
20 #define WAC_CMD_LED_CONTROL 0x20
21 #define WAC_CMD_ICON_START 0x21
22 #define WAC_CMD_ICON_XFER 0x23
23 #define WAC_CMD_ICON_BT_XFER 0x26
24 #define WAC_CMD_RETRIES 10
26 #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
27 #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
29 static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
30 size_t size, unsigned int retries)
35 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
37 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
42 static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
43 size_t size, unsigned int retries)
48 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
50 } while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
55 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
56 u8 *raw_data, int size)
58 struct wacom *wacom = hid_get_drvdata(hdev);
60 if (size > WACOM_PKGLEN_MAX)
63 memcpy(wacom->wacom_wac.data, raw_data, size);
65 wacom_wac_irq(&wacom->wacom_wac, size);
70 static int wacom_open(struct input_dev *dev)
72 struct wacom *wacom = input_get_drvdata(dev);
75 mutex_lock(&wacom->lock);
76 retval = hid_hw_open(wacom->hdev);
77 mutex_unlock(&wacom->lock);
82 static void wacom_close(struct input_dev *dev)
84 struct wacom *wacom = input_get_drvdata(dev);
86 mutex_lock(&wacom->lock);
87 hid_hw_close(wacom->hdev);
88 mutex_unlock(&wacom->lock);
92 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
94 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
95 unsigned unit, int exponent)
97 struct hid_field field = {
98 .logical_maximum = logical_extents,
99 .physical_maximum = physical_extents,
101 .unit_exponent = exponent,
104 return hidinput_calc_abs_res(&field, ABS_X);
107 static void wacom_feature_mapping(struct hid_device *hdev,
108 struct hid_field *field, struct hid_usage *usage)
110 struct wacom *wacom = hid_get_drvdata(hdev);
111 struct wacom_features *features = &wacom->wacom_wac.features;
112 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
116 switch (usage->hid) {
117 case HID_DG_CONTACTMAX:
118 /* leave touch_max as is if predefined */
119 if (!features->touch_max) {
121 data = kzalloc(2, GFP_KERNEL);
124 data[0] = field->report->id;
125 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
128 features->touch_max = data[1];
132 case HID_DG_INPUTMODE:
133 /* Ignore if value index is out of bounds. */
134 if (usage->usage_index >= field->report_count) {
135 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
139 hid_data->inputmode = field->report->id;
140 hid_data->inputmode_index = usage->usage_index;
146 * Interface Descriptor of wacom devices can be incomplete and
147 * inconsistent so wacom_features table is used to store stylus
148 * device's packet lengths, various maximum values, and tablet
149 * resolution based on product ID's.
151 * For devices that contain 2 interfaces, wacom_features table is
152 * inaccurate for the touch interface. Since the Interface Descriptor
153 * for touch interfaces has pretty complete data, this function exists
154 * to query tablet for this missing information instead of hard coding in
155 * an additional table.
157 * A typical Interface Descriptor for a stylus will contain a
158 * boot mouse application collection that is not of interest and this
159 * function will ignore it.
161 * It also contains a digitizer application collection that also is not
162 * of interest since any information it contains would be duplicate
163 * of what is in wacom_features. Usually it defines a report of an array
164 * of bytes that could be used as max length of the stylus packet returned.
165 * If it happens to define a Digitizer-Stylus Physical Collection then
166 * the X and Y logical values contain valid data but it is ignored.
168 * A typical Interface Descriptor for a touch interface will contain a
169 * Digitizer-Finger Physical Collection which will define both logical
170 * X/Y maximum as well as the physical size of tablet. Since touch
171 * interfaces haven't supported pressure or distance, this is enough
172 * information to override invalid values in the wacom_features table.
174 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
175 * data. We deal with them after returning from this function.
177 static void wacom_usage_mapping(struct hid_device *hdev,
178 struct hid_field *field, struct hid_usage *usage)
180 struct wacom *wacom = hid_get_drvdata(hdev);
181 struct wacom_features *features = &wacom->wacom_wac.features;
182 bool finger = (field->logical == HID_DG_FINGER) ||
183 (field->physical == HID_DG_FINGER);
184 bool pen = (field->logical == HID_DG_STYLUS) ||
185 (field->physical == HID_DG_STYLUS);
188 * Requiring Stylus Usage will ignore boot mouse
189 * X/Y values and some cases of invalid Digitizer X/Y
190 * values commonly reported.
196 * Bamboo models do not support HID_DG_CONTACTMAX.
197 * And, Bamboo Pen only descriptor contains touch.
199 if (features->type != BAMBOO_PT) {
200 /* ISDv4 touch devices at least supports one touch point */
201 if (finger && !features->touch_max)
202 features->touch_max = 1;
205 switch (usage->hid) {
207 features->x_max = field->logical_maximum;
209 features->device_type = BTN_TOOL_FINGER;
210 features->x_phy = field->physical_maximum;
211 if (features->type != BAMBOO_PT) {
212 features->unit = field->unit;
213 features->unitExpo = field->unit_exponent;
216 features->device_type = BTN_TOOL_PEN;
220 features->y_max = field->logical_maximum;
222 features->y_phy = field->physical_maximum;
223 if (features->type != BAMBOO_PT) {
224 features->unit = field->unit;
225 features->unitExpo = field->unit_exponent;
229 case HID_DG_TIPPRESSURE:
231 features->pressure_max = field->logical_maximum;
235 if (features->type == HID_GENERIC)
236 wacom_wac_usage_mapping(hdev, field, usage);
239 static void wacom_parse_hid(struct hid_device *hdev,
240 struct wacom_features *features)
242 struct hid_report_enum *rep_enum;
243 struct hid_report *hreport;
246 /* check features first */
247 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
248 list_for_each_entry(hreport, &rep_enum->report_list, list) {
249 for (i = 0; i < hreport->maxfield; i++) {
250 /* Ignore if report count is out of bounds. */
251 if (hreport->field[i]->report_count < 1)
254 for (j = 0; j < hreport->field[i]->maxusage; j++) {
255 wacom_feature_mapping(hdev, hreport->field[i],
256 hreport->field[i]->usage + j);
261 /* now check the input usages */
262 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
263 list_for_each_entry(hreport, &rep_enum->report_list, list) {
265 if (!hreport->maxfield)
268 for (i = 0; i < hreport->maxfield; i++)
269 for (j = 0; j < hreport->field[i]->maxusage; j++)
270 wacom_usage_mapping(hdev, hreport->field[i],
271 hreport->field[i]->usage + j);
275 static int wacom_hid_set_device_mode(struct hid_device *hdev)
277 struct wacom *wacom = hid_get_drvdata(hdev);
278 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
279 struct hid_report *r;
280 struct hid_report_enum *re;
282 if (hid_data->inputmode < 0)
285 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
286 r = re->report_id_hash[hid_data->inputmode];
288 r->field[0]->value[hid_data->inputmode_index] = 2;
289 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
294 static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
295 int length, int mode)
297 unsigned char *rep_data;
298 int error = -ENOMEM, limit = 0;
300 rep_data = kzalloc(length, GFP_KERNEL);
305 rep_data[0] = report_id;
308 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
311 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
312 rep_data, length, 1);
313 } while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
317 return error < 0 ? error : 0;
320 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
321 struct wacom_features *features)
323 struct wacom *wacom = hid_get_drvdata(hdev);
327 switch (features->type) {
331 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
335 rep_data[0] = speed == 0 ? 0x05 : 0x06;
338 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
342 wacom->wacom_wac.bt_high_speed = speed;
348 * Note that if the raw queries fail, it's not a hard failure
349 * and it is safe to continue
351 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
356 wacom->wacom_wac.bt_features &= ~0x20;
358 wacom->wacom_wac.bt_features |= 0x20;
361 rep_data[1] = wacom->wacom_wac.bt_features;
363 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
366 wacom->wacom_wac.bt_high_speed = speed;
374 * Switch the tablet into its most-capable mode. Wacom tablets are
375 * typically configured to power-up in a mode which sends mouse-like
376 * reports to the OS. To get absolute position, pressure data, etc.
377 * from the tablet, it is necessary to switch the tablet out of this
378 * mode and into one which sends the full range of tablet data.
380 static int wacom_query_tablet_data(struct hid_device *hdev,
381 struct wacom_features *features)
383 if (hdev->bus == BUS_BLUETOOTH)
384 return wacom_bt_query_tablet_data(hdev, 1, features);
386 if (features->type == HID_GENERIC)
387 return wacom_hid_set_device_mode(hdev);
389 if (features->device_type == BTN_TOOL_FINGER) {
390 if (features->type > TABLETPC) {
391 /* MT Tablet PC touch */
392 return wacom_set_device_mode(hdev, 3, 4, 4);
394 else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
395 return wacom_set_device_mode(hdev, 18, 3, 2);
397 } else if (features->device_type == BTN_TOOL_PEN) {
398 if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
399 return wacom_set_device_mode(hdev, 2, 2, 2);
406 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
407 struct wacom_features *features)
409 struct wacom *wacom = hid_get_drvdata(hdev);
410 struct usb_interface *intf = wacom->intf;
412 /* default features */
413 features->device_type = BTN_TOOL_PEN;
414 features->x_fuzz = 4;
415 features->y_fuzz = 4;
416 features->pressure_fuzz = 0;
417 features->distance_fuzz = 0;
420 * The wireless device HID is basic and layout conflicts with
421 * other tablets (monitor and touch interface can look like pen).
422 * Skip the query for this type and modify defaults based on
425 if (features->type == WIRELESS) {
426 if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
427 features->device_type = 0;
428 } else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
429 features->device_type = BTN_TOOL_FINGER;
430 features->pktlen = WACOM_PKGLEN_BBTOUCH3;
434 /* only devices that support touch need to retrieve the info */
435 if (features->type < BAMBOO_PT)
438 wacom_parse_hid(hdev, features);
441 struct wacom_hdev_data {
442 struct list_head list;
444 struct hid_device *dev;
445 struct wacom_shared shared;
448 static LIST_HEAD(wacom_udev_list);
449 static DEFINE_MUTEX(wacom_udev_list_lock);
451 static bool wacom_are_sibling(struct hid_device *hdev,
452 struct hid_device *sibling)
454 struct wacom *wacom = hid_get_drvdata(hdev);
455 struct wacom_features *features = &wacom->wacom_wac.features;
456 int vid = features->oVid;
457 int pid = features->oPid;
460 if (vid == 0 && pid == 0) {
465 if (vid != sibling->vendor || pid != sibling->product)
468 /* Compare the physical path. */
469 n1 = strrchr(hdev->phys, '.') - hdev->phys;
470 n2 = strrchr(sibling->phys, '.') - sibling->phys;
471 if (n1 != n2 || n1 <= 0 || n2 <= 0)
474 return !strncmp(hdev->phys, sibling->phys, n1);
477 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
479 struct wacom_hdev_data *data;
481 list_for_each_entry(data, &wacom_udev_list, list) {
482 if (wacom_are_sibling(hdev, data->dev)) {
483 kref_get(&data->kref);
491 static int wacom_add_shared_data(struct hid_device *hdev)
493 struct wacom *wacom = hid_get_drvdata(hdev);
494 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
495 struct wacom_hdev_data *data;
498 mutex_lock(&wacom_udev_list_lock);
500 data = wacom_get_hdev_data(hdev);
502 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
508 kref_init(&data->kref);
510 list_add_tail(&data->list, &wacom_udev_list);
513 wacom_wac->shared = &data->shared;
516 mutex_unlock(&wacom_udev_list_lock);
520 static void wacom_release_shared_data(struct kref *kref)
522 struct wacom_hdev_data *data =
523 container_of(kref, struct wacom_hdev_data, kref);
525 mutex_lock(&wacom_udev_list_lock);
526 list_del(&data->list);
527 mutex_unlock(&wacom_udev_list_lock);
532 static void wacom_remove_shared_data(struct wacom_wac *wacom)
534 struct wacom_hdev_data *data;
537 data = container_of(wacom->shared, struct wacom_hdev_data, shared);
538 kref_put(&data->kref, wacom_release_shared_data);
539 wacom->shared = NULL;
543 static int wacom_led_control(struct wacom *wacom)
547 unsigned char report_id = WAC_CMD_LED_CONTROL;
550 if (wacom->wacom_wac.pid) { /* wireless connected */
551 report_id = WAC_CMD_WL_LED_CONTROL;
554 buf = kzalloc(buf_size, GFP_KERNEL);
558 if (wacom->wacom_wac.features.type >= INTUOS5S &&
559 wacom->wacom_wac.features.type <= INTUOSPL) {
561 * Touch Ring and crop mark LED luminance may take on
562 * one of four values:
563 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
565 int ring_led = wacom->led.select[0] & 0x03;
566 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
568 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
571 if (wacom->wacom_wac.pid) {
572 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
573 buf, buf_size, WAC_CMD_RETRIES);
580 int led = wacom->led.select[0] | 0x4;
582 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
583 wacom->wacom_wac.features.type == WACOM_24HD)
584 led |= (wacom->led.select[1] << 4) | 0x40;
588 buf[2] = wacom->led.llv;
589 buf[3] = wacom->led.hlv;
590 buf[4] = wacom->led.img_lum;
593 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
600 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
601 const unsigned len, const void *img)
605 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
607 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
611 /* Send 'start' command */
612 buf[0] = WAC_CMD_ICON_START;
614 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
620 buf[1] = button_id & 0x07;
621 for (i = 0; i < 4; i++) {
623 memcpy(buf + 3, img + i * chunk_len, chunk_len);
625 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
626 buf, chunk_len + 3, WAC_CMD_RETRIES);
632 buf[0] = WAC_CMD_ICON_START;
634 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
642 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
643 const char *buf, size_t count)
645 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
646 struct wacom *wacom = hid_get_drvdata(hdev);
650 err = kstrtouint(buf, 10, &id);
654 mutex_lock(&wacom->lock);
656 wacom->led.select[set_id] = id & 0x3;
657 err = wacom_led_control(wacom);
659 mutex_unlock(&wacom->lock);
661 return err < 0 ? err : count;
664 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
665 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
666 struct device_attribute *attr, const char *buf, size_t count) \
668 return wacom_led_select_store(dev, SET_ID, buf, count); \
670 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
671 struct device_attribute *attr, char *buf) \
673 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
674 struct wacom *wacom = hid_get_drvdata(hdev); \
675 return scnprintf(buf, PAGE_SIZE, "%d\n", \
676 wacom->led.select[SET_ID]); \
678 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
679 wacom_led##SET_ID##_select_show, \
680 wacom_led##SET_ID##_select_store)
682 DEVICE_LED_SELECT_ATTR(0);
683 DEVICE_LED_SELECT_ATTR(1);
685 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
686 const char *buf, size_t count)
691 err = kstrtouint(buf, 10, &value);
695 mutex_lock(&wacom->lock);
697 *dest = value & 0x7f;
698 err = wacom_led_control(wacom);
700 mutex_unlock(&wacom->lock);
702 return err < 0 ? err : count;
705 #define DEVICE_LUMINANCE_ATTR(name, field) \
706 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
707 struct device_attribute *attr, const char *buf, size_t count) \
709 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
710 struct wacom *wacom = hid_get_drvdata(hdev); \
712 return wacom_luminance_store(wacom, &wacom->led.field, \
715 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
716 struct device_attribute *attr, char *buf) \
718 struct wacom *wacom = dev_get_drvdata(dev); \
719 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
721 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
722 wacom_##name##_luminance_show, \
723 wacom_##name##_luminance_store)
725 DEVICE_LUMINANCE_ATTR(status0, llv);
726 DEVICE_LUMINANCE_ATTR(status1, hlv);
727 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
729 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
730 const char *buf, size_t count)
732 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
733 struct wacom *wacom = hid_get_drvdata(hdev);
738 if (hdev->bus == BUS_BLUETOOTH) {
740 xfer_id = WAC_CMD_ICON_BT_XFER;
743 xfer_id = WAC_CMD_ICON_XFER;
749 mutex_lock(&wacom->lock);
751 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
753 mutex_unlock(&wacom->lock);
755 return err < 0 ? err : count;
758 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
759 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
760 struct device_attribute *attr, const char *buf, size_t count) \
762 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
764 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
765 NULL, wacom_btnimg##BUTTON_ID##_store)
767 DEVICE_BTNIMG_ATTR(0);
768 DEVICE_BTNIMG_ATTR(1);
769 DEVICE_BTNIMG_ATTR(2);
770 DEVICE_BTNIMG_ATTR(3);
771 DEVICE_BTNIMG_ATTR(4);
772 DEVICE_BTNIMG_ATTR(5);
773 DEVICE_BTNIMG_ATTR(6);
774 DEVICE_BTNIMG_ATTR(7);
776 static struct attribute *cintiq_led_attrs[] = {
777 &dev_attr_status_led0_select.attr,
778 &dev_attr_status_led1_select.attr,
782 static struct attribute_group cintiq_led_attr_group = {
784 .attrs = cintiq_led_attrs,
787 static struct attribute *intuos4_led_attrs[] = {
788 &dev_attr_status0_luminance.attr,
789 &dev_attr_status1_luminance.attr,
790 &dev_attr_status_led0_select.attr,
791 &dev_attr_buttons_luminance.attr,
792 &dev_attr_button0_rawimg.attr,
793 &dev_attr_button1_rawimg.attr,
794 &dev_attr_button2_rawimg.attr,
795 &dev_attr_button3_rawimg.attr,
796 &dev_attr_button4_rawimg.attr,
797 &dev_attr_button5_rawimg.attr,
798 &dev_attr_button6_rawimg.attr,
799 &dev_attr_button7_rawimg.attr,
803 static struct attribute_group intuos4_led_attr_group = {
805 .attrs = intuos4_led_attrs,
808 static struct attribute *intuos5_led_attrs[] = {
809 &dev_attr_status0_luminance.attr,
810 &dev_attr_status_led0_select.attr,
814 static struct attribute_group intuos5_led_attr_group = {
816 .attrs = intuos5_led_attrs,
819 static int wacom_initialize_leds(struct wacom *wacom)
823 /* Initialize default values */
824 switch (wacom->wacom_wac.features.type) {
829 wacom->led.select[0] = 0;
830 wacom->led.select[1] = 0;
833 wacom->led.img_lum = 10;
834 error = sysfs_create_group(&wacom->hdev->dev.kobj,
835 &intuos4_led_attr_group);
840 wacom->led.select[0] = 0;
841 wacom->led.select[1] = 0;
844 wacom->led.img_lum = 0;
846 error = sysfs_create_group(&wacom->hdev->dev.kobj,
847 &cintiq_led_attr_group);
856 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
857 wacom->led.select[0] = 0;
858 wacom->led.select[1] = 0;
861 wacom->led.img_lum = 0;
863 error = sysfs_create_group(&wacom->hdev->dev.kobj,
864 &intuos5_led_attr_group);
875 "cannot create sysfs group err: %d\n", error);
878 wacom_led_control(wacom);
879 wacom->led_initialized = true;
884 static void wacom_destroy_leds(struct wacom *wacom)
886 if (!wacom->led_initialized)
889 wacom->led_initialized = false;
891 switch (wacom->wacom_wac.features.type) {
896 sysfs_remove_group(&wacom->hdev->dev.kobj,
897 &intuos4_led_attr_group);
902 sysfs_remove_group(&wacom->hdev->dev.kobj,
903 &cintiq_led_attr_group);
912 if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
913 sysfs_remove_group(&wacom->hdev->dev.kobj,
914 &intuos5_led_attr_group);
919 static enum power_supply_property wacom_battery_props[] = {
920 POWER_SUPPLY_PROP_STATUS,
921 POWER_SUPPLY_PROP_SCOPE,
922 POWER_SUPPLY_PROP_CAPACITY
925 static enum power_supply_property wacom_ac_props[] = {
926 POWER_SUPPLY_PROP_PRESENT,
927 POWER_SUPPLY_PROP_ONLINE,
928 POWER_SUPPLY_PROP_SCOPE,
931 static int wacom_battery_get_property(struct power_supply *psy,
932 enum power_supply_property psp,
933 union power_supply_propval *val)
935 struct wacom *wacom = container_of(psy, struct wacom, battery);
939 case POWER_SUPPLY_PROP_SCOPE:
940 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
942 case POWER_SUPPLY_PROP_CAPACITY:
944 wacom->wacom_wac.battery_capacity;
946 case POWER_SUPPLY_PROP_STATUS:
947 if (wacom->wacom_wac.bat_charging)
948 val->intval = POWER_SUPPLY_STATUS_CHARGING;
949 else if (wacom->wacom_wac.battery_capacity == 100 &&
950 wacom->wacom_wac.ps_connected)
951 val->intval = POWER_SUPPLY_STATUS_FULL;
953 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
963 static int wacom_ac_get_property(struct power_supply *psy,
964 enum power_supply_property psp,
965 union power_supply_propval *val)
967 struct wacom *wacom = container_of(psy, struct wacom, ac);
971 case POWER_SUPPLY_PROP_PRESENT:
973 case POWER_SUPPLY_PROP_ONLINE:
974 val->intval = wacom->wacom_wac.ps_connected;
976 case POWER_SUPPLY_PROP_SCOPE:
977 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
986 static int wacom_initialize_battery(struct wacom *wacom)
988 static atomic_t battery_no = ATOMIC_INIT(0);
992 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
993 n = atomic_inc_return(&battery_no) - 1;
995 wacom->battery.properties = wacom_battery_props;
996 wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
997 wacom->battery.get_property = wacom_battery_get_property;
998 sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
999 wacom->battery.name = wacom->wacom_wac.bat_name;
1000 wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1001 wacom->battery.use_for_apm = 0;
1003 wacom->ac.properties = wacom_ac_props;
1004 wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
1005 wacom->ac.get_property = wacom_ac_get_property;
1006 sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
1007 wacom->ac.name = wacom->wacom_wac.ac_name;
1008 wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
1009 wacom->ac.use_for_apm = 0;
1011 error = power_supply_register(&wacom->hdev->dev,
1016 power_supply_powers(&wacom->battery, &wacom->hdev->dev);
1018 error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
1020 power_supply_unregister(&wacom->battery);
1024 power_supply_powers(&wacom->ac, &wacom->hdev->dev);
1030 static void wacom_destroy_battery(struct wacom *wacom)
1032 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1033 wacom->battery.dev) {
1034 power_supply_unregister(&wacom->battery);
1035 wacom->battery.dev = NULL;
1036 power_supply_unregister(&wacom->ac);
1037 wacom->ac.dev = NULL;
1041 static ssize_t wacom_show_speed(struct device *dev,
1042 struct device_attribute
1045 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1046 struct wacom *wacom = hid_get_drvdata(hdev);
1048 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1051 static ssize_t wacom_store_speed(struct device *dev,
1052 struct device_attribute *attr,
1053 const char *buf, size_t count)
1055 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1056 struct wacom *wacom = hid_get_drvdata(hdev);
1059 if (kstrtou8(buf, 0, &new_speed))
1062 if (new_speed != 0 && new_speed != 1)
1065 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1070 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1071 wacom_show_speed, wacom_store_speed);
1073 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1075 struct input_dev *input_dev;
1076 struct hid_device *hdev = wacom->hdev;
1077 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1079 input_dev = input_allocate_device();
1083 input_dev->name = wacom_wac->name;
1084 input_dev->phys = hdev->phys;
1085 input_dev->dev.parent = &hdev->dev;
1086 input_dev->open = wacom_open;
1087 input_dev->close = wacom_close;
1088 input_dev->uniq = hdev->uniq;
1089 input_dev->id.bustype = hdev->bus;
1090 input_dev->id.vendor = hdev->vendor;
1091 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1092 input_dev->id.version = hdev->version;
1093 input_set_drvdata(input_dev, wacom);
1098 static void wacom_free_inputs(struct wacom *wacom)
1100 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1102 if (wacom_wac->input)
1103 input_free_device(wacom_wac->input);
1104 if (wacom_wac->pad_input)
1105 input_free_device(wacom_wac->pad_input);
1106 wacom_wac->input = NULL;
1107 wacom_wac->pad_input = NULL;
1110 static int wacom_allocate_inputs(struct wacom *wacom)
1112 struct input_dev *input_dev, *pad_input_dev;
1113 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1115 input_dev = wacom_allocate_input(wacom);
1116 pad_input_dev = wacom_allocate_input(wacom);
1117 if (!input_dev || !pad_input_dev) {
1118 wacom_free_inputs(wacom);
1122 wacom_wac->input = input_dev;
1123 wacom_wac->pad_input = pad_input_dev;
1124 wacom_wac->pad_input->name = wacom_wac->pad_name;
1129 static void wacom_clean_inputs(struct wacom *wacom)
1131 if (wacom->wacom_wac.input) {
1132 if (wacom->wacom_wac.input_registered)
1133 input_unregister_device(wacom->wacom_wac.input);
1135 input_free_device(wacom->wacom_wac.input);
1137 if (wacom->wacom_wac.pad_input) {
1138 if (wacom->wacom_wac.pad_registered)
1139 input_unregister_device(wacom->wacom_wac.pad_input);
1141 input_free_device(wacom->wacom_wac.pad_input);
1143 wacom->wacom_wac.input = NULL;
1144 wacom->wacom_wac.pad_input = NULL;
1145 wacom_destroy_leds(wacom);
1148 static int wacom_register_inputs(struct wacom *wacom)
1150 struct input_dev *input_dev, *pad_input_dev;
1151 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1154 input_dev = wacom_wac->input;
1155 pad_input_dev = wacom_wac->pad_input;
1157 if (!input_dev || !pad_input_dev)
1160 error = wacom_setup_pentouch_input_capabilities(input_dev, wacom_wac);
1162 error = input_register_device(input_dev);
1165 wacom_wac->input_registered = true;
1168 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1170 /* no pad in use on this interface */
1171 input_free_device(pad_input_dev);
1172 wacom_wac->pad_input = NULL;
1173 pad_input_dev = NULL;
1175 error = input_register_device(pad_input_dev);
1177 goto fail_register_pad_input;
1178 wacom_wac->pad_registered = true;
1180 error = wacom_initialize_leds(wacom);
1188 input_unregister_device(pad_input_dev);
1189 pad_input_dev = NULL;
1190 wacom_wac->pad_registered = false;
1191 fail_register_pad_input:
1192 input_unregister_device(input_dev);
1193 wacom_wac->input = NULL;
1194 wacom_wac->input_registered = false;
1198 static void wacom_wireless_work(struct work_struct *work)
1200 struct wacom *wacom = container_of(work, struct wacom, work);
1201 struct usb_device *usbdev = wacom->usbdev;
1202 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1203 struct hid_device *hdev1, *hdev2;
1204 struct wacom *wacom1, *wacom2;
1205 struct wacom_wac *wacom_wac1, *wacom_wac2;
1209 * Regardless if this is a disconnect or a new tablet,
1210 * remove any existing input and battery devices.
1213 wacom_destroy_battery(wacom);
1215 /* Stylus interface */
1216 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1217 wacom1 = hid_get_drvdata(hdev1);
1218 wacom_wac1 = &(wacom1->wacom_wac);
1219 wacom_clean_inputs(wacom1);
1221 /* Touch interface */
1222 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1223 wacom2 = hid_get_drvdata(hdev2);
1224 wacom_wac2 = &(wacom2->wacom_wac);
1225 wacom_clean_inputs(wacom2);
1227 if (wacom_wac->pid == 0) {
1228 hid_info(wacom->hdev, "wireless tablet disconnected\n");
1229 wacom_wac1->shared->type = 0;
1231 const struct hid_device_id *id = wacom_ids;
1233 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1237 if (id->vendor == USB_VENDOR_ID_WACOM &&
1238 id->product == wacom_wac->pid)
1244 hid_info(wacom->hdev, "ignoring unknown PID.\n");
1248 /* Stylus interface */
1249 wacom_wac1->features =
1250 *((struct wacom_features *)id->driver_data);
1251 wacom_wac1->features.device_type = BTN_TOOL_PEN;
1252 snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1253 wacom_wac1->features.name);
1254 snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1255 wacom_wac1->features.name);
1256 wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1257 wacom_wac1->shared->type = wacom_wac1->features.type;
1258 wacom_wac1->pid = wacom_wac->pid;
1259 error = wacom_allocate_inputs(wacom1) ||
1260 wacom_register_inputs(wacom1);
1264 /* Touch interface */
1265 if (wacom_wac1->features.touch_max ||
1266 wacom_wac1->features.type == INTUOSHT) {
1267 wacom_wac2->features =
1268 *((struct wacom_features *)id->driver_data);
1269 wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1270 wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1271 wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1272 if (wacom_wac2->features.touch_max)
1273 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1274 "%s (WL) Finger",wacom_wac2->features.name);
1276 snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1277 "%s (WL) Pad",wacom_wac2->features.name);
1278 snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1279 "%s (WL) Pad", wacom_wac2->features.name);
1280 wacom_wac2->pid = wacom_wac->pid;
1281 error = wacom_allocate_inputs(wacom2) ||
1282 wacom_register_inputs(wacom2);
1286 if (wacom_wac1->features.type == INTUOSHT &&
1287 wacom_wac1->features.touch_max)
1288 wacom_wac->shared->touch_input = wacom_wac2->input;
1291 error = wacom_initialize_battery(wacom);
1299 wacom_clean_inputs(wacom1);
1300 wacom_clean_inputs(wacom2);
1305 * Not all devices report physical dimensions from HID.
1306 * Compute the default from hardcoded logical dimension
1307 * and resolution before driver overwrites them.
1309 static void wacom_set_default_phy(struct wacom_features *features)
1311 if (features->x_resolution) {
1312 features->x_phy = (features->x_max * 100) /
1313 features->x_resolution;
1314 features->y_phy = (features->y_max * 100) /
1315 features->y_resolution;
1319 static void wacom_calculate_res(struct wacom_features *features)
1321 features->x_resolution = wacom_calc_hid_res(features->x_max,
1324 features->unitExpo);
1325 features->y_resolution = wacom_calc_hid_res(features->y_max,
1328 features->unitExpo);
1331 static int wacom_hid_report_len(struct hid_report *report)
1333 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1334 return ((report->size - 1) >> 3) + 1 + (report->id > 0);
1337 static size_t wacom_compute_pktlen(struct hid_device *hdev)
1339 struct hid_report_enum *report_enum;
1340 struct hid_report *report;
1343 report_enum = hdev->report_enum + HID_INPUT_REPORT;
1345 list_for_each_entry(report, &report_enum->report_list, list) {
1346 size_t report_size = wacom_hid_report_len(report);
1347 if (report_size > size)
1354 static int wacom_probe(struct hid_device *hdev,
1355 const struct hid_device_id *id)
1357 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1358 struct usb_device *dev = interface_to_usbdev(intf);
1359 struct wacom *wacom;
1360 struct wacom_wac *wacom_wac;
1361 struct wacom_features *features;
1363 unsigned int connect_mask = HID_CONNECT_HIDRAW;
1365 if (!id->driver_data)
1368 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1370 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1374 hid_set_drvdata(hdev, wacom);
1377 /* ask for the report descriptor to be loaded by HID */
1378 error = hid_parse(hdev);
1380 hid_err(hdev, "parse failed\n");
1384 wacom_wac = &wacom->wacom_wac;
1385 wacom_wac->features = *((struct wacom_features *)id->driver_data);
1386 features = &wacom_wac->features;
1387 features->pktlen = wacom_compute_pktlen(hdev);
1388 if (features->pktlen > WACOM_PKGLEN_MAX) {
1393 if (features->check_for_hid_type && features->hid_type != hdev->type) {
1398 wacom->usbdev = dev;
1400 mutex_init(&wacom->lock);
1401 INIT_WORK(&wacom->work, wacom_wireless_work);
1403 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1404 error = wacom_allocate_inputs(wacom);
1406 goto fail_allocate_inputs;
1409 /* set the default size in case we do not get them from hid */
1410 wacom_set_default_phy(features);
1412 /* Retrieve the physical and logical size for touch devices */
1413 wacom_retrieve_hid_descriptor(hdev, features);
1416 * Intuos5 has no useful data about its touch interface in its
1417 * HID descriptor. If this is the touch interface (PacketSize
1418 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1420 if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
1421 if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
1422 features->device_type = BTN_TOOL_FINGER;
1424 features->x_max = 4096;
1425 features->y_max = 4096;
1427 features->device_type = BTN_TOOL_PEN;
1432 * Same thing for Bamboo 3rd gen.
1434 if ((features->type == BAMBOO_PT) &&
1435 (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1436 (features->device_type == BTN_TOOL_PEN)) {
1437 features->device_type = BTN_TOOL_FINGER;
1439 features->x_max = 4096;
1440 features->y_max = 4096;
1443 if (hdev->bus == BUS_BLUETOOTH)
1444 features->quirks |= WACOM_QUIRK_BATTERY;
1446 wacom_setup_device_quirks(features);
1448 /* set unit to "100th of a mm" for devices not reported by HID */
1449 if (!features->unit) {
1450 features->unit = 0x11;
1451 features->unitExpo = -3;
1453 wacom_calculate_res(features);
1455 strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1456 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1457 "%s Pad", features->name);
1459 if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
1460 /* Append the device type to the name */
1461 if (features->device_type != BTN_TOOL_FINGER)
1462 strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1463 else if (features->touch_max)
1464 strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1466 strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
1468 error = wacom_add_shared_data(hdev);
1470 goto fail_shared_data;
1473 if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1474 (features->quirks & WACOM_QUIRK_BATTERY)) {
1475 error = wacom_initialize_battery(wacom);
1480 if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1481 error = wacom_register_inputs(wacom);
1483 goto fail_register_inputs;
1486 if (hdev->bus == BUS_BLUETOOTH) {
1487 error = device_create_file(&hdev->dev, &dev_attr_speed);
1490 "can't create sysfs speed attribute err: %d\n",
1494 if (features->type == HID_GENERIC)
1495 connect_mask |= HID_CONNECT_DRIVER;
1497 /* Regular HID work starts now */
1498 error = hid_hw_start(hdev, connect_mask);
1500 hid_err(hdev, "hw start failed\n");
1504 /* Note that if query fails it is not a hard failure */
1505 wacom_query_tablet_data(hdev, features);
1507 if (features->quirks & WACOM_QUIRK_MONITOR)
1508 error = hid_hw_open(hdev);
1510 if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1511 if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1512 wacom_wac->shared->touch_input = wacom_wac->input;
1518 if (hdev->bus == BUS_BLUETOOTH)
1519 device_remove_file(&hdev->dev, &dev_attr_speed);
1520 fail_register_inputs:
1521 wacom_clean_inputs(wacom);
1522 wacom_destroy_battery(wacom);
1524 wacom_remove_shared_data(wacom_wac);
1526 wacom_clean_inputs(wacom);
1527 fail_allocate_inputs:
1532 hid_set_drvdata(hdev, NULL);
1536 static void wacom_remove(struct hid_device *hdev)
1538 struct wacom *wacom = hid_get_drvdata(hdev);
1542 cancel_work_sync(&wacom->work);
1543 wacom_clean_inputs(wacom);
1544 if (hdev->bus == BUS_BLUETOOTH)
1545 device_remove_file(&hdev->dev, &dev_attr_speed);
1546 wacom_destroy_battery(wacom);
1547 wacom_remove_shared_data(&wacom->wacom_wac);
1549 hid_set_drvdata(hdev, NULL);
1554 static int wacom_resume(struct hid_device *hdev)
1556 struct wacom *wacom = hid_get_drvdata(hdev);
1557 struct wacom_features *features = &wacom->wacom_wac.features;
1559 mutex_lock(&wacom->lock);
1561 /* switch to wacom mode first */
1562 wacom_query_tablet_data(hdev, features);
1563 wacom_led_control(wacom);
1565 mutex_unlock(&wacom->lock);
1570 static int wacom_reset_resume(struct hid_device *hdev)
1572 return wacom_resume(hdev);
1574 #endif /* CONFIG_PM */
1576 static struct hid_driver wacom_driver = {
1578 .id_table = wacom_ids,
1579 .probe = wacom_probe,
1580 .remove = wacom_remove,
1581 .event = wacom_wac_event,
1582 .report = wacom_wac_report,
1584 .resume = wacom_resume,
1585 .reset_resume = wacom_reset_resume,
1587 .raw_event = wacom_raw_event,
1589 module_hid_driver(wacom_driver);
1591 MODULE_VERSION(DRIVER_VERSION);
1592 MODULE_AUTHOR(DRIVER_AUTHOR);
1593 MODULE_DESCRIPTION(DRIVER_DESC);
1594 MODULE_LICENSE(DRIVER_LICENSE);