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 (wacom_wac
->has_mode_change
) {
329 if (wacom_wac
->is_direct_mode
)
330 features
->device_type
|= WACOM_DEVICETYPE_DIRECT
;
332 features
->device_type
&= ~WACOM_DEVICETYPE_DIRECT
;
335 if (features
->touch_max
> 1) {
336 if (features
->device_type
& WACOM_DEVICETYPE_DIRECT
)
337 input_mt_init_slots(wacom_wac
->touch_input
,
338 wacom_wac
->features
.touch_max
,
341 input_mt_init_slots(wacom_wac
->touch_input
,
342 wacom_wac
->features
.touch_max
,
348 static void wacom_parse_hid(struct hid_device
*hdev
,
349 struct wacom_features
*features
)
351 struct hid_report_enum
*rep_enum
;
352 struct hid_report
*hreport
;
355 /* check features first */
356 rep_enum
= &hdev
->report_enum
[HID_FEATURE_REPORT
];
357 list_for_each_entry(hreport
, &rep_enum
->report_list
, list
) {
358 for (i
= 0; i
< hreport
->maxfield
; i
++) {
359 /* Ignore if report count is out of bounds. */
360 if (hreport
->field
[i
]->report_count
< 1)
363 for (j
= 0; j
< hreport
->field
[i
]->maxusage
; j
++) {
364 wacom_feature_mapping(hdev
, hreport
->field
[i
],
365 hreport
->field
[i
]->usage
+ j
);
370 /* now check the input usages */
371 rep_enum
= &hdev
->report_enum
[HID_INPUT_REPORT
];
372 list_for_each_entry(hreport
, &rep_enum
->report_list
, list
) {
374 if (!hreport
->maxfield
)
377 for (i
= 0; i
< hreport
->maxfield
; i
++)
378 for (j
= 0; j
< hreport
->field
[i
]->maxusage
; j
++)
379 wacom_usage_mapping(hdev
, hreport
->field
[i
],
380 hreport
->field
[i
]->usage
+ j
);
383 wacom_post_parse_hid(hdev
, features
);
386 static int wacom_hid_set_device_mode(struct hid_device
*hdev
)
388 struct wacom
*wacom
= hid_get_drvdata(hdev
);
389 struct hid_data
*hid_data
= &wacom
->wacom_wac
.hid_data
;
390 struct hid_report
*r
;
391 struct hid_report_enum
*re
;
393 if (hid_data
->inputmode
< 0)
396 re
= &(hdev
->report_enum
[HID_FEATURE_REPORT
]);
397 r
= re
->report_id_hash
[hid_data
->inputmode
];
399 r
->field
[0]->value
[hid_data
->inputmode_index
] = 2;
400 hid_hw_request(hdev
, r
, HID_REQ_SET_REPORT
);
405 static int wacom_set_device_mode(struct hid_device
*hdev
,
406 struct wacom_wac
*wacom_wac
)
409 struct hid_report
*r
;
410 struct hid_report_enum
*re
;
412 int error
= -ENOMEM
, limit
= 0;
414 if (wacom_wac
->mode_report
< 0)
417 re
= &(hdev
->report_enum
[HID_FEATURE_REPORT
]);
418 r
= re
->report_id_hash
[wacom_wac
->mode_report
];
422 rep_data
= hid_alloc_report_buf(r
, GFP_KERNEL
);
426 length
= hid_report_len(r
);
429 rep_data
[0] = wacom_wac
->mode_report
;
430 rep_data
[1] = wacom_wac
->mode_value
;
432 error
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
,
435 error
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
436 rep_data
, length
, 1);
437 } while (error
>= 0 &&
438 rep_data
[1] != wacom_wac
->mode_report
&&
439 limit
++ < WAC_MSG_RETRIES
);
443 return error
< 0 ? error
: 0;
446 static int wacom_bt_query_tablet_data(struct hid_device
*hdev
, u8 speed
,
447 struct wacom_features
*features
)
449 struct wacom
*wacom
= hid_get_drvdata(hdev
);
453 switch (features
->type
) {
457 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
, 2,
461 rep_data
[0] = speed
== 0 ? 0x05 : 0x06;
464 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
,
468 wacom
->wacom_wac
.bt_high_speed
= speed
;
474 * Note that if the raw queries fail, it's not a hard failure
475 * and it is safe to continue
477 hid_warn(hdev
, "failed to poke device, command %d, err %d\n",
482 wacom
->wacom_wac
.bt_features
&= ~0x20;
484 wacom
->wacom_wac
.bt_features
|= 0x20;
487 rep_data
[1] = wacom
->wacom_wac
.bt_features
;
489 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
, 2,
492 wacom
->wacom_wac
.bt_high_speed
= speed
;
500 * Switch the tablet into its most-capable mode. Wacom tablets are
501 * typically configured to power-up in a mode which sends mouse-like
502 * reports to the OS. To get absolute position, pressure data, etc.
503 * from the tablet, it is necessary to switch the tablet out of this
504 * mode and into one which sends the full range of tablet data.
506 static int _wacom_query_tablet_data(struct wacom
*wacom
)
508 struct hid_device
*hdev
= wacom
->hdev
;
509 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
510 struct wacom_features
*features
= &wacom_wac
->features
;
512 if (hdev
->bus
== BUS_BLUETOOTH
)
513 return wacom_bt_query_tablet_data(hdev
, 1, features
);
515 if (features
->type
!= HID_GENERIC
) {
516 if (features
->device_type
& WACOM_DEVICETYPE_TOUCH
) {
517 if (features
->type
> TABLETPC
) {
518 /* MT Tablet PC touch */
519 wacom_wac
->mode_report
= 3;
520 wacom_wac
->mode_value
= 4;
521 } else if (features
->type
== WACOM_24HDT
) {
522 wacom_wac
->mode_report
= 18;
523 wacom_wac
->mode_value
= 2;
524 } else if (features
->type
== WACOM_27QHDT
) {
525 wacom_wac
->mode_report
= 131;
526 wacom_wac
->mode_value
= 2;
527 } else if (features
->type
== BAMBOO_PAD
) {
528 wacom_wac
->mode_report
= 2;
529 wacom_wac
->mode_value
= 2;
531 } else if (features
->device_type
& WACOM_DEVICETYPE_PEN
) {
532 if (features
->type
<= BAMBOO_PT
) {
533 wacom_wac
->mode_report
= 2;
534 wacom_wac
->mode_value
= 2;
539 wacom_set_device_mode(hdev
, wacom_wac
);
541 if (features
->type
== HID_GENERIC
)
542 return wacom_hid_set_device_mode(hdev
);
547 static void wacom_retrieve_hid_descriptor(struct hid_device
*hdev
,
548 struct wacom_features
*features
)
550 struct wacom
*wacom
= hid_get_drvdata(hdev
);
551 struct usb_interface
*intf
= wacom
->intf
;
553 /* default features */
554 features
->x_fuzz
= 4;
555 features
->y_fuzz
= 4;
556 features
->pressure_fuzz
= 0;
557 features
->distance_fuzz
= 1;
558 features
->tilt_fuzz
= 1;
561 * The wireless device HID is basic and layout conflicts with
562 * other tablets (monitor and touch interface can look like pen).
563 * Skip the query for this type and modify defaults based on
566 if (features
->type
== WIRELESS
) {
567 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 0)
568 features
->device_type
= WACOM_DEVICETYPE_WL_MONITOR
;
570 features
->device_type
= WACOM_DEVICETYPE_NONE
;
574 wacom_parse_hid(hdev
, features
);
577 struct wacom_hdev_data
{
578 struct list_head list
;
580 struct hid_device
*dev
;
581 struct wacom_shared shared
;
584 static LIST_HEAD(wacom_udev_list
);
585 static DEFINE_MUTEX(wacom_udev_list_lock
);
587 static bool compare_device_paths(struct hid_device
*hdev_a
,
588 struct hid_device
*hdev_b
, char separator
)
590 int n1
= strrchr(hdev_a
->phys
, separator
) - hdev_a
->phys
;
591 int n2
= strrchr(hdev_b
->phys
, separator
) - hdev_b
->phys
;
593 if (n1
!= n2
|| n1
<= 0 || n2
<= 0)
596 return !strncmp(hdev_a
->phys
, hdev_b
->phys
, n1
);
599 static bool wacom_are_sibling(struct hid_device
*hdev
,
600 struct hid_device
*sibling
)
602 struct wacom
*wacom
= hid_get_drvdata(hdev
);
603 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
604 struct wacom
*sibling_wacom
= hid_get_drvdata(sibling
);
605 struct wacom_features
*sibling_features
= &sibling_wacom
->wacom_wac
.features
;
606 __u32 oVid
= features
->oVid
? features
->oVid
: hdev
->vendor
;
607 __u32 oPid
= features
->oPid
? features
->oPid
: hdev
->product
;
609 /* The defined oVid/oPid must match that of the sibling */
610 if (features
->oVid
!= HID_ANY_ID
&& sibling
->vendor
!= oVid
)
612 if (features
->oPid
!= HID_ANY_ID
&& sibling
->product
!= oPid
)
616 * Devices with the same VID/PID must share the same physical
617 * device path, while those with different VID/PID must share
618 * the same physical parent device path.
620 if (hdev
->vendor
== sibling
->vendor
&& hdev
->product
== sibling
->product
) {
621 if (!compare_device_paths(hdev
, sibling
, '/'))
624 if (!compare_device_paths(hdev
, sibling
, '.'))
628 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
629 if (features
->type
!= HID_GENERIC
)
633 * Direct-input devices may not be siblings of indirect-input
636 if ((features
->device_type
& WACOM_DEVICETYPE_DIRECT
) &&
637 !(sibling_features
->device_type
& WACOM_DEVICETYPE_DIRECT
))
641 * Indirect-input devices may not be siblings of direct-input
644 if (!(features
->device_type
& WACOM_DEVICETYPE_DIRECT
) &&
645 (sibling_features
->device_type
& WACOM_DEVICETYPE_DIRECT
))
648 /* Pen devices may only be siblings of touch devices */
649 if ((features
->device_type
& WACOM_DEVICETYPE_PEN
) &&
650 !(sibling_features
->device_type
& WACOM_DEVICETYPE_TOUCH
))
653 /* Touch devices may only be siblings of pen devices */
654 if ((features
->device_type
& WACOM_DEVICETYPE_TOUCH
) &&
655 !(sibling_features
->device_type
& WACOM_DEVICETYPE_PEN
))
659 * No reason could be found for these two devices to NOT be
660 * siblings, so there's a good chance they ARE siblings
665 static struct wacom_hdev_data
*wacom_get_hdev_data(struct hid_device
*hdev
)
667 struct wacom_hdev_data
*data
;
669 /* Try to find an already-probed interface from the same device */
670 list_for_each_entry(data
, &wacom_udev_list
, list
) {
671 if (compare_device_paths(hdev
, data
->dev
, '/'))
675 /* Fallback to finding devices that appear to be "siblings" */
676 list_for_each_entry(data
, &wacom_udev_list
, list
) {
677 if (wacom_are_sibling(hdev
, data
->dev
)) {
678 kref_get(&data
->kref
);
686 static void wacom_release_shared_data(struct kref
*kref
)
688 struct wacom_hdev_data
*data
=
689 container_of(kref
, struct wacom_hdev_data
, kref
);
691 mutex_lock(&wacom_udev_list_lock
);
692 list_del(&data
->list
);
693 mutex_unlock(&wacom_udev_list_lock
);
698 static void wacom_remove_shared_data(void *res
)
700 struct wacom
*wacom
= res
;
701 struct wacom_hdev_data
*data
;
702 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
704 if (wacom_wac
->shared
) {
705 data
= container_of(wacom_wac
->shared
, struct wacom_hdev_data
,
708 if (wacom_wac
->shared
->touch
== wacom
->hdev
)
709 wacom_wac
->shared
->touch
= NULL
;
710 else if (wacom_wac
->shared
->pen
== wacom
->hdev
)
711 wacom_wac
->shared
->pen
= NULL
;
713 kref_put(&data
->kref
, wacom_release_shared_data
);
714 wacom_wac
->shared
= NULL
;
718 static int wacom_add_shared_data(struct hid_device
*hdev
)
720 struct wacom
*wacom
= hid_get_drvdata(hdev
);
721 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
722 struct wacom_hdev_data
*data
;
725 mutex_lock(&wacom_udev_list_lock
);
727 data
= wacom_get_hdev_data(hdev
);
729 data
= kzalloc(sizeof(struct wacom_hdev_data
), GFP_KERNEL
);
735 kref_init(&data
->kref
);
737 list_add_tail(&data
->list
, &wacom_udev_list
);
740 wacom_wac
->shared
= &data
->shared
;
742 retval
= devm_add_action(&hdev
->dev
, wacom_remove_shared_data
, wacom
);
744 mutex_unlock(&wacom_udev_list_lock
);
745 wacom_remove_shared_data(wacom
);
749 if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_TOUCH
)
750 wacom_wac
->shared
->touch
= hdev
;
751 else if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_PEN
)
752 wacom_wac
->shared
->pen
= hdev
;
755 mutex_unlock(&wacom_udev_list_lock
);
759 static int wacom_led_control(struct wacom
*wacom
)
763 unsigned char report_id
= WAC_CMD_LED_CONTROL
;
766 if (!wacom
->led
.groups
)
769 if (wacom
->wacom_wac
.pid
) { /* wireless connected */
770 report_id
= WAC_CMD_WL_LED_CONTROL
;
773 else if (wacom
->wacom_wac
.features
.type
== INTUOSP2_BT
) {
774 report_id
= WAC_CMD_WL_INTUOSP2
;
777 buf
= kzalloc(buf_size
, GFP_KERNEL
);
781 if (wacom
->wacom_wac
.features
.type
== HID_GENERIC
) {
782 buf
[0] = WAC_CMD_LED_CONTROL_GENERIC
;
783 buf
[1] = wacom
->led
.llv
;
784 buf
[2] = wacom
->led
.groups
[0].select
& 0x03;
786 } else if ((wacom
->wacom_wac
.features
.type
>= INTUOS5S
&&
787 wacom
->wacom_wac
.features
.type
<= INTUOSPL
)) {
789 * Touch Ring and crop mark LED luminance may take on
790 * one of four values:
791 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
793 int ring_led
= wacom
->led
.groups
[0].select
& 0x03;
794 int ring_lum
= (((wacom
->led
.llv
& 0x60) >> 5) - 1) & 0x03;
796 unsigned char led_bits
= (crop_lum
<< 4) | (ring_lum
<< 2) | (ring_led
);
799 if (wacom
->wacom_wac
.pid
) {
800 wacom_get_report(wacom
->hdev
, HID_FEATURE_REPORT
,
801 buf
, buf_size
, WAC_CMD_RETRIES
);
807 else if (wacom
->wacom_wac
.features
.type
== INTUOSP2_BT
) {
809 buf
[4] = 100; // Power Connection LED (ORANGE)
810 buf
[5] = 100; // BT Connection LED (BLUE)
811 buf
[6] = 100; // Paper Mode (RED?)
812 buf
[7] = 100; // Paper Mode (GREEN?)
813 buf
[8] = 100; // Paper Mode (BLUE?)
814 buf
[9] = wacom
->led
.llv
;
815 buf
[10] = wacom
->led
.groups
[0].select
& 0x03;
818 int led
= wacom
->led
.groups
[0].select
| 0x4;
820 if (wacom
->wacom_wac
.features
.type
== WACOM_21UX2
||
821 wacom
->wacom_wac
.features
.type
== WACOM_24HD
)
822 led
|= (wacom
->led
.groups
[1].select
<< 4) | 0x40;
826 buf
[2] = wacom
->led
.llv
;
827 buf
[3] = wacom
->led
.hlv
;
828 buf
[4] = wacom
->led
.img_lum
;
831 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, buf_size
,
838 static int wacom_led_putimage(struct wacom
*wacom
, int button_id
, u8 xfer_id
,
839 const unsigned len
, const void *img
)
843 const unsigned chunk_len
= len
/ 4; /* 4 chunks are needed to be sent */
845 buf
= kzalloc(chunk_len
+ 3 , GFP_KERNEL
);
849 /* Send 'start' command */
850 buf
[0] = WAC_CMD_ICON_START
;
852 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, 2,
858 buf
[1] = button_id
& 0x07;
859 for (i
= 0; i
< 4; i
++) {
861 memcpy(buf
+ 3, img
+ i
* chunk_len
, chunk_len
);
863 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
,
864 buf
, chunk_len
+ 3, WAC_CMD_RETRIES
);
870 buf
[0] = WAC_CMD_ICON_START
;
872 wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, 2,
880 static ssize_t
wacom_led_select_store(struct device
*dev
, int set_id
,
881 const char *buf
, size_t count
)
883 struct hid_device
*hdev
= to_hid_device(dev
);
884 struct wacom
*wacom
= hid_get_drvdata(hdev
);
888 err
= kstrtouint(buf
, 10, &id
);
892 mutex_lock(&wacom
->lock
);
894 wacom
->led
.groups
[set_id
].select
= id
& 0x3;
895 err
= wacom_led_control(wacom
);
897 mutex_unlock(&wacom
->lock
);
899 return err
< 0 ? err
: count
;
902 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
903 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
904 struct device_attribute *attr, const char *buf, size_t count) \
906 return wacom_led_select_store(dev, SET_ID, buf, count); \
908 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
909 struct device_attribute *attr, char *buf) \
911 struct hid_device *hdev = to_hid_device(dev);\
912 struct wacom *wacom = hid_get_drvdata(hdev); \
913 return scnprintf(buf, PAGE_SIZE, "%d\n", \
914 wacom->led.groups[SET_ID].select); \
916 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
917 wacom_led##SET_ID##_select_show, \
918 wacom_led##SET_ID##_select_store)
920 DEVICE_LED_SELECT_ATTR(0);
921 DEVICE_LED_SELECT_ATTR(1);
923 static ssize_t
wacom_luminance_store(struct wacom
*wacom
, u8
*dest
,
924 const char *buf
, size_t count
)
929 err
= kstrtouint(buf
, 10, &value
);
933 mutex_lock(&wacom
->lock
);
935 *dest
= value
& 0x7f;
936 err
= wacom_led_control(wacom
);
938 mutex_unlock(&wacom
->lock
);
940 return err
< 0 ? err
: count
;
943 #define DEVICE_LUMINANCE_ATTR(name, field) \
944 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
945 struct device_attribute *attr, const char *buf, size_t count) \
947 struct hid_device *hdev = to_hid_device(dev);\
948 struct wacom *wacom = hid_get_drvdata(hdev); \
950 return wacom_luminance_store(wacom, &wacom->led.field, \
953 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
954 struct device_attribute *attr, char *buf) \
956 struct wacom *wacom = dev_get_drvdata(dev); \
957 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
959 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
960 wacom_##name##_luminance_show, \
961 wacom_##name##_luminance_store)
963 DEVICE_LUMINANCE_ATTR(status0
, llv
);
964 DEVICE_LUMINANCE_ATTR(status1
, hlv
);
965 DEVICE_LUMINANCE_ATTR(buttons
, img_lum
);
967 static ssize_t
wacom_button_image_store(struct device
*dev
, int button_id
,
968 const char *buf
, size_t count
)
970 struct hid_device
*hdev
= to_hid_device(dev
);
971 struct wacom
*wacom
= hid_get_drvdata(hdev
);
976 if (hdev
->bus
== BUS_BLUETOOTH
) {
978 xfer_id
= WAC_CMD_ICON_BT_XFER
;
981 xfer_id
= WAC_CMD_ICON_XFER
;
987 mutex_lock(&wacom
->lock
);
989 err
= wacom_led_putimage(wacom
, button_id
, xfer_id
, len
, buf
);
991 mutex_unlock(&wacom
->lock
);
993 return err
< 0 ? err
: count
;
996 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
997 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
998 struct device_attribute *attr, const char *buf, size_t count) \
1000 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1002 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
1003 NULL, wacom_btnimg##BUTTON_ID##_store)
1005 DEVICE_BTNIMG_ATTR(0);
1006 DEVICE_BTNIMG_ATTR(1);
1007 DEVICE_BTNIMG_ATTR(2);
1008 DEVICE_BTNIMG_ATTR(3);
1009 DEVICE_BTNIMG_ATTR(4);
1010 DEVICE_BTNIMG_ATTR(5);
1011 DEVICE_BTNIMG_ATTR(6);
1012 DEVICE_BTNIMG_ATTR(7);
1014 static struct attribute
*cintiq_led_attrs
[] = {
1015 &dev_attr_status_led0_select
.attr
,
1016 &dev_attr_status_led1_select
.attr
,
1020 static struct attribute_group cintiq_led_attr_group
= {
1021 .name
= "wacom_led",
1022 .attrs
= cintiq_led_attrs
,
1025 static struct attribute
*intuos4_led_attrs
[] = {
1026 &dev_attr_status0_luminance
.attr
,
1027 &dev_attr_status1_luminance
.attr
,
1028 &dev_attr_status_led0_select
.attr
,
1029 &dev_attr_buttons_luminance
.attr
,
1030 &dev_attr_button0_rawimg
.attr
,
1031 &dev_attr_button1_rawimg
.attr
,
1032 &dev_attr_button2_rawimg
.attr
,
1033 &dev_attr_button3_rawimg
.attr
,
1034 &dev_attr_button4_rawimg
.attr
,
1035 &dev_attr_button5_rawimg
.attr
,
1036 &dev_attr_button6_rawimg
.attr
,
1037 &dev_attr_button7_rawimg
.attr
,
1041 static struct attribute_group intuos4_led_attr_group
= {
1042 .name
= "wacom_led",
1043 .attrs
= intuos4_led_attrs
,
1046 static struct attribute
*intuos5_led_attrs
[] = {
1047 &dev_attr_status0_luminance
.attr
,
1048 &dev_attr_status_led0_select
.attr
,
1052 static struct attribute_group intuos5_led_attr_group
= {
1053 .name
= "wacom_led",
1054 .attrs
= intuos5_led_attrs
,
1057 static struct attribute
*generic_led_attrs
[] = {
1058 &dev_attr_status0_luminance
.attr
,
1059 &dev_attr_status_led0_select
.attr
,
1063 static struct attribute_group generic_led_attr_group
= {
1064 .name
= "wacom_led",
1065 .attrs
= generic_led_attrs
,
1068 struct wacom_sysfs_group_devres
{
1069 struct attribute_group
*group
;
1070 struct kobject
*root
;
1073 static void wacom_devm_sysfs_group_release(struct device
*dev
, void *res
)
1075 struct wacom_sysfs_group_devres
*devres
= res
;
1076 struct kobject
*kobj
= devres
->root
;
1078 dev_dbg(dev
, "%s: dropping reference to %s\n",
1079 __func__
, devres
->group
->name
);
1080 sysfs_remove_group(kobj
, devres
->group
);
1083 static int __wacom_devm_sysfs_create_group(struct wacom
*wacom
,
1084 struct kobject
*root
,
1085 struct attribute_group
*group
)
1087 struct wacom_sysfs_group_devres
*devres
;
1090 devres
= devres_alloc(wacom_devm_sysfs_group_release
,
1091 sizeof(struct wacom_sysfs_group_devres
),
1096 devres
->group
= group
;
1097 devres
->root
= root
;
1099 error
= sysfs_create_group(devres
->root
, group
);
1103 devres_add(&wacom
->hdev
->dev
, devres
);
1108 static int wacom_devm_sysfs_create_group(struct wacom
*wacom
,
1109 struct attribute_group
*group
)
1111 return __wacom_devm_sysfs_create_group(wacom
, &wacom
->hdev
->dev
.kobj
,
1115 enum led_brightness
wacom_leds_brightness_get(struct wacom_led
*led
)
1117 struct wacom
*wacom
= led
->wacom
;
1119 if (wacom
->led
.max_hlv
)
1120 return led
->hlv
* LED_FULL
/ wacom
->led
.max_hlv
;
1122 if (wacom
->led
.max_llv
)
1123 return led
->llv
* LED_FULL
/ wacom
->led
.max_llv
;
1125 /* device doesn't support brightness tuning */
1129 static enum led_brightness
__wacom_led_brightness_get(struct led_classdev
*cdev
)
1131 struct wacom_led
*led
= container_of(cdev
, struct wacom_led
, cdev
);
1132 struct wacom
*wacom
= led
->wacom
;
1134 if (wacom
->led
.groups
[led
->group
].select
!= led
->id
)
1137 return wacom_leds_brightness_get(led
);
1140 static int wacom_led_brightness_set(struct led_classdev
*cdev
,
1141 enum led_brightness brightness
)
1143 struct wacom_led
*led
= container_of(cdev
, struct wacom_led
, cdev
);
1144 struct wacom
*wacom
= led
->wacom
;
1147 mutex_lock(&wacom
->lock
);
1149 if (!wacom
->led
.groups
|| (brightness
== LED_OFF
&&
1150 wacom
->led
.groups
[led
->group
].select
!= led
->id
)) {
1155 led
->llv
= wacom
->led
.llv
= wacom
->led
.max_llv
* brightness
/ LED_FULL
;
1156 led
->hlv
= wacom
->led
.hlv
= wacom
->led
.max_hlv
* brightness
/ LED_FULL
;
1158 wacom
->led
.groups
[led
->group
].select
= led
->id
;
1160 error
= wacom_led_control(wacom
);
1163 mutex_unlock(&wacom
->lock
);
1168 static void wacom_led_readonly_brightness_set(struct led_classdev
*cdev
,
1169 enum led_brightness brightness
)
1173 static int wacom_led_register_one(struct device
*dev
, struct wacom
*wacom
,
1174 struct wacom_led
*led
, unsigned int group
,
1175 unsigned int id
, bool read_only
)
1180 name
= devm_kasprintf(dev
, GFP_KERNEL
,
1189 led
->trigger
.name
= name
;
1190 error
= devm_led_trigger_register(dev
, &led
->trigger
);
1192 hid_err(wacom
->hdev
,
1193 "failed to register LED trigger %s: %d\n",
1194 led
->cdev
.name
, error
);
1202 led
->llv
= wacom
->led
.llv
;
1203 led
->hlv
= wacom
->led
.hlv
;
1204 led
->cdev
.name
= name
;
1205 led
->cdev
.max_brightness
= LED_FULL
;
1206 led
->cdev
.flags
= LED_HW_PLUGGABLE
;
1207 led
->cdev
.brightness_get
= __wacom_led_brightness_get
;
1209 led
->cdev
.brightness_set_blocking
= wacom_led_brightness_set
;
1210 led
->cdev
.default_trigger
= led
->cdev
.name
;
1212 led
->cdev
.brightness_set
= wacom_led_readonly_brightness_set
;
1215 error
= devm_led_classdev_register(dev
, &led
->cdev
);
1217 hid_err(wacom
->hdev
,
1218 "failed to register LED %s: %d\n",
1219 led
->cdev
.name
, error
);
1220 led
->cdev
.name
= NULL
;
1227 static void wacom_led_groups_release_one(void *data
)
1229 struct wacom_group_leds
*group
= data
;
1231 devres_release_group(group
->dev
, group
);
1234 static int wacom_led_groups_alloc_and_register_one(struct device
*dev
,
1235 struct wacom
*wacom
,
1236 int group_id
, int count
,
1239 struct wacom_led
*leds
;
1242 if (group_id
>= wacom
->led
.count
|| count
<= 0)
1245 if (!devres_open_group(dev
, &wacom
->led
.groups
[group_id
], GFP_KERNEL
))
1248 leds
= devm_kzalloc(dev
, sizeof(struct wacom_led
) * count
, GFP_KERNEL
);
1254 wacom
->led
.groups
[group_id
].leds
= leds
;
1255 wacom
->led
.groups
[group_id
].count
= count
;
1257 for (i
= 0; i
< count
; i
++) {
1258 error
= wacom_led_register_one(dev
, wacom
, &leds
[i
],
1259 group_id
, i
, read_only
);
1264 wacom
->led
.groups
[group_id
].dev
= dev
;
1266 devres_close_group(dev
, &wacom
->led
.groups
[group_id
]);
1269 * There is a bug (?) in devm_led_classdev_register() in which its
1270 * increments the refcount of the parent. If the parent is an input
1271 * device, that means the ref count never reaches 0 when
1272 * devm_input_device_release() gets called.
1273 * This means that the LEDs are still there after disconnect.
1274 * Manually force the release of the group so that the leds are released
1275 * once we are done using them.
1277 error
= devm_add_action_or_reset(&wacom
->hdev
->dev
,
1278 wacom_led_groups_release_one
,
1279 &wacom
->led
.groups
[group_id
]);
1286 devres_release_group(dev
, &wacom
->led
.groups
[group_id
]);
1290 struct wacom_led
*wacom_led_find(struct wacom
*wacom
, unsigned int group_id
,
1293 struct wacom_group_leds
*group
;
1295 if (group_id
>= wacom
->led
.count
)
1298 group
= &wacom
->led
.groups
[group_id
];
1305 return &group
->leds
[id
];
1309 * wacom_led_next: gives the next available led with a wacom trigger.
1311 * returns the next available struct wacom_led which has its default trigger
1312 * or the current one if none is available.
1314 struct wacom_led
*wacom_led_next(struct wacom
*wacom
, struct wacom_led
*cur
)
1316 struct wacom_led
*next_led
;
1326 next_led
= wacom_led_find(wacom
, group
, ++next
);
1327 if (!next_led
|| next_led
== cur
)
1329 } while (next_led
->cdev
.trigger
!= &next_led
->trigger
);
1334 static void wacom_led_groups_release(void *data
)
1336 struct wacom
*wacom
= data
;
1338 wacom
->led
.groups
= NULL
;
1339 wacom
->led
.count
= 0;
1342 static int wacom_led_groups_allocate(struct wacom
*wacom
, int count
)
1344 struct device
*dev
= &wacom
->hdev
->dev
;
1345 struct wacom_group_leds
*groups
;
1348 groups
= devm_kzalloc(dev
, sizeof(struct wacom_group_leds
) * count
,
1353 error
= devm_add_action_or_reset(dev
, wacom_led_groups_release
, wacom
);
1357 wacom
->led
.groups
= groups
;
1358 wacom
->led
.count
= count
;
1363 static int wacom_leds_alloc_and_register(struct wacom
*wacom
, int group_count
,
1364 int led_per_group
, bool read_only
)
1369 if (!wacom
->wacom_wac
.pad_input
)
1372 dev
= &wacom
->wacom_wac
.pad_input
->dev
;
1374 error
= wacom_led_groups_allocate(wacom
, group_count
);
1378 for (i
= 0; i
< group_count
; i
++) {
1379 error
= wacom_led_groups_alloc_and_register_one(dev
, wacom
, i
,
1389 int wacom_initialize_leds(struct wacom
*wacom
)
1393 if (!(wacom
->wacom_wac
.features
.device_type
& WACOM_DEVICETYPE_PAD
))
1396 /* Initialize default values */
1397 switch (wacom
->wacom_wac
.features
.type
) {
1399 if (!wacom
->generic_has_leds
)
1401 wacom
->led
.llv
= 100;
1402 wacom
->led
.max_llv
= 100;
1404 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1406 hid_err(wacom
->hdev
,
1407 "cannot create leds err: %d\n", error
);
1411 error
= wacom_devm_sysfs_create_group(wacom
,
1412 &generic_led_attr_group
);
1419 wacom
->led
.llv
= 10;
1420 wacom
->led
.hlv
= 20;
1421 wacom
->led
.max_llv
= 127;
1422 wacom
->led
.max_hlv
= 127;
1423 wacom
->led
.img_lum
= 10;
1425 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1427 hid_err(wacom
->hdev
,
1428 "cannot create leds err: %d\n", error
);
1432 error
= wacom_devm_sysfs_create_group(wacom
,
1433 &intuos4_led_attr_group
);
1440 wacom
->led
.img_lum
= 0;
1442 error
= wacom_leds_alloc_and_register(wacom
, 2, 4, false);
1444 hid_err(wacom
->hdev
,
1445 "cannot create leds err: %d\n", error
);
1449 error
= wacom_devm_sysfs_create_group(wacom
,
1450 &cintiq_led_attr_group
);
1459 wacom
->led
.llv
= 32;
1460 wacom
->led
.max_llv
= 96;
1462 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1464 hid_err(wacom
->hdev
,
1465 "cannot create leds err: %d\n", error
);
1469 error
= wacom_devm_sysfs_create_group(wacom
,
1470 &intuos5_led_attr_group
);
1474 wacom
->led
.llv
= 50;
1475 wacom
->led
.max_llv
= 100;
1476 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1478 hid_err(wacom
->hdev
,
1479 "cannot create leds err: %d\n", error
);
1485 wacom
->led
.llv
= 255;
1486 wacom
->led
.max_llv
= 255;
1487 error
= wacom_led_groups_allocate(wacom
, 5);
1489 hid_err(wacom
->hdev
,
1490 "cannot create leds err: %d\n", error
);
1500 hid_err(wacom
->hdev
,
1501 "cannot create sysfs group err: %d\n", error
);
1508 static void wacom_init_work(struct work_struct
*work
)
1510 struct wacom
*wacom
= container_of(work
, struct wacom
, init_work
.work
);
1512 _wacom_query_tablet_data(wacom
);
1513 wacom_led_control(wacom
);
1516 static void wacom_query_tablet_data(struct wacom
*wacom
)
1518 schedule_delayed_work(&wacom
->init_work
, msecs_to_jiffies(1000));
1521 static enum power_supply_property wacom_battery_props
[] = {
1522 POWER_SUPPLY_PROP_MODEL_NAME
,
1523 POWER_SUPPLY_PROP_PRESENT
,
1524 POWER_SUPPLY_PROP_STATUS
,
1525 POWER_SUPPLY_PROP_SCOPE
,
1526 POWER_SUPPLY_PROP_CAPACITY
1529 static int wacom_battery_get_property(struct power_supply
*psy
,
1530 enum power_supply_property psp
,
1531 union power_supply_propval
*val
)
1533 struct wacom_battery
*battery
= power_supply_get_drvdata(psy
);
1537 case POWER_SUPPLY_PROP_MODEL_NAME
:
1538 val
->strval
= battery
->wacom
->wacom_wac
.name
;
1540 case POWER_SUPPLY_PROP_PRESENT
:
1541 val
->intval
= battery
->bat_connected
;
1543 case POWER_SUPPLY_PROP_SCOPE
:
1544 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
1546 case POWER_SUPPLY_PROP_CAPACITY
:
1547 val
->intval
= battery
->battery_capacity
;
1549 case POWER_SUPPLY_PROP_STATUS
:
1550 if (battery
->bat_status
!= WACOM_POWER_SUPPLY_STATUS_AUTO
)
1551 val
->intval
= battery
->bat_status
;
1552 else if (battery
->bat_charging
)
1553 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
1554 else if (battery
->battery_capacity
== 100 &&
1555 battery
->ps_connected
)
1556 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
1557 else if (battery
->ps_connected
)
1558 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1560 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
1570 static int __wacom_initialize_battery(struct wacom
*wacom
,
1571 struct wacom_battery
*battery
)
1573 static atomic_t battery_no
= ATOMIC_INIT(0);
1574 struct device
*dev
= &wacom
->hdev
->dev
;
1575 struct power_supply_config psy_cfg
= { .drv_data
= battery
, };
1576 struct power_supply
*ps_bat
;
1577 struct power_supply_desc
*bat_desc
= &battery
->bat_desc
;
1581 if (!devres_open_group(dev
, bat_desc
, GFP_KERNEL
))
1584 battery
->wacom
= wacom
;
1586 n
= atomic_inc_return(&battery_no
) - 1;
1588 bat_desc
->properties
= wacom_battery_props
;
1589 bat_desc
->num_properties
= ARRAY_SIZE(wacom_battery_props
);
1590 bat_desc
->get_property
= wacom_battery_get_property
;
1591 sprintf(battery
->bat_name
, "wacom_battery_%ld", n
);
1592 bat_desc
->name
= battery
->bat_name
;
1593 bat_desc
->type
= POWER_SUPPLY_TYPE_USB
;
1594 bat_desc
->use_for_apm
= 0;
1596 ps_bat
= devm_power_supply_register(dev
, bat_desc
, &psy_cfg
);
1597 if (IS_ERR(ps_bat
)) {
1598 error
= PTR_ERR(ps_bat
);
1602 power_supply_powers(ps_bat
, &wacom
->hdev
->dev
);
1604 battery
->battery
= ps_bat
;
1606 devres_close_group(dev
, bat_desc
);
1610 devres_release_group(dev
, bat_desc
);
1614 static int wacom_initialize_battery(struct wacom
*wacom
)
1616 if (wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
)
1617 return __wacom_initialize_battery(wacom
, &wacom
->battery
);
1622 static void wacom_destroy_battery(struct wacom
*wacom
)
1624 if (wacom
->battery
.battery
) {
1625 devres_release_group(&wacom
->hdev
->dev
,
1626 &wacom
->battery
.bat_desc
);
1627 wacom
->battery
.battery
= NULL
;
1631 static ssize_t
wacom_show_speed(struct device
*dev
,
1632 struct device_attribute
1635 struct hid_device
*hdev
= to_hid_device(dev
);
1636 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1638 return snprintf(buf
, PAGE_SIZE
, "%i\n", wacom
->wacom_wac
.bt_high_speed
);
1641 static ssize_t
wacom_store_speed(struct device
*dev
,
1642 struct device_attribute
*attr
,
1643 const char *buf
, size_t count
)
1645 struct hid_device
*hdev
= to_hid_device(dev
);
1646 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1649 if (kstrtou8(buf
, 0, &new_speed
))
1652 if (new_speed
!= 0 && new_speed
!= 1)
1655 wacom_bt_query_tablet_data(hdev
, new_speed
, &wacom
->wacom_wac
.features
);
1660 static DEVICE_ATTR(speed
, DEV_ATTR_RW_PERM
,
1661 wacom_show_speed
, wacom_store_speed
);
1664 static ssize_t
wacom_show_remote_mode(struct kobject
*kobj
,
1665 struct kobj_attribute
*kattr
,
1666 char *buf
, int index
)
1668 struct device
*dev
= kobj_to_dev(kobj
->parent
);
1669 struct hid_device
*hdev
= to_hid_device(dev
);
1670 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1673 mode
= wacom
->led
.groups
[index
].select
;
1674 if (mode
>= 0 && mode
< 3)
1675 return snprintf(buf
, PAGE_SIZE
, "%d\n", mode
);
1677 return snprintf(buf
, PAGE_SIZE
, "%d\n", -1);
1680 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1681 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1682 struct kobj_attribute *kattr, char *buf) \
1684 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1686 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1687 .attr = {.name = "remote_mode", \
1688 .mode = DEV_ATTR_RO_PERM}, \
1689 .show = wacom_show_remote##SET_ID##_mode, \
1691 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1692 &remote##SET_ID##_mode_attr.attr, \
1695 static struct attribute_group remote##SET_ID##_serial_group = { \
1697 .attrs = remote##SET_ID##_serial_attrs, \
1700 DEVICE_EKR_ATTR_GROUP(0);
1701 DEVICE_EKR_ATTR_GROUP(1);
1702 DEVICE_EKR_ATTR_GROUP(2);
1703 DEVICE_EKR_ATTR_GROUP(3);
1704 DEVICE_EKR_ATTR_GROUP(4);
1706 static int wacom_remote_create_attr_group(struct wacom
*wacom
, __u32 serial
,
1710 struct wacom_remote
*remote
= wacom
->remote
;
1712 remote
->remotes
[index
].group
.name
= devm_kasprintf(&wacom
->hdev
->dev
,
1715 if (!remote
->remotes
[index
].group
.name
)
1718 error
= __wacom_devm_sysfs_create_group(wacom
, remote
->remote_dir
,
1719 &remote
->remotes
[index
].group
);
1721 remote
->remotes
[index
].group
.name
= NULL
;
1722 hid_err(wacom
->hdev
,
1723 "cannot create sysfs group err: %d\n", error
);
1730 static int wacom_cmd_unpair_remote(struct wacom
*wacom
, unsigned char selector
)
1732 const size_t buf_size
= 2;
1736 buf
= kzalloc(buf_size
, GFP_KERNEL
);
1740 buf
[0] = WAC_CMD_DELETE_PAIRING
;
1743 retval
= wacom_set_report(wacom
->hdev
, HID_OUTPUT_REPORT
, buf
,
1744 buf_size
, WAC_CMD_RETRIES
);
1750 static ssize_t
wacom_store_unpair_remote(struct kobject
*kobj
,
1751 struct kobj_attribute
*attr
,
1752 const char *buf
, size_t count
)
1754 unsigned char selector
= 0;
1755 struct device
*dev
= kobj_to_dev(kobj
->parent
);
1756 struct hid_device
*hdev
= to_hid_device(dev
);
1757 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1760 if (!strncmp(buf
, "*\n", 2)) {
1761 selector
= WAC_CMD_UNPAIR_ALL
;
1763 hid_info(wacom
->hdev
, "remote: unrecognized unpair code: %s\n",
1768 mutex_lock(&wacom
->lock
);
1770 err
= wacom_cmd_unpair_remote(wacom
, selector
);
1771 mutex_unlock(&wacom
->lock
);
1773 return err
< 0 ? err
: count
;
1776 static struct kobj_attribute unpair_remote_attr
= {
1777 .attr
= {.name
= "unpair_remote", .mode
= 0200},
1778 .store
= wacom_store_unpair_remote
,
1781 static const struct attribute
*remote_unpair_attrs
[] = {
1782 &unpair_remote_attr
.attr
,
1786 static void wacom_remotes_destroy(void *data
)
1788 struct wacom
*wacom
= data
;
1789 struct wacom_remote
*remote
= wacom
->remote
;
1794 kobject_put(remote
->remote_dir
);
1795 kfifo_free(&remote
->remote_fifo
);
1796 wacom
->remote
= NULL
;
1799 static int wacom_initialize_remotes(struct wacom
*wacom
)
1802 struct wacom_remote
*remote
;
1805 if (wacom
->wacom_wac
.features
.type
!= REMOTE
)
1808 remote
= devm_kzalloc(&wacom
->hdev
->dev
, sizeof(*wacom
->remote
),
1813 wacom
->remote
= remote
;
1815 spin_lock_init(&remote
->remote_lock
);
1817 error
= kfifo_alloc(&remote
->remote_fifo
,
1818 5 * sizeof(struct wacom_remote_data
),
1821 hid_err(wacom
->hdev
, "failed allocating remote_fifo\n");
1825 remote
->remotes
[0].group
= remote0_serial_group
;
1826 remote
->remotes
[1].group
= remote1_serial_group
;
1827 remote
->remotes
[2].group
= remote2_serial_group
;
1828 remote
->remotes
[3].group
= remote3_serial_group
;
1829 remote
->remotes
[4].group
= remote4_serial_group
;
1831 remote
->remote_dir
= kobject_create_and_add("wacom_remote",
1832 &wacom
->hdev
->dev
.kobj
);
1833 if (!remote
->remote_dir
)
1836 error
= sysfs_create_files(remote
->remote_dir
, remote_unpair_attrs
);
1839 hid_err(wacom
->hdev
,
1840 "cannot create sysfs group err: %d\n", error
);
1844 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
1845 wacom
->led
.groups
[i
].select
= WACOM_STATUS_UNKNOWN
;
1846 remote
->remotes
[i
].serial
= 0;
1849 error
= devm_add_action_or_reset(&wacom
->hdev
->dev
,
1850 wacom_remotes_destroy
, wacom
);
1857 static struct input_dev
*wacom_allocate_input(struct wacom
*wacom
)
1859 struct input_dev
*input_dev
;
1860 struct hid_device
*hdev
= wacom
->hdev
;
1861 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1863 input_dev
= devm_input_allocate_device(&hdev
->dev
);
1867 input_dev
->name
= wacom_wac
->features
.name
;
1868 input_dev
->phys
= hdev
->phys
;
1869 input_dev
->dev
.parent
= &hdev
->dev
;
1870 input_dev
->open
= wacom_open
;
1871 input_dev
->close
= wacom_close
;
1872 input_dev
->uniq
= hdev
->uniq
;
1873 input_dev
->id
.bustype
= hdev
->bus
;
1874 input_dev
->id
.vendor
= hdev
->vendor
;
1875 input_dev
->id
.product
= wacom_wac
->pid
? wacom_wac
->pid
: hdev
->product
;
1876 input_dev
->id
.version
= hdev
->version
;
1877 input_set_drvdata(input_dev
, wacom
);
1882 static int wacom_allocate_inputs(struct wacom
*wacom
)
1884 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1886 wacom_wac
->pen_input
= wacom_allocate_input(wacom
);
1887 wacom_wac
->touch_input
= wacom_allocate_input(wacom
);
1888 wacom_wac
->pad_input
= wacom_allocate_input(wacom
);
1889 if (!wacom_wac
->pen_input
||
1890 !wacom_wac
->touch_input
||
1891 !wacom_wac
->pad_input
)
1894 wacom_wac
->pen_input
->name
= wacom_wac
->pen_name
;
1895 wacom_wac
->touch_input
->name
= wacom_wac
->touch_name
;
1896 wacom_wac
->pad_input
->name
= wacom_wac
->pad_name
;
1901 static int wacom_register_inputs(struct wacom
*wacom
)
1903 struct input_dev
*pen_input_dev
, *touch_input_dev
, *pad_input_dev
;
1904 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1907 pen_input_dev
= wacom_wac
->pen_input
;
1908 touch_input_dev
= wacom_wac
->touch_input
;
1909 pad_input_dev
= wacom_wac
->pad_input
;
1911 if (!pen_input_dev
|| !touch_input_dev
|| !pad_input_dev
)
1914 error
= wacom_setup_pen_input_capabilities(pen_input_dev
, wacom_wac
);
1916 /* no pen in use on this interface */
1917 input_free_device(pen_input_dev
);
1918 wacom_wac
->pen_input
= NULL
;
1919 pen_input_dev
= NULL
;
1921 error
= input_register_device(pen_input_dev
);
1926 error
= wacom_setup_touch_input_capabilities(touch_input_dev
, wacom_wac
);
1928 /* no touch in use on this interface */
1929 input_free_device(touch_input_dev
);
1930 wacom_wac
->touch_input
= NULL
;
1931 touch_input_dev
= NULL
;
1933 error
= input_register_device(touch_input_dev
);
1938 error
= wacom_setup_pad_input_capabilities(pad_input_dev
, wacom_wac
);
1940 /* no pad in use on this interface */
1941 input_free_device(pad_input_dev
);
1942 wacom_wac
->pad_input
= NULL
;
1943 pad_input_dev
= NULL
;
1945 error
= input_register_device(pad_input_dev
);
1953 wacom_wac
->pad_input
= NULL
;
1954 wacom_wac
->touch_input
= NULL
;
1955 wacom_wac
->pen_input
= NULL
;
1960 * Not all devices report physical dimensions from HID.
1961 * Compute the default from hardcoded logical dimension
1962 * and resolution before driver overwrites them.
1964 static void wacom_set_default_phy(struct wacom_features
*features
)
1966 if (features
->x_resolution
) {
1967 features
->x_phy
= (features
->x_max
* 100) /
1968 features
->x_resolution
;
1969 features
->y_phy
= (features
->y_max
* 100) /
1970 features
->y_resolution
;
1974 static void wacom_calculate_res(struct wacom_features
*features
)
1976 /* set unit to "100th of a mm" for devices not reported by HID */
1977 if (!features
->unit
) {
1978 features
->unit
= 0x11;
1979 features
->unitExpo
= -3;
1982 features
->x_resolution
= wacom_calc_hid_res(features
->x_max
,
1985 features
->unitExpo
);
1986 features
->y_resolution
= wacom_calc_hid_res(features
->y_max
,
1989 features
->unitExpo
);
1992 void wacom_battery_work(struct work_struct
*work
)
1994 struct wacom
*wacom
= container_of(work
, struct wacom
, battery_work
);
1996 if ((wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
) &&
1997 !wacom
->battery
.battery
) {
1998 wacom_initialize_battery(wacom
);
2000 else if (!(wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
) &&
2001 wacom
->battery
.battery
) {
2002 wacom_destroy_battery(wacom
);
2006 static size_t wacom_compute_pktlen(struct hid_device
*hdev
)
2008 struct hid_report_enum
*report_enum
;
2009 struct hid_report
*report
;
2012 report_enum
= hdev
->report_enum
+ HID_INPUT_REPORT
;
2014 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
2015 size_t report_size
= hid_report_len(report
);
2016 if (report_size
> size
)
2023 static void wacom_update_name(struct wacom
*wacom
, const char *suffix
)
2025 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2026 struct wacom_features
*features
= &wacom_wac
->features
;
2027 char name
[WACOM_NAME_MAX
];
2029 /* Generic devices name unspecified */
2030 if ((features
->type
== HID_GENERIC
) && !strcmp("Wacom HID", features
->name
)) {
2031 if (strstr(wacom
->hdev
->name
, "Wacom") ||
2032 strstr(wacom
->hdev
->name
, "wacom") ||
2033 strstr(wacom
->hdev
->name
, "WACOM")) {
2034 /* name is in HID descriptor, use it */
2035 strlcpy(name
, wacom
->hdev
->name
, sizeof(name
));
2037 /* strip out excess whitespaces */
2039 char *gap
= strstr(name
, " ");
2042 /* shift everything including the terminator */
2043 memmove(gap
, gap
+1, strlen(gap
));
2046 /* strip off excessive prefixing */
2047 if (strstr(name
, "Wacom Co.,Ltd. Wacom ") == name
) {
2048 int n
= strlen(name
);
2049 int x
= strlen("Wacom Co.,Ltd. ");
2050 memmove(name
, name
+x
, n
-x
+1);
2052 if (strstr(name
, "Wacom Co., Ltd. Wacom ") == name
) {
2053 int n
= strlen(name
);
2054 int x
= strlen("Wacom Co., Ltd. ");
2055 memmove(name
, name
+x
, n
-x
+1);
2058 /* get rid of trailing whitespace */
2059 if (name
[strlen(name
)-1] == ' ')
2060 name
[strlen(name
)-1] = '\0';
2062 /* no meaningful name retrieved. use product ID */
2063 snprintf(name
, sizeof(name
),
2064 "%s %X", features
->name
, wacom
->hdev
->product
);
2067 strlcpy(name
, features
->name
, sizeof(name
));
2070 snprintf(wacom_wac
->name
, sizeof(wacom_wac
->name
), "%s%s",
2073 /* Append the device type to the name */
2074 snprintf(wacom_wac
->pen_name
, sizeof(wacom_wac
->pen_name
),
2075 "%s%s Pen", name
, suffix
);
2076 snprintf(wacom_wac
->touch_name
, sizeof(wacom_wac
->touch_name
),
2077 "%s%s Finger", name
, suffix
);
2078 snprintf(wacom_wac
->pad_name
, sizeof(wacom_wac
->pad_name
),
2079 "%s%s Pad", name
, suffix
);
2082 static void wacom_release_resources(struct wacom
*wacom
)
2084 struct hid_device
*hdev
= wacom
->hdev
;
2086 if (!wacom
->resources
)
2089 devres_release_group(&hdev
->dev
, wacom
);
2091 wacom
->resources
= false;
2093 wacom
->wacom_wac
.pen_input
= NULL
;
2094 wacom
->wacom_wac
.touch_input
= NULL
;
2095 wacom
->wacom_wac
.pad_input
= NULL
;
2098 static void wacom_set_shared_values(struct wacom_wac
*wacom_wac
)
2100 if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_TOUCH
) {
2101 wacom_wac
->shared
->type
= wacom_wac
->features
.type
;
2102 wacom_wac
->shared
->touch_input
= wacom_wac
->touch_input
;
2105 if (wacom_wac
->has_mute_touch_switch
) {
2106 wacom_wac
->shared
->has_mute_touch_switch
= true;
2107 wacom_wac
->shared
->is_touch_on
= true;
2110 if (wacom_wac
->shared
->has_mute_touch_switch
&&
2111 wacom_wac
->shared
->touch_input
) {
2112 set_bit(EV_SW
, wacom_wac
->shared
->touch_input
->evbit
);
2113 input_set_capability(wacom_wac
->shared
->touch_input
, EV_SW
,
2118 static int wacom_parse_and_register(struct wacom
*wacom
, bool wireless
)
2120 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2121 struct wacom_features
*features
= &wacom_wac
->features
;
2122 struct hid_device
*hdev
= wacom
->hdev
;
2124 unsigned int connect_mask
= HID_CONNECT_HIDRAW
;
2126 features
->pktlen
= wacom_compute_pktlen(hdev
);
2127 if (features
->pktlen
> WACOM_PKGLEN_MAX
)
2130 if (!devres_open_group(&hdev
->dev
, wacom
, GFP_KERNEL
))
2133 wacom
->resources
= true;
2135 error
= wacom_allocate_inputs(wacom
);
2140 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2141 * into debug mode for the touch part.
2142 * We ignore the other interfaces.
2144 if (features
->type
== BAMBOO_PAD
) {
2145 if (features
->pktlen
== WACOM_PKGLEN_PENABLED
) {
2146 features
->type
= HID_GENERIC
;
2147 } else if ((features
->pktlen
!= WACOM_PKGLEN_BPAD_TOUCH
) &&
2148 (features
->pktlen
!= WACOM_PKGLEN_BPAD_TOUCH_USB
)) {
2154 /* set the default size in case we do not get them from hid */
2155 wacom_set_default_phy(features
);
2157 /* Retrieve the physical and logical size for touch devices */
2158 wacom_retrieve_hid_descriptor(hdev
, features
);
2159 wacom_setup_device_quirks(wacom
);
2161 if (features
->device_type
== WACOM_DEVICETYPE_NONE
&&
2162 features
->type
!= WIRELESS
) {
2163 error
= features
->type
== HID_GENERIC
? -ENODEV
: 0;
2165 dev_warn(&hdev
->dev
, "Unknown device_type for '%s'. %s.",
2167 error
? "Ignoring" : "Assuming pen");
2172 features
->device_type
|= WACOM_DEVICETYPE_PEN
;
2175 wacom_calculate_res(features
);
2177 wacom_update_name(wacom
, wireless
? " (WL)" : "");
2179 /* pen only Bamboo neither support touch nor pad */
2180 if ((features
->type
== BAMBOO_PEN
) &&
2181 ((features
->device_type
& WACOM_DEVICETYPE_TOUCH
) ||
2182 (features
->device_type
& WACOM_DEVICETYPE_PAD
))) {
2187 error
= wacom_add_shared_data(hdev
);
2191 if (!(features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
) &&
2192 (features
->quirks
& WACOM_QUIRK_BATTERY
)) {
2193 error
= wacom_initialize_battery(wacom
);
2198 error
= wacom_register_inputs(wacom
);
2202 if (wacom
->wacom_wac
.features
.device_type
& WACOM_DEVICETYPE_PAD
) {
2203 error
= wacom_initialize_leds(wacom
);
2207 error
= wacom_initialize_remotes(wacom
);
2212 if (features
->type
== HID_GENERIC
)
2213 connect_mask
|= HID_CONNECT_DRIVER
;
2215 /* Regular HID work starts now */
2216 error
= hid_hw_start(hdev
, connect_mask
);
2218 hid_err(hdev
, "hw start failed\n");
2223 /* Note that if query fails it is not a hard failure */
2224 wacom_query_tablet_data(wacom
);
2227 /* touch only Bamboo doesn't support pen */
2228 if ((features
->type
== BAMBOO_TOUCH
) &&
2229 (features
->device_type
& WACOM_DEVICETYPE_PEN
)) {
2230 cancel_delayed_work_sync(&wacom
->init_work
);
2231 _wacom_query_tablet_data(wacom
);
2236 if (features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
)
2237 error
= hid_hw_open(hdev
);
2239 wacom_set_shared_values(wacom_wac
);
2240 devres_close_group(&hdev
->dev
, wacom
);
2247 wacom_release_resources(wacom
);
2251 static void wacom_wireless_work(struct work_struct
*work
)
2253 struct wacom
*wacom
= container_of(work
, struct wacom
, wireless_work
);
2254 struct usb_device
*usbdev
= wacom
->usbdev
;
2255 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2256 struct hid_device
*hdev1
, *hdev2
;
2257 struct wacom
*wacom1
, *wacom2
;
2258 struct wacom_wac
*wacom_wac1
, *wacom_wac2
;
2262 * Regardless if this is a disconnect or a new tablet,
2263 * remove any existing input and battery devices.
2266 wacom_destroy_battery(wacom
);
2268 /* Stylus interface */
2269 hdev1
= usb_get_intfdata(usbdev
->config
->interface
[1]);
2270 wacom1
= hid_get_drvdata(hdev1
);
2271 wacom_wac1
= &(wacom1
->wacom_wac
);
2272 wacom_release_resources(wacom1
);
2274 /* Touch interface */
2275 hdev2
= usb_get_intfdata(usbdev
->config
->interface
[2]);
2276 wacom2
= hid_get_drvdata(hdev2
);
2277 wacom_wac2
= &(wacom2
->wacom_wac
);
2278 wacom_release_resources(wacom2
);
2280 if (wacom_wac
->pid
== 0) {
2281 hid_info(wacom
->hdev
, "wireless tablet disconnected\n");
2283 const struct hid_device_id
*id
= wacom_ids
;
2285 hid_info(wacom
->hdev
, "wireless tablet connected with PID %x\n",
2289 if (id
->vendor
== USB_VENDOR_ID_WACOM
&&
2290 id
->product
== wacom_wac
->pid
)
2296 hid_info(wacom
->hdev
, "ignoring unknown PID.\n");
2300 /* Stylus interface */
2301 wacom_wac1
->features
=
2302 *((struct wacom_features
*)id
->driver_data
);
2304 wacom_wac1
->pid
= wacom_wac
->pid
;
2306 error
= wacom_parse_and_register(wacom1
, true);
2310 /* Touch interface */
2311 if (wacom_wac1
->features
.touch_max
||
2312 (wacom_wac1
->features
.type
>= INTUOSHT
&&
2313 wacom_wac1
->features
.type
<= BAMBOO_PT
)) {
2314 wacom_wac2
->features
=
2315 *((struct wacom_features
*)id
->driver_data
);
2316 wacom_wac2
->pid
= wacom_wac
->pid
;
2318 error
= wacom_parse_and_register(wacom2
, true);
2323 strlcpy(wacom_wac
->name
, wacom_wac1
->name
,
2324 sizeof(wacom_wac
->name
));
2325 error
= wacom_initialize_battery(wacom
);
2333 wacom_release_resources(wacom1
);
2334 wacom_release_resources(wacom2
);
2338 static void wacom_remote_destroy_one(struct wacom
*wacom
, unsigned int index
)
2340 struct wacom_remote
*remote
= wacom
->remote
;
2341 u32 serial
= remote
->remotes
[index
].serial
;
2343 unsigned long flags
;
2345 spin_lock_irqsave(&remote
->remote_lock
, flags
);
2346 remote
->remotes
[index
].registered
= false;
2347 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2349 if (remote
->remotes
[index
].battery
.battery
)
2350 devres_release_group(&wacom
->hdev
->dev
,
2351 &remote
->remotes
[index
].battery
.bat_desc
);
2353 if (remote
->remotes
[index
].group
.name
)
2354 devres_release_group(&wacom
->hdev
->dev
,
2355 &remote
->remotes
[index
]);
2357 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
2358 if (remote
->remotes
[i
].serial
== serial
) {
2359 remote
->remotes
[i
].serial
= 0;
2360 remote
->remotes
[i
].group
.name
= NULL
;
2361 remote
->remotes
[i
].registered
= false;
2362 remote
->remotes
[i
].battery
.battery
= NULL
;
2363 wacom
->led
.groups
[i
].select
= WACOM_STATUS_UNKNOWN
;
2368 static int wacom_remote_create_one(struct wacom
*wacom
, u32 serial
,
2371 struct wacom_remote
*remote
= wacom
->remote
;
2372 struct device
*dev
= &wacom
->hdev
->dev
;
2375 /* A remote can pair more than once with an EKR,
2376 * check to make sure this serial isn't already paired.
2378 for (k
= 0; k
< WACOM_MAX_REMOTES
; k
++) {
2379 if (remote
->remotes
[k
].serial
== serial
)
2383 if (k
< WACOM_MAX_REMOTES
) {
2384 remote
->remotes
[index
].serial
= serial
;
2388 if (!devres_open_group(dev
, &remote
->remotes
[index
], GFP_KERNEL
))
2391 error
= wacom_remote_create_attr_group(wacom
, serial
, index
);
2395 remote
->remotes
[index
].input
= wacom_allocate_input(wacom
);
2396 if (!remote
->remotes
[index
].input
) {
2400 remote
->remotes
[index
].input
->uniq
= remote
->remotes
[index
].group
.name
;
2401 remote
->remotes
[index
].input
->name
= wacom
->wacom_wac
.pad_name
;
2403 if (!remote
->remotes
[index
].input
->name
) {
2408 error
= wacom_setup_pad_input_capabilities(remote
->remotes
[index
].input
,
2413 remote
->remotes
[index
].serial
= serial
;
2415 error
= input_register_device(remote
->remotes
[index
].input
);
2419 error
= wacom_led_groups_alloc_and_register_one(
2420 &remote
->remotes
[index
].input
->dev
,
2421 wacom
, index
, 3, true);
2425 remote
->remotes
[index
].registered
= true;
2427 devres_close_group(dev
, &remote
->remotes
[index
]);
2431 devres_release_group(dev
, &remote
->remotes
[index
]);
2432 remote
->remotes
[index
].serial
= 0;
2436 static int wacom_remote_attach_battery(struct wacom
*wacom
, int index
)
2438 struct wacom_remote
*remote
= wacom
->remote
;
2441 if (!remote
->remotes
[index
].registered
)
2444 if (remote
->remotes
[index
].battery
.battery
)
2447 if (wacom
->led
.groups
[index
].select
== WACOM_STATUS_UNKNOWN
)
2450 error
= __wacom_initialize_battery(wacom
,
2451 &wacom
->remote
->remotes
[index
].battery
);
2458 static void wacom_remote_work(struct work_struct
*work
)
2460 struct wacom
*wacom
= container_of(work
, struct wacom
, remote_work
);
2461 struct wacom_remote
*remote
= wacom
->remote
;
2462 struct wacom_remote_data data
;
2463 unsigned long flags
;
2468 spin_lock_irqsave(&remote
->remote_lock
, flags
);
2470 count
= kfifo_out(&remote
->remote_fifo
, &data
, sizeof(data
));
2472 if (count
!= sizeof(data
)) {
2473 hid_err(wacom
->hdev
,
2474 "workitem triggered without status available\n");
2475 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2479 if (!kfifo_is_empty(&remote
->remote_fifo
))
2480 wacom_schedule_work(&wacom
->wacom_wac
, WACOM_WORKER_REMOTE
);
2482 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2484 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
2485 serial
= data
.remote
[i
].serial
;
2486 if (data
.remote
[i
].connected
) {
2488 if (remote
->remotes
[i
].serial
== serial
) {
2489 wacom_remote_attach_battery(wacom
, i
);
2493 if (remote
->remotes
[i
].serial
)
2494 wacom_remote_destroy_one(wacom
, i
);
2496 wacom_remote_create_one(wacom
, serial
, i
);
2498 } else if (remote
->remotes
[i
].serial
) {
2499 wacom_remote_destroy_one(wacom
, i
);
2504 static void wacom_mode_change_work(struct work_struct
*work
)
2506 struct wacom
*wacom
= container_of(work
, struct wacom
, mode_change_work
);
2507 struct wacom_shared
*shared
= wacom
->wacom_wac
.shared
;
2508 struct wacom
*wacom1
= NULL
;
2509 struct wacom
*wacom2
= NULL
;
2510 bool is_direct
= wacom
->wacom_wac
.is_direct_mode
;
2514 wacom1
= hid_get_drvdata(shared
->pen
);
2515 wacom_release_resources(wacom1
);
2516 hid_hw_stop(wacom1
->hdev
);
2517 wacom1
->wacom_wac
.has_mode_change
= true;
2518 wacom1
->wacom_wac
.is_direct_mode
= is_direct
;
2521 if (shared
->touch
) {
2522 wacom2
= hid_get_drvdata(shared
->touch
);
2523 wacom_release_resources(wacom2
);
2524 hid_hw_stop(wacom2
->hdev
);
2525 wacom2
->wacom_wac
.has_mode_change
= true;
2526 wacom2
->wacom_wac
.is_direct_mode
= is_direct
;
2530 error
= wacom_parse_and_register(wacom1
, false);
2536 error
= wacom_parse_and_register(wacom2
, false);
2544 static int wacom_probe(struct hid_device
*hdev
,
2545 const struct hid_device_id
*id
)
2547 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
2548 struct usb_device
*dev
= interface_to_usbdev(intf
);
2549 struct wacom
*wacom
;
2550 struct wacom_wac
*wacom_wac
;
2551 struct wacom_features
*features
;
2554 if (!id
->driver_data
)
2557 hdev
->quirks
|= HID_QUIRK_NO_INIT_REPORTS
;
2559 /* hid-core sets this quirk for the boot interface */
2560 hdev
->quirks
&= ~HID_QUIRK_NOGET
;
2562 wacom
= devm_kzalloc(&hdev
->dev
, sizeof(struct wacom
), GFP_KERNEL
);
2566 hid_set_drvdata(hdev
, wacom
);
2569 wacom_wac
= &wacom
->wacom_wac
;
2570 wacom_wac
->features
= *((struct wacom_features
*)id
->driver_data
);
2571 features
= &wacom_wac
->features
;
2573 if (features
->check_for_hid_type
&& features
->hid_type
!= hdev
->type
) {
2578 wacom_wac
->hid_data
.inputmode
= -1;
2579 wacom_wac
->mode_report
= -1;
2581 wacom
->usbdev
= dev
;
2583 mutex_init(&wacom
->lock
);
2584 INIT_DELAYED_WORK(&wacom
->init_work
, wacom_init_work
);
2585 INIT_WORK(&wacom
->wireless_work
, wacom_wireless_work
);
2586 INIT_WORK(&wacom
->battery_work
, wacom_battery_work
);
2587 INIT_WORK(&wacom
->remote_work
, wacom_remote_work
);
2588 INIT_WORK(&wacom
->mode_change_work
, wacom_mode_change_work
);
2590 /* ask for the report descriptor to be loaded by HID */
2591 error
= hid_parse(hdev
);
2593 hid_err(hdev
, "parse failed\n");
2597 error
= wacom_parse_and_register(wacom
, false);
2601 if (hdev
->bus
== BUS_BLUETOOTH
) {
2602 error
= device_create_file(&hdev
->dev
, &dev_attr_speed
);
2605 "can't create sysfs speed attribute err: %d\n",
2612 hid_set_drvdata(hdev
, NULL
);
2616 static void wacom_remove(struct hid_device
*hdev
)
2618 struct wacom
*wacom
= hid_get_drvdata(hdev
);
2619 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2620 struct wacom_features
*features
= &wacom_wac
->features
;
2622 if (features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
)
2627 cancel_delayed_work_sync(&wacom
->init_work
);
2628 cancel_work_sync(&wacom
->wireless_work
);
2629 cancel_work_sync(&wacom
->battery_work
);
2630 cancel_work_sync(&wacom
->remote_work
);
2631 cancel_work_sync(&wacom
->mode_change_work
);
2632 if (hdev
->bus
== BUS_BLUETOOTH
)
2633 device_remove_file(&hdev
->dev
, &dev_attr_speed
);
2635 /* make sure we don't trigger the LEDs */
2636 wacom_led_groups_release(wacom
);
2638 if (wacom
->wacom_wac
.features
.type
!= REMOTE
)
2639 wacom_release_resources(wacom
);
2641 hid_set_drvdata(hdev
, NULL
);
2645 static int wacom_resume(struct hid_device
*hdev
)
2647 struct wacom
*wacom
= hid_get_drvdata(hdev
);
2649 mutex_lock(&wacom
->lock
);
2651 /* switch to wacom mode first */
2652 _wacom_query_tablet_data(wacom
);
2653 wacom_led_control(wacom
);
2655 mutex_unlock(&wacom
->lock
);
2660 static int wacom_reset_resume(struct hid_device
*hdev
)
2662 return wacom_resume(hdev
);
2664 #endif /* CONFIG_PM */
2666 static struct hid_driver wacom_driver
= {
2668 .id_table
= wacom_ids
,
2669 .probe
= wacom_probe
,
2670 .remove
= wacom_remove
,
2671 .report
= wacom_wac_report
,
2673 .resume
= wacom_resume
,
2674 .reset_resume
= wacom_reset_resume
,
2676 .raw_event
= wacom_raw_event
,
2678 module_hid_driver(wacom_driver
);
2680 MODULE_VERSION(DRIVER_VERSION
);
2681 MODULE_AUTHOR(DRIVER_AUTHOR
);
2682 MODULE_DESCRIPTION(DRIVER_DESC
);
2683 MODULE_LICENSE("GPL");