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 /* defines to get HID report descriptor */
18 #define HID_DEVICET_HID (USB_TYPE_CLASS | 0x01)
19 #define HID_DEVICET_REPORT (USB_TYPE_CLASS | 0x02)
20 #define HID_USAGE_UNDEFINED 0x00
21 #define HID_USAGE_PAGE 0x05
22 #define HID_USAGE_PAGE_DIGITIZER 0x0d
23 #define HID_USAGE_PAGE_DESKTOP 0x01
24 #define HID_USAGE 0x09
25 #define HID_USAGE_X ((HID_USAGE_PAGE_DESKTOP << 16) | 0x30)
26 #define HID_USAGE_Y ((HID_USAGE_PAGE_DESKTOP << 16) | 0x31)
27 #define HID_USAGE_PRESSURE ((HID_USAGE_PAGE_DIGITIZER << 16) | 0x30)
28 #define HID_USAGE_X_TILT ((HID_USAGE_PAGE_DIGITIZER << 16) | 0x3d)
29 #define HID_USAGE_Y_TILT ((HID_USAGE_PAGE_DIGITIZER << 16) | 0x3e)
30 #define HID_USAGE_FINGER ((HID_USAGE_PAGE_DIGITIZER << 16) | 0x22)
31 #define HID_USAGE_STYLUS ((HID_USAGE_PAGE_DIGITIZER << 16) | 0x20)
32 #define HID_USAGE_CONTACTMAX ((HID_USAGE_PAGE_DIGITIZER << 16) | 0x55)
33 #define HID_COLLECTION 0xa1
34 #define HID_COLLECTION_LOGICAL 0x02
35 #define HID_COLLECTION_END 0xc0
37 struct hid_descriptor
{
38 struct usb_descriptor_header header
;
43 __le16 wDescriptorLength
;
44 } __attribute__ ((packed
));
46 /* defines to get/set USB message */
47 #define USB_REQ_GET_REPORT 0x01
48 #define USB_REQ_SET_REPORT 0x09
50 #define WAC_HID_FEATURE_REPORT 0x03
51 #define WAC_MSG_RETRIES 5
53 #define WAC_CMD_LED_CONTROL 0x20
54 #define WAC_CMD_ICON_START 0x21
55 #define WAC_CMD_ICON_XFER 0x23
56 #define WAC_CMD_RETRIES 10
58 static int wacom_get_report(struct usb_interface
*intf
, u8 type
, u8 id
,
59 void *buf
, size_t size
, unsigned int retries
)
61 struct usb_device
*dev
= interface_to_usbdev(intf
);
65 retval
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
67 USB_DIR_IN
| USB_TYPE_CLASS
|
70 intf
->altsetting
[0].desc
.bInterfaceNumber
,
72 } while ((retval
== -ETIMEDOUT
|| retval
== -EPIPE
) && --retries
);
77 static int wacom_set_report(struct usb_interface
*intf
, u8 type
, u8 id
,
78 void *buf
, size_t size
, unsigned int retries
)
80 struct usb_device
*dev
= interface_to_usbdev(intf
);
84 retval
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
86 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
88 intf
->altsetting
[0].desc
.bInterfaceNumber
,
90 } while ((retval
== -ETIMEDOUT
|| retval
== -EPIPE
) && --retries
);
95 static void wacom_sys_irq(struct urb
*urb
)
97 struct wacom
*wacom
= urb
->context
;
98 struct device
*dev
= &wacom
->intf
->dev
;
101 switch (urb
->status
) {
108 /* this urb is terminated, clean up */
109 dev_dbg(dev
, "%s - urb shutting down with status: %d\n",
110 __func__
, urb
->status
);
113 dev_dbg(dev
, "%s - nonzero urb status received: %d\n",
114 __func__
, urb
->status
);
118 wacom_wac_irq(&wacom
->wacom_wac
, urb
->actual_length
);
121 usb_mark_last_busy(wacom
->usbdev
);
122 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
124 dev_err(dev
, "%s - usb_submit_urb failed with result %d\n",
128 static int wacom_open(struct input_dev
*dev
)
130 struct wacom
*wacom
= input_get_drvdata(dev
);
133 if (usb_autopm_get_interface(wacom
->intf
) < 0)
136 mutex_lock(&wacom
->lock
);
138 if (usb_submit_urb(wacom
->irq
, GFP_KERNEL
)) {
144 wacom
->intf
->needs_remote_wakeup
= 1;
147 mutex_unlock(&wacom
->lock
);
148 usb_autopm_put_interface(wacom
->intf
);
152 static void wacom_close(struct input_dev
*dev
)
154 struct wacom
*wacom
= input_get_drvdata(dev
);
157 autopm_error
= usb_autopm_get_interface(wacom
->intf
);
159 mutex_lock(&wacom
->lock
);
160 usb_kill_urb(wacom
->irq
);
162 wacom
->intf
->needs_remote_wakeup
= 0;
163 mutex_unlock(&wacom
->lock
);
166 usb_autopm_put_interface(wacom
->intf
);
170 * Calculate the resolution of the X or Y axis, given appropriate HID data.
171 * This function is little more than hidinput_calc_abs_res stripped down.
173 static int wacom_calc_hid_res(int logical_extents
, int physical_extents
,
174 unsigned char unit
, unsigned char exponent
)
176 int prev
, unit_exponent
;
178 /* Check if the extents are sane */
179 if (logical_extents
<= 0 || physical_extents
<= 0)
182 /* Get signed value of nybble-sized twos-compliment exponent */
183 unit_exponent
= exponent
;
184 if (unit_exponent
> 7)
187 /* Convert physical_extents to millimeters */
188 if (unit
== 0x11) { /* If centimeters */
190 } else if (unit
== 0x13) { /* If inches */
191 prev
= physical_extents
;
192 physical_extents
*= 254;
193 if (physical_extents
< prev
)
200 /* Apply negative unit exponent */
201 for (; unit_exponent
< 0; unit_exponent
++) {
202 prev
= logical_extents
;
203 logical_extents
*= 10;
204 if (logical_extents
< prev
)
207 /* Apply positive unit exponent */
208 for (; unit_exponent
> 0; unit_exponent
--) {
209 prev
= physical_extents
;
210 physical_extents
*= 10;
211 if (physical_extents
< prev
)
215 /* Calculate resolution */
216 return logical_extents
/ physical_extents
;
219 static int wacom_parse_logical_collection(unsigned char *report
,
220 struct wacom_features
*features
)
224 if (features
->type
== BAMBOO_PT
) {
226 /* Logical collection is only used by 3rd gen Bamboo Touch */
227 features
->pktlen
= WACOM_PKGLEN_BBTOUCH3
;
228 features
->device_type
= BTN_TOOL_FINGER
;
230 features
->x_max
= features
->y_max
=
231 get_unaligned_le16(&report
[10]);
238 static void wacom_retrieve_report_data(struct usb_interface
*intf
,
239 struct wacom_features
*features
)
242 unsigned char *rep_data
;
244 rep_data
= kmalloc(2, GFP_KERNEL
);
248 result
= wacom_get_report(intf
, WAC_HID_FEATURE_REPORT
,
249 rep_data
[0], rep_data
, 2,
252 if (result
>= 0 && rep_data
[1] > 2)
253 features
->touch_max
= rep_data
[1];
260 * Interface Descriptor of wacom devices can be incomplete and
261 * inconsistent so wacom_features table is used to store stylus
262 * device's packet lengths, various maximum values, and tablet
263 * resolution based on product ID's.
265 * For devices that contain 2 interfaces, wacom_features table is
266 * inaccurate for the touch interface. Since the Interface Descriptor
267 * for touch interfaces has pretty complete data, this function exists
268 * to query tablet for this missing information instead of hard coding in
269 * an additional table.
271 * A typical Interface Descriptor for a stylus will contain a
272 * boot mouse application collection that is not of interest and this
273 * function will ignore it.
275 * It also contains a digitizer application collection that also is not
276 * of interest since any information it contains would be duplicate
277 * of what is in wacom_features. Usually it defines a report of an array
278 * of bytes that could be used as max length of the stylus packet returned.
279 * If it happens to define a Digitizer-Stylus Physical Collection then
280 * the X and Y logical values contain valid data but it is ignored.
282 * A typical Interface Descriptor for a touch interface will contain a
283 * Digitizer-Finger Physical Collection which will define both logical
284 * X/Y maximum as well as the physical size of tablet. Since touch
285 * interfaces haven't supported pressure or distance, this is enough
286 * information to override invalid values in the wacom_features table.
288 * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical
289 * Collection. Instead they define a Logical Collection with a single
290 * Logical Maximum for both X and Y.
292 * Intuos5 touch interface does not contain useful data. We deal with
293 * this after returning from this function.
295 static int wacom_parse_hid(struct usb_interface
*intf
,
296 struct hid_descriptor
*hid_desc
,
297 struct wacom_features
*features
)
299 struct usb_device
*dev
= interface_to_usbdev(intf
);
301 /* result has to be defined as int for some devices */
302 int result
= 0, touch_max
= 0;
303 int i
= 0, page
= 0, finger
= 0, pen
= 0;
304 unsigned char *report
;
306 report
= kzalloc(hid_desc
->wDescriptorLength
, GFP_KERNEL
);
310 /* retrive report descriptors */
312 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
313 USB_REQ_GET_DESCRIPTOR
,
314 USB_RECIP_INTERFACE
| USB_DIR_IN
,
315 HID_DEVICET_REPORT
<< 8,
316 intf
->altsetting
[0].desc
.bInterfaceNumber
, /* interface */
318 hid_desc
->wDescriptorLength
,
320 } while (result
< 0 && limit
++ < WAC_MSG_RETRIES
);
322 /* No need to parse the Descriptor. It isn't an error though */
326 for (i
= 0; i
< hid_desc
->wDescriptorLength
; i
++) {
330 page
= report
[i
+ 1];
335 switch (page
<< 16 | report
[i
+ 1]) {
338 features
->device_type
= BTN_TOOL_FINGER
;
339 /* touch device at least supports one touch point */
341 switch (features
->type
) {
343 features
->pktlen
= WACOM_PKGLEN_TPC2FG
;
348 features
->pktlen
= WACOM_PKGLEN_MTOUCH
;
353 features
->pktlen
= WACOM_PKGLEN_MTTPC
;
357 features
->pktlen
= WACOM_PKGLEN_BBTOUCH
;
361 features
->pktlen
= WACOM_PKGLEN_GRAPHIRE
;
365 switch (features
->type
) {
368 get_unaligned_le16(&report
[i
+ 5]);
370 get_unaligned_le16(&report
[i
+ 8]);
376 get_unaligned_le16(&report
[i
+ 3]);
378 get_unaligned_le16(&report
[i
+ 8]);
379 features
->unit
= report
[i
- 1];
380 features
->unitExpo
= report
[i
- 3];
386 get_unaligned_le16(&report
[i
+ 3]);
388 get_unaligned_le16(&report
[i
+ 6]);
389 features
->unit
= report
[i
- 5];
390 features
->unitExpo
= report
[i
- 3];
396 get_unaligned_le16(&report
[i
+ 3]);
398 get_unaligned_le16(&report
[i
+ 6]);
399 features
->unit
= report
[i
+ 9];
400 features
->unitExpo
= report
[i
+ 11];
405 /* penabled only accepts exact bytes of data */
406 if (features
->type
>= TABLETPC
)
407 features
->pktlen
= WACOM_PKGLEN_GRAPHIRE
;
408 features
->device_type
= BTN_TOOL_PEN
;
410 get_unaligned_le16(&report
[i
+ 3]);
417 switch (features
->type
) {
422 get_unaligned_le16(&report
[i
+ 3]);
424 get_unaligned_le16(&report
[i
+ 6]);
430 get_unaligned_le16(&report
[i
+ 3]);
432 get_unaligned_le16(&report
[i
- 2]);
438 get_unaligned_le16(&report
[i
+ 3]);
440 get_unaligned_le16(&report
[i
+ 6]);
446 get_unaligned_le16(&report
[i
+ 3]);
448 get_unaligned_le16(&report
[i
+ 6]);
456 get_unaligned_le16(&report
[i
+ 3]);
462 get_unaligned_le16(&report
[i
+ 3]);
467 case HID_USAGE_FINGER
:
473 * Requiring Stylus Usage will ignore boot mouse
474 * X/Y values and some cases of invalid Digitizer X/Y
475 * values commonly reported.
477 case HID_USAGE_STYLUS
:
482 case HID_USAGE_CONTACTMAX
:
483 /* leave touch_max as is if predefined */
484 if (!features
->touch_max
)
485 wacom_retrieve_report_data(intf
, features
);
489 case HID_USAGE_PRESSURE
:
491 features
->pressure_max
=
492 get_unaligned_le16(&report
[i
+ 3]);
499 case HID_COLLECTION_END
:
500 /* reset UsagePage and Finger */
507 case HID_COLLECTION_LOGICAL
:
508 i
+= wacom_parse_logical_collection(&report
[i
],
517 if (!features
->touch_max
&& touch_max
)
518 features
->touch_max
= touch_max
;
524 static int wacom_set_device_mode(struct usb_interface
*intf
, int report_id
, int length
, int mode
)
526 unsigned char *rep_data
;
527 int error
= -ENOMEM
, limit
= 0;
529 rep_data
= kzalloc(length
, GFP_KERNEL
);
534 rep_data
[0] = report_id
;
537 error
= wacom_set_report(intf
, WAC_HID_FEATURE_REPORT
,
538 report_id
, rep_data
, length
, 1);
540 error
= wacom_get_report(intf
, WAC_HID_FEATURE_REPORT
,
541 report_id
, rep_data
, length
, 1);
542 } while ((error
< 0 || rep_data
[1] != mode
) && limit
++ < WAC_MSG_RETRIES
);
546 return error
< 0 ? error
: 0;
550 * Switch the tablet into its most-capable mode. Wacom tablets are
551 * typically configured to power-up in a mode which sends mouse-like
552 * reports to the OS. To get absolute position, pressure data, etc.
553 * from the tablet, it is necessary to switch the tablet out of this
554 * mode and into one which sends the full range of tablet data.
556 static int wacom_query_tablet_data(struct usb_interface
*intf
, struct wacom_features
*features
)
558 if (features
->device_type
== BTN_TOOL_FINGER
) {
559 if (features
->type
> TABLETPC
) {
560 /* MT Tablet PC touch */
561 return wacom_set_device_mode(intf
, 3, 4, 4);
563 else if (features
->type
== WACOM_24HDT
|| features
->type
== CINTIQ_HYBRID
) {
564 return wacom_set_device_mode(intf
, 18, 3, 2);
566 } else if (features
->device_type
== BTN_TOOL_PEN
) {
567 if (features
->type
<= BAMBOO_PT
&& features
->type
!= WIRELESS
) {
568 return wacom_set_device_mode(intf
, 2, 2, 2);
575 static int wacom_retrieve_hid_descriptor(struct usb_interface
*intf
,
576 struct wacom_features
*features
)
579 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
580 struct hid_descriptor
*hid_desc
;
582 /* default features */
583 features
->device_type
= BTN_TOOL_PEN
;
584 features
->x_fuzz
= 4;
585 features
->y_fuzz
= 4;
586 features
->pressure_fuzz
= 0;
587 features
->distance_fuzz
= 0;
590 * The wireless device HID is basic and layout conflicts with
591 * other tablets (monitor and touch interface can look like pen).
592 * Skip the query for this type and modify defaults based on
595 if (features
->type
== WIRELESS
) {
596 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 0) {
597 features
->device_type
= 0;
598 } else if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 2) {
599 features
->device_type
= BTN_TOOL_FINGER
;
600 features
->pktlen
= WACOM_PKGLEN_BBTOUCH3
;
604 /* only devices that support touch need to retrieve the info */
605 if (features
->type
< BAMBOO_PT
) {
609 error
= usb_get_extra_descriptor(interface
, HID_DEVICET_HID
, &hid_desc
);
611 error
= usb_get_extra_descriptor(&interface
->endpoint
[0],
612 HID_DEVICET_REPORT
, &hid_desc
);
615 "can not retrieve extra class descriptor\n");
619 error
= wacom_parse_hid(intf
, hid_desc
, features
);
625 struct wacom_usbdev_data
{
626 struct list_head list
;
628 struct usb_device
*dev
;
629 struct wacom_shared shared
;
632 static LIST_HEAD(wacom_udev_list
);
633 static DEFINE_MUTEX(wacom_udev_list_lock
);
635 static struct usb_device
*wacom_get_sibling(struct usb_device
*dev
, int vendor
, int product
)
638 struct usb_device
*sibling
;
640 if (vendor
== 0 && product
== 0)
643 if (dev
->parent
== NULL
)
646 usb_hub_for_each_child(dev
->parent
, port1
, sibling
) {
647 struct usb_device_descriptor
*d
;
651 d
= &sibling
->descriptor
;
652 if (d
->idVendor
== vendor
&& d
->idProduct
== product
)
659 static struct wacom_usbdev_data
*wacom_get_usbdev_data(struct usb_device
*dev
)
661 struct wacom_usbdev_data
*data
;
663 list_for_each_entry(data
, &wacom_udev_list
, list
) {
664 if (data
->dev
== dev
) {
665 kref_get(&data
->kref
);
673 static int wacom_add_shared_data(struct wacom_wac
*wacom
,
674 struct usb_device
*dev
)
676 struct wacom_usbdev_data
*data
;
679 mutex_lock(&wacom_udev_list_lock
);
681 data
= wacom_get_usbdev_data(dev
);
683 data
= kzalloc(sizeof(struct wacom_usbdev_data
), GFP_KERNEL
);
689 kref_init(&data
->kref
);
691 list_add_tail(&data
->list
, &wacom_udev_list
);
694 wacom
->shared
= &data
->shared
;
697 mutex_unlock(&wacom_udev_list_lock
);
701 static void wacom_release_shared_data(struct kref
*kref
)
703 struct wacom_usbdev_data
*data
=
704 container_of(kref
, struct wacom_usbdev_data
, kref
);
706 mutex_lock(&wacom_udev_list_lock
);
707 list_del(&data
->list
);
708 mutex_unlock(&wacom_udev_list_lock
);
713 static void wacom_remove_shared_data(struct wacom_wac
*wacom
)
715 struct wacom_usbdev_data
*data
;
718 data
= container_of(wacom
->shared
, struct wacom_usbdev_data
, shared
);
719 kref_put(&data
->kref
, wacom_release_shared_data
);
720 wacom
->shared
= NULL
;
724 static int wacom_led_control(struct wacom
*wacom
)
729 buf
= kzalloc(9, GFP_KERNEL
);
733 if (wacom
->wacom_wac
.features
.type
>= INTUOS5S
&&
734 wacom
->wacom_wac
.features
.type
<= INTUOSPL
) {
736 * Touch Ring and crop mark LED luminance may take on
737 * one of four values:
738 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
740 int ring_led
= wacom
->led
.select
[0] & 0x03;
741 int ring_lum
= (((wacom
->led
.llv
& 0x60) >> 5) - 1) & 0x03;
744 buf
[0] = WAC_CMD_LED_CONTROL
;
745 buf
[1] = (crop_lum
<< 4) | (ring_lum
<< 2) | (ring_led
);
748 int led
= wacom
->led
.select
[0] | 0x4;
750 if (wacom
->wacom_wac
.features
.type
== WACOM_21UX2
||
751 wacom
->wacom_wac
.features
.type
== WACOM_24HD
)
752 led
|= (wacom
->led
.select
[1] << 4) | 0x40;
754 buf
[0] = WAC_CMD_LED_CONTROL
;
756 buf
[2] = wacom
->led
.llv
;
757 buf
[3] = wacom
->led
.hlv
;
758 buf
[4] = wacom
->led
.img_lum
;
761 retval
= wacom_set_report(wacom
->intf
, 0x03, WAC_CMD_LED_CONTROL
,
762 buf
, 9, WAC_CMD_RETRIES
);
768 static int wacom_led_putimage(struct wacom
*wacom
, int button_id
, const void *img
)
773 buf
= kzalloc(259, GFP_KERNEL
);
777 /* Send 'start' command */
778 buf
[0] = WAC_CMD_ICON_START
;
780 retval
= wacom_set_report(wacom
->intf
, 0x03, WAC_CMD_ICON_START
,
781 buf
, 2, WAC_CMD_RETRIES
);
785 buf
[0] = WAC_CMD_ICON_XFER
;
786 buf
[1] = button_id
& 0x07;
787 for (i
= 0; i
< 4; i
++) {
789 memcpy(buf
+ 3, img
+ i
* 256, 256);
791 retval
= wacom_set_report(wacom
->intf
, 0x03, WAC_CMD_ICON_XFER
,
792 buf
, 259, WAC_CMD_RETRIES
);
798 buf
[0] = WAC_CMD_ICON_START
;
800 wacom_set_report(wacom
->intf
, 0x03, WAC_CMD_ICON_START
,
801 buf
, 2, WAC_CMD_RETRIES
);
808 static ssize_t
wacom_led_select_store(struct device
*dev
, int set_id
,
809 const char *buf
, size_t count
)
811 struct wacom
*wacom
= dev_get_drvdata(dev
);
815 err
= kstrtouint(buf
, 10, &id
);
819 mutex_lock(&wacom
->lock
);
821 wacom
->led
.select
[set_id
] = id
& 0x3;
822 err
= wacom_led_control(wacom
);
824 mutex_unlock(&wacom
->lock
);
826 return err
< 0 ? err
: count
;
829 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
830 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
831 struct device_attribute *attr, const char *buf, size_t count) \
833 return wacom_led_select_store(dev, SET_ID, buf, count); \
835 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
836 struct device_attribute *attr, char *buf) \
838 struct wacom *wacom = dev_get_drvdata(dev); \
839 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
841 static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
842 wacom_led##SET_ID##_select_show, \
843 wacom_led##SET_ID##_select_store)
845 DEVICE_LED_SELECT_ATTR(0);
846 DEVICE_LED_SELECT_ATTR(1);
848 static ssize_t
wacom_luminance_store(struct wacom
*wacom
, u8
*dest
,
849 const char *buf
, size_t count
)
854 err
= kstrtouint(buf
, 10, &value
);
858 mutex_lock(&wacom
->lock
);
860 *dest
= value
& 0x7f;
861 err
= wacom_led_control(wacom
);
863 mutex_unlock(&wacom
->lock
);
865 return err
< 0 ? err
: count
;
868 #define DEVICE_LUMINANCE_ATTR(name, field) \
869 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
870 struct device_attribute *attr, const char *buf, size_t count) \
872 struct wacom *wacom = dev_get_drvdata(dev); \
874 return wacom_luminance_store(wacom, &wacom->led.field, \
877 static DEVICE_ATTR(name##_luminance, S_IWUSR, \
878 NULL, wacom_##name##_luminance_store)
880 DEVICE_LUMINANCE_ATTR(status0
, llv
);
881 DEVICE_LUMINANCE_ATTR(status1
, hlv
);
882 DEVICE_LUMINANCE_ATTR(buttons
, img_lum
);
884 static ssize_t
wacom_button_image_store(struct device
*dev
, int button_id
,
885 const char *buf
, size_t count
)
887 struct wacom
*wacom
= dev_get_drvdata(dev
);
893 mutex_lock(&wacom
->lock
);
895 err
= wacom_led_putimage(wacom
, button_id
, buf
);
897 mutex_unlock(&wacom
->lock
);
899 return err
< 0 ? err
: count
;
902 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
903 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
904 struct device_attribute *attr, const char *buf, size_t count) \
906 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
908 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
909 NULL, wacom_btnimg##BUTTON_ID##_store)
911 DEVICE_BTNIMG_ATTR(0);
912 DEVICE_BTNIMG_ATTR(1);
913 DEVICE_BTNIMG_ATTR(2);
914 DEVICE_BTNIMG_ATTR(3);
915 DEVICE_BTNIMG_ATTR(4);
916 DEVICE_BTNIMG_ATTR(5);
917 DEVICE_BTNIMG_ATTR(6);
918 DEVICE_BTNIMG_ATTR(7);
920 static struct attribute
*cintiq_led_attrs
[] = {
921 &dev_attr_status_led0_select
.attr
,
922 &dev_attr_status_led1_select
.attr
,
926 static struct attribute_group cintiq_led_attr_group
= {
928 .attrs
= cintiq_led_attrs
,
931 static struct attribute
*intuos4_led_attrs
[] = {
932 &dev_attr_status0_luminance
.attr
,
933 &dev_attr_status1_luminance
.attr
,
934 &dev_attr_status_led0_select
.attr
,
935 &dev_attr_buttons_luminance
.attr
,
936 &dev_attr_button0_rawimg
.attr
,
937 &dev_attr_button1_rawimg
.attr
,
938 &dev_attr_button2_rawimg
.attr
,
939 &dev_attr_button3_rawimg
.attr
,
940 &dev_attr_button4_rawimg
.attr
,
941 &dev_attr_button5_rawimg
.attr
,
942 &dev_attr_button6_rawimg
.attr
,
943 &dev_attr_button7_rawimg
.attr
,
947 static struct attribute_group intuos4_led_attr_group
= {
949 .attrs
= intuos4_led_attrs
,
952 static struct attribute
*intuos5_led_attrs
[] = {
953 &dev_attr_status0_luminance
.attr
,
954 &dev_attr_status_led0_select
.attr
,
958 static struct attribute_group intuos5_led_attr_group
= {
960 .attrs
= intuos5_led_attrs
,
963 static int wacom_initialize_leds(struct wacom
*wacom
)
967 /* Initialize default values */
968 switch (wacom
->wacom_wac
.features
.type
) {
972 wacom
->led
.select
[0] = 0;
973 wacom
->led
.select
[1] = 0;
976 wacom
->led
.img_lum
= 10;
977 error
= sysfs_create_group(&wacom
->intf
->dev
.kobj
,
978 &intuos4_led_attr_group
);
983 wacom
->led
.select
[0] = 0;
984 wacom
->led
.select
[1] = 0;
987 wacom
->led
.img_lum
= 0;
989 error
= sysfs_create_group(&wacom
->intf
->dev
.kobj
,
990 &cintiq_led_attr_group
);
999 if (wacom
->wacom_wac
.features
.device_type
== BTN_TOOL_PEN
) {
1000 wacom
->led
.select
[0] = 0;
1001 wacom
->led
.select
[1] = 0;
1002 wacom
->led
.llv
= 32;
1004 wacom
->led
.img_lum
= 0;
1006 error
= sysfs_create_group(&wacom
->intf
->dev
.kobj
,
1007 &intuos5_led_attr_group
);
1017 dev_err(&wacom
->intf
->dev
,
1018 "cannot create sysfs group err: %d\n", error
);
1021 wacom_led_control(wacom
);
1026 static void wacom_destroy_leds(struct wacom
*wacom
)
1028 switch (wacom
->wacom_wac
.features
.type
) {
1032 sysfs_remove_group(&wacom
->intf
->dev
.kobj
,
1033 &intuos4_led_attr_group
);
1038 sysfs_remove_group(&wacom
->intf
->dev
.kobj
,
1039 &cintiq_led_attr_group
);
1048 if (wacom
->wacom_wac
.features
.device_type
== BTN_TOOL_PEN
)
1049 sysfs_remove_group(&wacom
->intf
->dev
.kobj
,
1050 &intuos5_led_attr_group
);
1055 static enum power_supply_property wacom_battery_props
[] = {
1056 POWER_SUPPLY_PROP_SCOPE
,
1057 POWER_SUPPLY_PROP_CAPACITY
1060 static int wacom_battery_get_property(struct power_supply
*psy
,
1061 enum power_supply_property psp
,
1062 union power_supply_propval
*val
)
1064 struct wacom
*wacom
= container_of(psy
, struct wacom
, battery
);
1068 case POWER_SUPPLY_PROP_SCOPE
:
1069 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
1071 case POWER_SUPPLY_PROP_CAPACITY
:
1073 wacom
->wacom_wac
.battery_capacity
* 100 / 31;
1083 static int wacom_initialize_battery(struct wacom
*wacom
)
1087 if (wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_MONITOR
) {
1088 wacom
->battery
.properties
= wacom_battery_props
;
1089 wacom
->battery
.num_properties
= ARRAY_SIZE(wacom_battery_props
);
1090 wacom
->battery
.get_property
= wacom_battery_get_property
;
1091 wacom
->battery
.name
= "wacom_battery";
1092 wacom
->battery
.type
= POWER_SUPPLY_TYPE_BATTERY
;
1093 wacom
->battery
.use_for_apm
= 0;
1095 error
= power_supply_register(&wacom
->usbdev
->dev
,
1099 power_supply_powers(&wacom
->battery
,
1100 &wacom
->usbdev
->dev
);
1106 static void wacom_destroy_battery(struct wacom
*wacom
)
1108 if (wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_MONITOR
&&
1109 wacom
->battery
.dev
) {
1110 power_supply_unregister(&wacom
->battery
);
1111 wacom
->battery
.dev
= NULL
;
1115 static int wacom_register_input(struct wacom
*wacom
)
1117 struct input_dev
*input_dev
;
1118 struct usb_interface
*intf
= wacom
->intf
;
1119 struct usb_device
*dev
= interface_to_usbdev(intf
);
1120 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1123 input_dev
= input_allocate_device();
1129 input_dev
->name
= wacom_wac
->name
;
1130 input_dev
->dev
.parent
= &intf
->dev
;
1131 input_dev
->open
= wacom_open
;
1132 input_dev
->close
= wacom_close
;
1133 usb_to_input_id(dev
, &input_dev
->id
);
1134 input_set_drvdata(input_dev
, wacom
);
1136 wacom_wac
->input
= input_dev
;
1137 error
= wacom_setup_input_capabilities(input_dev
, wacom_wac
);
1141 error
= input_register_device(input_dev
);
1148 input_free_device(input_dev
);
1149 wacom_wac
->input
= NULL
;
1154 static void wacom_wireless_work(struct work_struct
*work
)
1156 struct wacom
*wacom
= container_of(work
, struct wacom
, work
);
1157 struct usb_device
*usbdev
= wacom
->usbdev
;
1158 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
1159 struct wacom
*wacom1
, *wacom2
;
1160 struct wacom_wac
*wacom_wac1
, *wacom_wac2
;
1164 * Regardless if this is a disconnect or a new tablet,
1165 * remove any existing input and battery devices.
1168 wacom_destroy_battery(wacom
);
1170 /* Stylus interface */
1171 wacom1
= usb_get_intfdata(usbdev
->config
->interface
[1]);
1172 wacom_wac1
= &(wacom1
->wacom_wac
);
1173 if (wacom_wac1
->input
)
1174 input_unregister_device(wacom_wac1
->input
);
1175 wacom_wac1
->input
= NULL
;
1177 /* Touch interface */
1178 wacom2
= usb_get_intfdata(usbdev
->config
->interface
[2]);
1179 wacom_wac2
= &(wacom2
->wacom_wac
);
1180 if (wacom_wac2
->input
)
1181 input_unregister_device(wacom_wac2
->input
);
1182 wacom_wac2
->input
= NULL
;
1184 if (wacom_wac
->pid
== 0) {
1185 dev_info(&wacom
->intf
->dev
, "wireless tablet disconnected\n");
1187 const struct usb_device_id
*id
= wacom_ids
;
1189 dev_info(&wacom
->intf
->dev
,
1190 "wireless tablet connected with PID %x\n",
1193 while (id
->match_flags
) {
1194 if (id
->idVendor
== USB_VENDOR_ID_WACOM
&&
1195 id
->idProduct
== wacom_wac
->pid
)
1200 if (!id
->match_flags
) {
1201 dev_info(&wacom
->intf
->dev
,
1202 "ignoring unknown PID.\n");
1206 /* Stylus interface */
1207 wacom_wac1
->features
=
1208 *((struct wacom_features
*)id
->driver_info
);
1209 wacom_wac1
->features
.device_type
= BTN_TOOL_PEN
;
1210 snprintf(wacom_wac1
->name
, WACOM_NAME_MAX
, "%s (WL) Pen",
1211 wacom_wac1
->features
.name
);
1212 wacom_wac1
->shared
->touch_max
= wacom_wac1
->features
.touch_max
;
1213 wacom_wac1
->shared
->type
= wacom_wac1
->features
.type
;
1214 error
= wacom_register_input(wacom1
);
1218 /* Touch interface */
1219 if (wacom_wac1
->features
.touch_max
||
1220 wacom_wac1
->features
.type
== INTUOSHT
) {
1221 wacom_wac2
->features
=
1222 *((struct wacom_features
*)id
->driver_info
);
1223 wacom_wac2
->features
.pktlen
= WACOM_PKGLEN_BBTOUCH3
;
1224 wacom_wac2
->features
.device_type
= BTN_TOOL_FINGER
;
1225 wacom_wac2
->features
.x_max
= wacom_wac2
->features
.y_max
= 4096;
1226 if (wacom_wac2
->features
.touch_max
)
1227 snprintf(wacom_wac2
->name
, WACOM_NAME_MAX
,
1228 "%s (WL) Finger",wacom_wac2
->features
.name
);
1230 snprintf(wacom_wac2
->name
, WACOM_NAME_MAX
,
1231 "%s (WL) Pad",wacom_wac2
->features
.name
);
1232 error
= wacom_register_input(wacom2
);
1236 if (wacom_wac1
->features
.type
== INTUOSHT
&&
1237 wacom_wac1
->features
.touch_max
)
1238 wacom_wac
->shared
->touch_input
= wacom_wac2
->input
;
1241 error
= wacom_initialize_battery(wacom
);
1249 if (wacom_wac2
->input
) {
1250 input_unregister_device(wacom_wac2
->input
);
1251 wacom_wac2
->input
= NULL
;
1254 if (wacom_wac1
->input
) {
1255 input_unregister_device(wacom_wac1
->input
);
1256 wacom_wac1
->input
= NULL
;
1262 * Not all devices report physical dimensions from HID.
1263 * Compute the default from hardcoded logical dimension
1264 * and resolution before driver overwrites them.
1266 static void wacom_set_default_phy(struct wacom_features
*features
)
1268 if (features
->x_resolution
) {
1269 features
->x_phy
= (features
->x_max
* 100) /
1270 features
->x_resolution
;
1271 features
->y_phy
= (features
->y_max
* 100) /
1272 features
->y_resolution
;
1276 static void wacom_calculate_res(struct wacom_features
*features
)
1278 features
->x_resolution
= wacom_calc_hid_res(features
->x_max
,
1281 features
->unitExpo
);
1282 features
->y_resolution
= wacom_calc_hid_res(features
->y_max
,
1285 features
->unitExpo
);
1288 static int wacom_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1290 struct usb_device
*dev
= interface_to_usbdev(intf
);
1291 struct usb_endpoint_descriptor
*endpoint
;
1292 struct wacom
*wacom
;
1293 struct wacom_wac
*wacom_wac
;
1294 struct wacom_features
*features
;
1297 if (!id
->driver_info
)
1300 wacom
= kzalloc(sizeof(struct wacom
), GFP_KERNEL
);
1304 wacom_wac
= &wacom
->wacom_wac
;
1305 wacom_wac
->features
= *((struct wacom_features
*)id
->driver_info
);
1306 features
= &wacom_wac
->features
;
1307 if (features
->pktlen
> WACOM_PKGLEN_MAX
) {
1312 wacom_wac
->data
= usb_alloc_coherent(dev
, WACOM_PKGLEN_MAX
,
1313 GFP_KERNEL
, &wacom
->data_dma
);
1314 if (!wacom_wac
->data
) {
1319 wacom
->irq
= usb_alloc_urb(0, GFP_KERNEL
);
1325 wacom
->usbdev
= dev
;
1327 mutex_init(&wacom
->lock
);
1328 INIT_WORK(&wacom
->work
, wacom_wireless_work
);
1329 usb_make_path(dev
, wacom
->phys
, sizeof(wacom
->phys
));
1330 strlcat(wacom
->phys
, "/input0", sizeof(wacom
->phys
));
1332 endpoint
= &intf
->cur_altsetting
->endpoint
[0].desc
;
1334 /* set the default size in case we do not get them from hid */
1335 wacom_set_default_phy(features
);
1337 /* Retrieve the physical and logical size for touch devices */
1338 error
= wacom_retrieve_hid_descriptor(intf
, features
);
1343 * Intuos5 has no useful data about its touch interface in its
1344 * HID descriptor. If this is the touch interface (wMaxPacketSize
1345 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1347 if (features
->type
>= INTUOS5S
&& features
->type
<= INTUOSHT
) {
1348 if (endpoint
->wMaxPacketSize
== WACOM_PKGLEN_BBTOUCH3
) {
1349 features
->device_type
= BTN_TOOL_FINGER
;
1350 features
->pktlen
= WACOM_PKGLEN_BBTOUCH3
;
1352 features
->x_max
= 4096;
1353 features
->y_max
= 4096;
1355 features
->device_type
= BTN_TOOL_PEN
;
1359 wacom_setup_device_quirks(features
);
1361 /* set unit to "100th of a mm" for devices not reported by HID */
1362 if (!features
->unit
) {
1363 features
->unit
= 0x11;
1364 features
->unitExpo
= 16 - 3;
1366 wacom_calculate_res(features
);
1368 strlcpy(wacom_wac
->name
, features
->name
, sizeof(wacom_wac
->name
));
1370 if (features
->quirks
& WACOM_QUIRK_MULTI_INPUT
) {
1371 struct usb_device
*other_dev
;
1373 /* Append the device type to the name */
1374 if (features
->device_type
!= BTN_TOOL_FINGER
)
1375 strlcat(wacom_wac
->name
, " Pen", WACOM_NAME_MAX
);
1376 else if (features
->touch_max
)
1377 strlcat(wacom_wac
->name
, " Finger", WACOM_NAME_MAX
);
1379 strlcat(wacom_wac
->name
, " Pad", WACOM_NAME_MAX
);
1381 other_dev
= wacom_get_sibling(dev
, features
->oVid
, features
->oPid
);
1382 if (other_dev
== NULL
|| wacom_get_usbdev_data(other_dev
) == NULL
)
1384 error
= wacom_add_shared_data(wacom_wac
, other_dev
);
1389 usb_fill_int_urb(wacom
->irq
, dev
,
1390 usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
),
1391 wacom_wac
->data
, features
->pktlen
,
1392 wacom_sys_irq
, wacom
, endpoint
->bInterval
);
1393 wacom
->irq
->transfer_dma
= wacom
->data_dma
;
1394 wacom
->irq
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1396 error
= wacom_initialize_leds(wacom
);
1400 if (!(features
->quirks
& WACOM_QUIRK_NO_INPUT
)) {
1401 error
= wacom_register_input(wacom
);
1406 /* Note that if query fails it is not a hard failure */
1407 wacom_query_tablet_data(intf
, features
);
1409 usb_set_intfdata(intf
, wacom
);
1411 if (features
->quirks
& WACOM_QUIRK_MONITOR
) {
1412 if (usb_submit_urb(wacom
->irq
, GFP_KERNEL
)) {
1418 if (wacom_wac
->features
.type
== INTUOSHT
&& wacom_wac
->features
.touch_max
) {
1419 if (wacom_wac
->features
.device_type
== BTN_TOOL_FINGER
)
1420 wacom_wac
->shared
->touch_input
= wacom_wac
->input
;
1425 fail5
: wacom_destroy_leds(wacom
);
1426 fail4
: wacom_remove_shared_data(wacom_wac
);
1427 fail3
: usb_free_urb(wacom
->irq
);
1428 fail2
: usb_free_coherent(dev
, WACOM_PKGLEN_MAX
, wacom_wac
->data
, wacom
->data_dma
);
1429 fail1
: kfree(wacom
);
1433 static void wacom_disconnect(struct usb_interface
*intf
)
1435 struct wacom
*wacom
= usb_get_intfdata(intf
);
1437 usb_set_intfdata(intf
, NULL
);
1439 usb_kill_urb(wacom
->irq
);
1440 cancel_work_sync(&wacom
->work
);
1441 if (wacom
->wacom_wac
.input
)
1442 input_unregister_device(wacom
->wacom_wac
.input
);
1443 wacom_destroy_battery(wacom
);
1444 wacom_destroy_leds(wacom
);
1445 usb_free_urb(wacom
->irq
);
1446 usb_free_coherent(interface_to_usbdev(intf
), WACOM_PKGLEN_MAX
,
1447 wacom
->wacom_wac
.data
, wacom
->data_dma
);
1448 wacom_remove_shared_data(&wacom
->wacom_wac
);
1452 static int wacom_suspend(struct usb_interface
*intf
, pm_message_t message
)
1454 struct wacom
*wacom
= usb_get_intfdata(intf
);
1456 mutex_lock(&wacom
->lock
);
1457 usb_kill_urb(wacom
->irq
);
1458 mutex_unlock(&wacom
->lock
);
1463 static int wacom_resume(struct usb_interface
*intf
)
1465 struct wacom
*wacom
= usb_get_intfdata(intf
);
1466 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
1469 mutex_lock(&wacom
->lock
);
1471 /* switch to wacom mode first */
1472 wacom_query_tablet_data(intf
, features
);
1473 wacom_led_control(wacom
);
1475 if ((wacom
->open
|| (features
->quirks
& WACOM_QUIRK_MONITOR
)) &&
1476 usb_submit_urb(wacom
->irq
, GFP_NOIO
) < 0)
1479 mutex_unlock(&wacom
->lock
);
1484 static int wacom_reset_resume(struct usb_interface
*intf
)
1486 return wacom_resume(intf
);
1489 static struct usb_driver wacom_driver
= {
1491 .id_table
= wacom_ids
,
1492 .probe
= wacom_probe
,
1493 .disconnect
= wacom_disconnect
,
1494 .suspend
= wacom_suspend
,
1495 .resume
= wacom_resume
,
1496 .reset_resume
= wacom_reset_resume
,
1497 .supports_autosuspend
= 1,
1500 module_usb_driver(wacom_driver
);