2 * drivers/input/tablet/wacom_sys.c
4 * USB Wacom tablet support - system specific code
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include "wacom_wac.h"
16 #include <linux/hid.h>
18 #define WAC_MSG_RETRIES 5
20 #define WAC_CMD_LED_CONTROL 0x20
21 #define WAC_CMD_ICON_START 0x21
22 #define WAC_CMD_ICON_XFER 0x23
23 #define WAC_CMD_RETRIES 10
25 static int wacom_get_report(struct hid_device
*hdev
, u8 type
, u8 id
,
26 void *buf
, size_t size
, unsigned int retries
)
31 retval
= hid_hw_raw_request(hdev
, id
, buf
, size
, type
,
33 } while ((retval
== -ETIMEDOUT
|| retval
== -EPIPE
) && --retries
);
38 static int wacom_set_report(struct hid_device
*hdev
, u8 type
, u8 id
,
39 void *buf
, size_t size
, unsigned int retries
)
44 retval
= hid_hw_raw_request(hdev
, id
, buf
, size
, type
,
46 } while ((retval
== -ETIMEDOUT
|| retval
== -EPIPE
) && --retries
);
51 static int wacom_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
52 u8
*raw_data
, int size
)
54 struct wacom
*wacom
= hid_get_drvdata(hdev
);
56 if (size
> WACOM_PKGLEN_MAX
)
59 memcpy(wacom
->wacom_wac
.data
, raw_data
, size
);
61 wacom_wac_irq(&wacom
->wacom_wac
, size
);
66 static int wacom_open(struct input_dev
*dev
)
68 struct wacom
*wacom
= input_get_drvdata(dev
);
71 mutex_lock(&wacom
->lock
);
72 retval
= hid_hw_open(wacom
->hdev
);
73 mutex_unlock(&wacom
->lock
);
78 static void wacom_close(struct input_dev
*dev
)
80 struct wacom
*wacom
= input_get_drvdata(dev
);
82 mutex_lock(&wacom
->lock
);
83 hid_hw_close(wacom
->hdev
);
84 mutex_unlock(&wacom
->lock
);
88 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
90 static int wacom_calc_hid_res(int logical_extents
, int physical_extents
,
91 unsigned unit
, int exponent
)
93 struct hid_field field
= {
94 .logical_maximum
= logical_extents
,
95 .physical_maximum
= physical_extents
,
97 .unit_exponent
= exponent
,
100 return hidinput_calc_abs_res(&field
, ABS_X
);
103 static void wacom_feature_mapping(struct hid_device
*hdev
,
104 struct hid_field
*field
, struct hid_usage
*usage
)
106 struct wacom
*wacom
= hid_get_drvdata(hdev
);
107 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
109 switch (usage
->hid
) {
110 case HID_DG_CONTACTMAX
:
111 /* leave touch_max as is if predefined */
112 if (!features
->touch_max
)
113 features
->touch_max
= field
->value
[0];
119 * Interface Descriptor of wacom devices can be incomplete and
120 * inconsistent so wacom_features table is used to store stylus
121 * device's packet lengths, various maximum values, and tablet
122 * resolution based on product ID's.
124 * For devices that contain 2 interfaces, wacom_features table is
125 * inaccurate for the touch interface. Since the Interface Descriptor
126 * for touch interfaces has pretty complete data, this function exists
127 * to query tablet for this missing information instead of hard coding in
128 * an additional table.
130 * A typical Interface Descriptor for a stylus will contain a
131 * boot mouse application collection that is not of interest and this
132 * function will ignore it.
134 * It also contains a digitizer application collection that also is not
135 * of interest since any information it contains would be duplicate
136 * of what is in wacom_features. Usually it defines a report of an array
137 * of bytes that could be used as max length of the stylus packet returned.
138 * If it happens to define a Digitizer-Stylus Physical Collection then
139 * the X and Y logical values contain valid data but it is ignored.
141 * A typical Interface Descriptor for a touch interface will contain a
142 * Digitizer-Finger Physical Collection which will define both logical
143 * X/Y maximum as well as the physical size of tablet. Since touch
144 * interfaces haven't supported pressure or distance, this is enough
145 * information to override invalid values in the wacom_features table.
147 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
148 * data. We deal with them after returning from this function.
150 static void wacom_usage_mapping(struct hid_device
*hdev
,
151 struct hid_field
*field
, struct hid_usage
*usage
)
153 struct wacom
*wacom
= hid_get_drvdata(hdev
);
154 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
155 bool finger
= (field
->logical
== HID_DG_FINGER
) ||
156 (field
->physical
== HID_DG_FINGER
);
157 bool pen
= (field
->logical
== HID_DG_STYLUS
) ||
158 (field
->physical
== HID_DG_STYLUS
);
161 * Requiring Stylus Usage will ignore boot mouse
162 * X/Y values and some cases of invalid Digitizer X/Y
163 * values commonly reported.
168 if (finger
&& !features
->touch_max
)
169 /* touch device at least supports one touch point */
170 features
->touch_max
= 1;
172 switch (usage
->hid
) {
174 features
->x_max
= field
->logical_maximum
;
176 features
->device_type
= BTN_TOOL_FINGER
;
177 features
->x_phy
= field
->physical_maximum
;
178 if (features
->type
!= BAMBOO_PT
) {
179 features
->unit
= field
->unit
;
180 features
->unitExpo
= field
->unit_exponent
;
183 features
->device_type
= BTN_TOOL_PEN
;
187 features
->y_max
= field
->logical_maximum
;
189 features
->y_phy
= field
->physical_maximum
;
190 if (features
->type
!= BAMBOO_PT
) {
191 features
->unit
= field
->unit
;
192 features
->unitExpo
= field
->unit_exponent
;
196 case HID_DG_TIPPRESSURE
:
198 features
->pressure_max
= field
->logical_maximum
;
203 static void wacom_parse_hid(struct hid_device
*hdev
,
204 struct wacom_features
*features
)
206 struct hid_report_enum
*rep_enum
;
207 struct hid_report
*hreport
;
210 /* check features first */
211 rep_enum
= &hdev
->report_enum
[HID_FEATURE_REPORT
];
212 list_for_each_entry(hreport
, &rep_enum
->report_list
, list
) {
213 for (i
= 0; i
< hreport
->maxfield
; i
++) {
214 /* Ignore if report count is out of bounds. */
215 if (hreport
->field
[i
]->report_count
< 1)
218 for (j
= 0; j
< hreport
->field
[i
]->maxusage
; j
++) {
219 wacom_feature_mapping(hdev
, hreport
->field
[i
],
220 hreport
->field
[i
]->usage
+ j
);
225 /* now check the input usages */
226 rep_enum
= &hdev
->report_enum
[HID_INPUT_REPORT
];
227 list_for_each_entry(hreport
, &rep_enum
->report_list
, list
) {
229 if (!hreport
->maxfield
)
232 for (i
= 0; i
< hreport
->maxfield
; i
++)
233 for (j
= 0; j
< hreport
->field
[i
]->maxusage
; j
++)
234 wacom_usage_mapping(hdev
, hreport
->field
[i
],
235 hreport
->field
[i
]->usage
+ j
);
239 static int wacom_set_device_mode(struct hid_device
*hdev
, int report_id
,
240 int length
, int mode
)
242 unsigned char *rep_data
;
243 int error
= -ENOMEM
, limit
= 0;
245 rep_data
= kzalloc(length
, GFP_KERNEL
);
250 rep_data
[0] = report_id
;
253 error
= wacom_set_report(hdev
, HID_FEATURE_REPORT
,
254 report_id
, rep_data
, length
, 1);
256 error
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
257 report_id
, rep_data
, length
, 1);
258 } while ((error
< 0 || rep_data
[1] != mode
) && limit
++ < WAC_MSG_RETRIES
);
262 return error
< 0 ? error
: 0;
265 static int wacom_bt_query_tablet_data(struct hid_device
*hdev
, u8 speed
,
266 struct wacom_features
*features
)
268 struct wacom
*wacom
= hid_get_drvdata(hdev
);
272 switch (features
->type
) {
276 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
,
277 rep_data
[0], rep_data
, 2, 3);
280 rep_data
[0] = speed
== 0 ? 0x05 : 0x06;
283 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
,
284 rep_data
[0], rep_data
, 2, 3);
287 wacom
->wacom_wac
.bt_high_speed
= speed
;
293 * Note that if the raw queries fail, it's not a hard failure
294 * and it is safe to continue
296 hid_warn(hdev
, "failed to poke device, command %d, err %d\n",
301 wacom
->wacom_wac
.bt_features
&= ~0x20;
303 wacom
->wacom_wac
.bt_features
|= 0x20;
306 rep_data
[1] = wacom
->wacom_wac
.bt_features
;
308 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
,
309 rep_data
[0], rep_data
, 2, 1);
311 wacom
->wacom_wac
.bt_high_speed
= speed
;
319 * Switch the tablet into its most-capable mode. Wacom tablets are
320 * typically configured to power-up in a mode which sends mouse-like
321 * reports to the OS. To get absolute position, pressure data, etc.
322 * from the tablet, it is necessary to switch the tablet out of this
323 * mode and into one which sends the full range of tablet data.
325 static int wacom_query_tablet_data(struct hid_device
*hdev
,
326 struct wacom_features
*features
)
328 if (hdev
->bus
== BUS_BLUETOOTH
)
329 return wacom_bt_query_tablet_data(hdev
, 1, features
);
331 if (features
->device_type
== BTN_TOOL_FINGER
) {
332 if (features
->type
> TABLETPC
) {
333 /* MT Tablet PC touch */
334 return wacom_set_device_mode(hdev
, 3, 4, 4);
336 else if (features
->type
== WACOM_24HDT
|| features
->type
== CINTIQ_HYBRID
) {
337 return wacom_set_device_mode(hdev
, 18, 3, 2);
339 } else if (features
->device_type
== BTN_TOOL_PEN
) {
340 if (features
->type
<= BAMBOO_PT
&& features
->type
!= WIRELESS
) {
341 return wacom_set_device_mode(hdev
, 2, 2, 2);
348 static void wacom_retrieve_hid_descriptor(struct hid_device
*hdev
,
349 struct wacom_features
*features
)
351 struct wacom
*wacom
= hid_get_drvdata(hdev
);
352 struct usb_interface
*intf
= wacom
->intf
;
354 /* default features */
355 features
->device_type
= BTN_TOOL_PEN
;
356 features
->x_fuzz
= 4;
357 features
->y_fuzz
= 4;
358 features
->pressure_fuzz
= 0;
359 features
->distance_fuzz
= 0;
362 * The wireless device HID is basic and layout conflicts with
363 * other tablets (monitor and touch interface can look like pen).
364 * Skip the query for this type and modify defaults based on
367 if (features
->type
== WIRELESS
) {
368 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 0) {
369 features
->device_type
= 0;
370 } else if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 2) {
371 features
->device_type
= BTN_TOOL_FINGER
;
372 features
->pktlen
= WACOM_PKGLEN_BBTOUCH3
;
376 /* only devices that support touch need to retrieve the info */
377 if (features
->type
< BAMBOO_PT
)
380 wacom_parse_hid(hdev
, features
);
383 struct wacom_hdev_data
{
384 struct list_head list
;
386 struct hid_device
*dev
;
387 struct wacom_shared shared
;
390 static LIST_HEAD(wacom_udev_list
);
391 static DEFINE_MUTEX(wacom_udev_list_lock
);
393 static bool wacom_are_sibling(struct hid_device
*hdev
,
394 struct hid_device
*sibling
)
396 struct wacom
*wacom
= hid_get_drvdata(hdev
);
397 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
398 int vid
= features
->oVid
;
399 int pid
= features
->oPid
;
402 if (vid
== 0 && pid
== 0) {
407 if (vid
!= sibling
->vendor
|| pid
!= sibling
->product
)
410 /* Compare the physical path. */
411 n1
= strrchr(hdev
->phys
, '.') - hdev
->phys
;
412 n2
= strrchr(sibling
->phys
, '.') - sibling
->phys
;
413 if (n1
!= n2
|| n1
<= 0 || n2
<= 0)
416 return !strncmp(hdev
->phys
, sibling
->phys
, n1
);
419 static struct wacom_hdev_data
*wacom_get_hdev_data(struct hid_device
*hdev
)
421 struct wacom_hdev_data
*data
;
423 list_for_each_entry(data
, &wacom_udev_list
, list
) {
424 if (wacom_are_sibling(hdev
, data
->dev
)) {
425 kref_get(&data
->kref
);
433 static int wacom_add_shared_data(struct hid_device
*hdev
)
435 struct wacom
*wacom
= hid_get_drvdata(hdev
);
436 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
437 struct wacom_hdev_data
*data
;
440 mutex_lock(&wacom_udev_list_lock
);
442 data
= wacom_get_hdev_data(hdev
);
444 data
= kzalloc(sizeof(struct wacom_hdev_data
), GFP_KERNEL
);
450 kref_init(&data
->kref
);
452 list_add_tail(&data
->list
, &wacom_udev_list
);
455 wacom_wac
->shared
= &data
->shared
;
458 mutex_unlock(&wacom_udev_list_lock
);
462 static void wacom_release_shared_data(struct kref
*kref
)
464 struct wacom_hdev_data
*data
=
465 container_of(kref
, struct wacom_hdev_data
, kref
);
467 mutex_lock(&wacom_udev_list_lock
);
468 list_del(&data
->list
);
469 mutex_unlock(&wacom_udev_list_lock
);
474 static void wacom_remove_shared_data(struct wacom_wac
*wacom
)
476 struct wacom_hdev_data
*data
;
479 data
= container_of(wacom
->shared
, struct wacom_hdev_data
, shared
);
480 kref_put(&data
->kref
, wacom_release_shared_data
);
481 wacom
->shared
= NULL
;
485 static int wacom_led_control(struct wacom
*wacom
)
490 buf
= kzalloc(9, GFP_KERNEL
);
494 if (wacom
->wacom_wac
.features
.type
>= INTUOS5S
&&
495 wacom
->wacom_wac
.features
.type
<= INTUOSPL
) {
497 * Touch Ring and crop mark LED luminance may take on
498 * one of four values:
499 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
501 int ring_led
= wacom
->led
.select
[0] & 0x03;
502 int ring_lum
= (((wacom
->led
.llv
& 0x60) >> 5) - 1) & 0x03;
505 buf
[0] = WAC_CMD_LED_CONTROL
;
506 buf
[1] = (crop_lum
<< 4) | (ring_lum
<< 2) | (ring_led
);
509 int led
= wacom
->led
.select
[0] | 0x4;
511 if (wacom
->wacom_wac
.features
.type
== WACOM_21UX2
||
512 wacom
->wacom_wac
.features
.type
== WACOM_24HD
)
513 led
|= (wacom
->led
.select
[1] << 4) | 0x40;
515 buf
[0] = WAC_CMD_LED_CONTROL
;
517 buf
[2] = wacom
->led
.llv
;
518 buf
[3] = wacom
->led
.hlv
;
519 buf
[4] = wacom
->led
.img_lum
;
522 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
,
523 WAC_CMD_LED_CONTROL
, buf
, 9, WAC_CMD_RETRIES
);
529 static int wacom_led_putimage(struct wacom
*wacom
, int button_id
, const void *img
)
534 buf
= kzalloc(259, GFP_KERNEL
);
538 /* Send 'start' command */
539 buf
[0] = WAC_CMD_ICON_START
;
541 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
,
542 WAC_CMD_ICON_START
, buf
, 2, WAC_CMD_RETRIES
);
546 buf
[0] = WAC_CMD_ICON_XFER
;
547 buf
[1] = button_id
& 0x07;
548 for (i
= 0; i
< 4; i
++) {
550 memcpy(buf
+ 3, img
+ i
* 256, 256);
552 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
,
554 buf
, 259, WAC_CMD_RETRIES
);
560 buf
[0] = WAC_CMD_ICON_START
;
562 wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, WAC_CMD_ICON_START
,
563 buf
, 2, WAC_CMD_RETRIES
);
570 static ssize_t
wacom_led_select_store(struct device
*dev
, int set_id
,
571 const char *buf
, size_t count
)
573 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
574 struct wacom
*wacom
= hid_get_drvdata(hdev
);
578 err
= kstrtouint(buf
, 10, &id
);
582 mutex_lock(&wacom
->lock
);
584 wacom
->led
.select
[set_id
] = id
& 0x3;
585 err
= wacom_led_control(wacom
);
587 mutex_unlock(&wacom
->lock
);
589 return err
< 0 ? err
: count
;
592 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
593 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
594 struct device_attribute *attr, const char *buf, size_t count) \
596 return wacom_led_select_store(dev, SET_ID, buf, count); \
598 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
599 struct device_attribute *attr, char *buf) \
601 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
602 struct wacom *wacom = hid_get_drvdata(hdev); \
603 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
605 static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
606 wacom_led##SET_ID##_select_show, \
607 wacom_led##SET_ID##_select_store)
609 DEVICE_LED_SELECT_ATTR(0);
610 DEVICE_LED_SELECT_ATTR(1);
612 static ssize_t
wacom_luminance_store(struct wacom
*wacom
, u8
*dest
,
613 const char *buf
, size_t count
)
618 err
= kstrtouint(buf
, 10, &value
);
622 mutex_lock(&wacom
->lock
);
624 *dest
= value
& 0x7f;
625 err
= wacom_led_control(wacom
);
627 mutex_unlock(&wacom
->lock
);
629 return err
< 0 ? err
: count
;
632 #define DEVICE_LUMINANCE_ATTR(name, field) \
633 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
634 struct device_attribute *attr, const char *buf, size_t count) \
636 struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
637 struct wacom *wacom = hid_get_drvdata(hdev); \
639 return wacom_luminance_store(wacom, &wacom->led.field, \
642 static DEVICE_ATTR(name##_luminance, S_IWUSR, \
643 NULL, wacom_##name##_luminance_store)
645 DEVICE_LUMINANCE_ATTR(status0
, llv
);
646 DEVICE_LUMINANCE_ATTR(status1
, hlv
);
647 DEVICE_LUMINANCE_ATTR(buttons
, img_lum
);
649 static ssize_t
wacom_button_image_store(struct device
*dev
, int button_id
,
650 const char *buf
, size_t count
)
652 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
653 struct wacom
*wacom
= hid_get_drvdata(hdev
);
659 mutex_lock(&wacom
->lock
);
661 err
= wacom_led_putimage(wacom
, button_id
, buf
);
663 mutex_unlock(&wacom
->lock
);
665 return err
< 0 ? err
: count
;
668 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
669 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
670 struct device_attribute *attr, const char *buf, size_t count) \
672 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
674 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
675 NULL, wacom_btnimg##BUTTON_ID##_store)
677 DEVICE_BTNIMG_ATTR(0);
678 DEVICE_BTNIMG_ATTR(1);
679 DEVICE_BTNIMG_ATTR(2);
680 DEVICE_BTNIMG_ATTR(3);
681 DEVICE_BTNIMG_ATTR(4);
682 DEVICE_BTNIMG_ATTR(5);
683 DEVICE_BTNIMG_ATTR(6);
684 DEVICE_BTNIMG_ATTR(7);
686 static struct attribute
*cintiq_led_attrs
[] = {
687 &dev_attr_status_led0_select
.attr
,
688 &dev_attr_status_led1_select
.attr
,
692 static struct attribute_group cintiq_led_attr_group
= {
694 .attrs
= cintiq_led_attrs
,
697 static struct attribute
*intuos4_led_attrs
[] = {
698 &dev_attr_status0_luminance
.attr
,
699 &dev_attr_status1_luminance
.attr
,
700 &dev_attr_status_led0_select
.attr
,
701 &dev_attr_buttons_luminance
.attr
,
702 &dev_attr_button0_rawimg
.attr
,
703 &dev_attr_button1_rawimg
.attr
,
704 &dev_attr_button2_rawimg
.attr
,
705 &dev_attr_button3_rawimg
.attr
,
706 &dev_attr_button4_rawimg
.attr
,
707 &dev_attr_button5_rawimg
.attr
,
708 &dev_attr_button6_rawimg
.attr
,
709 &dev_attr_button7_rawimg
.attr
,
713 static struct attribute_group intuos4_led_attr_group
= {
715 .attrs
= intuos4_led_attrs
,
718 static struct attribute
*intuos5_led_attrs
[] = {
719 &dev_attr_status0_luminance
.attr
,
720 &dev_attr_status_led0_select
.attr
,
724 static struct attribute_group intuos5_led_attr_group
= {
726 .attrs
= intuos5_led_attrs
,
729 static int wacom_initialize_leds(struct wacom
*wacom
)
733 /* Initialize default values */
734 switch (wacom
->wacom_wac
.features
.type
) {
739 wacom
->led
.select
[0] = 0;
740 wacom
->led
.select
[1] = 0;
743 wacom
->led
.img_lum
= 10;
744 error
= sysfs_create_group(&wacom
->hdev
->dev
.kobj
,
745 &intuos4_led_attr_group
);
750 wacom
->led
.select
[0] = 0;
751 wacom
->led
.select
[1] = 0;
754 wacom
->led
.img_lum
= 0;
756 error
= sysfs_create_group(&wacom
->hdev
->dev
.kobj
,
757 &cintiq_led_attr_group
);
766 if (wacom
->wacom_wac
.features
.device_type
== BTN_TOOL_PEN
) {
767 wacom
->led
.select
[0] = 0;
768 wacom
->led
.select
[1] = 0;
771 wacom
->led
.img_lum
= 0;
773 error
= sysfs_create_group(&wacom
->hdev
->dev
.kobj
,
774 &intuos5_led_attr_group
);
785 "cannot create sysfs group err: %d\n", error
);
788 wacom_led_control(wacom
);
789 wacom
->led_initialized
= true;
794 static void wacom_destroy_leds(struct wacom
*wacom
)
796 if (!wacom
->led_initialized
)
799 wacom
->led_initialized
= false;
801 switch (wacom
->wacom_wac
.features
.type
) {
806 sysfs_remove_group(&wacom
->hdev
->dev
.kobj
,
807 &intuos4_led_attr_group
);
812 sysfs_remove_group(&wacom
->hdev
->dev
.kobj
,
813 &cintiq_led_attr_group
);
822 if (wacom
->wacom_wac
.features
.device_type
== BTN_TOOL_PEN
)
823 sysfs_remove_group(&wacom
->hdev
->dev
.kobj
,
824 &intuos5_led_attr_group
);
829 static enum power_supply_property wacom_battery_props
[] = {
830 POWER_SUPPLY_PROP_STATUS
,
831 POWER_SUPPLY_PROP_SCOPE
,
832 POWER_SUPPLY_PROP_CAPACITY
835 static enum power_supply_property wacom_ac_props
[] = {
836 POWER_SUPPLY_PROP_PRESENT
,
837 POWER_SUPPLY_PROP_ONLINE
,
838 POWER_SUPPLY_PROP_SCOPE
,
841 static int wacom_battery_get_property(struct power_supply
*psy
,
842 enum power_supply_property psp
,
843 union power_supply_propval
*val
)
845 struct wacom
*wacom
= container_of(psy
, struct wacom
, battery
);
849 case POWER_SUPPLY_PROP_SCOPE
:
850 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
852 case POWER_SUPPLY_PROP_CAPACITY
:
854 wacom
->wacom_wac
.battery_capacity
;
856 case POWER_SUPPLY_PROP_STATUS
:
857 if (wacom
->wacom_wac
.bat_charging
)
858 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
859 else if (wacom
->wacom_wac
.battery_capacity
== 100 &&
860 wacom
->wacom_wac
.ps_connected
)
861 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
863 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
873 static int wacom_ac_get_property(struct power_supply
*psy
,
874 enum power_supply_property psp
,
875 union power_supply_propval
*val
)
877 struct wacom
*wacom
= container_of(psy
, struct wacom
, ac
);
881 case POWER_SUPPLY_PROP_PRESENT
:
883 case POWER_SUPPLY_PROP_ONLINE
:
884 val
->intval
= wacom
->wacom_wac
.ps_connected
;
886 case POWER_SUPPLY_PROP_SCOPE
:
887 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
896 static int wacom_initialize_battery(struct wacom
*wacom
)
898 static atomic_t battery_no
= ATOMIC_INIT(0);
902 if (wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
) {
903 n
= atomic_inc_return(&battery_no
) - 1;
905 wacom
->battery
.properties
= wacom_battery_props
;
906 wacom
->battery
.num_properties
= ARRAY_SIZE(wacom_battery_props
);
907 wacom
->battery
.get_property
= wacom_battery_get_property
;
908 sprintf(wacom
->wacom_wac
.bat_name
, "wacom_battery_%ld", n
);
909 wacom
->battery
.name
= wacom
->wacom_wac
.bat_name
;
910 wacom
->battery
.type
= POWER_SUPPLY_TYPE_BATTERY
;
911 wacom
->battery
.use_for_apm
= 0;
913 wacom
->ac
.properties
= wacom_ac_props
;
914 wacom
->ac
.num_properties
= ARRAY_SIZE(wacom_ac_props
);
915 wacom
->ac
.get_property
= wacom_ac_get_property
;
916 sprintf(wacom
->wacom_wac
.ac_name
, "wacom_ac_%ld", n
);
917 wacom
->ac
.name
= wacom
->wacom_wac
.ac_name
;
918 wacom
->ac
.type
= POWER_SUPPLY_TYPE_MAINS
;
919 wacom
->ac
.use_for_apm
= 0;
921 error
= power_supply_register(&wacom
->hdev
->dev
,
926 power_supply_powers(&wacom
->battery
, &wacom
->hdev
->dev
);
928 error
= power_supply_register(&wacom
->hdev
->dev
, &wacom
->ac
);
930 power_supply_unregister(&wacom
->battery
);
934 power_supply_powers(&wacom
->ac
, &wacom
->hdev
->dev
);
940 static void wacom_destroy_battery(struct wacom
*wacom
)
942 if ((wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
) &&
943 wacom
->battery
.dev
) {
944 power_supply_unregister(&wacom
->battery
);
945 wacom
->battery
.dev
= NULL
;
946 power_supply_unregister(&wacom
->ac
);
947 wacom
->ac
.dev
= NULL
;
951 static ssize_t
wacom_show_speed(struct device
*dev
,
952 struct device_attribute
955 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
956 struct wacom
*wacom
= hid_get_drvdata(hdev
);
958 return snprintf(buf
, PAGE_SIZE
, "%i\n", wacom
->wacom_wac
.bt_high_speed
);
961 static ssize_t
wacom_store_speed(struct device
*dev
,
962 struct device_attribute
*attr
,
963 const char *buf
, size_t count
)
965 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
966 struct wacom
*wacom
= hid_get_drvdata(hdev
);
969 if (kstrtou8(buf
, 0, &new_speed
))
972 if (new_speed
!= 0 && new_speed
!= 1)
975 wacom_bt_query_tablet_data(hdev
, new_speed
, &wacom
->wacom_wac
.features
);
980 static DEVICE_ATTR(speed
, S_IRUGO
| S_IWUSR
| S_IWGRP
,
981 wacom_show_speed
, wacom_store_speed
);
983 static struct input_dev
*wacom_allocate_input(struct wacom
*wacom
)
985 struct input_dev
*input_dev
;
986 struct hid_device
*hdev
= wacom
->hdev
;
987 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
989 input_dev
= input_allocate_device();
993 input_dev
->name
= wacom_wac
->name
;
994 input_dev
->phys
= hdev
->phys
;
995 input_dev
->dev
.parent
= &hdev
->dev
;
996 input_dev
->open
= wacom_open
;
997 input_dev
->close
= wacom_close
;
998 input_dev
->uniq
= hdev
->uniq
;
999 input_dev
->id
.bustype
= hdev
->bus
;
1000 input_dev
->id
.vendor
= hdev
->vendor
;
1001 input_dev
->id
.product
= hdev
->product
;
1002 input_dev
->id
.version
= hdev
->version
;
1003 input_set_drvdata(input_dev
, wacom
);
1008 static void wacom_unregister_inputs(struct wacom
*wacom
)
1010 if (wacom
->wacom_wac
.input
)
1011 input_unregister_device(wacom
->wacom_wac
.input
);
1012 if (wacom
->wacom_wac
.pad_input
)
1013 input_unregister_device(wacom
->wacom_wac
.pad_input
);
1014 wacom
->wacom_wac
.input
= NULL
;
1015 wacom
->wacom_wac
.pad_input
= NULL
;
1018 static int wacom_register_inputs(struct wacom
*wacom
)
1020 struct input_dev
*input_dev
, *pad_input_dev
;
1021 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1024 input_dev
= wacom_allocate_input(wacom
);
1025 pad_input_dev
= wacom_allocate_input(wacom
);
1026 if (!input_dev
|| !pad_input_dev
) {
1031 wacom_wac
->input
= input_dev
;
1032 wacom_wac
->pad_input
= pad_input_dev
;
1033 wacom_wac
->pad_input
->name
= wacom_wac
->pad_name
;
1035 error
= wacom_setup_input_capabilities(input_dev
, wacom_wac
);
1039 error
= input_register_device(input_dev
);
1043 error
= wacom_setup_pad_input_capabilities(pad_input_dev
, wacom_wac
);
1045 /* no pad in use on this interface */
1046 input_free_device(pad_input_dev
);
1047 wacom_wac
->pad_input
= NULL
;
1048 pad_input_dev
= NULL
;
1050 error
= input_register_device(pad_input_dev
);
1058 input_unregister_device(input_dev
);
1061 wacom_wac
->input
= NULL
;
1062 wacom_wac
->pad_input
= NULL
;
1065 input_free_device(input_dev
);
1067 input_free_device(pad_input_dev
);
1071 static void wacom_wireless_work(struct work_struct
*work
)
1073 struct wacom
*wacom
= container_of(work
, struct wacom
, work
);
1074 struct usb_device
*usbdev
= wacom
->usbdev
;
1075 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
1076 struct hid_device
*hdev1
, *hdev2
;
1077 struct wacom
*wacom1
, *wacom2
;
1078 struct wacom_wac
*wacom_wac1
, *wacom_wac2
;
1082 * Regardless if this is a disconnect or a new tablet,
1083 * remove any existing input and battery devices.
1086 wacom_destroy_battery(wacom
);
1088 /* Stylus interface */
1089 hdev1
= usb_get_intfdata(usbdev
->config
->interface
[1]);
1090 wacom1
= hid_get_drvdata(hdev1
);
1091 wacom_wac1
= &(wacom1
->wacom_wac
);
1092 wacom_unregister_inputs(wacom1
);
1094 /* Touch interface */
1095 hdev2
= usb_get_intfdata(usbdev
->config
->interface
[2]);
1096 wacom2
= hid_get_drvdata(hdev2
);
1097 wacom_wac2
= &(wacom2
->wacom_wac
);
1098 wacom_unregister_inputs(wacom2
);
1100 if (wacom_wac
->pid
== 0) {
1101 hid_info(wacom
->hdev
, "wireless tablet disconnected\n");
1102 wacom_wac1
->shared
->type
= 0;
1104 const struct hid_device_id
*id
= wacom_ids
;
1106 hid_info(wacom
->hdev
, "wireless tablet connected with PID %x\n",
1110 if (id
->vendor
== USB_VENDOR_ID_WACOM
&&
1111 id
->product
== wacom_wac
->pid
)
1117 hid_info(wacom
->hdev
, "ignoring unknown PID.\n");
1121 /* Stylus interface */
1122 wacom_wac1
->features
=
1123 *((struct wacom_features
*)id
->driver_data
);
1124 wacom_wac1
->features
.device_type
= BTN_TOOL_PEN
;
1125 snprintf(wacom_wac1
->name
, WACOM_NAME_MAX
, "%s (WL) Pen",
1126 wacom_wac1
->features
.name
);
1127 snprintf(wacom_wac1
->pad_name
, WACOM_NAME_MAX
, "%s (WL) Pad",
1128 wacom_wac1
->features
.name
);
1129 wacom_wac1
->shared
->touch_max
= wacom_wac1
->features
.touch_max
;
1130 wacom_wac1
->shared
->type
= wacom_wac1
->features
.type
;
1131 error
= wacom_register_inputs(wacom1
);
1135 /* Touch interface */
1136 if (wacom_wac1
->features
.touch_max
||
1137 wacom_wac1
->features
.type
== INTUOSHT
) {
1138 wacom_wac2
->features
=
1139 *((struct wacom_features
*)id
->driver_data
);
1140 wacom_wac2
->features
.pktlen
= WACOM_PKGLEN_BBTOUCH3
;
1141 wacom_wac2
->features
.device_type
= BTN_TOOL_FINGER
;
1142 wacom_wac2
->features
.x_max
= wacom_wac2
->features
.y_max
= 4096;
1143 if (wacom_wac2
->features
.touch_max
)
1144 snprintf(wacom_wac2
->name
, WACOM_NAME_MAX
,
1145 "%s (WL) Finger",wacom_wac2
->features
.name
);
1147 snprintf(wacom_wac2
->name
, WACOM_NAME_MAX
,
1148 "%s (WL) Pad",wacom_wac2
->features
.name
);
1149 snprintf(wacom_wac2
->pad_name
, WACOM_NAME_MAX
,
1150 "%s (WL) Pad", wacom_wac2
->features
.name
);
1151 error
= wacom_register_inputs(wacom2
);
1155 if (wacom_wac1
->features
.type
== INTUOSHT
&&
1156 wacom_wac1
->features
.touch_max
)
1157 wacom_wac
->shared
->touch_input
= wacom_wac2
->input
;
1160 error
= wacom_initialize_battery(wacom
);
1168 wacom_unregister_inputs(wacom1
);
1169 wacom_unregister_inputs(wacom2
);
1174 * Not all devices report physical dimensions from HID.
1175 * Compute the default from hardcoded logical dimension
1176 * and resolution before driver overwrites them.
1178 static void wacom_set_default_phy(struct wacom_features
*features
)
1180 if (features
->x_resolution
) {
1181 features
->x_phy
= (features
->x_max
* 100) /
1182 features
->x_resolution
;
1183 features
->y_phy
= (features
->y_max
* 100) /
1184 features
->y_resolution
;
1188 static void wacom_calculate_res(struct wacom_features
*features
)
1190 features
->x_resolution
= wacom_calc_hid_res(features
->x_max
,
1193 features
->unitExpo
);
1194 features
->y_resolution
= wacom_calc_hid_res(features
->y_max
,
1197 features
->unitExpo
);
1200 static int wacom_hid_report_len(struct hid_report
*report
)
1202 /* equivalent to DIV_ROUND_UP(report->size, 8) + !!(report->id > 0) */
1203 return ((report
->size
- 1) >> 3) + 1 + (report
->id
> 0);
1206 static size_t wacom_compute_pktlen(struct hid_device
*hdev
)
1208 struct hid_report_enum
*report_enum
;
1209 struct hid_report
*report
;
1212 report_enum
= hdev
->report_enum
+ HID_INPUT_REPORT
;
1214 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
1215 size_t report_size
= wacom_hid_report_len(report
);
1216 if (report_size
> size
)
1223 static int wacom_probe(struct hid_device
*hdev
,
1224 const struct hid_device_id
*id
)
1226 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
1227 struct usb_device
*dev
= interface_to_usbdev(intf
);
1228 struct wacom
*wacom
;
1229 struct wacom_wac
*wacom_wac
;
1230 struct wacom_features
*features
;
1233 if (!id
->driver_data
)
1236 wacom
= kzalloc(sizeof(struct wacom
), GFP_KERNEL
);
1240 hid_set_drvdata(hdev
, wacom
);
1243 /* ask for the report descriptor to be loaded by HID */
1244 error
= hid_parse(hdev
);
1246 hid_err(hdev
, "parse failed\n");
1250 wacom_wac
= &wacom
->wacom_wac
;
1251 wacom_wac
->features
= *((struct wacom_features
*)id
->driver_data
);
1252 features
= &wacom_wac
->features
;
1253 features
->pktlen
= wacom_compute_pktlen(hdev
);
1254 if (features
->pktlen
> WACOM_PKGLEN_MAX
) {
1259 if (features
->check_for_hid_type
&& features
->hid_type
!= hdev
->type
) {
1264 wacom
->usbdev
= dev
;
1266 mutex_init(&wacom
->lock
);
1267 INIT_WORK(&wacom
->work
, wacom_wireless_work
);
1269 /* set the default size in case we do not get them from hid */
1270 wacom_set_default_phy(features
);
1272 /* Retrieve the physical and logical size for touch devices */
1273 wacom_retrieve_hid_descriptor(hdev
, features
);
1276 * Intuos5 has no useful data about its touch interface in its
1277 * HID descriptor. If this is the touch interface (PacketSize
1278 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1280 if (features
->type
>= INTUOS5S
&& features
->type
<= INTUOSHT
) {
1281 if (features
->pktlen
== WACOM_PKGLEN_BBTOUCH3
) {
1282 features
->device_type
= BTN_TOOL_FINGER
;
1284 features
->x_max
= 4096;
1285 features
->y_max
= 4096;
1287 features
->device_type
= BTN_TOOL_PEN
;
1292 * Same thing for Bamboo 3rd gen.
1294 if ((features
->type
== BAMBOO_PT
) &&
1295 (features
->pktlen
== WACOM_PKGLEN_BBTOUCH3
) &&
1296 (features
->device_type
== BTN_TOOL_PEN
)) {
1297 features
->device_type
= BTN_TOOL_FINGER
;
1299 features
->x_max
= 4096;
1300 features
->y_max
= 4096;
1303 if (hdev
->bus
== BUS_BLUETOOTH
)
1304 features
->quirks
|= WACOM_QUIRK_BATTERY
;
1306 wacom_setup_device_quirks(features
);
1308 /* set unit to "100th of a mm" for devices not reported by HID */
1309 if (!features
->unit
) {
1310 features
->unit
= 0x11;
1311 features
->unitExpo
= -3;
1313 wacom_calculate_res(features
);
1315 strlcpy(wacom_wac
->name
, features
->name
, sizeof(wacom_wac
->name
));
1316 snprintf(wacom_wac
->pad_name
, sizeof(wacom_wac
->pad_name
),
1317 "%s Pad", features
->name
);
1319 if (features
->quirks
& WACOM_QUIRK_MULTI_INPUT
) {
1320 /* Append the device type to the name */
1321 if (features
->device_type
!= BTN_TOOL_FINGER
)
1322 strlcat(wacom_wac
->name
, " Pen", WACOM_NAME_MAX
);
1323 else if (features
->touch_max
)
1324 strlcat(wacom_wac
->name
, " Finger", WACOM_NAME_MAX
);
1326 strlcat(wacom_wac
->name
, " Pad", WACOM_NAME_MAX
);
1328 error
= wacom_add_shared_data(hdev
);
1333 error
= wacom_initialize_leds(wacom
);
1337 if (!(features
->quirks
& WACOM_QUIRK_MONITOR
) &&
1338 (features
->quirks
& WACOM_QUIRK_BATTERY
)) {
1339 error
= wacom_initialize_battery(wacom
);
1344 if (!(features
->quirks
& WACOM_QUIRK_NO_INPUT
)) {
1345 error
= wacom_register_inputs(wacom
);
1350 if (hdev
->bus
== BUS_BLUETOOTH
) {
1351 error
= device_create_file(&hdev
->dev
, &dev_attr_speed
);
1354 "can't create sysfs speed attribute err: %d\n",
1358 /* Note that if query fails it is not a hard failure */
1359 wacom_query_tablet_data(hdev
, features
);
1361 /* Regular HID work starts now */
1362 error
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
1364 hid_err(hdev
, "hw start failed\n");
1368 if (features
->quirks
& WACOM_QUIRK_MONITOR
)
1369 error
= hid_hw_open(hdev
);
1371 if (wacom_wac
->features
.type
== INTUOSHT
&& wacom_wac
->features
.touch_max
) {
1372 if (wacom_wac
->features
.device_type
== BTN_TOOL_FINGER
)
1373 wacom_wac
->shared
->touch_input
= wacom_wac
->input
;
1378 fail5
: if (hdev
->bus
== BUS_BLUETOOTH
)
1379 device_remove_file(&hdev
->dev
, &dev_attr_speed
);
1380 wacom_unregister_inputs(wacom
);
1381 fail4
: wacom_destroy_battery(wacom
);
1382 fail3
: wacom_destroy_leds(wacom
);
1383 fail2
: wacom_remove_shared_data(wacom_wac
);
1384 fail1
: kfree(wacom
);
1385 hid_set_drvdata(hdev
, NULL
);
1389 static void wacom_remove(struct hid_device
*hdev
)
1391 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1395 cancel_work_sync(&wacom
->work
);
1396 wacom_unregister_inputs(wacom
);
1397 if (hdev
->bus
== BUS_BLUETOOTH
)
1398 device_remove_file(&hdev
->dev
, &dev_attr_speed
);
1399 wacom_destroy_battery(wacom
);
1400 wacom_destroy_leds(wacom
);
1401 wacom_remove_shared_data(&wacom
->wacom_wac
);
1403 hid_set_drvdata(hdev
, NULL
);
1407 static int wacom_resume(struct hid_device
*hdev
)
1409 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1410 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
1412 mutex_lock(&wacom
->lock
);
1414 /* switch to wacom mode first */
1415 wacom_query_tablet_data(hdev
, features
);
1416 wacom_led_control(wacom
);
1418 mutex_unlock(&wacom
->lock
);
1423 static int wacom_reset_resume(struct hid_device
*hdev
)
1425 return wacom_resume(hdev
);
1428 static struct hid_driver wacom_driver
= {
1430 .id_table
= wacom_ids
,
1431 .probe
= wacom_probe
,
1432 .remove
= wacom_remove
,
1434 .resume
= wacom_resume
,
1435 .reset_resume
= wacom_reset_resume
,
1437 .raw_event
= wacom_raw_event
,
1439 module_hid_driver(wacom_driver
);