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/input/mt.h>
18 #define WAC_MSG_RETRIES 5
19 #define WAC_CMD_RETRIES 10
21 #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
22 #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
23 #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
25 static int wacom_get_report(struct hid_device
*hdev
, u8 type
, u8
*buf
,
26 size_t size
, unsigned int retries
)
31 retval
= hid_hw_raw_request(hdev
, buf
[0], buf
, size
, type
,
33 } while ((retval
== -ETIMEDOUT
|| retval
== -EAGAIN
) && --retries
);
36 hid_err(hdev
, "wacom_get_report: ran out of retries "
37 "(last error = %d)\n", retval
);
42 static int wacom_set_report(struct hid_device
*hdev
, u8 type
, u8
*buf
,
43 size_t size
, unsigned int retries
)
48 retval
= hid_hw_raw_request(hdev
, buf
[0], buf
, size
, type
,
50 } while ((retval
== -ETIMEDOUT
|| retval
== -EAGAIN
) && --retries
);
53 hid_err(hdev
, "wacom_set_report: ran out of retries "
54 "(last error = %d)\n", retval
);
59 static int wacom_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
60 u8
*raw_data
, int size
)
62 struct wacom
*wacom
= hid_get_drvdata(hdev
);
64 if (size
> WACOM_PKGLEN_MAX
)
67 memcpy(wacom
->wacom_wac
.data
, raw_data
, size
);
69 wacom_wac_irq(&wacom
->wacom_wac
, size
);
74 static int wacom_open(struct input_dev
*dev
)
76 struct wacom
*wacom
= input_get_drvdata(dev
);
78 return hid_hw_open(wacom
->hdev
);
81 static void wacom_close(struct input_dev
*dev
)
83 struct wacom
*wacom
= input_get_drvdata(dev
);
86 * wacom->hdev should never be null, but surprisingly, I had the case
87 * once while unplugging the Wacom Wireless Receiver.
90 hid_hw_close(wacom
->hdev
);
94 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
96 static int wacom_calc_hid_res(int logical_extents
, int physical_extents
,
97 unsigned unit
, int exponent
)
99 struct hid_field field
= {
100 .logical_maximum
= logical_extents
,
101 .physical_maximum
= physical_extents
,
103 .unit_exponent
= exponent
,
106 return hidinput_calc_abs_res(&field
, ABS_X
);
109 static void wacom_feature_mapping(struct hid_device
*hdev
,
110 struct hid_field
*field
, struct hid_usage
*usage
)
112 struct wacom
*wacom
= hid_get_drvdata(hdev
);
113 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
114 struct hid_data
*hid_data
= &wacom
->wacom_wac
.hid_data
;
115 unsigned int equivalent_usage
= wacom_equivalent_usage(usage
->hid
);
120 switch (equivalent_usage
) {
121 case WACOM_HID_WD_TOUCH_RING_SETTING
:
122 wacom
->generic_has_leds
= true;
124 case HID_DG_CONTACTMAX
:
125 /* leave touch_max as is if predefined */
126 if (!features
->touch_max
) {
128 n
= hid_report_len(field
->report
);
129 data
= hid_alloc_report_buf(field
->report
, GFP_KERNEL
);
132 data
[0] = field
->report
->id
;
133 ret
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
134 data
, n
, WAC_CMD_RETRIES
);
136 ret
= hid_report_raw_event(hdev
,
137 HID_FEATURE_REPORT
, data
, n
, 0);
139 features
->touch_max
= 16;
140 hid_warn(hdev
, "wacom_feature_mapping: "
141 "could not get HID_DG_CONTACTMAX, "
142 "defaulting to %d\n",
143 features
->touch_max
);
148 case HID_DG_INPUTMODE
:
149 /* Ignore if value index is out of bounds. */
150 if (usage
->usage_index
>= field
->report_count
) {
151 dev_err(&hdev
->dev
, "HID_DG_INPUTMODE out of range\n");
155 hid_data
->inputmode
= field
->report
->id
;
156 hid_data
->inputmode_index
= usage
->usage_index
;
159 case HID_UP_DIGITIZER
:
160 if (field
->report
->id
== 0x0B &&
161 (field
->application
== WACOM_HID_G9_PEN
||
162 field
->application
== WACOM_HID_G11_PEN
)) {
163 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
164 wacom
->wacom_wac
.mode_value
= 0;
168 case WACOM_HID_WD_DATAMODE
:
169 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
170 wacom
->wacom_wac
.mode_value
= 2;
173 case WACOM_HID_UP_G9
:
174 case WACOM_HID_UP_G11
:
175 if (field
->report
->id
== 0x03 &&
176 (field
->application
== WACOM_HID_G9_TOUCHSCREEN
||
177 field
->application
== WACOM_HID_G11_TOUCHSCREEN
)) {
178 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
179 wacom
->wacom_wac
.mode_value
= 0;
182 case WACOM_HID_WD_OFFSETLEFT
:
183 case WACOM_HID_WD_OFFSETTOP
:
184 case WACOM_HID_WD_OFFSETRIGHT
:
185 case WACOM_HID_WD_OFFSETBOTTOM
:
187 n
= hid_report_len(field
->report
);
188 data
= hid_alloc_report_buf(field
->report
, GFP_KERNEL
);
191 data
[0] = field
->report
->id
;
192 ret
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
193 data
, n
, WAC_CMD_RETRIES
);
195 ret
= hid_report_raw_event(hdev
, HID_FEATURE_REPORT
,
198 hid_warn(hdev
, "%s: could not retrieve sensor offsets\n",
205 if (hdev
->vendor
== USB_VENDOR_ID_WACOM
&&
206 hdev
->product
== 0x4200 /* Dell Canvas 27 */ &&
207 field
->application
== HID_UP_MSVENDOR
) {
208 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
209 wacom
->wacom_wac
.mode_value
= 2;
214 * Interface Descriptor of wacom devices can be incomplete and
215 * inconsistent so wacom_features table is used to store stylus
216 * device's packet lengths, various maximum values, and tablet
217 * resolution based on product ID's.
219 * For devices that contain 2 interfaces, wacom_features table is
220 * inaccurate for the touch interface. Since the Interface Descriptor
221 * for touch interfaces has pretty complete data, this function exists
222 * to query tablet for this missing information instead of hard coding in
223 * an additional table.
225 * A typical Interface Descriptor for a stylus will contain a
226 * boot mouse application collection that is not of interest and this
227 * function will ignore it.
229 * It also contains a digitizer application collection that also is not
230 * of interest since any information it contains would be duplicate
231 * of what is in wacom_features. Usually it defines a report of an array
232 * of bytes that could be used as max length of the stylus packet returned.
233 * If it happens to define a Digitizer-Stylus Physical Collection then
234 * the X and Y logical values contain valid data but it is ignored.
236 * A typical Interface Descriptor for a touch interface will contain a
237 * Digitizer-Finger Physical Collection which will define both logical
238 * X/Y maximum as well as the physical size of tablet. Since touch
239 * interfaces haven't supported pressure or distance, this is enough
240 * information to override invalid values in the wacom_features table.
242 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
243 * data. We deal with them after returning from this function.
245 static void wacom_usage_mapping(struct hid_device
*hdev
,
246 struct hid_field
*field
, struct hid_usage
*usage
)
248 struct wacom
*wacom
= hid_get_drvdata(hdev
);
249 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
250 bool finger
= WACOM_FINGER_FIELD(field
);
251 bool pen
= WACOM_PEN_FIELD(field
);
254 * Requiring Stylus Usage will ignore boot mouse
255 * X/Y values and some cases of invalid Digitizer X/Y
256 * values commonly reported.
259 features
->device_type
|= WACOM_DEVICETYPE_PEN
;
261 features
->device_type
|= WACOM_DEVICETYPE_TOUCH
;
266 * Bamboo models do not support HID_DG_CONTACTMAX.
267 * And, Bamboo Pen only descriptor contains touch.
269 if (features
->type
> BAMBOO_PT
) {
270 /* ISDv4 touch devices at least supports one touch point */
271 if (finger
&& !features
->touch_max
)
272 features
->touch_max
= 1;
276 * ISDv4 devices which predate HID's adoption of the
277 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
278 * position instead. We can accurately detect if a
279 * usage with that value should be HID_DG_BARRELSWITCH2
280 * based on the surrounding usages, which have remained
281 * constant across generations.
283 if (features
->type
== HID_GENERIC
&&
284 usage
->hid
== 0x000D0000 &&
285 field
->application
== HID_DG_PEN
&&
286 field
->physical
== HID_DG_STYLUS
) {
287 int i
= usage
->usage_index
;
289 if (i
-4 >= 0 && i
+1 < field
->maxusage
&&
290 field
->usage
[i
-4].hid
== HID_DG_TIPSWITCH
&&
291 field
->usage
[i
-3].hid
== HID_DG_BARRELSWITCH
&&
292 field
->usage
[i
-2].hid
== HID_DG_ERASER
&&
293 field
->usage
[i
-1].hid
== HID_DG_INVERT
&&
294 field
->usage
[i
+1].hid
== HID_DG_INRANGE
) {
295 usage
->hid
= HID_DG_BARRELSWITCH2
;
299 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
300 if (hdev
->vendor
== USB_VENDOR_ID_WACOM
&&
301 hdev
->product
== 0x0358 &&
302 WACOM_PEN_FIELD(field
) &&
303 wacom_equivalent_usage(usage
->hid
) == HID_GD_Y
) {
304 field
->logical_maximum
= 43200;
307 switch (usage
->hid
) {
309 features
->x_max
= field
->logical_maximum
;
311 features
->x_phy
= field
->physical_maximum
;
312 if ((features
->type
!= BAMBOO_PT
) &&
313 (features
->type
!= BAMBOO_TOUCH
)) {
314 features
->unit
= field
->unit
;
315 features
->unitExpo
= field
->unit_exponent
;
320 features
->y_max
= field
->logical_maximum
;
322 features
->y_phy
= field
->physical_maximum
;
323 if ((features
->type
!= BAMBOO_PT
) &&
324 (features
->type
!= BAMBOO_TOUCH
)) {
325 features
->unit
= field
->unit
;
326 features
->unitExpo
= field
->unit_exponent
;
330 case HID_DG_TIPPRESSURE
:
332 features
->pressure_max
= field
->logical_maximum
;
336 if (features
->type
== HID_GENERIC
)
337 wacom_wac_usage_mapping(hdev
, field
, usage
);
340 static void wacom_post_parse_hid(struct hid_device
*hdev
,
341 struct wacom_features
*features
)
343 struct wacom
*wacom
= hid_get_drvdata(hdev
);
344 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
346 if (features
->type
== HID_GENERIC
) {
347 /* Any last-minute generic device setup */
348 if (wacom_wac
->has_mode_change
) {
349 if (wacom_wac
->is_direct_mode
)
350 features
->device_type
|= WACOM_DEVICETYPE_DIRECT
;
352 features
->device_type
&= ~WACOM_DEVICETYPE_DIRECT
;
355 if (features
->touch_max
> 1) {
356 if (features
->device_type
& WACOM_DEVICETYPE_DIRECT
)
357 input_mt_init_slots(wacom_wac
->touch_input
,
358 wacom_wac
->features
.touch_max
,
361 input_mt_init_slots(wacom_wac
->touch_input
,
362 wacom_wac
->features
.touch_max
,
368 static void wacom_parse_hid(struct hid_device
*hdev
,
369 struct wacom_features
*features
)
371 struct hid_report_enum
*rep_enum
;
372 struct hid_report
*hreport
;
375 /* check features first */
376 rep_enum
= &hdev
->report_enum
[HID_FEATURE_REPORT
];
377 list_for_each_entry(hreport
, &rep_enum
->report_list
, list
) {
378 for (i
= 0; i
< hreport
->maxfield
; i
++) {
379 /* Ignore if report count is out of bounds. */
380 if (hreport
->field
[i
]->report_count
< 1)
383 for (j
= 0; j
< hreport
->field
[i
]->maxusage
; j
++) {
384 wacom_feature_mapping(hdev
, hreport
->field
[i
],
385 hreport
->field
[i
]->usage
+ j
);
390 /* now check the input usages */
391 rep_enum
= &hdev
->report_enum
[HID_INPUT_REPORT
];
392 list_for_each_entry(hreport
, &rep_enum
->report_list
, list
) {
394 if (!hreport
->maxfield
)
397 for (i
= 0; i
< hreport
->maxfield
; i
++)
398 for (j
= 0; j
< hreport
->field
[i
]->maxusage
; j
++)
399 wacom_usage_mapping(hdev
, hreport
->field
[i
],
400 hreport
->field
[i
]->usage
+ j
);
403 wacom_post_parse_hid(hdev
, features
);
406 static int wacom_hid_set_device_mode(struct hid_device
*hdev
)
408 struct wacom
*wacom
= hid_get_drvdata(hdev
);
409 struct hid_data
*hid_data
= &wacom
->wacom_wac
.hid_data
;
410 struct hid_report
*r
;
411 struct hid_report_enum
*re
;
413 if (hid_data
->inputmode
< 0)
416 re
= &(hdev
->report_enum
[HID_FEATURE_REPORT
]);
417 r
= re
->report_id_hash
[hid_data
->inputmode
];
419 r
->field
[0]->value
[hid_data
->inputmode_index
] = 2;
420 hid_hw_request(hdev
, r
, HID_REQ_SET_REPORT
);
425 static int wacom_set_device_mode(struct hid_device
*hdev
,
426 struct wacom_wac
*wacom_wac
)
429 struct hid_report
*r
;
430 struct hid_report_enum
*re
;
432 int error
= -ENOMEM
, limit
= 0;
434 if (wacom_wac
->mode_report
< 0)
437 re
= &(hdev
->report_enum
[HID_FEATURE_REPORT
]);
438 r
= re
->report_id_hash
[wacom_wac
->mode_report
];
442 rep_data
= hid_alloc_report_buf(r
, GFP_KERNEL
);
446 length
= hid_report_len(r
);
449 rep_data
[0] = wacom_wac
->mode_report
;
450 rep_data
[1] = wacom_wac
->mode_value
;
452 error
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
,
455 error
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
456 rep_data
, length
, 1);
457 } while (error
>= 0 &&
458 rep_data
[1] != wacom_wac
->mode_report
&&
459 limit
++ < WAC_MSG_RETRIES
);
463 return error
< 0 ? error
: 0;
466 static int wacom_bt_query_tablet_data(struct hid_device
*hdev
, u8 speed
,
467 struct wacom_features
*features
)
469 struct wacom
*wacom
= hid_get_drvdata(hdev
);
473 switch (features
->type
) {
477 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
, 2,
481 rep_data
[0] = speed
== 0 ? 0x05 : 0x06;
484 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
,
488 wacom
->wacom_wac
.bt_high_speed
= speed
;
494 * Note that if the raw queries fail, it's not a hard failure
495 * and it is safe to continue
497 hid_warn(hdev
, "failed to poke device, command %d, err %d\n",
502 wacom
->wacom_wac
.bt_features
&= ~0x20;
504 wacom
->wacom_wac
.bt_features
|= 0x20;
507 rep_data
[1] = wacom
->wacom_wac
.bt_features
;
509 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
, 2,
512 wacom
->wacom_wac
.bt_high_speed
= speed
;
520 * Switch the tablet into its most-capable mode. Wacom tablets are
521 * typically configured to power-up in a mode which sends mouse-like
522 * reports to the OS. To get absolute position, pressure data, etc.
523 * from the tablet, it is necessary to switch the tablet out of this
524 * mode and into one which sends the full range of tablet data.
526 static int _wacom_query_tablet_data(struct wacom
*wacom
)
528 struct hid_device
*hdev
= wacom
->hdev
;
529 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
530 struct wacom_features
*features
= &wacom_wac
->features
;
532 if (hdev
->bus
== BUS_BLUETOOTH
)
533 return wacom_bt_query_tablet_data(hdev
, 1, features
);
535 if (features
->type
!= HID_GENERIC
) {
536 if (features
->device_type
& WACOM_DEVICETYPE_TOUCH
) {
537 if (features
->type
> TABLETPC
) {
538 /* MT Tablet PC touch */
539 wacom_wac
->mode_report
= 3;
540 wacom_wac
->mode_value
= 4;
541 } else if (features
->type
== WACOM_24HDT
) {
542 wacom_wac
->mode_report
= 18;
543 wacom_wac
->mode_value
= 2;
544 } else if (features
->type
== WACOM_27QHDT
) {
545 wacom_wac
->mode_report
= 131;
546 wacom_wac
->mode_value
= 2;
547 } else if (features
->type
== BAMBOO_PAD
) {
548 wacom_wac
->mode_report
= 2;
549 wacom_wac
->mode_value
= 2;
551 } else if (features
->device_type
& WACOM_DEVICETYPE_PEN
) {
552 if (features
->type
<= BAMBOO_PT
) {
553 wacom_wac
->mode_report
= 2;
554 wacom_wac
->mode_value
= 2;
559 wacom_set_device_mode(hdev
, wacom_wac
);
561 if (features
->type
== HID_GENERIC
)
562 return wacom_hid_set_device_mode(hdev
);
567 static void wacom_retrieve_hid_descriptor(struct hid_device
*hdev
,
568 struct wacom_features
*features
)
570 struct wacom
*wacom
= hid_get_drvdata(hdev
);
571 struct usb_interface
*intf
= wacom
->intf
;
573 /* default features */
574 features
->x_fuzz
= 4;
575 features
->y_fuzz
= 4;
576 features
->pressure_fuzz
= 0;
577 features
->distance_fuzz
= 1;
578 features
->tilt_fuzz
= 1;
581 * The wireless device HID is basic and layout conflicts with
582 * other tablets (monitor and touch interface can look like pen).
583 * Skip the query for this type and modify defaults based on
586 if (features
->type
== WIRELESS
) {
587 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 0)
588 features
->device_type
= WACOM_DEVICETYPE_WL_MONITOR
;
590 features
->device_type
= WACOM_DEVICETYPE_NONE
;
594 wacom_parse_hid(hdev
, features
);
597 struct wacom_hdev_data
{
598 struct list_head list
;
600 struct hid_device
*dev
;
601 struct wacom_shared shared
;
604 static LIST_HEAD(wacom_udev_list
);
605 static DEFINE_MUTEX(wacom_udev_list_lock
);
607 static bool compare_device_paths(struct hid_device
*hdev_a
,
608 struct hid_device
*hdev_b
, char separator
)
610 int n1
= strrchr(hdev_a
->phys
, separator
) - hdev_a
->phys
;
611 int n2
= strrchr(hdev_b
->phys
, separator
) - hdev_b
->phys
;
613 if (n1
!= n2
|| n1
<= 0 || n2
<= 0)
616 return !strncmp(hdev_a
->phys
, hdev_b
->phys
, n1
);
619 static bool wacom_are_sibling(struct hid_device
*hdev
,
620 struct hid_device
*sibling
)
622 struct wacom
*wacom
= hid_get_drvdata(hdev
);
623 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
624 struct wacom
*sibling_wacom
= hid_get_drvdata(sibling
);
625 struct wacom_features
*sibling_features
= &sibling_wacom
->wacom_wac
.features
;
626 __u32 oVid
= features
->oVid
? features
->oVid
: hdev
->vendor
;
627 __u32 oPid
= features
->oPid
? features
->oPid
: hdev
->product
;
629 /* The defined oVid/oPid must match that of the sibling */
630 if (features
->oVid
!= HID_ANY_ID
&& sibling
->vendor
!= oVid
)
632 if (features
->oPid
!= HID_ANY_ID
&& sibling
->product
!= oPid
)
636 * Devices with the same VID/PID must share the same physical
637 * device path, while those with different VID/PID must share
638 * the same physical parent device path.
640 if (hdev
->vendor
== sibling
->vendor
&& hdev
->product
== sibling
->product
) {
641 if (!compare_device_paths(hdev
, sibling
, '/'))
644 if (!compare_device_paths(hdev
, sibling
, '.'))
648 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
649 if (features
->type
!= HID_GENERIC
)
653 * Direct-input devices may not be siblings of indirect-input
656 if ((features
->device_type
& WACOM_DEVICETYPE_DIRECT
) &&
657 !(sibling_features
->device_type
& WACOM_DEVICETYPE_DIRECT
))
661 * Indirect-input devices may not be siblings of direct-input
664 if (!(features
->device_type
& WACOM_DEVICETYPE_DIRECT
) &&
665 (sibling_features
->device_type
& WACOM_DEVICETYPE_DIRECT
))
668 /* Pen devices may only be siblings of touch devices */
669 if ((features
->device_type
& WACOM_DEVICETYPE_PEN
) &&
670 !(sibling_features
->device_type
& WACOM_DEVICETYPE_TOUCH
))
673 /* Touch devices may only be siblings of pen devices */
674 if ((features
->device_type
& WACOM_DEVICETYPE_TOUCH
) &&
675 !(sibling_features
->device_type
& WACOM_DEVICETYPE_PEN
))
679 * No reason could be found for these two devices to NOT be
680 * siblings, so there's a good chance they ARE siblings
685 static struct wacom_hdev_data
*wacom_get_hdev_data(struct hid_device
*hdev
)
687 struct wacom_hdev_data
*data
;
689 /* Try to find an already-probed interface from the same device */
690 list_for_each_entry(data
, &wacom_udev_list
, list
) {
691 if (compare_device_paths(hdev
, data
->dev
, '/')) {
692 kref_get(&data
->kref
);
697 /* Fallback to finding devices that appear to be "siblings" */
698 list_for_each_entry(data
, &wacom_udev_list
, list
) {
699 if (wacom_are_sibling(hdev
, data
->dev
)) {
700 kref_get(&data
->kref
);
708 static void wacom_release_shared_data(struct kref
*kref
)
710 struct wacom_hdev_data
*data
=
711 container_of(kref
, struct wacom_hdev_data
, kref
);
713 mutex_lock(&wacom_udev_list_lock
);
714 list_del(&data
->list
);
715 mutex_unlock(&wacom_udev_list_lock
);
720 static void wacom_remove_shared_data(void *res
)
722 struct wacom
*wacom
= res
;
723 struct wacom_hdev_data
*data
;
724 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
726 if (wacom_wac
->shared
) {
727 data
= container_of(wacom_wac
->shared
, struct wacom_hdev_data
,
730 if (wacom_wac
->shared
->touch
== wacom
->hdev
)
731 wacom_wac
->shared
->touch
= NULL
;
732 else if (wacom_wac
->shared
->pen
== wacom
->hdev
)
733 wacom_wac
->shared
->pen
= NULL
;
735 kref_put(&data
->kref
, wacom_release_shared_data
);
736 wacom_wac
->shared
= NULL
;
740 static int wacom_add_shared_data(struct hid_device
*hdev
)
742 struct wacom
*wacom
= hid_get_drvdata(hdev
);
743 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
744 struct wacom_hdev_data
*data
;
747 mutex_lock(&wacom_udev_list_lock
);
749 data
= wacom_get_hdev_data(hdev
);
751 data
= kzalloc(sizeof(struct wacom_hdev_data
), GFP_KERNEL
);
757 kref_init(&data
->kref
);
759 list_add_tail(&data
->list
, &wacom_udev_list
);
762 wacom_wac
->shared
= &data
->shared
;
764 retval
= devm_add_action(&hdev
->dev
, wacom_remove_shared_data
, wacom
);
766 mutex_unlock(&wacom_udev_list_lock
);
767 wacom_remove_shared_data(wacom
);
771 if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_TOUCH
)
772 wacom_wac
->shared
->touch
= hdev
;
773 else if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_PEN
)
774 wacom_wac
->shared
->pen
= hdev
;
777 mutex_unlock(&wacom_udev_list_lock
);
781 static int wacom_led_control(struct wacom
*wacom
)
785 unsigned char report_id
= WAC_CMD_LED_CONTROL
;
788 if (!wacom
->led
.groups
)
791 if (wacom
->wacom_wac
.features
.type
== REMOTE
)
794 if (wacom
->wacom_wac
.pid
) { /* wireless connected */
795 report_id
= WAC_CMD_WL_LED_CONTROL
;
798 else if (wacom
->wacom_wac
.features
.type
== INTUOSP2_BT
) {
799 report_id
= WAC_CMD_WL_INTUOSP2
;
802 buf
= kzalloc(buf_size
, GFP_KERNEL
);
806 if (wacom
->wacom_wac
.features
.type
== HID_GENERIC
) {
807 buf
[0] = WAC_CMD_LED_CONTROL_GENERIC
;
808 buf
[1] = wacom
->led
.llv
;
809 buf
[2] = wacom
->led
.groups
[0].select
& 0x03;
811 } else if ((wacom
->wacom_wac
.features
.type
>= INTUOS5S
&&
812 wacom
->wacom_wac
.features
.type
<= INTUOSPL
)) {
814 * Touch Ring and crop mark LED luminance may take on
815 * one of four values:
816 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
818 int ring_led
= wacom
->led
.groups
[0].select
& 0x03;
819 int ring_lum
= (((wacom
->led
.llv
& 0x60) >> 5) - 1) & 0x03;
821 unsigned char led_bits
= (crop_lum
<< 4) | (ring_lum
<< 2) | (ring_led
);
824 if (wacom
->wacom_wac
.pid
) {
825 wacom_get_report(wacom
->hdev
, HID_FEATURE_REPORT
,
826 buf
, buf_size
, WAC_CMD_RETRIES
);
832 else if (wacom
->wacom_wac
.features
.type
== INTUOSP2_BT
) {
834 buf
[4] = 100; // Power Connection LED (ORANGE)
835 buf
[5] = 100; // BT Connection LED (BLUE)
836 buf
[6] = 100; // Paper Mode (RED?)
837 buf
[7] = 100; // Paper Mode (GREEN?)
838 buf
[8] = 100; // Paper Mode (BLUE?)
839 buf
[9] = wacom
->led
.llv
;
840 buf
[10] = wacom
->led
.groups
[0].select
& 0x03;
843 int led
= wacom
->led
.groups
[0].select
| 0x4;
845 if (wacom
->wacom_wac
.features
.type
== WACOM_21UX2
||
846 wacom
->wacom_wac
.features
.type
== WACOM_24HD
)
847 led
|= (wacom
->led
.groups
[1].select
<< 4) | 0x40;
851 buf
[2] = wacom
->led
.llv
;
852 buf
[3] = wacom
->led
.hlv
;
853 buf
[4] = wacom
->led
.img_lum
;
856 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, buf_size
,
863 static int wacom_led_putimage(struct wacom
*wacom
, int button_id
, u8 xfer_id
,
864 const unsigned len
, const void *img
)
868 const unsigned chunk_len
= len
/ 4; /* 4 chunks are needed to be sent */
870 buf
= kzalloc(chunk_len
+ 3 , GFP_KERNEL
);
874 /* Send 'start' command */
875 buf
[0] = WAC_CMD_ICON_START
;
877 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, 2,
883 buf
[1] = button_id
& 0x07;
884 for (i
= 0; i
< 4; i
++) {
886 memcpy(buf
+ 3, img
+ i
* chunk_len
, chunk_len
);
888 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
,
889 buf
, chunk_len
+ 3, WAC_CMD_RETRIES
);
895 buf
[0] = WAC_CMD_ICON_START
;
897 wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, 2,
905 static ssize_t
wacom_led_select_store(struct device
*dev
, int set_id
,
906 const char *buf
, size_t count
)
908 struct hid_device
*hdev
= to_hid_device(dev
);
909 struct wacom
*wacom
= hid_get_drvdata(hdev
);
913 err
= kstrtouint(buf
, 10, &id
);
917 mutex_lock(&wacom
->lock
);
919 wacom
->led
.groups
[set_id
].select
= id
& 0x3;
920 err
= wacom_led_control(wacom
);
922 mutex_unlock(&wacom
->lock
);
924 return err
< 0 ? err
: count
;
927 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
928 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
929 struct device_attribute *attr, const char *buf, size_t count) \
931 return wacom_led_select_store(dev, SET_ID, buf, count); \
933 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
934 struct device_attribute *attr, char *buf) \
936 struct hid_device *hdev = to_hid_device(dev);\
937 struct wacom *wacom = hid_get_drvdata(hdev); \
938 return scnprintf(buf, PAGE_SIZE, "%d\n", \
939 wacom->led.groups[SET_ID].select); \
941 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
942 wacom_led##SET_ID##_select_show, \
943 wacom_led##SET_ID##_select_store)
945 DEVICE_LED_SELECT_ATTR(0);
946 DEVICE_LED_SELECT_ATTR(1);
948 static ssize_t
wacom_luminance_store(struct wacom
*wacom
, u8
*dest
,
949 const char *buf
, size_t count
)
954 err
= kstrtouint(buf
, 10, &value
);
958 mutex_lock(&wacom
->lock
);
960 *dest
= value
& 0x7f;
961 err
= wacom_led_control(wacom
);
963 mutex_unlock(&wacom
->lock
);
965 return err
< 0 ? err
: count
;
968 #define DEVICE_LUMINANCE_ATTR(name, field) \
969 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
970 struct device_attribute *attr, const char *buf, size_t count) \
972 struct hid_device *hdev = to_hid_device(dev);\
973 struct wacom *wacom = hid_get_drvdata(hdev); \
975 return wacom_luminance_store(wacom, &wacom->led.field, \
978 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
979 struct device_attribute *attr, char *buf) \
981 struct wacom *wacom = dev_get_drvdata(dev); \
982 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
984 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
985 wacom_##name##_luminance_show, \
986 wacom_##name##_luminance_store)
988 DEVICE_LUMINANCE_ATTR(status0
, llv
);
989 DEVICE_LUMINANCE_ATTR(status1
, hlv
);
990 DEVICE_LUMINANCE_ATTR(buttons
, img_lum
);
992 static ssize_t
wacom_button_image_store(struct device
*dev
, int button_id
,
993 const char *buf
, size_t count
)
995 struct hid_device
*hdev
= to_hid_device(dev
);
996 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1001 if (hdev
->bus
== BUS_BLUETOOTH
) {
1003 xfer_id
= WAC_CMD_ICON_BT_XFER
;
1006 xfer_id
= WAC_CMD_ICON_XFER
;
1012 mutex_lock(&wacom
->lock
);
1014 err
= wacom_led_putimage(wacom
, button_id
, xfer_id
, len
, buf
);
1016 mutex_unlock(&wacom
->lock
);
1018 return err
< 0 ? err
: count
;
1021 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1022 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1023 struct device_attribute *attr, const char *buf, size_t count) \
1025 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1027 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
1028 NULL, wacom_btnimg##BUTTON_ID##_store)
1030 DEVICE_BTNIMG_ATTR(0);
1031 DEVICE_BTNIMG_ATTR(1);
1032 DEVICE_BTNIMG_ATTR(2);
1033 DEVICE_BTNIMG_ATTR(3);
1034 DEVICE_BTNIMG_ATTR(4);
1035 DEVICE_BTNIMG_ATTR(5);
1036 DEVICE_BTNIMG_ATTR(6);
1037 DEVICE_BTNIMG_ATTR(7);
1039 static struct attribute
*cintiq_led_attrs
[] = {
1040 &dev_attr_status_led0_select
.attr
,
1041 &dev_attr_status_led1_select
.attr
,
1045 static struct attribute_group cintiq_led_attr_group
= {
1046 .name
= "wacom_led",
1047 .attrs
= cintiq_led_attrs
,
1050 static struct attribute
*intuos4_led_attrs
[] = {
1051 &dev_attr_status0_luminance
.attr
,
1052 &dev_attr_status1_luminance
.attr
,
1053 &dev_attr_status_led0_select
.attr
,
1054 &dev_attr_buttons_luminance
.attr
,
1055 &dev_attr_button0_rawimg
.attr
,
1056 &dev_attr_button1_rawimg
.attr
,
1057 &dev_attr_button2_rawimg
.attr
,
1058 &dev_attr_button3_rawimg
.attr
,
1059 &dev_attr_button4_rawimg
.attr
,
1060 &dev_attr_button5_rawimg
.attr
,
1061 &dev_attr_button6_rawimg
.attr
,
1062 &dev_attr_button7_rawimg
.attr
,
1066 static struct attribute_group intuos4_led_attr_group
= {
1067 .name
= "wacom_led",
1068 .attrs
= intuos4_led_attrs
,
1071 static struct attribute
*intuos5_led_attrs
[] = {
1072 &dev_attr_status0_luminance
.attr
,
1073 &dev_attr_status_led0_select
.attr
,
1077 static struct attribute_group intuos5_led_attr_group
= {
1078 .name
= "wacom_led",
1079 .attrs
= intuos5_led_attrs
,
1082 static struct attribute
*generic_led_attrs
[] = {
1083 &dev_attr_status0_luminance
.attr
,
1084 &dev_attr_status_led0_select
.attr
,
1088 static struct attribute_group generic_led_attr_group
= {
1089 .name
= "wacom_led",
1090 .attrs
= generic_led_attrs
,
1093 struct wacom_sysfs_group_devres
{
1094 struct attribute_group
*group
;
1095 struct kobject
*root
;
1098 static void wacom_devm_sysfs_group_release(struct device
*dev
, void *res
)
1100 struct wacom_sysfs_group_devres
*devres
= res
;
1101 struct kobject
*kobj
= devres
->root
;
1103 dev_dbg(dev
, "%s: dropping reference to %s\n",
1104 __func__
, devres
->group
->name
);
1105 sysfs_remove_group(kobj
, devres
->group
);
1108 static int __wacom_devm_sysfs_create_group(struct wacom
*wacom
,
1109 struct kobject
*root
,
1110 struct attribute_group
*group
)
1112 struct wacom_sysfs_group_devres
*devres
;
1115 devres
= devres_alloc(wacom_devm_sysfs_group_release
,
1116 sizeof(struct wacom_sysfs_group_devres
),
1121 devres
->group
= group
;
1122 devres
->root
= root
;
1124 error
= sysfs_create_group(devres
->root
, group
);
1126 devres_free(devres
);
1130 devres_add(&wacom
->hdev
->dev
, devres
);
1135 static int wacom_devm_sysfs_create_group(struct wacom
*wacom
,
1136 struct attribute_group
*group
)
1138 return __wacom_devm_sysfs_create_group(wacom
, &wacom
->hdev
->dev
.kobj
,
1142 enum led_brightness
wacom_leds_brightness_get(struct wacom_led
*led
)
1144 struct wacom
*wacom
= led
->wacom
;
1146 if (wacom
->led
.max_hlv
)
1147 return led
->hlv
* LED_FULL
/ wacom
->led
.max_hlv
;
1149 if (wacom
->led
.max_llv
)
1150 return led
->llv
* LED_FULL
/ wacom
->led
.max_llv
;
1152 /* device doesn't support brightness tuning */
1156 static enum led_brightness
__wacom_led_brightness_get(struct led_classdev
*cdev
)
1158 struct wacom_led
*led
= container_of(cdev
, struct wacom_led
, cdev
);
1159 struct wacom
*wacom
= led
->wacom
;
1161 if (wacom
->led
.groups
[led
->group
].select
!= led
->id
)
1164 return wacom_leds_brightness_get(led
);
1167 static int wacom_led_brightness_set(struct led_classdev
*cdev
,
1168 enum led_brightness brightness
)
1170 struct wacom_led
*led
= container_of(cdev
, struct wacom_led
, cdev
);
1171 struct wacom
*wacom
= led
->wacom
;
1174 mutex_lock(&wacom
->lock
);
1176 if (!wacom
->led
.groups
|| (brightness
== LED_OFF
&&
1177 wacom
->led
.groups
[led
->group
].select
!= led
->id
)) {
1182 led
->llv
= wacom
->led
.llv
= wacom
->led
.max_llv
* brightness
/ LED_FULL
;
1183 led
->hlv
= wacom
->led
.hlv
= wacom
->led
.max_hlv
* brightness
/ LED_FULL
;
1185 wacom
->led
.groups
[led
->group
].select
= led
->id
;
1187 error
= wacom_led_control(wacom
);
1190 mutex_unlock(&wacom
->lock
);
1195 static void wacom_led_readonly_brightness_set(struct led_classdev
*cdev
,
1196 enum led_brightness brightness
)
1200 static int wacom_led_register_one(struct device
*dev
, struct wacom
*wacom
,
1201 struct wacom_led
*led
, unsigned int group
,
1202 unsigned int id
, bool read_only
)
1207 name
= devm_kasprintf(dev
, GFP_KERNEL
,
1216 led
->trigger
.name
= name
;
1217 error
= devm_led_trigger_register(dev
, &led
->trigger
);
1219 hid_err(wacom
->hdev
,
1220 "failed to register LED trigger %s: %d\n",
1221 led
->cdev
.name
, error
);
1229 led
->llv
= wacom
->led
.llv
;
1230 led
->hlv
= wacom
->led
.hlv
;
1231 led
->cdev
.name
= name
;
1232 led
->cdev
.max_brightness
= LED_FULL
;
1233 led
->cdev
.flags
= LED_HW_PLUGGABLE
;
1234 led
->cdev
.brightness_get
= __wacom_led_brightness_get
;
1236 led
->cdev
.brightness_set_blocking
= wacom_led_brightness_set
;
1237 led
->cdev
.default_trigger
= led
->cdev
.name
;
1239 led
->cdev
.brightness_set
= wacom_led_readonly_brightness_set
;
1242 error
= devm_led_classdev_register(dev
, &led
->cdev
);
1244 hid_err(wacom
->hdev
,
1245 "failed to register LED %s: %d\n",
1246 led
->cdev
.name
, error
);
1247 led
->cdev
.name
= NULL
;
1254 static void wacom_led_groups_release_one(void *data
)
1256 struct wacom_group_leds
*group
= data
;
1258 devres_release_group(group
->dev
, group
);
1261 static int wacom_led_groups_alloc_and_register_one(struct device
*dev
,
1262 struct wacom
*wacom
,
1263 int group_id
, int count
,
1266 struct wacom_led
*leds
;
1269 if (group_id
>= wacom
->led
.count
|| count
<= 0)
1272 if (!devres_open_group(dev
, &wacom
->led
.groups
[group_id
], GFP_KERNEL
))
1275 leds
= devm_kzalloc(dev
, sizeof(struct wacom_led
) * count
, GFP_KERNEL
);
1281 wacom
->led
.groups
[group_id
].leds
= leds
;
1282 wacom
->led
.groups
[group_id
].count
= count
;
1284 for (i
= 0; i
< count
; i
++) {
1285 error
= wacom_led_register_one(dev
, wacom
, &leds
[i
],
1286 group_id
, i
, read_only
);
1291 wacom
->led
.groups
[group_id
].dev
= dev
;
1293 devres_close_group(dev
, &wacom
->led
.groups
[group_id
]);
1296 * There is a bug (?) in devm_led_classdev_register() in which its
1297 * increments the refcount of the parent. If the parent is an input
1298 * device, that means the ref count never reaches 0 when
1299 * devm_input_device_release() gets called.
1300 * This means that the LEDs are still there after disconnect.
1301 * Manually force the release of the group so that the leds are released
1302 * once we are done using them.
1304 error
= devm_add_action_or_reset(&wacom
->hdev
->dev
,
1305 wacom_led_groups_release_one
,
1306 &wacom
->led
.groups
[group_id
]);
1313 devres_release_group(dev
, &wacom
->led
.groups
[group_id
]);
1317 struct wacom_led
*wacom_led_find(struct wacom
*wacom
, unsigned int group_id
,
1320 struct wacom_group_leds
*group
;
1322 if (group_id
>= wacom
->led
.count
)
1325 group
= &wacom
->led
.groups
[group_id
];
1332 return &group
->leds
[id
];
1336 * wacom_led_next: gives the next available led with a wacom trigger.
1338 * returns the next available struct wacom_led which has its default trigger
1339 * or the current one if none is available.
1341 struct wacom_led
*wacom_led_next(struct wacom
*wacom
, struct wacom_led
*cur
)
1343 struct wacom_led
*next_led
;
1353 next_led
= wacom_led_find(wacom
, group
, ++next
);
1354 if (!next_led
|| next_led
== cur
)
1356 } while (next_led
->cdev
.trigger
!= &next_led
->trigger
);
1361 static void wacom_led_groups_release(void *data
)
1363 struct wacom
*wacom
= data
;
1365 wacom
->led
.groups
= NULL
;
1366 wacom
->led
.count
= 0;
1369 static int wacom_led_groups_allocate(struct wacom
*wacom
, int count
)
1371 struct device
*dev
= &wacom
->hdev
->dev
;
1372 struct wacom_group_leds
*groups
;
1375 groups
= devm_kzalloc(dev
, sizeof(struct wacom_group_leds
) * count
,
1380 error
= devm_add_action_or_reset(dev
, wacom_led_groups_release
, wacom
);
1384 wacom
->led
.groups
= groups
;
1385 wacom
->led
.count
= count
;
1390 static int wacom_leds_alloc_and_register(struct wacom
*wacom
, int group_count
,
1391 int led_per_group
, bool read_only
)
1396 if (!wacom
->wacom_wac
.pad_input
)
1399 dev
= &wacom
->wacom_wac
.pad_input
->dev
;
1401 error
= wacom_led_groups_allocate(wacom
, group_count
);
1405 for (i
= 0; i
< group_count
; i
++) {
1406 error
= wacom_led_groups_alloc_and_register_one(dev
, wacom
, i
,
1416 int wacom_initialize_leds(struct wacom
*wacom
)
1420 if (!(wacom
->wacom_wac
.features
.device_type
& WACOM_DEVICETYPE_PAD
))
1423 /* Initialize default values */
1424 switch (wacom
->wacom_wac
.features
.type
) {
1426 if (!wacom
->generic_has_leds
)
1428 wacom
->led
.llv
= 100;
1429 wacom
->led
.max_llv
= 100;
1431 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1433 hid_err(wacom
->hdev
,
1434 "cannot create leds err: %d\n", error
);
1438 error
= wacom_devm_sysfs_create_group(wacom
,
1439 &generic_led_attr_group
);
1446 wacom
->led
.llv
= 10;
1447 wacom
->led
.hlv
= 20;
1448 wacom
->led
.max_llv
= 127;
1449 wacom
->led
.max_hlv
= 127;
1450 wacom
->led
.img_lum
= 10;
1452 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1454 hid_err(wacom
->hdev
,
1455 "cannot create leds err: %d\n", error
);
1459 error
= wacom_devm_sysfs_create_group(wacom
,
1460 &intuos4_led_attr_group
);
1467 wacom
->led
.img_lum
= 0;
1469 error
= wacom_leds_alloc_and_register(wacom
, 2, 4, false);
1471 hid_err(wacom
->hdev
,
1472 "cannot create leds err: %d\n", error
);
1476 error
= wacom_devm_sysfs_create_group(wacom
,
1477 &cintiq_led_attr_group
);
1486 wacom
->led
.llv
= 32;
1487 wacom
->led
.max_llv
= 96;
1489 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1491 hid_err(wacom
->hdev
,
1492 "cannot create leds err: %d\n", error
);
1496 error
= wacom_devm_sysfs_create_group(wacom
,
1497 &intuos5_led_attr_group
);
1501 wacom
->led
.llv
= 50;
1502 wacom
->led
.max_llv
= 100;
1503 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1505 hid_err(wacom
->hdev
,
1506 "cannot create leds err: %d\n", error
);
1512 wacom
->led
.llv
= 255;
1513 wacom
->led
.max_llv
= 255;
1514 error
= wacom_led_groups_allocate(wacom
, 5);
1516 hid_err(wacom
->hdev
,
1517 "cannot create leds err: %d\n", error
);
1527 hid_err(wacom
->hdev
,
1528 "cannot create sysfs group err: %d\n", error
);
1535 static void wacom_init_work(struct work_struct
*work
)
1537 struct wacom
*wacom
= container_of(work
, struct wacom
, init_work
.work
);
1539 _wacom_query_tablet_data(wacom
);
1540 wacom_led_control(wacom
);
1543 static void wacom_query_tablet_data(struct wacom
*wacom
)
1545 schedule_delayed_work(&wacom
->init_work
, msecs_to_jiffies(1000));
1548 static enum power_supply_property wacom_battery_props
[] = {
1549 POWER_SUPPLY_PROP_MODEL_NAME
,
1550 POWER_SUPPLY_PROP_PRESENT
,
1551 POWER_SUPPLY_PROP_STATUS
,
1552 POWER_SUPPLY_PROP_SCOPE
,
1553 POWER_SUPPLY_PROP_CAPACITY
1556 static int wacom_battery_get_property(struct power_supply
*psy
,
1557 enum power_supply_property psp
,
1558 union power_supply_propval
*val
)
1560 struct wacom_battery
*battery
= power_supply_get_drvdata(psy
);
1564 case POWER_SUPPLY_PROP_MODEL_NAME
:
1565 val
->strval
= battery
->wacom
->wacom_wac
.name
;
1567 case POWER_SUPPLY_PROP_PRESENT
:
1568 val
->intval
= battery
->bat_connected
;
1570 case POWER_SUPPLY_PROP_SCOPE
:
1571 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
1573 case POWER_SUPPLY_PROP_CAPACITY
:
1574 val
->intval
= battery
->battery_capacity
;
1576 case POWER_SUPPLY_PROP_STATUS
:
1577 if (battery
->bat_status
!= WACOM_POWER_SUPPLY_STATUS_AUTO
)
1578 val
->intval
= battery
->bat_status
;
1579 else if (battery
->bat_charging
)
1580 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
1581 else if (battery
->battery_capacity
== 100 &&
1582 battery
->ps_connected
)
1583 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
1584 else if (battery
->ps_connected
)
1585 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1587 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
1597 static int __wacom_initialize_battery(struct wacom
*wacom
,
1598 struct wacom_battery
*battery
)
1600 static atomic_t battery_no
= ATOMIC_INIT(0);
1601 struct device
*dev
= &wacom
->hdev
->dev
;
1602 struct power_supply_config psy_cfg
= { .drv_data
= battery
, };
1603 struct power_supply
*ps_bat
;
1604 struct power_supply_desc
*bat_desc
= &battery
->bat_desc
;
1608 if (!devres_open_group(dev
, bat_desc
, GFP_KERNEL
))
1611 battery
->wacom
= wacom
;
1613 n
= atomic_inc_return(&battery_no
) - 1;
1615 bat_desc
->properties
= wacom_battery_props
;
1616 bat_desc
->num_properties
= ARRAY_SIZE(wacom_battery_props
);
1617 bat_desc
->get_property
= wacom_battery_get_property
;
1618 sprintf(battery
->bat_name
, "wacom_battery_%ld", n
);
1619 bat_desc
->name
= battery
->bat_name
;
1620 bat_desc
->type
= POWER_SUPPLY_TYPE_USB
;
1621 bat_desc
->use_for_apm
= 0;
1623 ps_bat
= devm_power_supply_register(dev
, bat_desc
, &psy_cfg
);
1624 if (IS_ERR(ps_bat
)) {
1625 error
= PTR_ERR(ps_bat
);
1629 power_supply_powers(ps_bat
, &wacom
->hdev
->dev
);
1631 battery
->battery
= ps_bat
;
1633 devres_close_group(dev
, bat_desc
);
1637 devres_release_group(dev
, bat_desc
);
1641 static int wacom_initialize_battery(struct wacom
*wacom
)
1643 if (wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
)
1644 return __wacom_initialize_battery(wacom
, &wacom
->battery
);
1649 static void wacom_destroy_battery(struct wacom
*wacom
)
1651 if (wacom
->battery
.battery
) {
1652 devres_release_group(&wacom
->hdev
->dev
,
1653 &wacom
->battery
.bat_desc
);
1654 wacom
->battery
.battery
= NULL
;
1658 static ssize_t
wacom_show_speed(struct device
*dev
,
1659 struct device_attribute
1662 struct hid_device
*hdev
= to_hid_device(dev
);
1663 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1665 return snprintf(buf
, PAGE_SIZE
, "%i\n", wacom
->wacom_wac
.bt_high_speed
);
1668 static ssize_t
wacom_store_speed(struct device
*dev
,
1669 struct device_attribute
*attr
,
1670 const char *buf
, size_t count
)
1672 struct hid_device
*hdev
= to_hid_device(dev
);
1673 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1676 if (kstrtou8(buf
, 0, &new_speed
))
1679 if (new_speed
!= 0 && new_speed
!= 1)
1682 wacom_bt_query_tablet_data(hdev
, new_speed
, &wacom
->wacom_wac
.features
);
1687 static DEVICE_ATTR(speed
, DEV_ATTR_RW_PERM
,
1688 wacom_show_speed
, wacom_store_speed
);
1691 static ssize_t
wacom_show_remote_mode(struct kobject
*kobj
,
1692 struct kobj_attribute
*kattr
,
1693 char *buf
, int index
)
1695 struct device
*dev
= kobj_to_dev(kobj
->parent
);
1696 struct hid_device
*hdev
= to_hid_device(dev
);
1697 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1700 mode
= wacom
->led
.groups
[index
].select
;
1701 return sprintf(buf
, "%d\n", mode
< 3 ? mode
: -1);
1704 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1705 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1706 struct kobj_attribute *kattr, char *buf) \
1708 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1710 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1711 .attr = {.name = "remote_mode", \
1712 .mode = DEV_ATTR_RO_PERM}, \
1713 .show = wacom_show_remote##SET_ID##_mode, \
1715 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1716 &remote##SET_ID##_mode_attr.attr, \
1719 static struct attribute_group remote##SET_ID##_serial_group = { \
1721 .attrs = remote##SET_ID##_serial_attrs, \
1724 DEVICE_EKR_ATTR_GROUP(0);
1725 DEVICE_EKR_ATTR_GROUP(1);
1726 DEVICE_EKR_ATTR_GROUP(2);
1727 DEVICE_EKR_ATTR_GROUP(3);
1728 DEVICE_EKR_ATTR_GROUP(4);
1730 static int wacom_remote_create_attr_group(struct wacom
*wacom
, __u32 serial
,
1734 struct wacom_remote
*remote
= wacom
->remote
;
1736 remote
->remotes
[index
].group
.name
= devm_kasprintf(&wacom
->hdev
->dev
,
1739 if (!remote
->remotes
[index
].group
.name
)
1742 error
= __wacom_devm_sysfs_create_group(wacom
, remote
->remote_dir
,
1743 &remote
->remotes
[index
].group
);
1745 remote
->remotes
[index
].group
.name
= NULL
;
1746 hid_err(wacom
->hdev
,
1747 "cannot create sysfs group err: %d\n", error
);
1754 static int wacom_cmd_unpair_remote(struct wacom
*wacom
, unsigned char selector
)
1756 const size_t buf_size
= 2;
1760 buf
= kzalloc(buf_size
, GFP_KERNEL
);
1764 buf
[0] = WAC_CMD_DELETE_PAIRING
;
1767 retval
= wacom_set_report(wacom
->hdev
, HID_OUTPUT_REPORT
, buf
,
1768 buf_size
, WAC_CMD_RETRIES
);
1774 static ssize_t
wacom_store_unpair_remote(struct kobject
*kobj
,
1775 struct kobj_attribute
*attr
,
1776 const char *buf
, size_t count
)
1778 unsigned char selector
= 0;
1779 struct device
*dev
= kobj_to_dev(kobj
->parent
);
1780 struct hid_device
*hdev
= to_hid_device(dev
);
1781 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1784 if (!strncmp(buf
, "*\n", 2)) {
1785 selector
= WAC_CMD_UNPAIR_ALL
;
1787 hid_info(wacom
->hdev
, "remote: unrecognized unpair code: %s\n",
1792 mutex_lock(&wacom
->lock
);
1794 err
= wacom_cmd_unpair_remote(wacom
, selector
);
1795 mutex_unlock(&wacom
->lock
);
1797 return err
< 0 ? err
: count
;
1800 static struct kobj_attribute unpair_remote_attr
= {
1801 .attr
= {.name
= "unpair_remote", .mode
= 0200},
1802 .store
= wacom_store_unpair_remote
,
1805 static const struct attribute
*remote_unpair_attrs
[] = {
1806 &unpair_remote_attr
.attr
,
1810 static void wacom_remotes_destroy(void *data
)
1812 struct wacom
*wacom
= data
;
1813 struct wacom_remote
*remote
= wacom
->remote
;
1818 kobject_put(remote
->remote_dir
);
1819 kfifo_free(&remote
->remote_fifo
);
1820 wacom
->remote
= NULL
;
1823 static int wacom_initialize_remotes(struct wacom
*wacom
)
1826 struct wacom_remote
*remote
;
1829 if (wacom
->wacom_wac
.features
.type
!= REMOTE
)
1832 remote
= devm_kzalloc(&wacom
->hdev
->dev
, sizeof(*wacom
->remote
),
1837 wacom
->remote
= remote
;
1839 spin_lock_init(&remote
->remote_lock
);
1841 error
= kfifo_alloc(&remote
->remote_fifo
,
1842 5 * sizeof(struct wacom_remote_data
),
1845 hid_err(wacom
->hdev
, "failed allocating remote_fifo\n");
1849 remote
->remotes
[0].group
= remote0_serial_group
;
1850 remote
->remotes
[1].group
= remote1_serial_group
;
1851 remote
->remotes
[2].group
= remote2_serial_group
;
1852 remote
->remotes
[3].group
= remote3_serial_group
;
1853 remote
->remotes
[4].group
= remote4_serial_group
;
1855 remote
->remote_dir
= kobject_create_and_add("wacom_remote",
1856 &wacom
->hdev
->dev
.kobj
);
1857 if (!remote
->remote_dir
)
1860 error
= sysfs_create_files(remote
->remote_dir
, remote_unpair_attrs
);
1863 hid_err(wacom
->hdev
,
1864 "cannot create sysfs group err: %d\n", error
);
1868 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
1869 wacom
->led
.groups
[i
].select
= WACOM_STATUS_UNKNOWN
;
1870 remote
->remotes
[i
].serial
= 0;
1873 error
= devm_add_action_or_reset(&wacom
->hdev
->dev
,
1874 wacom_remotes_destroy
, wacom
);
1881 static struct input_dev
*wacom_allocate_input(struct wacom
*wacom
)
1883 struct input_dev
*input_dev
;
1884 struct hid_device
*hdev
= wacom
->hdev
;
1885 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1887 input_dev
= devm_input_allocate_device(&hdev
->dev
);
1891 input_dev
->name
= wacom_wac
->features
.name
;
1892 input_dev
->phys
= hdev
->phys
;
1893 input_dev
->dev
.parent
= &hdev
->dev
;
1894 input_dev
->open
= wacom_open
;
1895 input_dev
->close
= wacom_close
;
1896 input_dev
->uniq
= hdev
->uniq
;
1897 input_dev
->id
.bustype
= hdev
->bus
;
1898 input_dev
->id
.vendor
= hdev
->vendor
;
1899 input_dev
->id
.product
= wacom_wac
->pid
? wacom_wac
->pid
: hdev
->product
;
1900 input_dev
->id
.version
= hdev
->version
;
1901 input_set_drvdata(input_dev
, wacom
);
1906 static int wacom_allocate_inputs(struct wacom
*wacom
)
1908 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1910 wacom_wac
->pen_input
= wacom_allocate_input(wacom
);
1911 wacom_wac
->touch_input
= wacom_allocate_input(wacom
);
1912 wacom_wac
->pad_input
= wacom_allocate_input(wacom
);
1913 if (!wacom_wac
->pen_input
||
1914 !wacom_wac
->touch_input
||
1915 !wacom_wac
->pad_input
)
1918 wacom_wac
->pen_input
->name
= wacom_wac
->pen_name
;
1919 wacom_wac
->touch_input
->name
= wacom_wac
->touch_name
;
1920 wacom_wac
->pad_input
->name
= wacom_wac
->pad_name
;
1925 static int wacom_register_inputs(struct wacom
*wacom
)
1927 struct input_dev
*pen_input_dev
, *touch_input_dev
, *pad_input_dev
;
1928 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1931 pen_input_dev
= wacom_wac
->pen_input
;
1932 touch_input_dev
= wacom_wac
->touch_input
;
1933 pad_input_dev
= wacom_wac
->pad_input
;
1935 if (!pen_input_dev
|| !touch_input_dev
|| !pad_input_dev
)
1938 error
= wacom_setup_pen_input_capabilities(pen_input_dev
, wacom_wac
);
1940 /* no pen in use on this interface */
1941 input_free_device(pen_input_dev
);
1942 wacom_wac
->pen_input
= NULL
;
1943 pen_input_dev
= NULL
;
1945 error
= input_register_device(pen_input_dev
);
1950 error
= wacom_setup_touch_input_capabilities(touch_input_dev
, wacom_wac
);
1952 /* no touch in use on this interface */
1953 input_free_device(touch_input_dev
);
1954 wacom_wac
->touch_input
= NULL
;
1955 touch_input_dev
= NULL
;
1957 error
= input_register_device(touch_input_dev
);
1962 error
= wacom_setup_pad_input_capabilities(pad_input_dev
, wacom_wac
);
1964 /* no pad in use on this interface */
1965 input_free_device(pad_input_dev
);
1966 wacom_wac
->pad_input
= NULL
;
1967 pad_input_dev
= NULL
;
1969 error
= input_register_device(pad_input_dev
);
1977 wacom_wac
->pad_input
= NULL
;
1978 wacom_wac
->touch_input
= NULL
;
1979 wacom_wac
->pen_input
= NULL
;
1984 * Not all devices report physical dimensions from HID.
1985 * Compute the default from hardcoded logical dimension
1986 * and resolution before driver overwrites them.
1988 static void wacom_set_default_phy(struct wacom_features
*features
)
1990 if (features
->x_resolution
) {
1991 features
->x_phy
= (features
->x_max
* 100) /
1992 features
->x_resolution
;
1993 features
->y_phy
= (features
->y_max
* 100) /
1994 features
->y_resolution
;
1998 static void wacom_calculate_res(struct wacom_features
*features
)
2000 /* set unit to "100th of a mm" for devices not reported by HID */
2001 if (!features
->unit
) {
2002 features
->unit
= 0x11;
2003 features
->unitExpo
= -3;
2006 features
->x_resolution
= wacom_calc_hid_res(features
->x_max
,
2009 features
->unitExpo
);
2010 features
->y_resolution
= wacom_calc_hid_res(features
->y_max
,
2013 features
->unitExpo
);
2016 void wacom_battery_work(struct work_struct
*work
)
2018 struct wacom
*wacom
= container_of(work
, struct wacom
, battery_work
);
2020 if ((wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
) &&
2021 !wacom
->battery
.battery
) {
2022 wacom_initialize_battery(wacom
);
2024 else if (!(wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
) &&
2025 wacom
->battery
.battery
) {
2026 wacom_destroy_battery(wacom
);
2030 static size_t wacom_compute_pktlen(struct hid_device
*hdev
)
2032 struct hid_report_enum
*report_enum
;
2033 struct hid_report
*report
;
2036 report_enum
= hdev
->report_enum
+ HID_INPUT_REPORT
;
2038 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
2039 size_t report_size
= hid_report_len(report
);
2040 if (report_size
> size
)
2047 static void wacom_update_name(struct wacom
*wacom
, const char *suffix
)
2049 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2050 struct wacom_features
*features
= &wacom_wac
->features
;
2051 char name
[WACOM_NAME_MAX
];
2053 /* Generic devices name unspecified */
2054 if ((features
->type
== HID_GENERIC
) && !strcmp("Wacom HID", features
->name
)) {
2055 char *product_name
= wacom
->hdev
->name
;
2057 if (hid_is_using_ll_driver(wacom
->hdev
, &usb_hid_driver
)) {
2058 struct usb_interface
*intf
= to_usb_interface(wacom
->hdev
->dev
.parent
);
2059 struct usb_device
*dev
= interface_to_usbdev(intf
);
2060 product_name
= dev
->product
;
2063 if (wacom
->hdev
->bus
== BUS_I2C
) {
2064 snprintf(name
, sizeof(name
), "%s %X",
2065 features
->name
, wacom
->hdev
->product
);
2066 } else if (strstr(product_name
, "Wacom") ||
2067 strstr(product_name
, "wacom") ||
2068 strstr(product_name
, "WACOM")) {
2069 strlcpy(name
, product_name
, sizeof(name
));
2071 snprintf(name
, sizeof(name
), "Wacom %s", product_name
);
2074 /* strip out excess whitespaces */
2076 char *gap
= strstr(name
, " ");
2079 /* shift everything including the terminator */
2080 memmove(gap
, gap
+1, strlen(gap
));
2083 /* get rid of trailing whitespace */
2084 if (name
[strlen(name
)-1] == ' ')
2085 name
[strlen(name
)-1] = '\0';
2087 strlcpy(name
, features
->name
, sizeof(name
));
2090 snprintf(wacom_wac
->name
, sizeof(wacom_wac
->name
), "%s%s",
2093 /* Append the device type to the name */
2094 snprintf(wacom_wac
->pen_name
, sizeof(wacom_wac
->pen_name
),
2095 "%s%s Pen", name
, suffix
);
2096 snprintf(wacom_wac
->touch_name
, sizeof(wacom_wac
->touch_name
),
2097 "%s%s Finger", name
, suffix
);
2098 snprintf(wacom_wac
->pad_name
, sizeof(wacom_wac
->pad_name
),
2099 "%s%s Pad", name
, suffix
);
2102 static void wacom_release_resources(struct wacom
*wacom
)
2104 struct hid_device
*hdev
= wacom
->hdev
;
2106 if (!wacom
->resources
)
2109 devres_release_group(&hdev
->dev
, wacom
);
2111 wacom
->resources
= false;
2113 wacom
->wacom_wac
.pen_input
= NULL
;
2114 wacom
->wacom_wac
.touch_input
= NULL
;
2115 wacom
->wacom_wac
.pad_input
= NULL
;
2118 static void wacom_set_shared_values(struct wacom_wac
*wacom_wac
)
2120 if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_TOUCH
) {
2121 wacom_wac
->shared
->type
= wacom_wac
->features
.type
;
2122 wacom_wac
->shared
->touch_input
= wacom_wac
->touch_input
;
2125 if (wacom_wac
->has_mute_touch_switch
) {
2126 wacom_wac
->shared
->has_mute_touch_switch
= true;
2127 wacom_wac
->shared
->is_touch_on
= true;
2130 if (wacom_wac
->shared
->has_mute_touch_switch
&&
2131 wacom_wac
->shared
->touch_input
) {
2132 set_bit(EV_SW
, wacom_wac
->shared
->touch_input
->evbit
);
2133 input_set_capability(wacom_wac
->shared
->touch_input
, EV_SW
,
2138 static int wacom_parse_and_register(struct wacom
*wacom
, bool wireless
)
2140 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2141 struct wacom_features
*features
= &wacom_wac
->features
;
2142 struct hid_device
*hdev
= wacom
->hdev
;
2144 unsigned int connect_mask
= HID_CONNECT_HIDRAW
;
2146 features
->pktlen
= wacom_compute_pktlen(hdev
);
2147 if (features
->pktlen
> WACOM_PKGLEN_MAX
)
2150 if (!devres_open_group(&hdev
->dev
, wacom
, GFP_KERNEL
))
2153 wacom
->resources
= true;
2155 error
= wacom_allocate_inputs(wacom
);
2160 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2161 * into debug mode for the touch part.
2162 * We ignore the other interfaces.
2164 if (features
->type
== BAMBOO_PAD
) {
2165 if (features
->pktlen
== WACOM_PKGLEN_PENABLED
) {
2166 features
->type
= HID_GENERIC
;
2167 } else if ((features
->pktlen
!= WACOM_PKGLEN_BPAD_TOUCH
) &&
2168 (features
->pktlen
!= WACOM_PKGLEN_BPAD_TOUCH_USB
)) {
2174 /* set the default size in case we do not get them from hid */
2175 wacom_set_default_phy(features
);
2177 /* Retrieve the physical and logical size for touch devices */
2178 wacom_retrieve_hid_descriptor(hdev
, features
);
2179 wacom_setup_device_quirks(wacom
);
2181 if (features
->device_type
== WACOM_DEVICETYPE_NONE
&&
2182 features
->type
!= WIRELESS
) {
2183 error
= features
->type
== HID_GENERIC
? -ENODEV
: 0;
2185 dev_warn(&hdev
->dev
, "Unknown device_type for '%s'. %s.",
2187 error
? "Ignoring" : "Assuming pen");
2192 features
->device_type
|= WACOM_DEVICETYPE_PEN
;
2195 wacom_calculate_res(features
);
2197 wacom_update_name(wacom
, wireless
? " (WL)" : "");
2199 /* pen only Bamboo neither support touch nor pad */
2200 if ((features
->type
== BAMBOO_PEN
) &&
2201 ((features
->device_type
& WACOM_DEVICETYPE_TOUCH
) ||
2202 (features
->device_type
& WACOM_DEVICETYPE_PAD
))) {
2207 error
= wacom_add_shared_data(hdev
);
2211 if (!(features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
) &&
2212 (features
->quirks
& WACOM_QUIRK_BATTERY
)) {
2213 error
= wacom_initialize_battery(wacom
);
2218 error
= wacom_register_inputs(wacom
);
2222 if (wacom
->wacom_wac
.features
.device_type
& WACOM_DEVICETYPE_PAD
) {
2223 error
= wacom_initialize_leds(wacom
);
2227 error
= wacom_initialize_remotes(wacom
);
2232 if (features
->type
== HID_GENERIC
)
2233 connect_mask
|= HID_CONNECT_DRIVER
;
2235 /* Regular HID work starts now */
2236 error
= hid_hw_start(hdev
, connect_mask
);
2238 hid_err(hdev
, "hw start failed\n");
2243 /* Note that if query fails it is not a hard failure */
2244 wacom_query_tablet_data(wacom
);
2247 /* touch only Bamboo doesn't support pen */
2248 if ((features
->type
== BAMBOO_TOUCH
) &&
2249 (features
->device_type
& WACOM_DEVICETYPE_PEN
)) {
2250 cancel_delayed_work_sync(&wacom
->init_work
);
2251 _wacom_query_tablet_data(wacom
);
2256 if (features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
)
2257 error
= hid_hw_open(hdev
);
2259 wacom_set_shared_values(wacom_wac
);
2260 devres_close_group(&hdev
->dev
, wacom
);
2267 wacom_release_resources(wacom
);
2271 static void wacom_wireless_work(struct work_struct
*work
)
2273 struct wacom
*wacom
= container_of(work
, struct wacom
, wireless_work
);
2274 struct usb_device
*usbdev
= wacom
->usbdev
;
2275 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2276 struct hid_device
*hdev1
, *hdev2
;
2277 struct wacom
*wacom1
, *wacom2
;
2278 struct wacom_wac
*wacom_wac1
, *wacom_wac2
;
2282 * Regardless if this is a disconnect or a new tablet,
2283 * remove any existing input and battery devices.
2286 wacom_destroy_battery(wacom
);
2288 /* Stylus interface */
2289 hdev1
= usb_get_intfdata(usbdev
->config
->interface
[1]);
2290 wacom1
= hid_get_drvdata(hdev1
);
2291 wacom_wac1
= &(wacom1
->wacom_wac
);
2292 wacom_release_resources(wacom1
);
2294 /* Touch interface */
2295 hdev2
= usb_get_intfdata(usbdev
->config
->interface
[2]);
2296 wacom2
= hid_get_drvdata(hdev2
);
2297 wacom_wac2
= &(wacom2
->wacom_wac
);
2298 wacom_release_resources(wacom2
);
2300 if (wacom_wac
->pid
== 0) {
2301 hid_info(wacom
->hdev
, "wireless tablet disconnected\n");
2303 const struct hid_device_id
*id
= wacom_ids
;
2305 hid_info(wacom
->hdev
, "wireless tablet connected with PID %x\n",
2309 if (id
->vendor
== USB_VENDOR_ID_WACOM
&&
2310 id
->product
== wacom_wac
->pid
)
2316 hid_info(wacom
->hdev
, "ignoring unknown PID.\n");
2320 /* Stylus interface */
2321 wacom_wac1
->features
=
2322 *((struct wacom_features
*)id
->driver_data
);
2324 wacom_wac1
->pid
= wacom_wac
->pid
;
2326 error
= wacom_parse_and_register(wacom1
, true);
2330 /* Touch interface */
2331 if (wacom_wac1
->features
.touch_max
||
2332 (wacom_wac1
->features
.type
>= INTUOSHT
&&
2333 wacom_wac1
->features
.type
<= BAMBOO_PT
)) {
2334 wacom_wac2
->features
=
2335 *((struct wacom_features
*)id
->driver_data
);
2336 wacom_wac2
->pid
= wacom_wac
->pid
;
2338 error
= wacom_parse_and_register(wacom2
, true);
2343 strlcpy(wacom_wac
->name
, wacom_wac1
->name
,
2344 sizeof(wacom_wac
->name
));
2345 error
= wacom_initialize_battery(wacom
);
2353 wacom_release_resources(wacom1
);
2354 wacom_release_resources(wacom2
);
2358 static void wacom_remote_destroy_one(struct wacom
*wacom
, unsigned int index
)
2360 struct wacom_remote
*remote
= wacom
->remote
;
2361 u32 serial
= remote
->remotes
[index
].serial
;
2363 unsigned long flags
;
2365 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
2366 if (remote
->remotes
[i
].serial
== serial
) {
2368 spin_lock_irqsave(&remote
->remote_lock
, flags
);
2369 remote
->remotes
[i
].registered
= false;
2370 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2372 if (remote
->remotes
[i
].battery
.battery
)
2373 devres_release_group(&wacom
->hdev
->dev
,
2374 &remote
->remotes
[i
].battery
.bat_desc
);
2376 if (remote
->remotes
[i
].group
.name
)
2377 devres_release_group(&wacom
->hdev
->dev
,
2378 &remote
->remotes
[i
]);
2380 remote
->remotes
[i
].serial
= 0;
2381 remote
->remotes
[i
].group
.name
= NULL
;
2382 remote
->remotes
[i
].battery
.battery
= NULL
;
2383 wacom
->led
.groups
[i
].select
= WACOM_STATUS_UNKNOWN
;
2388 static int wacom_remote_create_one(struct wacom
*wacom
, u32 serial
,
2391 struct wacom_remote
*remote
= wacom
->remote
;
2392 struct device
*dev
= &wacom
->hdev
->dev
;
2395 /* A remote can pair more than once with an EKR,
2396 * check to make sure this serial isn't already paired.
2398 for (k
= 0; k
< WACOM_MAX_REMOTES
; k
++) {
2399 if (remote
->remotes
[k
].serial
== serial
)
2403 if (k
< WACOM_MAX_REMOTES
) {
2404 remote
->remotes
[index
].serial
= serial
;
2408 if (!devres_open_group(dev
, &remote
->remotes
[index
], GFP_KERNEL
))
2411 error
= wacom_remote_create_attr_group(wacom
, serial
, index
);
2415 remote
->remotes
[index
].input
= wacom_allocate_input(wacom
);
2416 if (!remote
->remotes
[index
].input
) {
2420 remote
->remotes
[index
].input
->uniq
= remote
->remotes
[index
].group
.name
;
2421 remote
->remotes
[index
].input
->name
= wacom
->wacom_wac
.pad_name
;
2423 if (!remote
->remotes
[index
].input
->name
) {
2428 error
= wacom_setup_pad_input_capabilities(remote
->remotes
[index
].input
,
2433 remote
->remotes
[index
].serial
= serial
;
2435 error
= input_register_device(remote
->remotes
[index
].input
);
2439 error
= wacom_led_groups_alloc_and_register_one(
2440 &remote
->remotes
[index
].input
->dev
,
2441 wacom
, index
, 3, true);
2445 remote
->remotes
[index
].registered
= true;
2447 devres_close_group(dev
, &remote
->remotes
[index
]);
2451 devres_release_group(dev
, &remote
->remotes
[index
]);
2452 remote
->remotes
[index
].serial
= 0;
2456 static int wacom_remote_attach_battery(struct wacom
*wacom
, int index
)
2458 struct wacom_remote
*remote
= wacom
->remote
;
2461 if (!remote
->remotes
[index
].registered
)
2464 if (remote
->remotes
[index
].battery
.battery
)
2467 if (wacom
->led
.groups
[index
].select
== WACOM_STATUS_UNKNOWN
)
2470 error
= __wacom_initialize_battery(wacom
,
2471 &wacom
->remote
->remotes
[index
].battery
);
2478 static void wacom_remote_work(struct work_struct
*work
)
2480 struct wacom
*wacom
= container_of(work
, struct wacom
, remote_work
);
2481 struct wacom_remote
*remote
= wacom
->remote
;
2482 struct wacom_remote_data data
;
2483 unsigned long flags
;
2488 spin_lock_irqsave(&remote
->remote_lock
, flags
);
2490 count
= kfifo_out(&remote
->remote_fifo
, &data
, sizeof(data
));
2492 if (count
!= sizeof(data
)) {
2493 hid_err(wacom
->hdev
,
2494 "workitem triggered without status available\n");
2495 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2499 if (!kfifo_is_empty(&remote
->remote_fifo
))
2500 wacom_schedule_work(&wacom
->wacom_wac
, WACOM_WORKER_REMOTE
);
2502 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2504 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
2505 serial
= data
.remote
[i
].serial
;
2506 if (data
.remote
[i
].connected
) {
2508 if (remote
->remotes
[i
].serial
== serial
) {
2509 wacom_remote_attach_battery(wacom
, i
);
2513 if (remote
->remotes
[i
].serial
)
2514 wacom_remote_destroy_one(wacom
, i
);
2516 wacom_remote_create_one(wacom
, serial
, i
);
2518 } else if (remote
->remotes
[i
].serial
) {
2519 wacom_remote_destroy_one(wacom
, i
);
2524 static void wacom_mode_change_work(struct work_struct
*work
)
2526 struct wacom
*wacom
= container_of(work
, struct wacom
, mode_change_work
);
2527 struct wacom_shared
*shared
= wacom
->wacom_wac
.shared
;
2528 struct wacom
*wacom1
= NULL
;
2529 struct wacom
*wacom2
= NULL
;
2530 bool is_direct
= wacom
->wacom_wac
.is_direct_mode
;
2534 wacom1
= hid_get_drvdata(shared
->pen
);
2535 wacom_release_resources(wacom1
);
2536 hid_hw_stop(wacom1
->hdev
);
2537 wacom1
->wacom_wac
.has_mode_change
= true;
2538 wacom1
->wacom_wac
.is_direct_mode
= is_direct
;
2541 if (shared
->touch
) {
2542 wacom2
= hid_get_drvdata(shared
->touch
);
2543 wacom_release_resources(wacom2
);
2544 hid_hw_stop(wacom2
->hdev
);
2545 wacom2
->wacom_wac
.has_mode_change
= true;
2546 wacom2
->wacom_wac
.is_direct_mode
= is_direct
;
2550 error
= wacom_parse_and_register(wacom1
, false);
2556 error
= wacom_parse_and_register(wacom2
, false);
2564 static int wacom_probe(struct hid_device
*hdev
,
2565 const struct hid_device_id
*id
)
2567 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
2568 struct usb_device
*dev
= interface_to_usbdev(intf
);
2569 struct wacom
*wacom
;
2570 struct wacom_wac
*wacom_wac
;
2571 struct wacom_features
*features
;
2574 if (!id
->driver_data
)
2577 hdev
->quirks
|= HID_QUIRK_NO_INIT_REPORTS
;
2579 /* hid-core sets this quirk for the boot interface */
2580 hdev
->quirks
&= ~HID_QUIRK_NOGET
;
2582 wacom
= devm_kzalloc(&hdev
->dev
, sizeof(struct wacom
), GFP_KERNEL
);
2586 hid_set_drvdata(hdev
, wacom
);
2589 wacom_wac
= &wacom
->wacom_wac
;
2590 wacom_wac
->features
= *((struct wacom_features
*)id
->driver_data
);
2591 features
= &wacom_wac
->features
;
2593 if (features
->check_for_hid_type
&& features
->hid_type
!= hdev
->type
) {
2598 wacom_wac
->hid_data
.inputmode
= -1;
2599 wacom_wac
->mode_report
= -1;
2601 wacom
->usbdev
= dev
;
2603 mutex_init(&wacom
->lock
);
2604 INIT_DELAYED_WORK(&wacom
->init_work
, wacom_init_work
);
2605 INIT_WORK(&wacom
->wireless_work
, wacom_wireless_work
);
2606 INIT_WORK(&wacom
->battery_work
, wacom_battery_work
);
2607 INIT_WORK(&wacom
->remote_work
, wacom_remote_work
);
2608 INIT_WORK(&wacom
->mode_change_work
, wacom_mode_change_work
);
2610 /* ask for the report descriptor to be loaded by HID */
2611 error
= hid_parse(hdev
);
2613 hid_err(hdev
, "parse failed\n");
2617 error
= wacom_parse_and_register(wacom
, false);
2621 if (hdev
->bus
== BUS_BLUETOOTH
) {
2622 error
= device_create_file(&hdev
->dev
, &dev_attr_speed
);
2625 "can't create sysfs speed attribute err: %d\n",
2632 hid_set_drvdata(hdev
, NULL
);
2636 static void wacom_remove(struct hid_device
*hdev
)
2638 struct wacom
*wacom
= hid_get_drvdata(hdev
);
2639 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2640 struct wacom_features
*features
= &wacom_wac
->features
;
2642 if (features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
)
2647 cancel_delayed_work_sync(&wacom
->init_work
);
2648 cancel_work_sync(&wacom
->wireless_work
);
2649 cancel_work_sync(&wacom
->battery_work
);
2650 cancel_work_sync(&wacom
->remote_work
);
2651 cancel_work_sync(&wacom
->mode_change_work
);
2652 if (hdev
->bus
== BUS_BLUETOOTH
)
2653 device_remove_file(&hdev
->dev
, &dev_attr_speed
);
2655 /* make sure we don't trigger the LEDs */
2656 wacom_led_groups_release(wacom
);
2658 if (wacom
->wacom_wac
.features
.type
!= REMOTE
)
2659 wacom_release_resources(wacom
);
2661 hid_set_drvdata(hdev
, NULL
);
2665 static int wacom_resume(struct hid_device
*hdev
)
2667 struct wacom
*wacom
= hid_get_drvdata(hdev
);
2669 mutex_lock(&wacom
->lock
);
2671 /* switch to wacom mode first */
2672 _wacom_query_tablet_data(wacom
);
2673 wacom_led_control(wacom
);
2675 mutex_unlock(&wacom
->lock
);
2680 static int wacom_reset_resume(struct hid_device
*hdev
)
2682 return wacom_resume(hdev
);
2684 #endif /* CONFIG_PM */
2686 static struct hid_driver wacom_driver
= {
2688 .id_table
= wacom_ids
,
2689 .probe
= wacom_probe
,
2690 .remove
= wacom_remove
,
2691 .report
= wacom_wac_report
,
2693 .resume
= wacom_resume
,
2694 .reset_resume
= wacom_reset_resume
,
2696 .raw_event
= wacom_raw_event
,
2698 module_hid_driver(wacom_driver
);
2700 MODULE_VERSION(DRIVER_VERSION
);
2701 MODULE_AUTHOR(DRIVER_AUTHOR
);
2702 MODULE_DESCRIPTION(DRIVER_DESC
);
2703 MODULE_LICENSE("GPL");