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 HID_DG_CONTACTMAX
:
122 /* leave touch_max as is if predefined */
123 if (!features
->touch_max
) {
125 data
= kzalloc(2, GFP_KERNEL
);
128 data
[0] = field
->report
->id
;
129 ret
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
130 data
, 2, WAC_CMD_RETRIES
);
132 features
->touch_max
= data
[1];
134 features
->touch_max
= 16;
135 hid_warn(hdev
, "wacom_feature_mapping: "
136 "could not get HID_DG_CONTACTMAX, "
137 "defaulting to %d\n",
138 features
->touch_max
);
143 case HID_DG_INPUTMODE
:
144 /* Ignore if value index is out of bounds. */
145 if (usage
->usage_index
>= field
->report_count
) {
146 dev_err(&hdev
->dev
, "HID_DG_INPUTMODE out of range\n");
150 hid_data
->inputmode
= field
->report
->id
;
151 hid_data
->inputmode_index
= usage
->usage_index
;
154 case HID_UP_DIGITIZER
:
155 if (field
->report
->id
== 0x0B &&
156 (field
->application
== WACOM_HID_G9_PEN
||
157 field
->application
== WACOM_HID_G11_PEN
)) {
158 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
159 wacom
->wacom_wac
.mode_value
= 0;
163 case WACOM_HID_WD_DATAMODE
:
164 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
165 wacom
->wacom_wac
.mode_value
= 2;
168 case WACOM_HID_UP_G9
:
169 case WACOM_HID_UP_G11
:
170 if (field
->report
->id
== 0x03 &&
171 (field
->application
== WACOM_HID_G9_TOUCHSCREEN
||
172 field
->application
== WACOM_HID_G11_TOUCHSCREEN
)) {
173 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
174 wacom
->wacom_wac
.mode_value
= 0;
177 case WACOM_HID_WD_OFFSETLEFT
:
178 case WACOM_HID_WD_OFFSETTOP
:
179 case WACOM_HID_WD_OFFSETRIGHT
:
180 case WACOM_HID_WD_OFFSETBOTTOM
:
182 n
= hid_report_len(field
->report
);
183 data
= hid_alloc_report_buf(field
->report
, GFP_KERNEL
);
186 data
[0] = field
->report
->id
;
187 ret
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
188 data
, n
, WAC_CMD_RETRIES
);
190 ret
= hid_report_raw_event(hdev
, HID_FEATURE_REPORT
,
193 hid_warn(hdev
, "%s: could not retrieve sensor offsets\n",
202 * Interface Descriptor of wacom devices can be incomplete and
203 * inconsistent so wacom_features table is used to store stylus
204 * device's packet lengths, various maximum values, and tablet
205 * resolution based on product ID's.
207 * For devices that contain 2 interfaces, wacom_features table is
208 * inaccurate for the touch interface. Since the Interface Descriptor
209 * for touch interfaces has pretty complete data, this function exists
210 * to query tablet for this missing information instead of hard coding in
211 * an additional table.
213 * A typical Interface Descriptor for a stylus will contain a
214 * boot mouse application collection that is not of interest and this
215 * function will ignore it.
217 * It also contains a digitizer application collection that also is not
218 * of interest since any information it contains would be duplicate
219 * of what is in wacom_features. Usually it defines a report of an array
220 * of bytes that could be used as max length of the stylus packet returned.
221 * If it happens to define a Digitizer-Stylus Physical Collection then
222 * the X and Y logical values contain valid data but it is ignored.
224 * A typical Interface Descriptor for a touch interface will contain a
225 * Digitizer-Finger Physical Collection which will define both logical
226 * X/Y maximum as well as the physical size of tablet. Since touch
227 * interfaces haven't supported pressure or distance, this is enough
228 * information to override invalid values in the wacom_features table.
230 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
231 * data. We deal with them after returning from this function.
233 static void wacom_usage_mapping(struct hid_device
*hdev
,
234 struct hid_field
*field
, struct hid_usage
*usage
)
236 struct wacom
*wacom
= hid_get_drvdata(hdev
);
237 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
238 bool finger
= WACOM_FINGER_FIELD(field
);
239 bool pen
= WACOM_PEN_FIELD(field
);
242 * Requiring Stylus Usage will ignore boot mouse
243 * X/Y values and some cases of invalid Digitizer X/Y
244 * values commonly reported.
247 features
->device_type
|= WACOM_DEVICETYPE_PEN
;
249 features
->device_type
|= WACOM_DEVICETYPE_TOUCH
;
254 * Bamboo models do not support HID_DG_CONTACTMAX.
255 * And, Bamboo Pen only descriptor contains touch.
257 if (features
->type
> BAMBOO_PT
) {
258 /* ISDv4 touch devices at least supports one touch point */
259 if (finger
&& !features
->touch_max
)
260 features
->touch_max
= 1;
264 * ISDv4 devices which predate HID's adoption of the
265 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
266 * position instead. We can accurately detect if a
267 * usage with that value should be HID_DG_BARRELSWITCH2
268 * based on the surrounding usages, which have remained
269 * constant across generations.
271 if (features
->type
== HID_GENERIC
&&
272 usage
->hid
== 0x000D0000 &&
273 field
->application
== HID_DG_PEN
&&
274 field
->physical
== HID_DG_STYLUS
) {
275 int i
= usage
->usage_index
;
277 if (i
-4 >= 0 && i
+1 < field
->maxusage
&&
278 field
->usage
[i
-4].hid
== HID_DG_TIPSWITCH
&&
279 field
->usage
[i
-3].hid
== HID_DG_BARRELSWITCH
&&
280 field
->usage
[i
-2].hid
== HID_DG_ERASER
&&
281 field
->usage
[i
-1].hid
== HID_DG_INVERT
&&
282 field
->usage
[i
+1].hid
== HID_DG_INRANGE
) {
283 usage
->hid
= HID_DG_BARRELSWITCH2
;
287 switch (usage
->hid
) {
289 features
->x_max
= field
->logical_maximum
;
291 features
->x_phy
= field
->physical_maximum
;
292 if ((features
->type
!= BAMBOO_PT
) &&
293 (features
->type
!= BAMBOO_TOUCH
)) {
294 features
->unit
= field
->unit
;
295 features
->unitExpo
= field
->unit_exponent
;
300 features
->y_max
= field
->logical_maximum
;
302 features
->y_phy
= field
->physical_maximum
;
303 if ((features
->type
!= BAMBOO_PT
) &&
304 (features
->type
!= BAMBOO_TOUCH
)) {
305 features
->unit
= field
->unit
;
306 features
->unitExpo
= field
->unit_exponent
;
310 case HID_DG_TIPPRESSURE
:
312 features
->pressure_max
= field
->logical_maximum
;
316 if (features
->type
== HID_GENERIC
)
317 wacom_wac_usage_mapping(hdev
, field
, usage
);
320 static void wacom_post_parse_hid(struct hid_device
*hdev
,
321 struct wacom_features
*features
)
323 struct wacom
*wacom
= hid_get_drvdata(hdev
);
324 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
326 if (features
->type
== HID_GENERIC
) {
327 /* Any last-minute generic device setup */
328 if (features
->touch_max
> 1) {
329 if (features
->device_type
& WACOM_DEVICETYPE_DIRECT
)
330 input_mt_init_slots(wacom_wac
->touch_input
,
331 wacom_wac
->features
.touch_max
,
334 input_mt_init_slots(wacom_wac
->touch_input
,
335 wacom_wac
->features
.touch_max
,
341 static void wacom_parse_hid(struct hid_device
*hdev
,
342 struct wacom_features
*features
)
344 struct hid_report_enum
*rep_enum
;
345 struct hid_report
*hreport
;
348 /* check features first */
349 rep_enum
= &hdev
->report_enum
[HID_FEATURE_REPORT
];
350 list_for_each_entry(hreport
, &rep_enum
->report_list
, list
) {
351 for (i
= 0; i
< hreport
->maxfield
; i
++) {
352 /* Ignore if report count is out of bounds. */
353 if (hreport
->field
[i
]->report_count
< 1)
356 for (j
= 0; j
< hreport
->field
[i
]->maxusage
; j
++) {
357 wacom_feature_mapping(hdev
, hreport
->field
[i
],
358 hreport
->field
[i
]->usage
+ j
);
363 /* now check the input usages */
364 rep_enum
= &hdev
->report_enum
[HID_INPUT_REPORT
];
365 list_for_each_entry(hreport
, &rep_enum
->report_list
, list
) {
367 if (!hreport
->maxfield
)
370 for (i
= 0; i
< hreport
->maxfield
; i
++)
371 for (j
= 0; j
< hreport
->field
[i
]->maxusage
; j
++)
372 wacom_usage_mapping(hdev
, hreport
->field
[i
],
373 hreport
->field
[i
]->usage
+ j
);
376 wacom_post_parse_hid(hdev
, features
);
379 static int wacom_hid_set_device_mode(struct hid_device
*hdev
)
381 struct wacom
*wacom
= hid_get_drvdata(hdev
);
382 struct hid_data
*hid_data
= &wacom
->wacom_wac
.hid_data
;
383 struct hid_report
*r
;
384 struct hid_report_enum
*re
;
386 if (hid_data
->inputmode
< 0)
389 re
= &(hdev
->report_enum
[HID_FEATURE_REPORT
]);
390 r
= re
->report_id_hash
[hid_data
->inputmode
];
392 r
->field
[0]->value
[hid_data
->inputmode_index
] = 2;
393 hid_hw_request(hdev
, r
, HID_REQ_SET_REPORT
);
398 static int wacom_set_device_mode(struct hid_device
*hdev
,
399 struct wacom_wac
*wacom_wac
)
402 struct hid_report
*r
;
403 struct hid_report_enum
*re
;
405 int error
= -ENOMEM
, limit
= 0;
407 if (wacom_wac
->mode_report
< 0)
410 re
= &(hdev
->report_enum
[HID_FEATURE_REPORT
]);
411 r
= re
->report_id_hash
[wacom_wac
->mode_report
];
415 rep_data
= hid_alloc_report_buf(r
, GFP_KERNEL
);
419 length
= hid_report_len(r
);
422 rep_data
[0] = wacom_wac
->mode_report
;
423 rep_data
[1] = wacom_wac
->mode_value
;
425 error
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
,
428 error
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
429 rep_data
, length
, 1);
430 } while (error
>= 0 &&
431 rep_data
[1] != wacom_wac
->mode_report
&&
432 limit
++ < WAC_MSG_RETRIES
);
436 return error
< 0 ? error
: 0;
439 static int wacom_bt_query_tablet_data(struct hid_device
*hdev
, u8 speed
,
440 struct wacom_features
*features
)
442 struct wacom
*wacom
= hid_get_drvdata(hdev
);
446 switch (features
->type
) {
450 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
, 2,
454 rep_data
[0] = speed
== 0 ? 0x05 : 0x06;
457 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
,
461 wacom
->wacom_wac
.bt_high_speed
= speed
;
467 * Note that if the raw queries fail, it's not a hard failure
468 * and it is safe to continue
470 hid_warn(hdev
, "failed to poke device, command %d, err %d\n",
475 wacom
->wacom_wac
.bt_features
&= ~0x20;
477 wacom
->wacom_wac
.bt_features
|= 0x20;
480 rep_data
[1] = wacom
->wacom_wac
.bt_features
;
482 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
, 2,
485 wacom
->wacom_wac
.bt_high_speed
= speed
;
493 * Switch the tablet into its most-capable mode. Wacom tablets are
494 * typically configured to power-up in a mode which sends mouse-like
495 * reports to the OS. To get absolute position, pressure data, etc.
496 * from the tablet, it is necessary to switch the tablet out of this
497 * mode and into one which sends the full range of tablet data.
499 static int _wacom_query_tablet_data(struct wacom
*wacom
)
501 struct hid_device
*hdev
= wacom
->hdev
;
502 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
503 struct wacom_features
*features
= &wacom_wac
->features
;
505 if (hdev
->bus
== BUS_BLUETOOTH
)
506 return wacom_bt_query_tablet_data(hdev
, 1, features
);
508 if (features
->type
!= HID_GENERIC
) {
509 if (features
->device_type
& WACOM_DEVICETYPE_TOUCH
) {
510 if (features
->type
> TABLETPC
) {
511 /* MT Tablet PC touch */
512 wacom_wac
->mode_report
= 3;
513 wacom_wac
->mode_value
= 4;
514 } else if (features
->type
== WACOM_24HDT
) {
515 wacom_wac
->mode_report
= 18;
516 wacom_wac
->mode_value
= 2;
517 } else if (features
->type
== WACOM_27QHDT
) {
518 wacom_wac
->mode_report
= 131;
519 wacom_wac
->mode_value
= 2;
520 } else if (features
->type
== BAMBOO_PAD
) {
521 wacom_wac
->mode_report
= 2;
522 wacom_wac
->mode_value
= 2;
524 } else if (features
->device_type
& WACOM_DEVICETYPE_PEN
) {
525 if (features
->type
<= BAMBOO_PT
) {
526 wacom_wac
->mode_report
= 2;
527 wacom_wac
->mode_value
= 2;
532 wacom_set_device_mode(hdev
, wacom_wac
);
534 if (features
->type
== HID_GENERIC
)
535 return wacom_hid_set_device_mode(hdev
);
540 static void wacom_retrieve_hid_descriptor(struct hid_device
*hdev
,
541 struct wacom_features
*features
)
543 struct wacom
*wacom
= hid_get_drvdata(hdev
);
544 struct usb_interface
*intf
= wacom
->intf
;
546 /* default features */
547 features
->x_fuzz
= 4;
548 features
->y_fuzz
= 4;
549 features
->pressure_fuzz
= 0;
550 features
->distance_fuzz
= 1;
551 features
->tilt_fuzz
= 1;
554 * The wireless device HID is basic and layout conflicts with
555 * other tablets (monitor and touch interface can look like pen).
556 * Skip the query for this type and modify defaults based on
559 if (features
->type
== WIRELESS
) {
560 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 0)
561 features
->device_type
= WACOM_DEVICETYPE_WL_MONITOR
;
563 features
->device_type
= WACOM_DEVICETYPE_NONE
;
567 wacom_parse_hid(hdev
, features
);
570 struct wacom_hdev_data
{
571 struct list_head list
;
573 struct hid_device
*dev
;
574 struct wacom_shared shared
;
577 static LIST_HEAD(wacom_udev_list
);
578 static DEFINE_MUTEX(wacom_udev_list_lock
);
580 static bool compare_device_paths(struct hid_device
*hdev_a
,
581 struct hid_device
*hdev_b
, char separator
)
583 int n1
= strrchr(hdev_a
->phys
, separator
) - hdev_a
->phys
;
584 int n2
= strrchr(hdev_b
->phys
, separator
) - hdev_b
->phys
;
586 if (n1
!= n2
|| n1
<= 0 || n2
<= 0)
589 return !strncmp(hdev_a
->phys
, hdev_b
->phys
, n1
);
592 static bool wacom_are_sibling(struct hid_device
*hdev
,
593 struct hid_device
*sibling
)
595 struct wacom
*wacom
= hid_get_drvdata(hdev
);
596 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
597 struct wacom
*sibling_wacom
= hid_get_drvdata(sibling
);
598 struct wacom_features
*sibling_features
= &sibling_wacom
->wacom_wac
.features
;
599 __u32 oVid
= features
->oVid
? features
->oVid
: hdev
->vendor
;
600 __u32 oPid
= features
->oPid
? features
->oPid
: hdev
->product
;
602 /* The defined oVid/oPid must match that of the sibling */
603 if (features
->oVid
!= HID_ANY_ID
&& sibling
->vendor
!= oVid
)
605 if (features
->oPid
!= HID_ANY_ID
&& sibling
->product
!= oPid
)
609 * Devices with the same VID/PID must share the same physical
610 * device path, while those with different VID/PID must share
611 * the same physical parent device path.
613 if (hdev
->vendor
== sibling
->vendor
&& hdev
->product
== sibling
->product
) {
614 if (!compare_device_paths(hdev
, sibling
, '/'))
617 if (!compare_device_paths(hdev
, sibling
, '.'))
621 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
622 if (features
->type
!= HID_GENERIC
)
626 * Direct-input devices may not be siblings of indirect-input
629 if ((features
->device_type
& WACOM_DEVICETYPE_DIRECT
) &&
630 !(sibling_features
->device_type
& WACOM_DEVICETYPE_DIRECT
))
634 * Indirect-input devices may not be siblings of direct-input
637 if (!(features
->device_type
& WACOM_DEVICETYPE_DIRECT
) &&
638 (sibling_features
->device_type
& WACOM_DEVICETYPE_DIRECT
))
641 /* Pen devices may only be siblings of touch devices */
642 if ((features
->device_type
& WACOM_DEVICETYPE_PEN
) &&
643 !(sibling_features
->device_type
& WACOM_DEVICETYPE_TOUCH
))
646 /* Touch devices may only be siblings of pen devices */
647 if ((features
->device_type
& WACOM_DEVICETYPE_TOUCH
) &&
648 !(sibling_features
->device_type
& WACOM_DEVICETYPE_PEN
))
652 * No reason could be found for these two devices to NOT be
653 * siblings, so there's a good chance they ARE siblings
658 static struct wacom_hdev_data
*wacom_get_hdev_data(struct hid_device
*hdev
)
660 struct wacom_hdev_data
*data
;
662 /* Try to find an already-probed interface from the same device */
663 list_for_each_entry(data
, &wacom_udev_list
, list
) {
664 if (compare_device_paths(hdev
, data
->dev
, '/'))
668 /* Fallback to finding devices that appear to be "siblings" */
669 list_for_each_entry(data
, &wacom_udev_list
, list
) {
670 if (wacom_are_sibling(hdev
, data
->dev
)) {
671 kref_get(&data
->kref
);
679 static void wacom_release_shared_data(struct kref
*kref
)
681 struct wacom_hdev_data
*data
=
682 container_of(kref
, struct wacom_hdev_data
, kref
);
684 mutex_lock(&wacom_udev_list_lock
);
685 list_del(&data
->list
);
686 mutex_unlock(&wacom_udev_list_lock
);
691 static void wacom_remove_shared_data(void *res
)
693 struct wacom
*wacom
= res
;
694 struct wacom_hdev_data
*data
;
695 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
697 if (wacom_wac
->shared
) {
698 data
= container_of(wacom_wac
->shared
, struct wacom_hdev_data
,
701 if (wacom_wac
->shared
->touch
== wacom
->hdev
)
702 wacom_wac
->shared
->touch
= NULL
;
703 else if (wacom_wac
->shared
->pen
== wacom
->hdev
)
704 wacom_wac
->shared
->pen
= NULL
;
706 kref_put(&data
->kref
, wacom_release_shared_data
);
707 wacom_wac
->shared
= NULL
;
711 static int wacom_add_shared_data(struct hid_device
*hdev
)
713 struct wacom
*wacom
= hid_get_drvdata(hdev
);
714 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
715 struct wacom_hdev_data
*data
;
718 mutex_lock(&wacom_udev_list_lock
);
720 data
= wacom_get_hdev_data(hdev
);
722 data
= kzalloc(sizeof(struct wacom_hdev_data
), GFP_KERNEL
);
728 kref_init(&data
->kref
);
730 list_add_tail(&data
->list
, &wacom_udev_list
);
733 wacom_wac
->shared
= &data
->shared
;
735 retval
= devm_add_action(&hdev
->dev
, wacom_remove_shared_data
, wacom
);
737 mutex_unlock(&wacom_udev_list_lock
);
738 wacom_remove_shared_data(wacom
);
742 if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_TOUCH
)
743 wacom_wac
->shared
->touch
= hdev
;
744 else if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_PEN
)
745 wacom_wac
->shared
->pen
= hdev
;
748 mutex_unlock(&wacom_udev_list_lock
);
752 static int wacom_led_control(struct wacom
*wacom
)
756 unsigned char report_id
= WAC_CMD_LED_CONTROL
;
759 if (!wacom
->led
.groups
)
762 if (wacom
->wacom_wac
.pid
) { /* wireless connected */
763 report_id
= WAC_CMD_WL_LED_CONTROL
;
766 else if (wacom
->wacom_wac
.features
.type
== INTUOSP2_BT
) {
767 report_id
= WAC_CMD_WL_INTUOSP2
;
770 buf
= kzalloc(buf_size
, GFP_KERNEL
);
774 if (wacom
->wacom_wac
.features
.type
== HID_GENERIC
) {
775 buf
[0] = WAC_CMD_LED_CONTROL_GENERIC
;
776 buf
[1] = wacom
->led
.llv
;
777 buf
[2] = wacom
->led
.groups
[0].select
& 0x03;
779 } else if ((wacom
->wacom_wac
.features
.type
>= INTUOS5S
&&
780 wacom
->wacom_wac
.features
.type
<= INTUOSPL
)) {
782 * Touch Ring and crop mark LED luminance may take on
783 * one of four values:
784 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
786 int ring_led
= wacom
->led
.groups
[0].select
& 0x03;
787 int ring_lum
= (((wacom
->led
.llv
& 0x60) >> 5) - 1) & 0x03;
789 unsigned char led_bits
= (crop_lum
<< 4) | (ring_lum
<< 2) | (ring_led
);
792 if (wacom
->wacom_wac
.pid
) {
793 wacom_get_report(wacom
->hdev
, HID_FEATURE_REPORT
,
794 buf
, buf_size
, WAC_CMD_RETRIES
);
800 else if (wacom
->wacom_wac
.features
.type
== INTUOSP2_BT
) {
802 buf
[4] = 100; // Power Connection LED (ORANGE)
803 buf
[5] = 100; // BT Connection LED (BLUE)
804 buf
[6] = 100; // Paper Mode (RED?)
805 buf
[7] = 100; // Paper Mode (GREEN?)
806 buf
[8] = 100; // Paper Mode (BLUE?)
807 buf
[9] = wacom
->led
.llv
;
808 buf
[10] = wacom
->led
.groups
[0].select
& 0x03;
811 int led
= wacom
->led
.groups
[0].select
| 0x4;
813 if (wacom
->wacom_wac
.features
.type
== WACOM_21UX2
||
814 wacom
->wacom_wac
.features
.type
== WACOM_24HD
)
815 led
|= (wacom
->led
.groups
[1].select
<< 4) | 0x40;
819 buf
[2] = wacom
->led
.llv
;
820 buf
[3] = wacom
->led
.hlv
;
821 buf
[4] = wacom
->led
.img_lum
;
824 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, buf_size
,
831 static int wacom_led_putimage(struct wacom
*wacom
, int button_id
, u8 xfer_id
,
832 const unsigned len
, const void *img
)
836 const unsigned chunk_len
= len
/ 4; /* 4 chunks are needed to be sent */
838 buf
= kzalloc(chunk_len
+ 3 , GFP_KERNEL
);
842 /* Send 'start' command */
843 buf
[0] = WAC_CMD_ICON_START
;
845 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, 2,
851 buf
[1] = button_id
& 0x07;
852 for (i
= 0; i
< 4; i
++) {
854 memcpy(buf
+ 3, img
+ i
* chunk_len
, chunk_len
);
856 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
,
857 buf
, chunk_len
+ 3, WAC_CMD_RETRIES
);
863 buf
[0] = WAC_CMD_ICON_START
;
865 wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, 2,
873 static ssize_t
wacom_led_select_store(struct device
*dev
, int set_id
,
874 const char *buf
, size_t count
)
876 struct hid_device
*hdev
= to_hid_device(dev
);
877 struct wacom
*wacom
= hid_get_drvdata(hdev
);
881 err
= kstrtouint(buf
, 10, &id
);
885 mutex_lock(&wacom
->lock
);
887 wacom
->led
.groups
[set_id
].select
= id
& 0x3;
888 err
= wacom_led_control(wacom
);
890 mutex_unlock(&wacom
->lock
);
892 return err
< 0 ? err
: count
;
895 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
896 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
897 struct device_attribute *attr, const char *buf, size_t count) \
899 return wacom_led_select_store(dev, SET_ID, buf, count); \
901 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
902 struct device_attribute *attr, char *buf) \
904 struct hid_device *hdev = to_hid_device(dev);\
905 struct wacom *wacom = hid_get_drvdata(hdev); \
906 return scnprintf(buf, PAGE_SIZE, "%d\n", \
907 wacom->led.groups[SET_ID].select); \
909 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
910 wacom_led##SET_ID##_select_show, \
911 wacom_led##SET_ID##_select_store)
913 DEVICE_LED_SELECT_ATTR(0);
914 DEVICE_LED_SELECT_ATTR(1);
916 static ssize_t
wacom_luminance_store(struct wacom
*wacom
, u8
*dest
,
917 const char *buf
, size_t count
)
922 err
= kstrtouint(buf
, 10, &value
);
926 mutex_lock(&wacom
->lock
);
928 *dest
= value
& 0x7f;
929 err
= wacom_led_control(wacom
);
931 mutex_unlock(&wacom
->lock
);
933 return err
< 0 ? err
: count
;
936 #define DEVICE_LUMINANCE_ATTR(name, field) \
937 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
938 struct device_attribute *attr, const char *buf, size_t count) \
940 struct hid_device *hdev = to_hid_device(dev);\
941 struct wacom *wacom = hid_get_drvdata(hdev); \
943 return wacom_luminance_store(wacom, &wacom->led.field, \
946 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
947 struct device_attribute *attr, char *buf) \
949 struct wacom *wacom = dev_get_drvdata(dev); \
950 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
952 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
953 wacom_##name##_luminance_show, \
954 wacom_##name##_luminance_store)
956 DEVICE_LUMINANCE_ATTR(status0
, llv
);
957 DEVICE_LUMINANCE_ATTR(status1
, hlv
);
958 DEVICE_LUMINANCE_ATTR(buttons
, img_lum
);
960 static ssize_t
wacom_button_image_store(struct device
*dev
, int button_id
,
961 const char *buf
, size_t count
)
963 struct hid_device
*hdev
= to_hid_device(dev
);
964 struct wacom
*wacom
= hid_get_drvdata(hdev
);
969 if (hdev
->bus
== BUS_BLUETOOTH
) {
971 xfer_id
= WAC_CMD_ICON_BT_XFER
;
974 xfer_id
= WAC_CMD_ICON_XFER
;
980 mutex_lock(&wacom
->lock
);
982 err
= wacom_led_putimage(wacom
, button_id
, xfer_id
, len
, buf
);
984 mutex_unlock(&wacom
->lock
);
986 return err
< 0 ? err
: count
;
989 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
990 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
991 struct device_attribute *attr, const char *buf, size_t count) \
993 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
995 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
996 NULL, wacom_btnimg##BUTTON_ID##_store)
998 DEVICE_BTNIMG_ATTR(0);
999 DEVICE_BTNIMG_ATTR(1);
1000 DEVICE_BTNIMG_ATTR(2);
1001 DEVICE_BTNIMG_ATTR(3);
1002 DEVICE_BTNIMG_ATTR(4);
1003 DEVICE_BTNIMG_ATTR(5);
1004 DEVICE_BTNIMG_ATTR(6);
1005 DEVICE_BTNIMG_ATTR(7);
1007 static struct attribute
*cintiq_led_attrs
[] = {
1008 &dev_attr_status_led0_select
.attr
,
1009 &dev_attr_status_led1_select
.attr
,
1013 static struct attribute_group cintiq_led_attr_group
= {
1014 .name
= "wacom_led",
1015 .attrs
= cintiq_led_attrs
,
1018 static struct attribute
*intuos4_led_attrs
[] = {
1019 &dev_attr_status0_luminance
.attr
,
1020 &dev_attr_status1_luminance
.attr
,
1021 &dev_attr_status_led0_select
.attr
,
1022 &dev_attr_buttons_luminance
.attr
,
1023 &dev_attr_button0_rawimg
.attr
,
1024 &dev_attr_button1_rawimg
.attr
,
1025 &dev_attr_button2_rawimg
.attr
,
1026 &dev_attr_button3_rawimg
.attr
,
1027 &dev_attr_button4_rawimg
.attr
,
1028 &dev_attr_button5_rawimg
.attr
,
1029 &dev_attr_button6_rawimg
.attr
,
1030 &dev_attr_button7_rawimg
.attr
,
1034 static struct attribute_group intuos4_led_attr_group
= {
1035 .name
= "wacom_led",
1036 .attrs
= intuos4_led_attrs
,
1039 static struct attribute
*intuos5_led_attrs
[] = {
1040 &dev_attr_status0_luminance
.attr
,
1041 &dev_attr_status_led0_select
.attr
,
1045 static struct attribute_group intuos5_led_attr_group
= {
1046 .name
= "wacom_led",
1047 .attrs
= intuos5_led_attrs
,
1050 static struct attribute
*generic_led_attrs
[] = {
1051 &dev_attr_status0_luminance
.attr
,
1052 &dev_attr_status_led0_select
.attr
,
1056 static struct attribute_group generic_led_attr_group
= {
1057 .name
= "wacom_led",
1058 .attrs
= generic_led_attrs
,
1061 struct wacom_sysfs_group_devres
{
1062 struct attribute_group
*group
;
1063 struct kobject
*root
;
1066 static void wacom_devm_sysfs_group_release(struct device
*dev
, void *res
)
1068 struct wacom_sysfs_group_devres
*devres
= res
;
1069 struct kobject
*kobj
= devres
->root
;
1071 dev_dbg(dev
, "%s: dropping reference to %s\n",
1072 __func__
, devres
->group
->name
);
1073 sysfs_remove_group(kobj
, devres
->group
);
1076 static int __wacom_devm_sysfs_create_group(struct wacom
*wacom
,
1077 struct kobject
*root
,
1078 struct attribute_group
*group
)
1080 struct wacom_sysfs_group_devres
*devres
;
1083 devres
= devres_alloc(wacom_devm_sysfs_group_release
,
1084 sizeof(struct wacom_sysfs_group_devres
),
1089 devres
->group
= group
;
1090 devres
->root
= root
;
1092 error
= sysfs_create_group(devres
->root
, group
);
1096 devres_add(&wacom
->hdev
->dev
, devres
);
1101 static int wacom_devm_sysfs_create_group(struct wacom
*wacom
,
1102 struct attribute_group
*group
)
1104 return __wacom_devm_sysfs_create_group(wacom
, &wacom
->hdev
->dev
.kobj
,
1108 enum led_brightness
wacom_leds_brightness_get(struct wacom_led
*led
)
1110 struct wacom
*wacom
= led
->wacom
;
1112 if (wacom
->led
.max_hlv
)
1113 return led
->hlv
* LED_FULL
/ wacom
->led
.max_hlv
;
1115 if (wacom
->led
.max_llv
)
1116 return led
->llv
* LED_FULL
/ wacom
->led
.max_llv
;
1118 /* device doesn't support brightness tuning */
1122 static enum led_brightness
__wacom_led_brightness_get(struct led_classdev
*cdev
)
1124 struct wacom_led
*led
= container_of(cdev
, struct wacom_led
, cdev
);
1125 struct wacom
*wacom
= led
->wacom
;
1127 if (wacom
->led
.groups
[led
->group
].select
!= led
->id
)
1130 return wacom_leds_brightness_get(led
);
1133 static int wacom_led_brightness_set(struct led_classdev
*cdev
,
1134 enum led_brightness brightness
)
1136 struct wacom_led
*led
= container_of(cdev
, struct wacom_led
, cdev
);
1137 struct wacom
*wacom
= led
->wacom
;
1140 mutex_lock(&wacom
->lock
);
1142 if (!wacom
->led
.groups
|| (brightness
== LED_OFF
&&
1143 wacom
->led
.groups
[led
->group
].select
!= led
->id
)) {
1148 led
->llv
= wacom
->led
.llv
= wacom
->led
.max_llv
* brightness
/ LED_FULL
;
1149 led
->hlv
= wacom
->led
.hlv
= wacom
->led
.max_hlv
* brightness
/ LED_FULL
;
1151 wacom
->led
.groups
[led
->group
].select
= led
->id
;
1153 error
= wacom_led_control(wacom
);
1156 mutex_unlock(&wacom
->lock
);
1161 static void wacom_led_readonly_brightness_set(struct led_classdev
*cdev
,
1162 enum led_brightness brightness
)
1166 static int wacom_led_register_one(struct device
*dev
, struct wacom
*wacom
,
1167 struct wacom_led
*led
, unsigned int group
,
1168 unsigned int id
, bool read_only
)
1173 name
= devm_kasprintf(dev
, GFP_KERNEL
,
1182 led
->trigger
.name
= name
;
1183 error
= devm_led_trigger_register(dev
, &led
->trigger
);
1185 hid_err(wacom
->hdev
,
1186 "failed to register LED trigger %s: %d\n",
1187 led
->cdev
.name
, error
);
1195 led
->llv
= wacom
->led
.llv
;
1196 led
->hlv
= wacom
->led
.hlv
;
1197 led
->cdev
.name
= name
;
1198 led
->cdev
.max_brightness
= LED_FULL
;
1199 led
->cdev
.flags
= LED_HW_PLUGGABLE
;
1200 led
->cdev
.brightness_get
= __wacom_led_brightness_get
;
1202 led
->cdev
.brightness_set_blocking
= wacom_led_brightness_set
;
1203 led
->cdev
.default_trigger
= led
->cdev
.name
;
1205 led
->cdev
.brightness_set
= wacom_led_readonly_brightness_set
;
1208 error
= devm_led_classdev_register(dev
, &led
->cdev
);
1210 hid_err(wacom
->hdev
,
1211 "failed to register LED %s: %d\n",
1212 led
->cdev
.name
, error
);
1213 led
->cdev
.name
= NULL
;
1220 static void wacom_led_groups_release_one(void *data
)
1222 struct wacom_group_leds
*group
= data
;
1224 devres_release_group(group
->dev
, group
);
1227 static int wacom_led_groups_alloc_and_register_one(struct device
*dev
,
1228 struct wacom
*wacom
,
1229 int group_id
, int count
,
1232 struct wacom_led
*leds
;
1235 if (group_id
>= wacom
->led
.count
|| count
<= 0)
1238 if (!devres_open_group(dev
, &wacom
->led
.groups
[group_id
], GFP_KERNEL
))
1241 leds
= devm_kzalloc(dev
, sizeof(struct wacom_led
) * count
, GFP_KERNEL
);
1247 wacom
->led
.groups
[group_id
].leds
= leds
;
1248 wacom
->led
.groups
[group_id
].count
= count
;
1250 for (i
= 0; i
< count
; i
++) {
1251 error
= wacom_led_register_one(dev
, wacom
, &leds
[i
],
1252 group_id
, i
, read_only
);
1257 wacom
->led
.groups
[group_id
].dev
= dev
;
1259 devres_close_group(dev
, &wacom
->led
.groups
[group_id
]);
1262 * There is a bug (?) in devm_led_classdev_register() in which its
1263 * increments the refcount of the parent. If the parent is an input
1264 * device, that means the ref count never reaches 0 when
1265 * devm_input_device_release() gets called.
1266 * This means that the LEDs are still there after disconnect.
1267 * Manually force the release of the group so that the leds are released
1268 * once we are done using them.
1270 error
= devm_add_action_or_reset(&wacom
->hdev
->dev
,
1271 wacom_led_groups_release_one
,
1272 &wacom
->led
.groups
[group_id
]);
1279 devres_release_group(dev
, &wacom
->led
.groups
[group_id
]);
1283 struct wacom_led
*wacom_led_find(struct wacom
*wacom
, unsigned int group_id
,
1286 struct wacom_group_leds
*group
;
1288 if (group_id
>= wacom
->led
.count
)
1291 group
= &wacom
->led
.groups
[group_id
];
1298 return &group
->leds
[id
];
1302 * wacom_led_next: gives the next available led with a wacom trigger.
1304 * returns the next available struct wacom_led which has its default trigger
1305 * or the current one if none is available.
1307 struct wacom_led
*wacom_led_next(struct wacom
*wacom
, struct wacom_led
*cur
)
1309 struct wacom_led
*next_led
;
1319 next_led
= wacom_led_find(wacom
, group
, ++next
);
1320 if (!next_led
|| next_led
== cur
)
1322 } while (next_led
->cdev
.trigger
!= &next_led
->trigger
);
1327 static void wacom_led_groups_release(void *data
)
1329 struct wacom
*wacom
= data
;
1331 wacom
->led
.groups
= NULL
;
1332 wacom
->led
.count
= 0;
1335 static int wacom_led_groups_allocate(struct wacom
*wacom
, int count
)
1337 struct device
*dev
= &wacom
->hdev
->dev
;
1338 struct wacom_group_leds
*groups
;
1341 groups
= devm_kzalloc(dev
, sizeof(struct wacom_group_leds
) * count
,
1346 error
= devm_add_action_or_reset(dev
, wacom_led_groups_release
, wacom
);
1350 wacom
->led
.groups
= groups
;
1351 wacom
->led
.count
= count
;
1356 static int wacom_leds_alloc_and_register(struct wacom
*wacom
, int group_count
,
1357 int led_per_group
, bool read_only
)
1362 if (!wacom
->wacom_wac
.pad_input
)
1365 dev
= &wacom
->wacom_wac
.pad_input
->dev
;
1367 error
= wacom_led_groups_allocate(wacom
, group_count
);
1371 for (i
= 0; i
< group_count
; i
++) {
1372 error
= wacom_led_groups_alloc_and_register_one(dev
, wacom
, i
,
1382 int wacom_initialize_leds(struct wacom
*wacom
)
1386 if (!(wacom
->wacom_wac
.features
.device_type
& WACOM_DEVICETYPE_PAD
))
1389 /* Initialize default values */
1390 switch (wacom
->wacom_wac
.features
.type
) {
1392 if (!wacom
->generic_has_leds
)
1394 wacom
->led
.llv
= 100;
1395 wacom
->led
.max_llv
= 100;
1397 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1399 hid_err(wacom
->hdev
,
1400 "cannot create leds err: %d\n", error
);
1404 error
= wacom_devm_sysfs_create_group(wacom
,
1405 &generic_led_attr_group
);
1412 wacom
->led
.llv
= 10;
1413 wacom
->led
.hlv
= 20;
1414 wacom
->led
.max_llv
= 127;
1415 wacom
->led
.max_hlv
= 127;
1416 wacom
->led
.img_lum
= 10;
1418 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1420 hid_err(wacom
->hdev
,
1421 "cannot create leds err: %d\n", error
);
1425 error
= wacom_devm_sysfs_create_group(wacom
,
1426 &intuos4_led_attr_group
);
1433 wacom
->led
.img_lum
= 0;
1435 error
= wacom_leds_alloc_and_register(wacom
, 2, 4, false);
1437 hid_err(wacom
->hdev
,
1438 "cannot create leds err: %d\n", error
);
1442 error
= wacom_devm_sysfs_create_group(wacom
,
1443 &cintiq_led_attr_group
);
1452 wacom
->led
.llv
= 32;
1453 wacom
->led
.max_llv
= 96;
1455 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1457 hid_err(wacom
->hdev
,
1458 "cannot create leds err: %d\n", error
);
1462 error
= wacom_devm_sysfs_create_group(wacom
,
1463 &intuos5_led_attr_group
);
1467 wacom
->led
.llv
= 50;
1468 wacom
->led
.max_llv
= 100;
1469 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1471 hid_err(wacom
->hdev
,
1472 "cannot create leds err: %d\n", error
);
1478 wacom
->led
.llv
= 255;
1479 wacom
->led
.max_llv
= 255;
1480 error
= wacom_led_groups_allocate(wacom
, 5);
1482 hid_err(wacom
->hdev
,
1483 "cannot create leds err: %d\n", error
);
1493 hid_err(wacom
->hdev
,
1494 "cannot create sysfs group err: %d\n", error
);
1501 static void wacom_init_work(struct work_struct
*work
)
1503 struct wacom
*wacom
= container_of(work
, struct wacom
, init_work
.work
);
1505 _wacom_query_tablet_data(wacom
);
1506 wacom_led_control(wacom
);
1509 static void wacom_query_tablet_data(struct wacom
*wacom
)
1511 schedule_delayed_work(&wacom
->init_work
, msecs_to_jiffies(1000));
1514 static enum power_supply_property wacom_battery_props
[] = {
1515 POWER_SUPPLY_PROP_MODEL_NAME
,
1516 POWER_SUPPLY_PROP_PRESENT
,
1517 POWER_SUPPLY_PROP_STATUS
,
1518 POWER_SUPPLY_PROP_SCOPE
,
1519 POWER_SUPPLY_PROP_CAPACITY
1522 static int wacom_battery_get_property(struct power_supply
*psy
,
1523 enum power_supply_property psp
,
1524 union power_supply_propval
*val
)
1526 struct wacom_battery
*battery
= power_supply_get_drvdata(psy
);
1530 case POWER_SUPPLY_PROP_MODEL_NAME
:
1531 val
->strval
= battery
->wacom
->wacom_wac
.name
;
1533 case POWER_SUPPLY_PROP_PRESENT
:
1534 val
->intval
= battery
->bat_connected
;
1536 case POWER_SUPPLY_PROP_SCOPE
:
1537 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
1539 case POWER_SUPPLY_PROP_CAPACITY
:
1540 val
->intval
= battery
->battery_capacity
;
1542 case POWER_SUPPLY_PROP_STATUS
:
1543 if (battery
->bat_charging
)
1544 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
1545 else if (battery
->battery_capacity
== 100 &&
1546 battery
->ps_connected
)
1547 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
1548 else if (battery
->ps_connected
)
1549 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1551 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
1561 static int __wacom_initialize_battery(struct wacom
*wacom
,
1562 struct wacom_battery
*battery
)
1564 static atomic_t battery_no
= ATOMIC_INIT(0);
1565 struct device
*dev
= &wacom
->hdev
->dev
;
1566 struct power_supply_config psy_cfg
= { .drv_data
= battery
, };
1567 struct power_supply
*ps_bat
;
1568 struct power_supply_desc
*bat_desc
= &battery
->bat_desc
;
1572 if (!devres_open_group(dev
, bat_desc
, GFP_KERNEL
))
1575 battery
->wacom
= wacom
;
1577 n
= atomic_inc_return(&battery_no
) - 1;
1579 bat_desc
->properties
= wacom_battery_props
;
1580 bat_desc
->num_properties
= ARRAY_SIZE(wacom_battery_props
);
1581 bat_desc
->get_property
= wacom_battery_get_property
;
1582 sprintf(battery
->bat_name
, "wacom_battery_%ld", n
);
1583 bat_desc
->name
= battery
->bat_name
;
1584 bat_desc
->type
= POWER_SUPPLY_TYPE_USB
;
1585 bat_desc
->use_for_apm
= 0;
1587 ps_bat
= devm_power_supply_register(dev
, bat_desc
, &psy_cfg
);
1588 if (IS_ERR(ps_bat
)) {
1589 error
= PTR_ERR(ps_bat
);
1593 power_supply_powers(ps_bat
, &wacom
->hdev
->dev
);
1595 battery
->battery
= ps_bat
;
1597 devres_close_group(dev
, bat_desc
);
1601 devres_release_group(dev
, bat_desc
);
1605 static int wacom_initialize_battery(struct wacom
*wacom
)
1607 if (wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
)
1608 return __wacom_initialize_battery(wacom
, &wacom
->battery
);
1613 static void wacom_destroy_battery(struct wacom
*wacom
)
1615 if (wacom
->battery
.battery
) {
1616 devres_release_group(&wacom
->hdev
->dev
,
1617 &wacom
->battery
.bat_desc
);
1618 wacom
->battery
.battery
= NULL
;
1622 static ssize_t
wacom_show_speed(struct device
*dev
,
1623 struct device_attribute
1626 struct hid_device
*hdev
= to_hid_device(dev
);
1627 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1629 return snprintf(buf
, PAGE_SIZE
, "%i\n", wacom
->wacom_wac
.bt_high_speed
);
1632 static ssize_t
wacom_store_speed(struct device
*dev
,
1633 struct device_attribute
*attr
,
1634 const char *buf
, size_t count
)
1636 struct hid_device
*hdev
= to_hid_device(dev
);
1637 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1640 if (kstrtou8(buf
, 0, &new_speed
))
1643 if (new_speed
!= 0 && new_speed
!= 1)
1646 wacom_bt_query_tablet_data(hdev
, new_speed
, &wacom
->wacom_wac
.features
);
1651 static DEVICE_ATTR(speed
, DEV_ATTR_RW_PERM
,
1652 wacom_show_speed
, wacom_store_speed
);
1655 static ssize_t
wacom_show_remote_mode(struct kobject
*kobj
,
1656 struct kobj_attribute
*kattr
,
1657 char *buf
, int index
)
1659 struct device
*dev
= kobj_to_dev(kobj
->parent
);
1660 struct hid_device
*hdev
= to_hid_device(dev
);
1661 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1664 mode
= wacom
->led
.groups
[index
].select
;
1665 if (mode
>= 0 && mode
< 3)
1666 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
1668 return snprintf(buf
, PAGE_SIZE
, "%d\n", -1);
1671 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1672 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1673 struct kobj_attribute *kattr, char *buf) \
1675 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1677 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1678 .attr = {.name = "remote_mode", \
1679 .mode = DEV_ATTR_RO_PERM}, \
1680 .show = wacom_show_remote##SET_ID##_mode, \
1682 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1683 &remote##SET_ID##_mode_attr.attr, \
1686 static struct attribute_group remote##SET_ID##_serial_group = { \
1688 .attrs = remote##SET_ID##_serial_attrs, \
1691 DEVICE_EKR_ATTR_GROUP(0);
1692 DEVICE_EKR_ATTR_GROUP(1);
1693 DEVICE_EKR_ATTR_GROUP(2);
1694 DEVICE_EKR_ATTR_GROUP(3);
1695 DEVICE_EKR_ATTR_GROUP(4);
1697 static int wacom_remote_create_attr_group(struct wacom
*wacom
, __u32 serial
,
1701 struct wacom_remote
*remote
= wacom
->remote
;
1703 remote
->remotes
[index
].group
.name
= devm_kasprintf(&wacom
->hdev
->dev
,
1706 if (!remote
->remotes
[index
].group
.name
)
1709 error
= __wacom_devm_sysfs_create_group(wacom
, remote
->remote_dir
,
1710 &remote
->remotes
[index
].group
);
1712 remote
->remotes
[index
].group
.name
= NULL
;
1713 hid_err(wacom
->hdev
,
1714 "cannot create sysfs group err: %d\n", error
);
1721 static int wacom_cmd_unpair_remote(struct wacom
*wacom
, unsigned char selector
)
1723 const size_t buf_size
= 2;
1727 buf
= kzalloc(buf_size
, GFP_KERNEL
);
1731 buf
[0] = WAC_CMD_DELETE_PAIRING
;
1734 retval
= wacom_set_report(wacom
->hdev
, HID_OUTPUT_REPORT
, buf
,
1735 buf_size
, WAC_CMD_RETRIES
);
1741 static ssize_t
wacom_store_unpair_remote(struct kobject
*kobj
,
1742 struct kobj_attribute
*attr
,
1743 const char *buf
, size_t count
)
1745 unsigned char selector
= 0;
1746 struct device
*dev
= kobj_to_dev(kobj
->parent
);
1747 struct hid_device
*hdev
= to_hid_device(dev
);
1748 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1751 if (!strncmp(buf
, "*\n", 2)) {
1752 selector
= WAC_CMD_UNPAIR_ALL
;
1754 hid_info(wacom
->hdev
, "remote: unrecognized unpair code: %s\n",
1759 mutex_lock(&wacom
->lock
);
1761 err
= wacom_cmd_unpair_remote(wacom
, selector
);
1762 mutex_unlock(&wacom
->lock
);
1764 return err
< 0 ? err
: count
;
1767 static struct kobj_attribute unpair_remote_attr
= {
1768 .attr
= {.name
= "unpair_remote", .mode
= 0200},
1769 .store
= wacom_store_unpair_remote
,
1772 static const struct attribute
*remote_unpair_attrs
[] = {
1773 &unpair_remote_attr
.attr
,
1777 static void wacom_remotes_destroy(void *data
)
1779 struct wacom
*wacom
= data
;
1780 struct wacom_remote
*remote
= wacom
->remote
;
1785 kobject_put(remote
->remote_dir
);
1786 kfifo_free(&remote
->remote_fifo
);
1787 wacom
->remote
= NULL
;
1790 static int wacom_initialize_remotes(struct wacom
*wacom
)
1793 struct wacom_remote
*remote
;
1796 if (wacom
->wacom_wac
.features
.type
!= REMOTE
)
1799 remote
= devm_kzalloc(&wacom
->hdev
->dev
, sizeof(*wacom
->remote
),
1804 wacom
->remote
= remote
;
1806 spin_lock_init(&remote
->remote_lock
);
1808 error
= kfifo_alloc(&remote
->remote_fifo
,
1809 5 * sizeof(struct wacom_remote_data
),
1812 hid_err(wacom
->hdev
, "failed allocating remote_fifo\n");
1816 remote
->remotes
[0].group
= remote0_serial_group
;
1817 remote
->remotes
[1].group
= remote1_serial_group
;
1818 remote
->remotes
[2].group
= remote2_serial_group
;
1819 remote
->remotes
[3].group
= remote3_serial_group
;
1820 remote
->remotes
[4].group
= remote4_serial_group
;
1822 remote
->remote_dir
= kobject_create_and_add("wacom_remote",
1823 &wacom
->hdev
->dev
.kobj
);
1824 if (!remote
->remote_dir
)
1827 error
= sysfs_create_files(remote
->remote_dir
, remote_unpair_attrs
);
1830 hid_err(wacom
->hdev
,
1831 "cannot create sysfs group err: %d\n", error
);
1835 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
1836 wacom
->led
.groups
[i
].select
= WACOM_STATUS_UNKNOWN
;
1837 remote
->remotes
[i
].serial
= 0;
1840 error
= devm_add_action_or_reset(&wacom
->hdev
->dev
,
1841 wacom_remotes_destroy
, wacom
);
1848 static struct input_dev
*wacom_allocate_input(struct wacom
*wacom
)
1850 struct input_dev
*input_dev
;
1851 struct hid_device
*hdev
= wacom
->hdev
;
1852 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1854 input_dev
= devm_input_allocate_device(&hdev
->dev
);
1858 input_dev
->name
= wacom_wac
->features
.name
;
1859 input_dev
->phys
= hdev
->phys
;
1860 input_dev
->dev
.parent
= &hdev
->dev
;
1861 input_dev
->open
= wacom_open
;
1862 input_dev
->close
= wacom_close
;
1863 input_dev
->uniq
= hdev
->uniq
;
1864 input_dev
->id
.bustype
= hdev
->bus
;
1865 input_dev
->id
.vendor
= hdev
->vendor
;
1866 input_dev
->id
.product
= wacom_wac
->pid
? wacom_wac
->pid
: hdev
->product
;
1867 input_dev
->id
.version
= hdev
->version
;
1868 input_set_drvdata(input_dev
, wacom
);
1873 static int wacom_allocate_inputs(struct wacom
*wacom
)
1875 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1877 wacom_wac
->pen_input
= wacom_allocate_input(wacom
);
1878 wacom_wac
->touch_input
= wacom_allocate_input(wacom
);
1879 wacom_wac
->pad_input
= wacom_allocate_input(wacom
);
1880 if (!wacom_wac
->pen_input
||
1881 !wacom_wac
->touch_input
||
1882 !wacom_wac
->pad_input
)
1885 wacom_wac
->pen_input
->name
= wacom_wac
->pen_name
;
1886 wacom_wac
->touch_input
->name
= wacom_wac
->touch_name
;
1887 wacom_wac
->pad_input
->name
= wacom_wac
->pad_name
;
1892 static int wacom_register_inputs(struct wacom
*wacom
)
1894 struct input_dev
*pen_input_dev
, *touch_input_dev
, *pad_input_dev
;
1895 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1898 pen_input_dev
= wacom_wac
->pen_input
;
1899 touch_input_dev
= wacom_wac
->touch_input
;
1900 pad_input_dev
= wacom_wac
->pad_input
;
1902 if (!pen_input_dev
|| !touch_input_dev
|| !pad_input_dev
)
1905 error
= wacom_setup_pen_input_capabilities(pen_input_dev
, wacom_wac
);
1907 /* no pen in use on this interface */
1908 input_free_device(pen_input_dev
);
1909 wacom_wac
->pen_input
= NULL
;
1910 pen_input_dev
= NULL
;
1912 error
= input_register_device(pen_input_dev
);
1917 error
= wacom_setup_touch_input_capabilities(touch_input_dev
, wacom_wac
);
1919 /* no touch in use on this interface */
1920 input_free_device(touch_input_dev
);
1921 wacom_wac
->touch_input
= NULL
;
1922 touch_input_dev
= NULL
;
1924 error
= input_register_device(touch_input_dev
);
1929 error
= wacom_setup_pad_input_capabilities(pad_input_dev
, wacom_wac
);
1931 /* no pad in use on this interface */
1932 input_free_device(pad_input_dev
);
1933 wacom_wac
->pad_input
= NULL
;
1934 pad_input_dev
= NULL
;
1936 error
= input_register_device(pad_input_dev
);
1944 wacom_wac
->pad_input
= NULL
;
1945 wacom_wac
->touch_input
= NULL
;
1946 wacom_wac
->pen_input
= NULL
;
1951 * Not all devices report physical dimensions from HID.
1952 * Compute the default from hardcoded logical dimension
1953 * and resolution before driver overwrites them.
1955 static void wacom_set_default_phy(struct wacom_features
*features
)
1957 if (features
->x_resolution
) {
1958 features
->x_phy
= (features
->x_max
* 100) /
1959 features
->x_resolution
;
1960 features
->y_phy
= (features
->y_max
* 100) /
1961 features
->y_resolution
;
1965 static void wacom_calculate_res(struct wacom_features
*features
)
1967 /* set unit to "100th of a mm" for devices not reported by HID */
1968 if (!features
->unit
) {
1969 features
->unit
= 0x11;
1970 features
->unitExpo
= -3;
1973 features
->x_resolution
= wacom_calc_hid_res(features
->x_max
,
1976 features
->unitExpo
);
1977 features
->y_resolution
= wacom_calc_hid_res(features
->y_max
,
1980 features
->unitExpo
);
1983 void wacom_battery_work(struct work_struct
*work
)
1985 struct wacom
*wacom
= container_of(work
, struct wacom
, battery_work
);
1987 if ((wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
) &&
1988 !wacom
->battery
.battery
) {
1989 wacom_initialize_battery(wacom
);
1991 else if (!(wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
) &&
1992 wacom
->battery
.battery
) {
1993 wacom_destroy_battery(wacom
);
1997 static size_t wacom_compute_pktlen(struct hid_device
*hdev
)
1999 struct hid_report_enum
*report_enum
;
2000 struct hid_report
*report
;
2003 report_enum
= hdev
->report_enum
+ HID_INPUT_REPORT
;
2005 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
2006 size_t report_size
= hid_report_len(report
);
2007 if (report_size
> size
)
2014 static void wacom_update_name(struct wacom
*wacom
, const char *suffix
)
2016 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2017 struct wacom_features
*features
= &wacom_wac
->features
;
2018 char name
[WACOM_NAME_MAX
];
2020 /* Generic devices name unspecified */
2021 if ((features
->type
== HID_GENERIC
) && !strcmp("Wacom HID", features
->name
)) {
2022 if (strstr(wacom
->hdev
->name
, "Wacom") ||
2023 strstr(wacom
->hdev
->name
, "wacom") ||
2024 strstr(wacom
->hdev
->name
, "WACOM")) {
2025 /* name is in HID descriptor, use it */
2026 strlcpy(name
, wacom
->hdev
->name
, sizeof(name
));
2028 /* strip out excess whitespaces */
2030 char *gap
= strstr(name
, " ");
2033 /* shift everything including the terminator */
2034 memmove(gap
, gap
+1, strlen(gap
));
2037 /* strip off excessive prefixing */
2038 if (strstr(name
, "Wacom Co.,Ltd. Wacom ") == name
) {
2039 int n
= strlen(name
);
2040 int x
= strlen("Wacom Co.,Ltd. ");
2041 memmove(name
, name
+x
, n
-x
+1);
2043 if (strstr(name
, "Wacom Co., Ltd. Wacom ") == name
) {
2044 int n
= strlen(name
);
2045 int x
= strlen("Wacom Co., Ltd. ");
2046 memmove(name
, name
+x
, n
-x
+1);
2049 /* get rid of trailing whitespace */
2050 if (name
[strlen(name
)-1] == ' ')
2051 name
[strlen(name
)-1] = '\0';
2053 /* no meaningful name retrieved. use product ID */
2054 snprintf(name
, sizeof(name
),
2055 "%s %X", features
->name
, wacom
->hdev
->product
);
2058 strlcpy(name
, features
->name
, sizeof(name
));
2061 snprintf(wacom_wac
->name
, sizeof(wacom_wac
->name
), "%s%s",
2064 /* Append the device type to the name */
2065 snprintf(wacom_wac
->pen_name
, sizeof(wacom_wac
->pen_name
),
2066 "%s%s Pen", name
, suffix
);
2067 snprintf(wacom_wac
->touch_name
, sizeof(wacom_wac
->touch_name
),
2068 "%s%s Finger", name
, suffix
);
2069 snprintf(wacom_wac
->pad_name
, sizeof(wacom_wac
->pad_name
),
2070 "%s%s Pad", name
, suffix
);
2073 static void wacom_release_resources(struct wacom
*wacom
)
2075 struct hid_device
*hdev
= wacom
->hdev
;
2077 if (!wacom
->resources
)
2080 devres_release_group(&hdev
->dev
, wacom
);
2082 wacom
->resources
= false;
2084 wacom
->wacom_wac
.pen_input
= NULL
;
2085 wacom
->wacom_wac
.touch_input
= NULL
;
2086 wacom
->wacom_wac
.pad_input
= NULL
;
2089 static void wacom_set_shared_values(struct wacom_wac
*wacom_wac
)
2091 if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_TOUCH
) {
2092 wacom_wac
->shared
->type
= wacom_wac
->features
.type
;
2093 wacom_wac
->shared
->touch_input
= wacom_wac
->touch_input
;
2096 if (wacom_wac
->has_mute_touch_switch
)
2097 wacom_wac
->shared
->has_mute_touch_switch
= true;
2099 if (wacom_wac
->shared
->has_mute_touch_switch
&&
2100 wacom_wac
->shared
->touch_input
) {
2101 set_bit(EV_SW
, wacom_wac
->shared
->touch_input
->evbit
);
2102 input_set_capability(wacom_wac
->shared
->touch_input
, EV_SW
,
2107 static int wacom_parse_and_register(struct wacom
*wacom
, bool wireless
)
2109 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2110 struct wacom_features
*features
= &wacom_wac
->features
;
2111 struct hid_device
*hdev
= wacom
->hdev
;
2113 unsigned int connect_mask
= HID_CONNECT_HIDRAW
;
2115 features
->pktlen
= wacom_compute_pktlen(hdev
);
2116 if (features
->pktlen
> WACOM_PKGLEN_MAX
)
2119 if (!devres_open_group(&hdev
->dev
, wacom
, GFP_KERNEL
))
2122 wacom
->resources
= true;
2124 error
= wacom_allocate_inputs(wacom
);
2129 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2130 * into debug mode for the touch part.
2131 * We ignore the other interfaces.
2133 if (features
->type
== BAMBOO_PAD
) {
2134 if (features
->pktlen
== WACOM_PKGLEN_PENABLED
) {
2135 features
->type
= HID_GENERIC
;
2136 } else if ((features
->pktlen
!= WACOM_PKGLEN_BPAD_TOUCH
) &&
2137 (features
->pktlen
!= WACOM_PKGLEN_BPAD_TOUCH_USB
)) {
2143 /* set the default size in case we do not get them from hid */
2144 wacom_set_default_phy(features
);
2146 /* Retrieve the physical and logical size for touch devices */
2147 wacom_retrieve_hid_descriptor(hdev
, features
);
2148 wacom_setup_device_quirks(wacom
);
2150 if (features
->device_type
== WACOM_DEVICETYPE_NONE
&&
2151 features
->type
!= WIRELESS
) {
2152 error
= features
->type
== HID_GENERIC
? -ENODEV
: 0;
2154 dev_warn(&hdev
->dev
, "Unknown device_type for '%s'. %s.",
2156 error
? "Ignoring" : "Assuming pen");
2161 features
->device_type
|= WACOM_DEVICETYPE_PEN
;
2164 wacom_calculate_res(features
);
2166 wacom_update_name(wacom
, wireless
? " (WL)" : "");
2168 error
= wacom_add_shared_data(hdev
);
2172 if (!(features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
) &&
2173 (features
->quirks
& WACOM_QUIRK_BATTERY
)) {
2174 error
= wacom_initialize_battery(wacom
);
2179 error
= wacom_register_inputs(wacom
);
2183 if (wacom
->wacom_wac
.features
.device_type
& WACOM_DEVICETYPE_PAD
) {
2184 error
= wacom_initialize_leds(wacom
);
2188 error
= wacom_initialize_remotes(wacom
);
2193 if (features
->type
== HID_GENERIC
)
2194 connect_mask
|= HID_CONNECT_DRIVER
;
2196 /* Regular HID work starts now */
2197 error
= hid_hw_start(hdev
, connect_mask
);
2199 hid_err(hdev
, "hw start failed\n");
2204 /* Note that if query fails it is not a hard failure */
2205 wacom_query_tablet_data(wacom
);
2208 /* touch only Bamboo doesn't support pen */
2209 if ((features
->type
== BAMBOO_TOUCH
) &&
2210 (features
->device_type
& WACOM_DEVICETYPE_PEN
)) {
2215 /* pen only Bamboo neither support touch nor pad */
2216 if ((features
->type
== BAMBOO_PEN
) &&
2217 ((features
->device_type
& WACOM_DEVICETYPE_TOUCH
) ||
2218 (features
->device_type
& WACOM_DEVICETYPE_PAD
))) {
2223 if (features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
)
2224 error
= hid_hw_open(hdev
);
2226 wacom_set_shared_values(wacom_wac
);
2227 devres_close_group(&hdev
->dev
, wacom
);
2234 wacom_release_resources(wacom
);
2238 static void wacom_wireless_work(struct work_struct
*work
)
2240 struct wacom
*wacom
= container_of(work
, struct wacom
, wireless_work
);
2241 struct usb_device
*usbdev
= wacom
->usbdev
;
2242 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2243 struct hid_device
*hdev1
, *hdev2
;
2244 struct wacom
*wacom1
, *wacom2
;
2245 struct wacom_wac
*wacom_wac1
, *wacom_wac2
;
2249 * Regardless if this is a disconnect or a new tablet,
2250 * remove any existing input and battery devices.
2253 wacom_destroy_battery(wacom
);
2255 /* Stylus interface */
2256 hdev1
= usb_get_intfdata(usbdev
->config
->interface
[1]);
2257 wacom1
= hid_get_drvdata(hdev1
);
2258 wacom_wac1
= &(wacom1
->wacom_wac
);
2259 wacom_release_resources(wacom1
);
2261 /* Touch interface */
2262 hdev2
= usb_get_intfdata(usbdev
->config
->interface
[2]);
2263 wacom2
= hid_get_drvdata(hdev2
);
2264 wacom_wac2
= &(wacom2
->wacom_wac
);
2265 wacom_release_resources(wacom2
);
2267 if (wacom_wac
->pid
== 0) {
2268 hid_info(wacom
->hdev
, "wireless tablet disconnected\n");
2270 const struct hid_device_id
*id
= wacom_ids
;
2272 hid_info(wacom
->hdev
, "wireless tablet connected with PID %x\n",
2276 if (id
->vendor
== USB_VENDOR_ID_WACOM
&&
2277 id
->product
== wacom_wac
->pid
)
2283 hid_info(wacom
->hdev
, "ignoring unknown PID.\n");
2287 /* Stylus interface */
2288 wacom_wac1
->features
=
2289 *((struct wacom_features
*)id
->driver_data
);
2291 wacom_wac1
->pid
= wacom_wac
->pid
;
2293 error
= wacom_parse_and_register(wacom1
, true);
2297 /* Touch interface */
2298 if (wacom_wac1
->features
.touch_max
||
2299 (wacom_wac1
->features
.type
>= INTUOSHT
&&
2300 wacom_wac1
->features
.type
<= BAMBOO_PT
)) {
2301 wacom_wac2
->features
=
2302 *((struct wacom_features
*)id
->driver_data
);
2303 wacom_wac2
->pid
= wacom_wac
->pid
;
2305 error
= wacom_parse_and_register(wacom2
, true);
2310 strlcpy(wacom_wac
->name
, wacom_wac1
->name
,
2311 sizeof(wacom_wac
->name
));
2312 error
= wacom_initialize_battery(wacom
);
2320 wacom_release_resources(wacom1
);
2321 wacom_release_resources(wacom2
);
2325 static void wacom_remote_destroy_one(struct wacom
*wacom
, unsigned int index
)
2327 struct wacom_remote
*remote
= wacom
->remote
;
2328 u32 serial
= remote
->remotes
[index
].serial
;
2330 unsigned long flags
;
2332 spin_lock_irqsave(&remote
->remote_lock
, flags
);
2333 remote
->remotes
[index
].registered
= false;
2334 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2336 if (remote
->remotes
[index
].battery
.battery
)
2337 devres_release_group(&wacom
->hdev
->dev
,
2338 &remote
->remotes
[index
].battery
.bat_desc
);
2340 if (remote
->remotes
[index
].group
.name
)
2341 devres_release_group(&wacom
->hdev
->dev
,
2342 &remote
->remotes
[index
]);
2344 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
2345 if (remote
->remotes
[i
].serial
== serial
) {
2346 remote
->remotes
[i
].serial
= 0;
2347 remote
->remotes
[i
].group
.name
= NULL
;
2348 remote
->remotes
[i
].registered
= false;
2349 remote
->remotes
[i
].battery
.battery
= NULL
;
2350 wacom
->led
.groups
[i
].select
= WACOM_STATUS_UNKNOWN
;
2355 static int wacom_remote_create_one(struct wacom
*wacom
, u32 serial
,
2358 struct wacom_remote
*remote
= wacom
->remote
;
2359 struct device
*dev
= &wacom
->hdev
->dev
;
2362 /* A remote can pair more than once with an EKR,
2363 * check to make sure this serial isn't already paired.
2365 for (k
= 0; k
< WACOM_MAX_REMOTES
; k
++) {
2366 if (remote
->remotes
[k
].serial
== serial
)
2370 if (k
< WACOM_MAX_REMOTES
) {
2371 remote
->remotes
[index
].serial
= serial
;
2375 if (!devres_open_group(dev
, &remote
->remotes
[index
], GFP_KERNEL
))
2378 error
= wacom_remote_create_attr_group(wacom
, serial
, index
);
2382 remote
->remotes
[index
].input
= wacom_allocate_input(wacom
);
2383 if (!remote
->remotes
[index
].input
) {
2387 remote
->remotes
[index
].input
->uniq
= remote
->remotes
[index
].group
.name
;
2388 remote
->remotes
[index
].input
->name
= wacom
->wacom_wac
.pad_name
;
2390 if (!remote
->remotes
[index
].input
->name
) {
2395 error
= wacom_setup_pad_input_capabilities(remote
->remotes
[index
].input
,
2400 remote
->remotes
[index
].serial
= serial
;
2402 error
= input_register_device(remote
->remotes
[index
].input
);
2406 error
= wacom_led_groups_alloc_and_register_one(
2407 &remote
->remotes
[index
].input
->dev
,
2408 wacom
, index
, 3, true);
2412 remote
->remotes
[index
].registered
= true;
2414 devres_close_group(dev
, &remote
->remotes
[index
]);
2418 devres_release_group(dev
, &remote
->remotes
[index
]);
2419 remote
->remotes
[index
].serial
= 0;
2423 static int wacom_remote_attach_battery(struct wacom
*wacom
, int index
)
2425 struct wacom_remote
*remote
= wacom
->remote
;
2428 if (!remote
->remotes
[index
].registered
)
2431 if (remote
->remotes
[index
].battery
.battery
)
2434 if (wacom
->led
.groups
[index
].select
== WACOM_STATUS_UNKNOWN
)
2437 error
= __wacom_initialize_battery(wacom
,
2438 &wacom
->remote
->remotes
[index
].battery
);
2445 static void wacom_remote_work(struct work_struct
*work
)
2447 struct wacom
*wacom
= container_of(work
, struct wacom
, remote_work
);
2448 struct wacom_remote
*remote
= wacom
->remote
;
2449 struct wacom_remote_data data
;
2450 unsigned long flags
;
2455 spin_lock_irqsave(&remote
->remote_lock
, flags
);
2457 count
= kfifo_out(&remote
->remote_fifo
, &data
, sizeof(data
));
2459 if (count
!= sizeof(data
)) {
2460 hid_err(wacom
->hdev
,
2461 "workitem triggered without status available\n");
2462 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2466 if (!kfifo_is_empty(&remote
->remote_fifo
))
2467 wacom_schedule_work(&wacom
->wacom_wac
, WACOM_WORKER_REMOTE
);
2469 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2471 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
2472 serial
= data
.remote
[i
].serial
;
2473 if (data
.remote
[i
].connected
) {
2475 if (remote
->remotes
[i
].serial
== serial
) {
2476 wacom_remote_attach_battery(wacom
, i
);
2480 if (remote
->remotes
[i
].serial
)
2481 wacom_remote_destroy_one(wacom
, i
);
2483 wacom_remote_create_one(wacom
, serial
, i
);
2485 } else if (remote
->remotes
[i
].serial
) {
2486 wacom_remote_destroy_one(wacom
, i
);
2491 static int wacom_probe(struct hid_device
*hdev
,
2492 const struct hid_device_id
*id
)
2494 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
2495 struct usb_device
*dev
= interface_to_usbdev(intf
);
2496 struct wacom
*wacom
;
2497 struct wacom_wac
*wacom_wac
;
2498 struct wacom_features
*features
;
2501 if (!id
->driver_data
)
2504 hdev
->quirks
|= HID_QUIRK_NO_INIT_REPORTS
;
2506 /* hid-core sets this quirk for the boot interface */
2507 hdev
->quirks
&= ~HID_QUIRK_NOGET
;
2509 wacom
= devm_kzalloc(&hdev
->dev
, sizeof(struct wacom
), GFP_KERNEL
);
2513 hid_set_drvdata(hdev
, wacom
);
2516 wacom_wac
= &wacom
->wacom_wac
;
2517 wacom_wac
->features
= *((struct wacom_features
*)id
->driver_data
);
2518 features
= &wacom_wac
->features
;
2520 if (features
->check_for_hid_type
&& features
->hid_type
!= hdev
->type
) {
2525 wacom_wac
->hid_data
.inputmode
= -1;
2526 wacom_wac
->mode_report
= -1;
2528 wacom
->usbdev
= dev
;
2530 mutex_init(&wacom
->lock
);
2531 INIT_DELAYED_WORK(&wacom
->init_work
, wacom_init_work
);
2532 INIT_WORK(&wacom
->wireless_work
, wacom_wireless_work
);
2533 INIT_WORK(&wacom
->battery_work
, wacom_battery_work
);
2534 INIT_WORK(&wacom
->remote_work
, wacom_remote_work
);
2536 /* ask for the report descriptor to be loaded by HID */
2537 error
= hid_parse(hdev
);
2539 hid_err(hdev
, "parse failed\n");
2543 error
= wacom_parse_and_register(wacom
, false);
2547 if (hdev
->bus
== BUS_BLUETOOTH
) {
2548 error
= device_create_file(&hdev
->dev
, &dev_attr_speed
);
2551 "can't create sysfs speed attribute err: %d\n",
2558 hid_set_drvdata(hdev
, NULL
);
2562 static void wacom_remove(struct hid_device
*hdev
)
2564 struct wacom
*wacom
= hid_get_drvdata(hdev
);
2565 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2566 struct wacom_features
*features
= &wacom_wac
->features
;
2568 if (features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
)
2573 cancel_delayed_work_sync(&wacom
->init_work
);
2574 cancel_work_sync(&wacom
->wireless_work
);
2575 cancel_work_sync(&wacom
->battery_work
);
2576 cancel_work_sync(&wacom
->remote_work
);
2577 if (hdev
->bus
== BUS_BLUETOOTH
)
2578 device_remove_file(&hdev
->dev
, &dev_attr_speed
);
2580 /* make sure we don't trigger the LEDs */
2581 wacom_led_groups_release(wacom
);
2582 wacom_release_resources(wacom
);
2584 hid_set_drvdata(hdev
, NULL
);
2588 static int wacom_resume(struct hid_device
*hdev
)
2590 struct wacom
*wacom
= hid_get_drvdata(hdev
);
2592 mutex_lock(&wacom
->lock
);
2594 /* switch to wacom mode first */
2595 _wacom_query_tablet_data(wacom
);
2596 wacom_led_control(wacom
);
2598 mutex_unlock(&wacom
->lock
);
2603 static int wacom_reset_resume(struct hid_device
*hdev
)
2605 return wacom_resume(hdev
);
2607 #endif /* CONFIG_PM */
2609 static struct hid_driver wacom_driver
= {
2611 .id_table
= wacom_ids
,
2612 .probe
= wacom_probe
,
2613 .remove
= wacom_remove
,
2614 .report
= wacom_wac_report
,
2616 .resume
= wacom_resume
,
2617 .reset_resume
= wacom_reset_resume
,
2619 .raw_event
= wacom_raw_event
,
2621 module_hid_driver(wacom_driver
);
2623 MODULE_VERSION(DRIVER_VERSION
);
2624 MODULE_AUTHOR(DRIVER_AUTHOR
);
2625 MODULE_DESCRIPTION(DRIVER_DESC
);
2626 MODULE_LICENSE("GPL");