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 HID_HDESC_USAGE_UNDEFINED 0x00
19 #define HID_HDESC_USAGE_PAGE 0x05
20 #define HID_HDESC_USAGE 0x09
21 #define HID_HDESC_COLLECTION 0xa1
22 #define HID_HDESC_COLLECTION_LOGICAL 0x02
23 #define HID_HDESC_COLLECTION_END 0xc0
25 #define WAC_MSG_RETRIES 5
27 #define WAC_CMD_LED_CONTROL 0x20
28 #define WAC_CMD_ICON_START 0x21
29 #define WAC_CMD_ICON_XFER 0x23
30 #define WAC_CMD_RETRIES 10
32 static int wacom_get_report(struct hid_device
*hdev
, u8 type
, u8 id
,
33 void *buf
, size_t size
, unsigned int retries
)
38 retval
= hid_hw_raw_request(hdev
, id
, buf
, size
, type
,
40 } while ((retval
== -ETIMEDOUT
|| retval
== -EPIPE
) && --retries
);
45 static int wacom_set_report(struct hid_device
*hdev
, u8 type
, u8 id
,
46 void *buf
, size_t size
, unsigned int retries
)
51 retval
= hid_hw_raw_request(hdev
, id
, buf
, size
, type
,
53 } while ((retval
== -ETIMEDOUT
|| retval
== -EPIPE
) && --retries
);
58 static int wacom_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
59 u8
*raw_data
, int size
)
61 struct wacom
*wacom
= hid_get_drvdata(hdev
);
63 if (size
> WACOM_PKGLEN_MAX
)
66 memcpy(wacom
->wacom_wac
.data
, raw_data
, size
);
68 wacom_wac_irq(&wacom
->wacom_wac
, size
);
73 static int wacom_open(struct input_dev
*dev
)
75 struct wacom
*wacom
= input_get_drvdata(dev
);
78 mutex_lock(&wacom
->lock
);
79 retval
= hid_hw_open(wacom
->hdev
);
80 mutex_unlock(&wacom
->lock
);
85 static void wacom_close(struct input_dev
*dev
)
87 struct wacom
*wacom
= input_get_drvdata(dev
);
89 mutex_lock(&wacom
->lock
);
90 hid_hw_close(wacom
->hdev
);
91 mutex_unlock(&wacom
->lock
);
95 * Calculate the resolution of the X or Y axis, given appropriate HID data.
96 * This function is little more than hidinput_calc_abs_res stripped down.
98 static int wacom_calc_hid_res(int logical_extents
, int physical_extents
,
99 unsigned char unit
, unsigned char exponent
)
101 int prev
, unit_exponent
;
103 /* Check if the extents are sane */
104 if (logical_extents
<= 0 || physical_extents
<= 0)
107 /* Get signed value of nybble-sized twos-compliment exponent */
108 unit_exponent
= exponent
;
109 if (unit_exponent
> 7)
112 /* Convert physical_extents to millimeters */
113 if (unit
== 0x11) { /* If centimeters */
115 } else if (unit
== 0x13) { /* If inches */
116 prev
= physical_extents
;
117 physical_extents
*= 254;
118 if (physical_extents
< prev
)
125 /* Apply negative unit exponent */
126 for (; unit_exponent
< 0; unit_exponent
++) {
127 prev
= logical_extents
;
128 logical_extents
*= 10;
129 if (logical_extents
< prev
)
132 /* Apply positive unit exponent */
133 for (; unit_exponent
> 0; unit_exponent
--) {
134 prev
= physical_extents
;
135 physical_extents
*= 10;
136 if (physical_extents
< prev
)
140 /* Calculate resolution */
141 return logical_extents
/ physical_extents
;
144 static int wacom_parse_logical_collection(unsigned char *report
,
145 struct wacom_features
*features
)
149 if (features
->type
== BAMBOO_PT
) {
151 /* Logical collection is only used by 3rd gen Bamboo Touch */
152 features
->device_type
= BTN_TOOL_FINGER
;
154 features
->x_max
= features
->y_max
=
155 get_unaligned_le16(&report
[10]);
162 static void wacom_retrieve_report_data(struct hid_device
*hdev
,
163 struct wacom_features
*features
)
166 unsigned char *rep_data
;
168 rep_data
= kmalloc(2, GFP_KERNEL
);
172 result
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
173 rep_data
[0], rep_data
, 2,
176 if (result
>= 0 && rep_data
[1] > 2)
177 features
->touch_max
= rep_data
[1];
184 * Interface Descriptor of wacom devices can be incomplete and
185 * inconsistent so wacom_features table is used to store stylus
186 * device's packet lengths, various maximum values, and tablet
187 * resolution based on product ID's.
189 * For devices that contain 2 interfaces, wacom_features table is
190 * inaccurate for the touch interface. Since the Interface Descriptor
191 * for touch interfaces has pretty complete data, this function exists
192 * to query tablet for this missing information instead of hard coding in
193 * an additional table.
195 * A typical Interface Descriptor for a stylus will contain a
196 * boot mouse application collection that is not of interest and this
197 * function will ignore it.
199 * It also contains a digitizer application collection that also is not
200 * of interest since any information it contains would be duplicate
201 * of what is in wacom_features. Usually it defines a report of an array
202 * of bytes that could be used as max length of the stylus packet returned.
203 * If it happens to define a Digitizer-Stylus Physical Collection then
204 * the X and Y logical values contain valid data but it is ignored.
206 * A typical Interface Descriptor for a touch interface will contain a
207 * Digitizer-Finger Physical Collection which will define both logical
208 * X/Y maximum as well as the physical size of tablet. Since touch
209 * interfaces haven't supported pressure or distance, this is enough
210 * information to override invalid values in the wacom_features table.
212 * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical
213 * Collection. Instead they define a Logical Collection with a single
214 * Logical Maximum for both X and Y.
216 * Intuos5 touch interface does not contain useful data. We deal with
217 * this after returning from this function.
219 static int wacom_parse_hid(struct hid_device
*hdev
,
220 struct wacom_features
*features
)
222 /* result has to be defined as int for some devices */
223 int result
= 0, touch_max
= 0;
224 int i
= 0, page
= 0, finger
= 0, pen
= 0;
225 unsigned char *report
= hdev
->rdesc
;
227 for (i
= 0; i
< hdev
->rsize
; i
++) {
230 case HID_HDESC_USAGE_PAGE
:
231 page
= report
[i
+ 1];
235 case HID_HDESC_USAGE
:
236 switch (page
<< 16 | report
[i
+ 1]) {
239 features
->device_type
= BTN_TOOL_FINGER
;
240 /* touch device at least supports one touch point */
243 switch (features
->type
) {
246 get_unaligned_le16(&report
[i
+ 5]);
248 get_unaligned_le16(&report
[i
+ 8]);
254 get_unaligned_le16(&report
[i
+ 3]);
256 get_unaligned_le16(&report
[i
+ 8]);
257 features
->unit
= report
[i
- 1];
258 features
->unitExpo
= report
[i
- 3];
264 get_unaligned_le16(&report
[i
+ 3]);
266 get_unaligned_le16(&report
[i
+ 6]);
267 features
->unit
= report
[i
- 5];
268 features
->unitExpo
= report
[i
- 3];
274 get_unaligned_le16(&report
[i
+ 3]);
276 get_unaligned_le16(&report
[i
+ 6]);
277 features
->unit
= report
[i
+ 9];
278 features
->unitExpo
= report
[i
+ 11];
283 /* penabled only accepts exact bytes of data */
284 features
->device_type
= BTN_TOOL_PEN
;
286 get_unaligned_le16(&report
[i
+ 3]);
293 switch (features
->type
) {
298 get_unaligned_le16(&report
[i
+ 3]);
300 get_unaligned_le16(&report
[i
+ 6]);
306 get_unaligned_le16(&report
[i
+ 3]);
308 get_unaligned_le16(&report
[i
- 2]);
314 get_unaligned_le16(&report
[i
+ 3]);
316 get_unaligned_le16(&report
[i
+ 6]);
322 get_unaligned_le16(&report
[i
+ 3]);
324 get_unaligned_le16(&report
[i
+ 6]);
332 get_unaligned_le16(&report
[i
+ 3]);
338 get_unaligned_le16(&report
[i
+ 3]);
349 * Requiring Stylus Usage will ignore boot mouse
350 * X/Y values and some cases of invalid Digitizer X/Y
351 * values commonly reported.
358 case HID_DG_CONTACTMAX
:
359 /* leave touch_max as is if predefined */
360 if (!features
->touch_max
)
361 wacom_retrieve_report_data(hdev
, features
);
365 case HID_DG_TIPPRESSURE
:
367 features
->pressure_max
=
368 get_unaligned_le16(&report
[i
+ 3]);
375 case HID_HDESC_COLLECTION_END
:
376 /* reset UsagePage and Finger */
380 case HID_HDESC_COLLECTION
:
383 case HID_HDESC_COLLECTION_LOGICAL
:
384 i
+= wacom_parse_logical_collection(&report
[i
],
392 if (!features
->touch_max
&& touch_max
)
393 features
->touch_max
= touch_max
;
398 static int wacom_set_device_mode(struct hid_device
*hdev
, int report_id
,
399 int length
, int mode
)
401 unsigned char *rep_data
;
402 int error
= -ENOMEM
, limit
= 0;
404 rep_data
= kzalloc(length
, GFP_KERNEL
);
409 rep_data
[0] = report_id
;
412 error
= wacom_set_report(hdev
, HID_FEATURE_REPORT
,
413 report_id
, rep_data
, length
, 1);
415 error
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
416 report_id
, rep_data
, length
, 1);
417 } while ((error
< 0 || rep_data
[1] != mode
) && limit
++ < WAC_MSG_RETRIES
);
421 return error
< 0 ? error
: 0;
425 * Switch the tablet into its most-capable mode. Wacom tablets are
426 * typically configured to power-up in a mode which sends mouse-like
427 * reports to the OS. To get absolute position, pressure data, etc.
428 * from the tablet, it is necessary to switch the tablet out of this
429 * mode and into one which sends the full range of tablet data.
431 static int wacom_query_tablet_data(struct hid_device
*hdev
,
432 struct wacom_features
*features
)
434 if (features
->device_type
== BTN_TOOL_FINGER
) {
435 if (features
->type
> TABLETPC
) {
436 /* MT Tablet PC touch */
437 return wacom_set_device_mode(hdev
, 3, 4, 4);
439 else if (features
->type
== WACOM_24HDT
|| features
->type
== CINTIQ_HYBRID
) {
440 return wacom_set_device_mode(hdev
, 18, 3, 2);
442 } else if (features
->device_type
== BTN_TOOL_PEN
) {
443 if (features
->type
<= BAMBOO_PT
&& features
->type
!= WIRELESS
) {
444 return wacom_set_device_mode(hdev
, 2, 2, 2);
451 static int wacom_retrieve_hid_descriptor(struct hid_device
*hdev
,
452 struct wacom_features
*features
)
455 struct wacom
*wacom
= hid_get_drvdata(hdev
);
456 struct usb_interface
*intf
= wacom
->intf
;
458 /* default features */
459 features
->device_type
= BTN_TOOL_PEN
;
460 features
->x_fuzz
= 4;
461 features
->y_fuzz
= 4;
462 features
->pressure_fuzz
= 0;
463 features
->distance_fuzz
= 0;
466 * The wireless device HID is basic and layout conflicts with
467 * other tablets (monitor and touch interface can look like pen).
468 * Skip the query for this type and modify defaults based on
471 if (features
->type
== WIRELESS
) {
472 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 0) {
473 features
->device_type
= 0;
474 } else if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 2) {
475 features
->device_type
= BTN_TOOL_FINGER
;
476 features
->pktlen
= WACOM_PKGLEN_BBTOUCH3
;
480 /* only devices that support touch need to retrieve the info */
481 if (features
->type
< BAMBOO_PT
) {
485 error
= wacom_parse_hid(hdev
, features
);
491 struct wacom_usbdev_data
{
492 struct list_head list
;
494 struct usb_device
*dev
;
495 struct wacom_shared shared
;
498 static LIST_HEAD(wacom_udev_list
);
499 static DEFINE_MUTEX(wacom_udev_list_lock
);
501 static struct usb_device
*wacom_get_sibling(struct usb_device
*dev
, int vendor
, int product
)
504 struct usb_device
*sibling
;
506 if (vendor
== 0 && product
== 0)
509 if (dev
->parent
== NULL
)
512 usb_hub_for_each_child(dev
->parent
, port1
, sibling
) {
513 struct usb_device_descriptor
*d
;
517 d
= &sibling
->descriptor
;
518 if (d
->idVendor
== vendor
&& d
->idProduct
== product
)
525 static struct wacom_usbdev_data
*wacom_get_usbdev_data(struct usb_device
*dev
)
527 struct wacom_usbdev_data
*data
;
529 list_for_each_entry(data
, &wacom_udev_list
, list
) {
530 if (data
->dev
== dev
) {
531 kref_get(&data
->kref
);
539 static int wacom_add_shared_data(struct wacom_wac
*wacom
,
540 struct usb_device
*dev
)
542 struct wacom_usbdev_data
*data
;
545 mutex_lock(&wacom_udev_list_lock
);
547 data
= wacom_get_usbdev_data(dev
);
549 data
= kzalloc(sizeof(struct wacom_usbdev_data
), GFP_KERNEL
);
555 kref_init(&data
->kref
);
557 list_add_tail(&data
->list
, &wacom_udev_list
);
560 wacom
->shared
= &data
->shared
;
563 mutex_unlock(&wacom_udev_list_lock
);
567 static void wacom_release_shared_data(struct kref
*kref
)
569 struct wacom_usbdev_data
*data
=
570 container_of(kref
, struct wacom_usbdev_data
, kref
);
572 mutex_lock(&wacom_udev_list_lock
);
573 list_del(&data
->list
);
574 mutex_unlock(&wacom_udev_list_lock
);
579 static void wacom_remove_shared_data(struct wacom_wac
*wacom
)
581 struct wacom_usbdev_data
*data
;
584 data
= container_of(wacom
->shared
, struct wacom_usbdev_data
, shared
);
585 kref_put(&data
->kref
, wacom_release_shared_data
);
586 wacom
->shared
= NULL
;
590 static int wacom_led_control(struct wacom
*wacom
)
595 buf
= kzalloc(9, GFP_KERNEL
);
599 if (wacom
->wacom_wac
.features
.type
>= INTUOS5S
&&
600 wacom
->wacom_wac
.features
.type
<= INTUOSPL
) {
602 * Touch Ring and crop mark LED luminance may take on
603 * one of four values:
604 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
606 int ring_led
= wacom
->led
.select
[0] & 0x03;
607 int ring_lum
= (((wacom
->led
.llv
& 0x60) >> 5) - 1) & 0x03;
610 buf
[0] = WAC_CMD_LED_CONTROL
;
611 buf
[1] = (crop_lum
<< 4) | (ring_lum
<< 2) | (ring_led
);
614 int led
= wacom
->led
.select
[0] | 0x4;
616 if (wacom
->wacom_wac
.features
.type
== WACOM_21UX2
||
617 wacom
->wacom_wac
.features
.type
== WACOM_24HD
)
618 led
|= (wacom
->led
.select
[1] << 4) | 0x40;
620 buf
[0] = WAC_CMD_LED_CONTROL
;
622 buf
[2] = wacom
->led
.llv
;
623 buf
[3] = wacom
->led
.hlv
;
624 buf
[4] = wacom
->led
.img_lum
;
627 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
,
628 WAC_CMD_LED_CONTROL
, buf
, 9, WAC_CMD_RETRIES
);
634 static int wacom_led_putimage(struct wacom
*wacom
, int button_id
, const void *img
)
639 buf
= kzalloc(259, GFP_KERNEL
);
643 /* Send 'start' command */
644 buf
[0] = WAC_CMD_ICON_START
;
646 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
,
647 WAC_CMD_ICON_START
, buf
, 2, WAC_CMD_RETRIES
);
651 buf
[0] = WAC_CMD_ICON_XFER
;
652 buf
[1] = button_id
& 0x07;
653 for (i
= 0; i
< 4; i
++) {
655 memcpy(buf
+ 3, img
+ i
* 256, 256);
657 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
,
659 buf
, 259, WAC_CMD_RETRIES
);
665 buf
[0] = WAC_CMD_ICON_START
;
667 wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, WAC_CMD_ICON_START
,
668 buf
, 2, WAC_CMD_RETRIES
);
675 static ssize_t
wacom_led_select_store(struct device
*dev
, int set_id
,
676 const char *buf
, size_t count
)
678 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
679 struct wacom
*wacom
= hid_get_drvdata(hdev
);
683 err
= kstrtouint(buf
, 10, &id
);
687 mutex_lock(&wacom
->lock
);
689 wacom
->led
.select
[set_id
] = id
& 0x3;
690 err
= wacom_led_control(wacom
);
692 mutex_unlock(&wacom
->lock
);
694 return err
< 0 ? err
: count
;
697 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
698 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
699 struct device_attribute *attr, const char *buf, size_t count) \
701 return wacom_led_select_store(dev, SET_ID, buf, count); \
703 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
704 struct device_attribute *attr, char *buf) \
706 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
707 struct wacom *wacom = hid_get_drvdata(hdev); \
708 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
710 static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
711 wacom_led##SET_ID##_select_show, \
712 wacom_led##SET_ID##_select_store)
714 DEVICE_LED_SELECT_ATTR(0);
715 DEVICE_LED_SELECT_ATTR(1);
717 static ssize_t
wacom_luminance_store(struct wacom
*wacom
, u8
*dest
,
718 const char *buf
, size_t count
)
723 err
= kstrtouint(buf
, 10, &value
);
727 mutex_lock(&wacom
->lock
);
729 *dest
= value
& 0x7f;
730 err
= wacom_led_control(wacom
);
732 mutex_unlock(&wacom
->lock
);
734 return err
< 0 ? err
: count
;
737 #define DEVICE_LUMINANCE_ATTR(name, field) \
738 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
739 struct device_attribute *attr, const char *buf, size_t count) \
741 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
742 struct wacom *wacom = hid_get_drvdata(hdev); \
744 return wacom_luminance_store(wacom, &wacom->led.field, \
747 static DEVICE_ATTR(name##_luminance, S_IWUSR, \
748 NULL, wacom_##name##_luminance_store)
750 DEVICE_LUMINANCE_ATTR(status0
, llv
);
751 DEVICE_LUMINANCE_ATTR(status1
, hlv
);
752 DEVICE_LUMINANCE_ATTR(buttons
, img_lum
);
754 static ssize_t
wacom_button_image_store(struct device
*dev
, int button_id
,
755 const char *buf
, size_t count
)
757 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
758 struct wacom
*wacom
= hid_get_drvdata(hdev
);
764 mutex_lock(&wacom
->lock
);
766 err
= wacom_led_putimage(wacom
, button_id
, buf
);
768 mutex_unlock(&wacom
->lock
);
770 return err
< 0 ? err
: count
;
773 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
774 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
775 struct device_attribute *attr, const char *buf, size_t count) \
777 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
779 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
780 NULL, wacom_btnimg##BUTTON_ID##_store)
782 DEVICE_BTNIMG_ATTR(0);
783 DEVICE_BTNIMG_ATTR(1);
784 DEVICE_BTNIMG_ATTR(2);
785 DEVICE_BTNIMG_ATTR(3);
786 DEVICE_BTNIMG_ATTR(4);
787 DEVICE_BTNIMG_ATTR(5);
788 DEVICE_BTNIMG_ATTR(6);
789 DEVICE_BTNIMG_ATTR(7);
791 static struct attribute
*cintiq_led_attrs
[] = {
792 &dev_attr_status_led0_select
.attr
,
793 &dev_attr_status_led1_select
.attr
,
797 static struct attribute_group cintiq_led_attr_group
= {
799 .attrs
= cintiq_led_attrs
,
802 static struct attribute
*intuos4_led_attrs
[] = {
803 &dev_attr_status0_luminance
.attr
,
804 &dev_attr_status1_luminance
.attr
,
805 &dev_attr_status_led0_select
.attr
,
806 &dev_attr_buttons_luminance
.attr
,
807 &dev_attr_button0_rawimg
.attr
,
808 &dev_attr_button1_rawimg
.attr
,
809 &dev_attr_button2_rawimg
.attr
,
810 &dev_attr_button3_rawimg
.attr
,
811 &dev_attr_button4_rawimg
.attr
,
812 &dev_attr_button5_rawimg
.attr
,
813 &dev_attr_button6_rawimg
.attr
,
814 &dev_attr_button7_rawimg
.attr
,
818 static struct attribute_group intuos4_led_attr_group
= {
820 .attrs
= intuos4_led_attrs
,
823 static struct attribute
*intuos5_led_attrs
[] = {
824 &dev_attr_status0_luminance
.attr
,
825 &dev_attr_status_led0_select
.attr
,
829 static struct attribute_group intuos5_led_attr_group
= {
831 .attrs
= intuos5_led_attrs
,
834 static int wacom_initialize_leds(struct wacom
*wacom
)
838 /* Initialize default values */
839 switch (wacom
->wacom_wac
.features
.type
) {
843 wacom
->led
.select
[0] = 0;
844 wacom
->led
.select
[1] = 0;
847 wacom
->led
.img_lum
= 10;
848 error
= sysfs_create_group(&wacom
->hdev
->dev
.kobj
,
849 &intuos4_led_attr_group
);
854 wacom
->led
.select
[0] = 0;
855 wacom
->led
.select
[1] = 0;
858 wacom
->led
.img_lum
= 0;
860 error
= sysfs_create_group(&wacom
->hdev
->dev
.kobj
,
861 &cintiq_led_attr_group
);
870 if (wacom
->wacom_wac
.features
.device_type
== BTN_TOOL_PEN
) {
871 wacom
->led
.select
[0] = 0;
872 wacom
->led
.select
[1] = 0;
875 wacom
->led
.img_lum
= 0;
877 error
= sysfs_create_group(&wacom
->hdev
->dev
.kobj
,
878 &intuos5_led_attr_group
);
889 "cannot create sysfs group err: %d\n", error
);
892 wacom_led_control(wacom
);
897 static void wacom_destroy_leds(struct wacom
*wacom
)
899 switch (wacom
->wacom_wac
.features
.type
) {
903 sysfs_remove_group(&wacom
->hdev
->dev
.kobj
,
904 &intuos4_led_attr_group
);
909 sysfs_remove_group(&wacom
->hdev
->dev
.kobj
,
910 &cintiq_led_attr_group
);
919 if (wacom
->wacom_wac
.features
.device_type
== BTN_TOOL_PEN
)
920 sysfs_remove_group(&wacom
->hdev
->dev
.kobj
,
921 &intuos5_led_attr_group
);
926 static enum power_supply_property wacom_battery_props
[] = {
927 POWER_SUPPLY_PROP_SCOPE
,
928 POWER_SUPPLY_PROP_CAPACITY
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
* 100 / 31;
954 static int wacom_initialize_battery(struct wacom
*wacom
)
958 if (wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_MONITOR
) {
959 wacom
->battery
.properties
= wacom_battery_props
;
960 wacom
->battery
.num_properties
= ARRAY_SIZE(wacom_battery_props
);
961 wacom
->battery
.get_property
= wacom_battery_get_property
;
962 wacom
->battery
.name
= "wacom_battery";
963 wacom
->battery
.type
= POWER_SUPPLY_TYPE_BATTERY
;
964 wacom
->battery
.use_for_apm
= 0;
966 error
= power_supply_register(&wacom
->usbdev
->dev
,
970 power_supply_powers(&wacom
->battery
,
971 &wacom
->usbdev
->dev
);
977 static void wacom_destroy_battery(struct wacom
*wacom
)
979 if (wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_MONITOR
&&
980 wacom
->battery
.dev
) {
981 power_supply_unregister(&wacom
->battery
);
982 wacom
->battery
.dev
= NULL
;
986 static struct input_dev
*wacom_allocate_input(struct wacom
*wacom
)
988 struct input_dev
*input_dev
;
989 struct hid_device
*hdev
= wacom
->hdev
;
990 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
992 input_dev
= input_allocate_device();
996 input_dev
->name
= wacom_wac
->name
;
997 input_dev
->phys
= hdev
->phys
;
998 input_dev
->dev
.parent
= &hdev
->dev
;
999 input_dev
->open
= wacom_open
;
1000 input_dev
->close
= wacom_close
;
1001 input_dev
->uniq
= hdev
->uniq
;
1002 input_dev
->id
.bustype
= hdev
->bus
;
1003 input_dev
->id
.vendor
= hdev
->vendor
;
1004 input_dev
->id
.product
= hdev
->product
;
1005 input_dev
->id
.version
= hdev
->version
;
1006 input_set_drvdata(input_dev
, wacom
);
1011 static void wacom_unregister_inputs(struct wacom
*wacom
)
1013 if (wacom
->wacom_wac
.input
)
1014 input_unregister_device(wacom
->wacom_wac
.input
);
1015 if (wacom
->wacom_wac
.pad_input
)
1016 input_unregister_device(wacom
->wacom_wac
.pad_input
);
1017 wacom
->wacom_wac
.input
= NULL
;
1018 wacom
->wacom_wac
.pad_input
= NULL
;
1021 static int wacom_register_inputs(struct wacom
*wacom
)
1023 struct input_dev
*input_dev
, *pad_input_dev
;
1024 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1027 input_dev
= wacom_allocate_input(wacom
);
1028 pad_input_dev
= wacom_allocate_input(wacom
);
1029 if (!input_dev
|| !pad_input_dev
) {
1034 wacom_wac
->input
= input_dev
;
1035 wacom_wac
->pad_input
= pad_input_dev
;
1036 wacom_wac
->pad_input
->name
= wacom_wac
->pad_name
;
1038 error
= wacom_setup_input_capabilities(input_dev
, wacom_wac
);
1042 error
= input_register_device(input_dev
);
1046 error
= wacom_setup_pad_input_capabilities(pad_input_dev
, wacom_wac
);
1048 /* no pad in use on this interface */
1049 input_free_device(pad_input_dev
);
1050 wacom_wac
->pad_input
= NULL
;
1051 pad_input_dev
= NULL
;
1053 error
= input_register_device(pad_input_dev
);
1061 input_unregister_device(input_dev
);
1064 wacom_wac
->input
= NULL
;
1065 wacom_wac
->pad_input
= NULL
;
1068 input_free_device(input_dev
);
1070 input_free_device(pad_input_dev
);
1074 static void wacom_wireless_work(struct work_struct
*work
)
1076 struct wacom
*wacom
= container_of(work
, struct wacom
, work
);
1077 struct usb_device
*usbdev
= wacom
->usbdev
;
1078 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
1079 struct hid_device
*hdev1
, *hdev2
;
1080 struct wacom
*wacom1
, *wacom2
;
1081 struct wacom_wac
*wacom_wac1
, *wacom_wac2
;
1085 * Regardless if this is a disconnect or a new tablet,
1086 * remove any existing input and battery devices.
1089 wacom_destroy_battery(wacom
);
1091 /* Stylus interface */
1092 hdev1
= usb_get_intfdata(usbdev
->config
->interface
[1]);
1093 wacom1
= hid_get_drvdata(hdev1
);
1094 wacom_wac1
= &(wacom1
->wacom_wac
);
1095 wacom_unregister_inputs(wacom1
);
1097 /* Touch interface */
1098 hdev2
= usb_get_intfdata(usbdev
->config
->interface
[2]);
1099 wacom2
= hid_get_drvdata(hdev2
);
1100 wacom_wac2
= &(wacom2
->wacom_wac
);
1101 wacom_unregister_inputs(wacom2
);
1103 if (wacom_wac
->pid
== 0) {
1104 dev_info(&wacom
->intf
->dev
, "wireless tablet disconnected\n");
1106 const struct hid_device_id
*id
= wacom_ids
;
1108 dev_info(&wacom
->intf
->dev
,
1109 "wireless tablet connected with PID %x\n",
1113 if (id
->vendor
== USB_VENDOR_ID_WACOM
&&
1114 id
->product
== wacom_wac
->pid
)
1120 dev_info(&wacom
->intf
->dev
,
1121 "ignoring unknown PID.\n");
1125 /* Stylus interface */
1126 wacom_wac1
->features
=
1127 *((struct wacom_features
*)id
->driver_data
);
1128 wacom_wac1
->features
.device_type
= BTN_TOOL_PEN
;
1129 snprintf(wacom_wac1
->name
, WACOM_NAME_MAX
, "%s (WL) Pen",
1130 wacom_wac1
->features
.name
);
1131 snprintf(wacom_wac1
->pad_name
, WACOM_NAME_MAX
, "%s (WL) Pad",
1132 wacom_wac1
->features
.name
);
1133 wacom_wac1
->shared
->touch_max
= wacom_wac1
->features
.touch_max
;
1134 wacom_wac1
->shared
->type
= wacom_wac1
->features
.type
;
1135 error
= wacom_register_inputs(wacom1
);
1139 /* Touch interface */
1140 if (wacom_wac1
->features
.touch_max
||
1141 wacom_wac1
->features
.type
== INTUOSHT
) {
1142 wacom_wac2
->features
=
1143 *((struct wacom_features
*)id
->driver_data
);
1144 wacom_wac2
->features
.pktlen
= WACOM_PKGLEN_BBTOUCH3
;
1145 wacom_wac2
->features
.device_type
= BTN_TOOL_FINGER
;
1146 wacom_wac2
->features
.x_max
= wacom_wac2
->features
.y_max
= 4096;
1147 if (wacom_wac2
->features
.touch_max
)
1148 snprintf(wacom_wac2
->name
, WACOM_NAME_MAX
,
1149 "%s (WL) Finger",wacom_wac2
->features
.name
);
1151 snprintf(wacom_wac2
->name
, WACOM_NAME_MAX
,
1152 "%s (WL) Pad",wacom_wac2
->features
.name
);
1153 snprintf(wacom_wac2
->pad_name
, WACOM_NAME_MAX
,
1154 "%s (WL) Pad", wacom_wac2
->features
.name
);
1155 error
= wacom_register_inputs(wacom2
);
1159 if (wacom_wac1
->features
.type
== INTUOSHT
&&
1160 wacom_wac1
->features
.touch_max
)
1161 wacom_wac
->shared
->touch_input
= wacom_wac2
->input
;
1164 error
= wacom_initialize_battery(wacom
);
1172 wacom_unregister_inputs(wacom1
);
1173 wacom_unregister_inputs(wacom2
);
1178 * Not all devices report physical dimensions from HID.
1179 * Compute the default from hardcoded logical dimension
1180 * and resolution before driver overwrites them.
1182 static void wacom_set_default_phy(struct wacom_features
*features
)
1184 if (features
->x_resolution
) {
1185 features
->x_phy
= (features
->x_max
* 100) /
1186 features
->x_resolution
;
1187 features
->y_phy
= (features
->y_max
* 100) /
1188 features
->y_resolution
;
1192 static void wacom_calculate_res(struct wacom_features
*features
)
1194 features
->x_resolution
= wacom_calc_hid_res(features
->x_max
,
1197 features
->unitExpo
);
1198 features
->y_resolution
= wacom_calc_hid_res(features
->y_max
,
1201 features
->unitExpo
);
1204 static int wacom_hid_report_len(struct hid_report
*report
)
1206 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1207 return ((report
->size
- 1) >> 3) + 1 + (report
->id
> 0);
1210 static size_t wacom_compute_pktlen(struct hid_device
*hdev
)
1212 struct hid_report_enum
*report_enum
;
1213 struct hid_report
*report
;
1216 report_enum
= hdev
->report_enum
+ HID_INPUT_REPORT
;
1218 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
1219 size_t report_size
= wacom_hid_report_len(report
);
1220 if (report_size
> size
)
1227 static int wacom_probe(struct hid_device
*hdev
,
1228 const struct hid_device_id
*id
)
1230 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
1231 struct usb_device
*dev
= interface_to_usbdev(intf
);
1232 struct wacom
*wacom
;
1233 struct wacom_wac
*wacom_wac
;
1234 struct wacom_features
*features
;
1237 if (!id
->driver_data
)
1240 wacom
= kzalloc(sizeof(struct wacom
), GFP_KERNEL
);
1244 hid_set_drvdata(hdev
, wacom
);
1247 /* ask for the report descriptor to be loaded by HID */
1248 error
= hid_parse(hdev
);
1250 hid_err(hdev
, "parse failed\n");
1254 wacom_wac
= &wacom
->wacom_wac
;
1255 wacom_wac
->features
= *((struct wacom_features
*)id
->driver_data
);
1256 features
= &wacom_wac
->features
;
1257 features
->pktlen
= wacom_compute_pktlen(hdev
);
1258 if (features
->pktlen
> WACOM_PKGLEN_MAX
) {
1263 if (features
->check_for_hid_type
&& features
->hid_type
!= hdev
->type
) {
1268 wacom
->usbdev
= dev
;
1270 mutex_init(&wacom
->lock
);
1271 INIT_WORK(&wacom
->work
, wacom_wireless_work
);
1273 /* set the default size in case we do not get them from hid */
1274 wacom_set_default_phy(features
);
1276 /* Retrieve the physical and logical size for touch devices */
1277 error
= wacom_retrieve_hid_descriptor(hdev
, features
);
1282 * Intuos5 has no useful data about its touch interface in its
1283 * HID descriptor. If this is the touch interface (PacketSize
1284 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1286 if (features
->type
>= INTUOS5S
&& features
->type
<= INTUOSHT
) {
1287 if (features
->pktlen
== WACOM_PKGLEN_BBTOUCH3
) {
1288 features
->device_type
= BTN_TOOL_FINGER
;
1290 features
->x_max
= 4096;
1291 features
->y_max
= 4096;
1293 features
->device_type
= BTN_TOOL_PEN
;
1297 wacom_setup_device_quirks(features
);
1299 /* set unit to "100th of a mm" for devices not reported by HID */
1300 if (!features
->unit
) {
1301 features
->unit
= 0x11;
1302 features
->unitExpo
= 16 - 3;
1304 wacom_calculate_res(features
);
1306 strlcpy(wacom_wac
->name
, features
->name
, sizeof(wacom_wac
->name
));
1307 snprintf(wacom_wac
->pad_name
, sizeof(wacom_wac
->pad_name
),
1308 "%s Pad", features
->name
);
1310 if (features
->quirks
& WACOM_QUIRK_MULTI_INPUT
) {
1311 struct usb_device
*other_dev
;
1313 /* Append the device type to the name */
1314 if (features
->device_type
!= BTN_TOOL_FINGER
)
1315 strlcat(wacom_wac
->name
, " Pen", WACOM_NAME_MAX
);
1316 else if (features
->touch_max
)
1317 strlcat(wacom_wac
->name
, " Finger", WACOM_NAME_MAX
);
1319 strlcat(wacom_wac
->name
, " Pad", WACOM_NAME_MAX
);
1321 other_dev
= wacom_get_sibling(dev
, features
->oVid
, features
->oPid
);
1322 if (other_dev
== NULL
|| wacom_get_usbdev_data(other_dev
) == NULL
)
1324 error
= wacom_add_shared_data(wacom_wac
, other_dev
);
1329 error
= wacom_initialize_leds(wacom
);
1333 if (!(features
->quirks
& WACOM_QUIRK_NO_INPUT
)) {
1334 error
= wacom_register_inputs(wacom
);
1339 /* Note that if query fails it is not a hard failure */
1340 wacom_query_tablet_data(hdev
, features
);
1342 /* Regular HID work starts now */
1343 error
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
1345 hid_err(hdev
, "hw start failed\n");
1349 if (features
->quirks
& WACOM_QUIRK_MONITOR
)
1350 error
= hid_hw_open(hdev
);
1352 if (wacom_wac
->features
.type
== INTUOSHT
&& wacom_wac
->features
.touch_max
) {
1353 if (wacom_wac
->features
.device_type
== BTN_TOOL_FINGER
)
1354 wacom_wac
->shared
->touch_input
= wacom_wac
->input
;
1359 fail4
: wacom_unregister_inputs(wacom
);
1360 fail3
: wacom_destroy_leds(wacom
);
1361 fail2
: wacom_remove_shared_data(wacom_wac
);
1362 fail1
: kfree(wacom
);
1363 hid_set_drvdata(hdev
, NULL
);
1367 static void wacom_remove(struct hid_device
*hdev
)
1369 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1373 cancel_work_sync(&wacom
->work
);
1374 wacom_unregister_inputs(wacom
);
1375 wacom_destroy_battery(wacom
);
1376 wacom_destroy_leds(wacom
);
1377 wacom_remove_shared_data(&wacom
->wacom_wac
);
1379 hid_set_drvdata(hdev
, NULL
);
1383 static int wacom_resume(struct hid_device
*hdev
)
1385 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1386 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
1388 mutex_lock(&wacom
->lock
);
1390 /* switch to wacom mode first */
1391 wacom_query_tablet_data(hdev
, features
);
1392 wacom_led_control(wacom
);
1394 mutex_unlock(&wacom
->lock
);
1399 static int wacom_reset_resume(struct hid_device
*hdev
)
1401 return wacom_resume(hdev
);
1404 static struct hid_driver wacom_driver
= {
1406 .id_table
= wacom_ids
,
1407 .probe
= wacom_probe
,
1408 .remove
= wacom_remove
,
1410 .resume
= wacom_resume
,
1411 .reset_resume
= wacom_reset_resume
,
1413 .raw_event
= wacom_raw_event
,
1415 module_hid_driver(wacom_driver
);