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 /* defines to get HID report descriptor */
19 #define HID_DEVICET_HID (USB_TYPE_CLASS | 0x01)
20 #define HID_DEVICET_REPORT (USB_TYPE_CLASS | 0x02)
21 #define HID_HDESC_USAGE_UNDEFINED 0x00
22 #define HID_HDESC_USAGE_PAGE 0x05
23 #define HID_HDESC_USAGE 0x09
24 #define HID_HDESC_COLLECTION 0xa1
25 #define HID_HDESC_COLLECTION_LOGICAL 0x02
26 #define HID_HDESC_COLLECTION_END 0xc0
28 struct wac_hid_descriptor
{
29 struct usb_descriptor_header header
;
34 __le16 wDescriptorLength
;
35 } __attribute__ ((packed
));
37 /* defines to get/set USB message */
38 #define USB_REQ_GET_REPORT 0x01
39 #define USB_REQ_SET_REPORT 0x09
41 #define WAC_HID_FEATURE_REPORT 0x03
42 #define WAC_MSG_RETRIES 5
44 #define WAC_CMD_LED_CONTROL 0x20
45 #define WAC_CMD_ICON_START 0x21
46 #define WAC_CMD_ICON_XFER 0x23
47 #define WAC_CMD_RETRIES 10
49 static int wacom_get_report(struct usb_interface
*intf
, u8 type
, u8 id
,
50 void *buf
, size_t size
, unsigned int retries
)
52 struct usb_device
*dev
= interface_to_usbdev(intf
);
56 retval
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
58 USB_DIR_IN
| USB_TYPE_CLASS
|
61 intf
->altsetting
[0].desc
.bInterfaceNumber
,
63 } while ((retval
== -ETIMEDOUT
|| retval
== -EPIPE
) && --retries
);
68 static int wacom_set_report(struct usb_interface
*intf
, u8 type
, u8 id
,
69 void *buf
, size_t size
, unsigned int retries
)
71 struct usb_device
*dev
= interface_to_usbdev(intf
);
75 retval
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
77 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
79 intf
->altsetting
[0].desc
.bInterfaceNumber
,
81 } while ((retval
== -ETIMEDOUT
|| retval
== -EPIPE
) && --retries
);
86 static void wacom_sys_irq(struct urb
*urb
)
88 struct wacom
*wacom
= urb
->context
;
89 struct device
*dev
= &wacom
->intf
->dev
;
92 switch (urb
->status
) {
99 /* this urb is terminated, clean up */
100 dev_dbg(dev
, "%s - urb shutting down with status: %d\n",
101 __func__
, urb
->status
);
104 dev_dbg(dev
, "%s - nonzero urb status received: %d\n",
105 __func__
, urb
->status
);
109 wacom_wac_irq(&wacom
->wacom_wac
, urb
->actual_length
);
112 usb_mark_last_busy(wacom
->usbdev
);
113 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
115 dev_err(dev
, "%s - usb_submit_urb failed with result %d\n",
119 static int wacom_open(struct input_dev
*dev
)
121 struct wacom
*wacom
= input_get_drvdata(dev
);
124 if (usb_autopm_get_interface(wacom
->intf
) < 0)
127 mutex_lock(&wacom
->lock
);
132 if (usb_submit_urb(wacom
->irq
, GFP_KERNEL
)) {
138 wacom
->intf
->needs_remote_wakeup
= 1;
141 mutex_unlock(&wacom
->lock
);
142 usb_autopm_put_interface(wacom
->intf
);
146 static void wacom_close(struct input_dev
*dev
)
148 struct wacom
*wacom
= input_get_drvdata(dev
);
151 autopm_error
= usb_autopm_get_interface(wacom
->intf
);
153 mutex_lock(&wacom
->lock
);
157 usb_kill_urb(wacom
->irq
);
159 wacom
->intf
->needs_remote_wakeup
= 0;
162 mutex_unlock(&wacom
->lock
);
165 usb_autopm_put_interface(wacom
->intf
);
169 * Calculate the resolution of the X or Y axis, given appropriate HID data.
170 * This function is little more than hidinput_calc_abs_res stripped down.
172 static int wacom_calc_hid_res(int logical_extents
, int physical_extents
,
173 unsigned char unit
, unsigned char exponent
)
175 int prev
, unit_exponent
;
177 /* Check if the extents are sane */
178 if (logical_extents
<= 0 || physical_extents
<= 0)
181 /* Get signed value of nybble-sized twos-compliment exponent */
182 unit_exponent
= exponent
;
183 if (unit_exponent
> 7)
186 /* Convert physical_extents to millimeters */
187 if (unit
== 0x11) { /* If centimeters */
189 } else if (unit
== 0x13) { /* If inches */
190 prev
= physical_extents
;
191 physical_extents
*= 254;
192 if (physical_extents
< prev
)
199 /* Apply negative unit exponent */
200 for (; unit_exponent
< 0; unit_exponent
++) {
201 prev
= logical_extents
;
202 logical_extents
*= 10;
203 if (logical_extents
< prev
)
206 /* Apply positive unit exponent */
207 for (; unit_exponent
> 0; unit_exponent
--) {
208 prev
= physical_extents
;
209 physical_extents
*= 10;
210 if (physical_extents
< prev
)
214 /* Calculate resolution */
215 return logical_extents
/ physical_extents
;
218 static int wacom_parse_logical_collection(unsigned char *report
,
219 struct wacom_features
*features
)
223 if (features
->type
== BAMBOO_PT
) {
225 /* Logical collection is only used by 3rd gen Bamboo Touch */
226 features
->pktlen
= WACOM_PKGLEN_BBTOUCH3
;
227 features
->device_type
= BTN_TOOL_FINGER
;
229 features
->x_max
= features
->y_max
=
230 get_unaligned_le16(&report
[10]);
237 static void wacom_retrieve_report_data(struct usb_interface
*intf
,
238 struct wacom_features
*features
)
241 unsigned char *rep_data
;
243 rep_data
= kmalloc(2, GFP_KERNEL
);
247 result
= wacom_get_report(intf
, WAC_HID_FEATURE_REPORT
,
248 rep_data
[0], rep_data
, 2,
251 if (result
>= 0 && rep_data
[1] > 2)
252 features
->touch_max
= rep_data
[1];
259 * Interface Descriptor of wacom devices can be incomplete and
260 * inconsistent so wacom_features table is used to store stylus
261 * device's packet lengths, various maximum values, and tablet
262 * resolution based on product ID's.
264 * For devices that contain 2 interfaces, wacom_features table is
265 * inaccurate for the touch interface. Since the Interface Descriptor
266 * for touch interfaces has pretty complete data, this function exists
267 * to query tablet for this missing information instead of hard coding in
268 * an additional table.
270 * A typical Interface Descriptor for a stylus will contain a
271 * boot mouse application collection that is not of interest and this
272 * function will ignore it.
274 * It also contains a digitizer application collection that also is not
275 * of interest since any information it contains would be duplicate
276 * of what is in wacom_features. Usually it defines a report of an array
277 * of bytes that could be used as max length of the stylus packet returned.
278 * If it happens to define a Digitizer-Stylus Physical Collection then
279 * the X and Y logical values contain valid data but it is ignored.
281 * A typical Interface Descriptor for a touch interface will contain a
282 * Digitizer-Finger Physical Collection which will define both logical
283 * X/Y maximum as well as the physical size of tablet. Since touch
284 * interfaces haven't supported pressure or distance, this is enough
285 * information to override invalid values in the wacom_features table.
287 * 3rd gen Bamboo Touch no longer define a Digitizer-Finger Pysical
288 * Collection. Instead they define a Logical Collection with a single
289 * Logical Maximum for both X and Y.
291 * Intuos5 touch interface does not contain useful data. We deal with
292 * this after returning from this function.
294 static int wacom_parse_hid(struct usb_interface
*intf
,
295 struct wac_hid_descriptor
*hid_desc
,
296 struct wacom_features
*features
)
298 struct usb_device
*dev
= interface_to_usbdev(intf
);
300 /* result has to be defined as int for some devices */
301 int result
= 0, touch_max
= 0;
302 int i
= 0, page
= 0, finger
= 0, pen
= 0;
303 unsigned char *report
;
305 report
= kzalloc(hid_desc
->wDescriptorLength
, GFP_KERNEL
);
309 /* retrive report descriptors */
311 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
312 USB_REQ_GET_DESCRIPTOR
,
313 USB_RECIP_INTERFACE
| USB_DIR_IN
,
314 HID_DEVICET_REPORT
<< 8,
315 intf
->altsetting
[0].desc
.bInterfaceNumber
, /* interface */
317 hid_desc
->wDescriptorLength
,
319 } while (result
< 0 && limit
++ < WAC_MSG_RETRIES
);
321 /* No need to parse the Descriptor. It isn't an error though */
325 for (i
= 0; i
< hid_desc
->wDescriptorLength
; i
++) {
328 case HID_HDESC_USAGE_PAGE
:
329 page
= report
[i
+ 1];
333 case HID_HDESC_USAGE
:
334 switch (page
<< 16 | report
[i
+ 1]) {
337 features
->device_type
= BTN_TOOL_FINGER
;
338 /* touch device at least supports one touch point */
340 switch (features
->type
) {
342 features
->pktlen
= WACOM_PKGLEN_TPC2FG
;
347 features
->pktlen
= WACOM_PKGLEN_MTOUCH
;
352 features
->pktlen
= WACOM_PKGLEN_MTTPC
;
356 features
->pktlen
= WACOM_PKGLEN_BBTOUCH
;
360 features
->pktlen
= WACOM_PKGLEN_GRAPHIRE
;
364 switch (features
->type
) {
367 get_unaligned_le16(&report
[i
+ 5]);
369 get_unaligned_le16(&report
[i
+ 8]);
375 get_unaligned_le16(&report
[i
+ 3]);
377 get_unaligned_le16(&report
[i
+ 8]);
378 features
->unit
= report
[i
- 1];
379 features
->unitExpo
= report
[i
- 3];
385 get_unaligned_le16(&report
[i
+ 3]);
387 get_unaligned_le16(&report
[i
+ 6]);
388 features
->unit
= report
[i
- 5];
389 features
->unitExpo
= report
[i
- 3];
395 get_unaligned_le16(&report
[i
+ 3]);
397 get_unaligned_le16(&report
[i
+ 6]);
398 features
->unit
= report
[i
+ 9];
399 features
->unitExpo
= report
[i
+ 11];
404 /* penabled only accepts exact bytes of data */
405 if (features
->type
>= TABLETPC
)
406 features
->pktlen
= WACOM_PKGLEN_GRAPHIRE
;
407 features
->device_type
= BTN_TOOL_PEN
;
409 get_unaligned_le16(&report
[i
+ 3]);
416 switch (features
->type
) {
421 get_unaligned_le16(&report
[i
+ 3]);
423 get_unaligned_le16(&report
[i
+ 6]);
429 get_unaligned_le16(&report
[i
+ 3]);
431 get_unaligned_le16(&report
[i
- 2]);
437 get_unaligned_le16(&report
[i
+ 3]);
439 get_unaligned_le16(&report
[i
+ 6]);
445 get_unaligned_le16(&report
[i
+ 3]);
447 get_unaligned_le16(&report
[i
+ 6]);
455 get_unaligned_le16(&report
[i
+ 3]);
461 get_unaligned_le16(&report
[i
+ 3]);
472 * Requiring Stylus Usage will ignore boot mouse
473 * X/Y values and some cases of invalid Digitizer X/Y
474 * values commonly reported.
481 case HID_DG_CONTACTMAX
:
482 /* leave touch_max as is if predefined */
483 if (!features
->touch_max
)
484 wacom_retrieve_report_data(intf
, features
);
488 case HID_DG_TIPPRESSURE
:
490 features
->pressure_max
=
491 get_unaligned_le16(&report
[i
+ 3]);
498 case HID_HDESC_COLLECTION_END
:
499 /* reset UsagePage and Finger */
503 case HID_HDESC_COLLECTION
:
506 case HID_HDESC_COLLECTION_LOGICAL
:
507 i
+= wacom_parse_logical_collection(&report
[i
],
516 if (!features
->touch_max
&& touch_max
)
517 features
->touch_max
= touch_max
;
523 static int wacom_set_device_mode(struct usb_interface
*intf
, int report_id
, int length
, int mode
)
525 unsigned char *rep_data
;
526 int error
= -ENOMEM
, limit
= 0;
528 rep_data
= kzalloc(length
, GFP_KERNEL
);
533 rep_data
[0] = report_id
;
536 error
= wacom_set_report(intf
, WAC_HID_FEATURE_REPORT
,
537 report_id
, rep_data
, length
, 1);
539 error
= wacom_get_report(intf
, WAC_HID_FEATURE_REPORT
,
540 report_id
, rep_data
, length
, 1);
541 } while ((error
< 0 || rep_data
[1] != mode
) && limit
++ < WAC_MSG_RETRIES
);
545 return error
< 0 ? error
: 0;
549 * Switch the tablet into its most-capable mode. Wacom tablets are
550 * typically configured to power-up in a mode which sends mouse-like
551 * reports to the OS. To get absolute position, pressure data, etc.
552 * from the tablet, it is necessary to switch the tablet out of this
553 * mode and into one which sends the full range of tablet data.
555 static int wacom_query_tablet_data(struct usb_interface
*intf
, struct wacom_features
*features
)
557 if (features
->device_type
== BTN_TOOL_FINGER
) {
558 if (features
->type
> TABLETPC
) {
559 /* MT Tablet PC touch */
560 return wacom_set_device_mode(intf
, 3, 4, 4);
562 else if (features
->type
== WACOM_24HDT
|| features
->type
== CINTIQ_HYBRID
) {
563 return wacom_set_device_mode(intf
, 18, 3, 2);
565 } else if (features
->device_type
== BTN_TOOL_PEN
) {
566 if (features
->type
<= BAMBOO_PT
&& features
->type
!= WIRELESS
) {
567 return wacom_set_device_mode(intf
, 2, 2, 2);
574 static int wacom_retrieve_hid_descriptor(struct usb_interface
*intf
,
575 struct wacom_features
*features
)
578 struct usb_host_interface
*interface
= intf
->cur_altsetting
;
579 struct wac_hid_descriptor
*hid_desc
;
581 /* default features */
582 features
->device_type
= BTN_TOOL_PEN
;
583 features
->x_fuzz
= 4;
584 features
->y_fuzz
= 4;
585 features
->pressure_fuzz
= 0;
586 features
->distance_fuzz
= 0;
589 * The wireless device HID is basic and layout conflicts with
590 * other tablets (monitor and touch interface can look like pen).
591 * Skip the query for this type and modify defaults based on
594 if (features
->type
== WIRELESS
) {
595 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 0) {
596 features
->device_type
= 0;
597 } else if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 2) {
598 features
->device_type
= BTN_TOOL_FINGER
;
599 features
->pktlen
= WACOM_PKGLEN_BBTOUCH3
;
603 /* only devices that support touch need to retrieve the info */
604 if (features
->type
< BAMBOO_PT
) {
608 error
= usb_get_extra_descriptor(interface
, HID_DEVICET_HID
, &hid_desc
);
610 error
= usb_get_extra_descriptor(&interface
->endpoint
[0],
611 HID_DEVICET_REPORT
, &hid_desc
);
614 "can not retrieve extra class descriptor\n");
618 error
= wacom_parse_hid(intf
, hid_desc
, features
);
624 struct wacom_usbdev_data
{
625 struct list_head list
;
627 struct usb_device
*dev
;
628 struct wacom_shared shared
;
631 static LIST_HEAD(wacom_udev_list
);
632 static DEFINE_MUTEX(wacom_udev_list_lock
);
634 static struct usb_device
*wacom_get_sibling(struct usb_device
*dev
, int vendor
, int product
)
637 struct usb_device
*sibling
;
639 if (vendor
== 0 && product
== 0)
642 if (dev
->parent
== NULL
)
645 usb_hub_for_each_child(dev
->parent
, port1
, sibling
) {
646 struct usb_device_descriptor
*d
;
650 d
= &sibling
->descriptor
;
651 if (d
->idVendor
== vendor
&& d
->idProduct
== product
)
658 static struct wacom_usbdev_data
*wacom_get_usbdev_data(struct usb_device
*dev
)
660 struct wacom_usbdev_data
*data
;
662 list_for_each_entry(data
, &wacom_udev_list
, list
) {
663 if (data
->dev
== dev
) {
664 kref_get(&data
->kref
);
672 static int wacom_add_shared_data(struct wacom_wac
*wacom
,
673 struct usb_device
*dev
)
675 struct wacom_usbdev_data
*data
;
678 mutex_lock(&wacom_udev_list_lock
);
680 data
= wacom_get_usbdev_data(dev
);
682 data
= kzalloc(sizeof(struct wacom_usbdev_data
), GFP_KERNEL
);
688 kref_init(&data
->kref
);
690 list_add_tail(&data
->list
, &wacom_udev_list
);
693 wacom
->shared
= &data
->shared
;
696 mutex_unlock(&wacom_udev_list_lock
);
700 static void wacom_release_shared_data(struct kref
*kref
)
702 struct wacom_usbdev_data
*data
=
703 container_of(kref
, struct wacom_usbdev_data
, kref
);
705 mutex_lock(&wacom_udev_list_lock
);
706 list_del(&data
->list
);
707 mutex_unlock(&wacom_udev_list_lock
);
712 static void wacom_remove_shared_data(struct wacom_wac
*wacom
)
714 struct wacom_usbdev_data
*data
;
717 data
= container_of(wacom
->shared
, struct wacom_usbdev_data
, shared
);
718 kref_put(&data
->kref
, wacom_release_shared_data
);
719 wacom
->shared
= NULL
;
723 static int wacom_led_control(struct wacom
*wacom
)
728 buf
= kzalloc(9, GFP_KERNEL
);
732 if (wacom
->wacom_wac
.features
.type
>= INTUOS5S
&&
733 wacom
->wacom_wac
.features
.type
<= INTUOSPL
) {
735 * Touch Ring and crop mark LED luminance may take on
736 * one of four values:
737 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
739 int ring_led
= wacom
->led
.select
[0] & 0x03;
740 int ring_lum
= (((wacom
->led
.llv
& 0x60) >> 5) - 1) & 0x03;
743 buf
[0] = WAC_CMD_LED_CONTROL
;
744 buf
[1] = (crop_lum
<< 4) | (ring_lum
<< 2) | (ring_led
);
747 int led
= wacom
->led
.select
[0] | 0x4;
749 if (wacom
->wacom_wac
.features
.type
== WACOM_21UX2
||
750 wacom
->wacom_wac
.features
.type
== WACOM_24HD
)
751 led
|= (wacom
->led
.select
[1] << 4) | 0x40;
753 buf
[0] = WAC_CMD_LED_CONTROL
;
755 buf
[2] = wacom
->led
.llv
;
756 buf
[3] = wacom
->led
.hlv
;
757 buf
[4] = wacom
->led
.img_lum
;
760 retval
= wacom_set_report(wacom
->intf
, 0x03, WAC_CMD_LED_CONTROL
,
761 buf
, 9, WAC_CMD_RETRIES
);
767 static int wacom_led_putimage(struct wacom
*wacom
, int button_id
, const void *img
)
772 buf
= kzalloc(259, GFP_KERNEL
);
776 /* Send 'start' command */
777 buf
[0] = WAC_CMD_ICON_START
;
779 retval
= wacom_set_report(wacom
->intf
, 0x03, WAC_CMD_ICON_START
,
780 buf
, 2, WAC_CMD_RETRIES
);
784 buf
[0] = WAC_CMD_ICON_XFER
;
785 buf
[1] = button_id
& 0x07;
786 for (i
= 0; i
< 4; i
++) {
788 memcpy(buf
+ 3, img
+ i
* 256, 256);
790 retval
= wacom_set_report(wacom
->intf
, 0x03, WAC_CMD_ICON_XFER
,
791 buf
, 259, WAC_CMD_RETRIES
);
797 buf
[0] = WAC_CMD_ICON_START
;
799 wacom_set_report(wacom
->intf
, 0x03, WAC_CMD_ICON_START
,
800 buf
, 2, WAC_CMD_RETRIES
);
807 static ssize_t
wacom_led_select_store(struct device
*dev
, int set_id
,
808 const char *buf
, size_t count
)
810 struct wacom
*wacom
= dev_get_drvdata(dev
);
814 err
= kstrtouint(buf
, 10, &id
);
818 mutex_lock(&wacom
->lock
);
820 wacom
->led
.select
[set_id
] = id
& 0x3;
821 err
= wacom_led_control(wacom
);
823 mutex_unlock(&wacom
->lock
);
825 return err
< 0 ? err
: count
;
828 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
829 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
830 struct device_attribute *attr, const char *buf, size_t count) \
832 return wacom_led_select_store(dev, SET_ID, buf, count); \
834 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
835 struct device_attribute *attr, char *buf) \
837 struct wacom *wacom = dev_get_drvdata(dev); \
838 return snprintf(buf, 2, "%d\n", wacom->led.select[SET_ID]); \
840 static DEVICE_ATTR(status_led##SET_ID##_select, S_IWUSR | S_IRUSR, \
841 wacom_led##SET_ID##_select_show, \
842 wacom_led##SET_ID##_select_store)
844 DEVICE_LED_SELECT_ATTR(0);
845 DEVICE_LED_SELECT_ATTR(1);
847 static ssize_t
wacom_luminance_store(struct wacom
*wacom
, u8
*dest
,
848 const char *buf
, size_t count
)
853 err
= kstrtouint(buf
, 10, &value
);
857 mutex_lock(&wacom
->lock
);
859 *dest
= value
& 0x7f;
860 err
= wacom_led_control(wacom
);
862 mutex_unlock(&wacom
->lock
);
864 return err
< 0 ? err
: count
;
867 #define DEVICE_LUMINANCE_ATTR(name, field) \
868 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
869 struct device_attribute *attr, const char *buf, size_t count) \
871 struct wacom *wacom = dev_get_drvdata(dev); \
873 return wacom_luminance_store(wacom, &wacom->led.field, \
876 static DEVICE_ATTR(name##_luminance, S_IWUSR, \
877 NULL, wacom_##name##_luminance_store)
879 DEVICE_LUMINANCE_ATTR(status0
, llv
);
880 DEVICE_LUMINANCE_ATTR(status1
, hlv
);
881 DEVICE_LUMINANCE_ATTR(buttons
, img_lum
);
883 static ssize_t
wacom_button_image_store(struct device
*dev
, int button_id
,
884 const char *buf
, size_t count
)
886 struct wacom
*wacom
= dev_get_drvdata(dev
);
892 mutex_lock(&wacom
->lock
);
894 err
= wacom_led_putimage(wacom
, button_id
, buf
);
896 mutex_unlock(&wacom
->lock
);
898 return err
< 0 ? err
: count
;
901 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
902 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
903 struct device_attribute *attr, const char *buf, size_t count) \
905 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
907 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, S_IWUSR, \
908 NULL, wacom_btnimg##BUTTON_ID##_store)
910 DEVICE_BTNIMG_ATTR(0);
911 DEVICE_BTNIMG_ATTR(1);
912 DEVICE_BTNIMG_ATTR(2);
913 DEVICE_BTNIMG_ATTR(3);
914 DEVICE_BTNIMG_ATTR(4);
915 DEVICE_BTNIMG_ATTR(5);
916 DEVICE_BTNIMG_ATTR(6);
917 DEVICE_BTNIMG_ATTR(7);
919 static struct attribute
*cintiq_led_attrs
[] = {
920 &dev_attr_status_led0_select
.attr
,
921 &dev_attr_status_led1_select
.attr
,
925 static struct attribute_group cintiq_led_attr_group
= {
927 .attrs
= cintiq_led_attrs
,
930 static struct attribute
*intuos4_led_attrs
[] = {
931 &dev_attr_status0_luminance
.attr
,
932 &dev_attr_status1_luminance
.attr
,
933 &dev_attr_status_led0_select
.attr
,
934 &dev_attr_buttons_luminance
.attr
,
935 &dev_attr_button0_rawimg
.attr
,
936 &dev_attr_button1_rawimg
.attr
,
937 &dev_attr_button2_rawimg
.attr
,
938 &dev_attr_button3_rawimg
.attr
,
939 &dev_attr_button4_rawimg
.attr
,
940 &dev_attr_button5_rawimg
.attr
,
941 &dev_attr_button6_rawimg
.attr
,
942 &dev_attr_button7_rawimg
.attr
,
946 static struct attribute_group intuos4_led_attr_group
= {
948 .attrs
= intuos4_led_attrs
,
951 static struct attribute
*intuos5_led_attrs
[] = {
952 &dev_attr_status0_luminance
.attr
,
953 &dev_attr_status_led0_select
.attr
,
957 static struct attribute_group intuos5_led_attr_group
= {
959 .attrs
= intuos5_led_attrs
,
962 static int wacom_initialize_leds(struct wacom
*wacom
)
966 /* Initialize default values */
967 switch (wacom
->wacom_wac
.features
.type
) {
971 wacom
->led
.select
[0] = 0;
972 wacom
->led
.select
[1] = 0;
975 wacom
->led
.img_lum
= 10;
976 error
= sysfs_create_group(&wacom
->intf
->dev
.kobj
,
977 &intuos4_led_attr_group
);
982 wacom
->led
.select
[0] = 0;
983 wacom
->led
.select
[1] = 0;
986 wacom
->led
.img_lum
= 0;
988 error
= sysfs_create_group(&wacom
->intf
->dev
.kobj
,
989 &cintiq_led_attr_group
);
998 if (wacom
->wacom_wac
.features
.device_type
== BTN_TOOL_PEN
) {
999 wacom
->led
.select
[0] = 0;
1000 wacom
->led
.select
[1] = 0;
1001 wacom
->led
.llv
= 32;
1003 wacom
->led
.img_lum
= 0;
1005 error
= sysfs_create_group(&wacom
->intf
->dev
.kobj
,
1006 &intuos5_led_attr_group
);
1016 dev_err(&wacom
->intf
->dev
,
1017 "cannot create sysfs group err: %d\n", error
);
1020 wacom_led_control(wacom
);
1025 static void wacom_destroy_leds(struct wacom
*wacom
)
1027 switch (wacom
->wacom_wac
.features
.type
) {
1031 sysfs_remove_group(&wacom
->intf
->dev
.kobj
,
1032 &intuos4_led_attr_group
);
1037 sysfs_remove_group(&wacom
->intf
->dev
.kobj
,
1038 &cintiq_led_attr_group
);
1047 if (wacom
->wacom_wac
.features
.device_type
== BTN_TOOL_PEN
)
1048 sysfs_remove_group(&wacom
->intf
->dev
.kobj
,
1049 &intuos5_led_attr_group
);
1054 static enum power_supply_property wacom_battery_props
[] = {
1055 POWER_SUPPLY_PROP_SCOPE
,
1056 POWER_SUPPLY_PROP_CAPACITY
1059 static int wacom_battery_get_property(struct power_supply
*psy
,
1060 enum power_supply_property psp
,
1061 union power_supply_propval
*val
)
1063 struct wacom
*wacom
= container_of(psy
, struct wacom
, battery
);
1067 case POWER_SUPPLY_PROP_SCOPE
:
1068 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
1070 case POWER_SUPPLY_PROP_CAPACITY
:
1072 wacom
->wacom_wac
.battery_capacity
* 100 / 31;
1082 static int wacom_initialize_battery(struct wacom
*wacom
)
1086 if (wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_MONITOR
) {
1087 wacom
->battery
.properties
= wacom_battery_props
;
1088 wacom
->battery
.num_properties
= ARRAY_SIZE(wacom_battery_props
);
1089 wacom
->battery
.get_property
= wacom_battery_get_property
;
1090 wacom
->battery
.name
= "wacom_battery";
1091 wacom
->battery
.type
= POWER_SUPPLY_TYPE_BATTERY
;
1092 wacom
->battery
.use_for_apm
= 0;
1094 error
= power_supply_register(&wacom
->usbdev
->dev
,
1098 power_supply_powers(&wacom
->battery
,
1099 &wacom
->usbdev
->dev
);
1105 static void wacom_destroy_battery(struct wacom
*wacom
)
1107 if (wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_MONITOR
&&
1108 wacom
->battery
.dev
) {
1109 power_supply_unregister(&wacom
->battery
);
1110 wacom
->battery
.dev
= NULL
;
1114 static struct input_dev
*wacom_allocate_input(struct wacom
*wacom
)
1116 struct input_dev
*input_dev
;
1117 struct usb_interface
*intf
= wacom
->intf
;
1118 struct usb_device
*dev
= interface_to_usbdev(intf
);
1119 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1121 input_dev
= input_allocate_device();
1125 input_dev
->name
= wacom_wac
->name
;
1126 input_dev
->phys
= wacom
->phys
;
1127 input_dev
->dev
.parent
= &intf
->dev
;
1128 input_dev
->open
= wacom_open
;
1129 input_dev
->close
= wacom_close
;
1130 usb_to_input_id(dev
, &input_dev
->id
);
1131 input_set_drvdata(input_dev
, wacom
);
1136 static void wacom_unregister_inputs(struct wacom
*wacom
)
1138 if (wacom
->wacom_wac
.input
)
1139 input_unregister_device(wacom
->wacom_wac
.input
);
1140 if (wacom
->wacom_wac
.pad_input
)
1141 input_unregister_device(wacom
->wacom_wac
.pad_input
);
1142 wacom
->wacom_wac
.input
= NULL
;
1143 wacom
->wacom_wac
.pad_input
= NULL
;
1146 static int wacom_register_inputs(struct wacom
*wacom
)
1148 struct input_dev
*input_dev
, *pad_input_dev
;
1149 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1152 input_dev
= wacom_allocate_input(wacom
);
1153 pad_input_dev
= wacom_allocate_input(wacom
);
1154 if (!input_dev
|| !pad_input_dev
) {
1159 wacom_wac
->input
= input_dev
;
1160 wacom_wac
->pad_input
= pad_input_dev
;
1161 wacom_wac
->pad_input
->name
= wacom_wac
->pad_name
;
1163 error
= wacom_setup_input_capabilities(input_dev
, wacom_wac
);
1167 error
= input_register_device(input_dev
);
1171 error
= wacom_setup_pad_input_capabilities(pad_input_dev
, wacom_wac
);
1173 /* no pad in use on this interface */
1174 input_free_device(pad_input_dev
);
1175 wacom_wac
->pad_input
= NULL
;
1176 pad_input_dev
= NULL
;
1178 error
= input_register_device(pad_input_dev
);
1186 input_unregister_device(input_dev
);
1189 wacom_wac
->input
= NULL
;
1190 wacom_wac
->pad_input
= NULL
;
1193 input_free_device(input_dev
);
1195 input_free_device(pad_input_dev
);
1199 static void wacom_wireless_work(struct work_struct
*work
)
1201 struct wacom
*wacom
= container_of(work
, struct wacom
, work
);
1202 struct usb_device
*usbdev
= wacom
->usbdev
;
1203 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
1204 struct wacom
*wacom1
, *wacom2
;
1205 struct wacom_wac
*wacom_wac1
, *wacom_wac2
;
1209 * Regardless if this is a disconnect or a new tablet,
1210 * remove any existing input and battery devices.
1213 wacom_destroy_battery(wacom
);
1215 /* Stylus interface */
1216 wacom1
= usb_get_intfdata(usbdev
->config
->interface
[1]);
1217 wacom_wac1
= &(wacom1
->wacom_wac
);
1218 wacom_unregister_inputs(wacom1
);
1220 /* Touch interface */
1221 wacom2
= usb_get_intfdata(usbdev
->config
->interface
[2]);
1222 wacom_wac2
= &(wacom2
->wacom_wac
);
1223 wacom_unregister_inputs(wacom2
);
1225 if (wacom_wac
->pid
== 0) {
1226 dev_info(&wacom
->intf
->dev
, "wireless tablet disconnected\n");
1228 const struct usb_device_id
*id
= wacom_ids
;
1230 dev_info(&wacom
->intf
->dev
,
1231 "wireless tablet connected with PID %x\n",
1234 while (id
->match_flags
) {
1235 if (id
->idVendor
== USB_VENDOR_ID_WACOM
&&
1236 id
->idProduct
== wacom_wac
->pid
)
1241 if (!id
->match_flags
) {
1242 dev_info(&wacom
->intf
->dev
,
1243 "ignoring unknown PID.\n");
1247 /* Stylus interface */
1248 wacom_wac1
->features
=
1249 *((struct wacom_features
*)id
->driver_info
);
1250 wacom_wac1
->features
.device_type
= BTN_TOOL_PEN
;
1251 snprintf(wacom_wac1
->name
, WACOM_NAME_MAX
, "%s (WL) Pen",
1252 wacom_wac1
->features
.name
);
1253 snprintf(wacom_wac1
->pad_name
, WACOM_NAME_MAX
, "%s (WL) Pad",
1254 wacom_wac1
->features
.name
);
1255 wacom_wac1
->shared
->touch_max
= wacom_wac1
->features
.touch_max
;
1256 wacom_wac1
->shared
->type
= wacom_wac1
->features
.type
;
1257 error
= wacom_register_inputs(wacom1
);
1261 /* Touch interface */
1262 if (wacom_wac1
->features
.touch_max
||
1263 wacom_wac1
->features
.type
== INTUOSHT
) {
1264 wacom_wac2
->features
=
1265 *((struct wacom_features
*)id
->driver_info
);
1266 wacom_wac2
->features
.pktlen
= WACOM_PKGLEN_BBTOUCH3
;
1267 wacom_wac2
->features
.device_type
= BTN_TOOL_FINGER
;
1268 wacom_wac2
->features
.x_max
= wacom_wac2
->features
.y_max
= 4096;
1269 if (wacom_wac2
->features
.touch_max
)
1270 snprintf(wacom_wac2
->name
, WACOM_NAME_MAX
,
1271 "%s (WL) Finger",wacom_wac2
->features
.name
);
1273 snprintf(wacom_wac2
->name
, WACOM_NAME_MAX
,
1274 "%s (WL) Pad",wacom_wac2
->features
.name
);
1275 snprintf(wacom_wac2
->pad_name
, WACOM_NAME_MAX
,
1276 "%s (WL) Pad", wacom_wac2
->features
.name
);
1277 error
= wacom_register_inputs(wacom2
);
1281 if (wacom_wac1
->features
.type
== INTUOSHT
&&
1282 wacom_wac1
->features
.touch_max
)
1283 wacom_wac
->shared
->touch_input
= wacom_wac2
->input
;
1286 error
= wacom_initialize_battery(wacom
);
1294 wacom_unregister_inputs(wacom1
);
1295 wacom_unregister_inputs(wacom2
);
1300 * Not all devices report physical dimensions from HID.
1301 * Compute the default from hardcoded logical dimension
1302 * and resolution before driver overwrites them.
1304 static void wacom_set_default_phy(struct wacom_features
*features
)
1306 if (features
->x_resolution
) {
1307 features
->x_phy
= (features
->x_max
* 100) /
1308 features
->x_resolution
;
1309 features
->y_phy
= (features
->y_max
* 100) /
1310 features
->y_resolution
;
1314 static void wacom_calculate_res(struct wacom_features
*features
)
1316 features
->x_resolution
= wacom_calc_hid_res(features
->x_max
,
1319 features
->unitExpo
);
1320 features
->y_resolution
= wacom_calc_hid_res(features
->y_max
,
1323 features
->unitExpo
);
1326 static int wacom_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1328 struct usb_device
*dev
= interface_to_usbdev(intf
);
1329 struct usb_endpoint_descriptor
*endpoint
;
1330 struct wacom
*wacom
;
1331 struct wacom_wac
*wacom_wac
;
1332 struct wacom_features
*features
;
1335 if (!id
->driver_info
)
1338 wacom
= kzalloc(sizeof(struct wacom
), GFP_KERNEL
);
1342 wacom_wac
= &wacom
->wacom_wac
;
1343 wacom_wac
->features
= *((struct wacom_features
*)id
->driver_info
);
1344 features
= &wacom_wac
->features
;
1345 if (features
->pktlen
> WACOM_PKGLEN_MAX
) {
1350 wacom_wac
->data
= usb_alloc_coherent(dev
, WACOM_PKGLEN_MAX
,
1351 GFP_KERNEL
, &wacom
->data_dma
);
1352 if (!wacom_wac
->data
) {
1357 wacom
->irq
= usb_alloc_urb(0, GFP_KERNEL
);
1363 wacom
->usbdev
= dev
;
1365 mutex_init(&wacom
->lock
);
1366 INIT_WORK(&wacom
->work
, wacom_wireless_work
);
1367 usb_make_path(dev
, wacom
->phys
, sizeof(wacom
->phys
));
1368 strlcat(wacom
->phys
, "/input0", sizeof(wacom
->phys
));
1370 endpoint
= &intf
->cur_altsetting
->endpoint
[0].desc
;
1372 /* set the default size in case we do not get them from hid */
1373 wacom_set_default_phy(features
);
1375 /* Retrieve the physical and logical size for touch devices */
1376 error
= wacom_retrieve_hid_descriptor(intf
, features
);
1381 * Intuos5 has no useful data about its touch interface in its
1382 * HID descriptor. If this is the touch interface (wMaxPacketSize
1383 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1385 if (features
->type
>= INTUOS5S
&& features
->type
<= INTUOSHT
) {
1386 if (endpoint
->wMaxPacketSize
== WACOM_PKGLEN_BBTOUCH3
) {
1387 features
->device_type
= BTN_TOOL_FINGER
;
1388 features
->pktlen
= WACOM_PKGLEN_BBTOUCH3
;
1390 features
->x_max
= 4096;
1391 features
->y_max
= 4096;
1393 features
->device_type
= BTN_TOOL_PEN
;
1397 wacom_setup_device_quirks(features
);
1399 /* set unit to "100th of a mm" for devices not reported by HID */
1400 if (!features
->unit
) {
1401 features
->unit
= 0x11;
1402 features
->unitExpo
= 16 - 3;
1404 wacom_calculate_res(features
);
1406 strlcpy(wacom_wac
->name
, features
->name
, sizeof(wacom_wac
->name
));
1407 snprintf(wacom_wac
->pad_name
, sizeof(wacom_wac
->pad_name
),
1408 "%s Pad", features
->name
);
1410 if (features
->quirks
& WACOM_QUIRK_MULTI_INPUT
) {
1411 struct usb_device
*other_dev
;
1413 /* Append the device type to the name */
1414 if (features
->device_type
!= BTN_TOOL_FINGER
)
1415 strlcat(wacom_wac
->name
, " Pen", WACOM_NAME_MAX
);
1416 else if (features
->touch_max
)
1417 strlcat(wacom_wac
->name
, " Finger", WACOM_NAME_MAX
);
1419 strlcat(wacom_wac
->name
, " Pad", WACOM_NAME_MAX
);
1421 other_dev
= wacom_get_sibling(dev
, features
->oVid
, features
->oPid
);
1422 if (other_dev
== NULL
|| wacom_get_usbdev_data(other_dev
) == NULL
)
1424 error
= wacom_add_shared_data(wacom_wac
, other_dev
);
1429 usb_fill_int_urb(wacom
->irq
, dev
,
1430 usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
),
1431 wacom_wac
->data
, features
->pktlen
,
1432 wacom_sys_irq
, wacom
, endpoint
->bInterval
);
1433 wacom
->irq
->transfer_dma
= wacom
->data_dma
;
1434 wacom
->irq
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1436 error
= wacom_initialize_leds(wacom
);
1440 if (!(features
->quirks
& WACOM_QUIRK_NO_INPUT
)) {
1441 error
= wacom_register_inputs(wacom
);
1446 /* Note that if query fails it is not a hard failure */
1447 wacom_query_tablet_data(intf
, features
);
1449 usb_set_intfdata(intf
, wacom
);
1451 if (features
->quirks
& WACOM_QUIRK_MONITOR
) {
1452 if (usb_submit_urb(wacom
->irq
, GFP_KERNEL
)) {
1458 if (wacom_wac
->features
.type
== INTUOSHT
&& wacom_wac
->features
.touch_max
) {
1459 if (wacom_wac
->features
.device_type
== BTN_TOOL_FINGER
)
1460 wacom_wac
->shared
->touch_input
= wacom_wac
->input
;
1465 fail5
: wacom_destroy_leds(wacom
);
1466 fail4
: wacom_remove_shared_data(wacom_wac
);
1467 fail3
: usb_free_urb(wacom
->irq
);
1468 fail2
: usb_free_coherent(dev
, WACOM_PKGLEN_MAX
, wacom_wac
->data
, wacom
->data_dma
);
1469 fail1
: kfree(wacom
);
1473 static void wacom_disconnect(struct usb_interface
*intf
)
1475 struct wacom
*wacom
= usb_get_intfdata(intf
);
1477 usb_set_intfdata(intf
, NULL
);
1479 usb_kill_urb(wacom
->irq
);
1480 cancel_work_sync(&wacom
->work
);
1481 wacom_unregister_inputs(wacom
);
1482 wacom_destroy_battery(wacom
);
1483 wacom_destroy_leds(wacom
);
1484 usb_free_urb(wacom
->irq
);
1485 usb_free_coherent(interface_to_usbdev(intf
), WACOM_PKGLEN_MAX
,
1486 wacom
->wacom_wac
.data
, wacom
->data_dma
);
1487 wacom_remove_shared_data(&wacom
->wacom_wac
);
1491 static int wacom_suspend(struct usb_interface
*intf
, pm_message_t message
)
1493 struct wacom
*wacom
= usb_get_intfdata(intf
);
1495 mutex_lock(&wacom
->lock
);
1496 usb_kill_urb(wacom
->irq
);
1497 mutex_unlock(&wacom
->lock
);
1502 static int wacom_resume(struct usb_interface
*intf
)
1504 struct wacom
*wacom
= usb_get_intfdata(intf
);
1505 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
1508 mutex_lock(&wacom
->lock
);
1510 /* switch to wacom mode first */
1511 wacom_query_tablet_data(intf
, features
);
1512 wacom_led_control(wacom
);
1514 if ((wacom
->open
|| (features
->quirks
& WACOM_QUIRK_MONITOR
)) &&
1515 usb_submit_urb(wacom
->irq
, GFP_NOIO
) < 0)
1518 mutex_unlock(&wacom
->lock
);
1523 static int wacom_reset_resume(struct usb_interface
*intf
)
1525 return wacom_resume(intf
);
1528 static struct usb_driver wacom_driver
= {
1530 .id_table
= wacom_ids
,
1531 .probe
= wacom_probe
,
1532 .disconnect
= wacom_disconnect
,
1533 .suspend
= wacom_suspend
,
1534 .resume
= wacom_resume
,
1535 .reset_resume
= wacom_reset_resume
,
1536 .supports_autosuspend
= 1,
1539 module_usb_driver(wacom_driver
);