1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drivers/input/tablet/wacom_sys.c
5 * USB Wacom tablet support - system specific code
11 #include "wacom_wac.h"
13 #include <linux/input/mt.h>
15 #define WAC_MSG_RETRIES 5
16 #define WAC_CMD_RETRIES 10
18 #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
19 #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
20 #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
22 static int wacom_get_report(struct hid_device
*hdev
, u8 type
, u8
*buf
,
23 size_t size
, unsigned int retries
)
28 retval
= hid_hw_raw_request(hdev
, buf
[0], buf
, size
, type
,
30 } while ((retval
== -ETIMEDOUT
|| retval
== -EAGAIN
) && --retries
);
33 hid_err(hdev
, "wacom_get_report: ran out of retries "
34 "(last error = %d)\n", retval
);
39 static int wacom_set_report(struct hid_device
*hdev
, u8 type
, u8
*buf
,
40 size_t size
, unsigned int retries
)
45 retval
= hid_hw_raw_request(hdev
, buf
[0], buf
, size
, type
,
47 } while ((retval
== -ETIMEDOUT
|| retval
== -EAGAIN
) && --retries
);
50 hid_err(hdev
, "wacom_set_report: ran out of retries "
51 "(last error = %d)\n", retval
);
56 static void wacom_wac_queue_insert(struct hid_device
*hdev
,
57 struct kfifo_rec_ptr_2
*fifo
,
58 u8
*raw_data
, int size
)
62 while (kfifo_avail(fifo
) < size
) {
64 hid_warn(hdev
, "%s: kfifo has filled, starting to drop events\n", __func__
);
70 kfifo_in(fifo
, raw_data
, size
);
73 static void wacom_wac_queue_flush(struct hid_device
*hdev
,
74 struct kfifo_rec_ptr_2
*fifo
)
76 while (!kfifo_is_empty(fifo
)) {
77 u8 buf
[WACOM_PKGLEN_MAX
];
81 size
= kfifo_out(fifo
, buf
, sizeof(buf
));
82 err
= hid_report_raw_event(hdev
, HID_INPUT_REPORT
, buf
, size
, false);
84 hid_warn(hdev
, "%s: unable to flush event due to error %d\n",
90 static int wacom_wac_pen_serial_enforce(struct hid_device
*hdev
,
91 struct hid_report
*report
, u8
*raw_data
, int report_size
)
93 struct wacom
*wacom
= hid_get_drvdata(hdev
);
94 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
95 struct wacom_features
*features
= &wacom_wac
->features
;
100 if (wacom_wac
->serial
[0] || !(features
->quirks
& WACOM_QUIRK_TOOLSERIAL
))
103 /* Queue events which have invalid tool type or serial number */
104 for (i
= 0; i
< report
->maxfield
; i
++) {
105 for (j
= 0; j
< report
->field
[i
]->maxusage
; j
++) {
106 struct hid_field
*field
= report
->field
[i
];
107 struct hid_usage
*usage
= &field
->usage
[j
];
108 unsigned int equivalent_usage
= wacom_equivalent_usage(usage
->hid
);
113 if (equivalent_usage
!= HID_DG_INRANGE
&&
114 equivalent_usage
!= HID_DG_TOOLSERIALNUMBER
&&
115 equivalent_usage
!= WACOM_HID_WD_SERIALHI
&&
116 equivalent_usage
!= WACOM_HID_WD_TOOLTYPE
)
119 offset
= field
->report_offset
;
120 size
= field
->report_size
;
121 value
= hid_field_extract(hdev
, raw_data
+1, offset
+ j
* size
, size
);
123 /* If we go out of range, we need to flush the queue ASAP */
124 if (equivalent_usage
== HID_DG_INRANGE
)
129 switch (equivalent_usage
) {
130 case HID_DG_TOOLSERIALNUMBER
:
131 wacom_wac
->serial
[0] = value
;
134 case WACOM_HID_WD_SERIALHI
:
135 wacom_wac
->serial
[0] |= ((__u64
)value
) << 32;
138 case WACOM_HID_WD_TOOLTYPE
:
139 wacom_wac
->id
[0] = value
;
150 wacom_wac_queue_flush(hdev
, &wacom_wac
->pen_fifo
);
152 wacom_wac_queue_insert(hdev
, &wacom_wac
->pen_fifo
,
153 raw_data
, report_size
);
155 return insert
&& !flush
;
158 static int wacom_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
159 u8
*raw_data
, int size
)
161 struct wacom
*wacom
= hid_get_drvdata(hdev
);
163 if (size
> WACOM_PKGLEN_MAX
)
166 if (wacom_wac_pen_serial_enforce(hdev
, report
, raw_data
, size
))
169 memcpy(wacom
->wacom_wac
.data
, raw_data
, size
);
171 wacom_wac_irq(&wacom
->wacom_wac
, size
);
176 static int wacom_open(struct input_dev
*dev
)
178 struct wacom
*wacom
= input_get_drvdata(dev
);
180 return hid_hw_open(wacom
->hdev
);
183 static void wacom_close(struct input_dev
*dev
)
185 struct wacom
*wacom
= input_get_drvdata(dev
);
188 * wacom->hdev should never be null, but surprisingly, I had the case
189 * once while unplugging the Wacom Wireless Receiver.
192 hid_hw_close(wacom
->hdev
);
196 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
198 static int wacom_calc_hid_res(int logical_extents
, int physical_extents
,
199 unsigned unit
, int exponent
)
201 struct hid_field field
= {
202 .logical_maximum
= logical_extents
,
203 .physical_maximum
= physical_extents
,
205 .unit_exponent
= exponent
,
208 return hidinput_calc_abs_res(&field
, ABS_X
);
211 static void wacom_hid_usage_quirk(struct hid_device
*hdev
,
212 struct hid_field
*field
, struct hid_usage
*usage
)
214 struct wacom
*wacom
= hid_get_drvdata(hdev
);
215 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
216 unsigned int equivalent_usage
= wacom_equivalent_usage(usage
->hid
);
219 * The Dell Canvas 27 needs to be switched to its vendor-defined
220 * report to provide the best resolution.
222 if (hdev
->vendor
== USB_VENDOR_ID_WACOM
&&
223 hdev
->product
== 0x4200 &&
224 field
->application
== HID_UP_MSVENDOR
) {
225 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
226 wacom
->wacom_wac
.mode_value
= 2;
230 * ISDv4 devices which predate HID's adoption of the
231 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
232 * position instead. We can accurately detect if a
233 * usage with that value should be HID_DG_BARRELSWITCH2
234 * based on the surrounding usages, which have remained
235 * constant across generations.
237 if (features
->type
== HID_GENERIC
&&
238 usage
->hid
== 0x000D0000 &&
239 field
->application
== HID_DG_PEN
&&
240 field
->physical
== HID_DG_STYLUS
) {
241 int i
= usage
->usage_index
;
243 if (i
-4 >= 0 && i
+1 < field
->maxusage
&&
244 field
->usage
[i
-4].hid
== HID_DG_TIPSWITCH
&&
245 field
->usage
[i
-3].hid
== HID_DG_BARRELSWITCH
&&
246 field
->usage
[i
-2].hid
== HID_DG_ERASER
&&
247 field
->usage
[i
-1].hid
== HID_DG_INVERT
&&
248 field
->usage
[i
+1].hid
== HID_DG_INRANGE
) {
249 usage
->hid
= HID_DG_BARRELSWITCH2
;
254 * Wacom's AES devices use different vendor-defined usages to
255 * report serial number information compared to their branded
256 * hardware. The usages are also sometimes ill-defined and do
257 * not have the correct logical min/max values set. Lets patch
258 * the descriptor to use the branded usage convention and fix
261 if (usage
->hid
== WACOM_HID_WT_SERIALNUMBER
&&
262 field
->report_size
== 16 &&
263 field
->index
+ 2 < field
->report
->maxfield
) {
264 struct hid_field
*a
= field
->report
->field
[field
->index
+ 1];
265 struct hid_field
*b
= field
->report
->field
[field
->index
+ 2];
267 if (a
->maxusage
> 0 &&
268 a
->usage
[0].hid
== HID_DG_TOOLSERIALNUMBER
&&
269 a
->report_size
== 32 &&
271 b
->usage
[0].hid
== 0xFF000000 &&
272 b
->report_size
== 8) {
273 features
->quirks
|= WACOM_QUIRK_AESPEN
;
274 usage
->hid
= WACOM_HID_WD_TOOLTYPE
;
275 field
->logical_minimum
= S16_MIN
;
276 field
->logical_maximum
= S16_MAX
;
277 a
->logical_minimum
= S32_MIN
;
278 a
->logical_maximum
= S32_MAX
;
279 b
->usage
[0].hid
= WACOM_HID_WD_SERIALHI
;
280 b
->logical_minimum
= 0;
281 b
->logical_maximum
= U8_MAX
;
285 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
286 if (hdev
->vendor
== USB_VENDOR_ID_WACOM
&&
287 hdev
->product
== 0x0358 &&
288 WACOM_PEN_FIELD(field
) &&
289 equivalent_usage
== HID_GD_Y
) {
290 field
->logical_maximum
= 43200;
294 static void wacom_feature_mapping(struct hid_device
*hdev
,
295 struct hid_field
*field
, struct hid_usage
*usage
)
297 struct wacom
*wacom
= hid_get_drvdata(hdev
);
298 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
299 struct hid_data
*hid_data
= &wacom
->wacom_wac
.hid_data
;
300 unsigned int equivalent_usage
= wacom_equivalent_usage(usage
->hid
);
305 wacom_hid_usage_quirk(hdev
, field
, usage
);
307 switch (equivalent_usage
) {
308 case WACOM_HID_WD_TOUCH_RING_SETTING
:
309 wacom
->generic_has_leds
= true;
311 case HID_DG_CONTACTMAX
:
312 /* leave touch_max as is if predefined */
313 if (!features
->touch_max
) {
315 n
= hid_report_len(field
->report
);
316 data
= hid_alloc_report_buf(field
->report
, GFP_KERNEL
);
319 data
[0] = field
->report
->id
;
320 ret
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
321 data
, n
, WAC_CMD_RETRIES
);
323 ret
= hid_report_raw_event(hdev
,
324 HID_FEATURE_REPORT
, data
, n
, 0);
326 features
->touch_max
= 16;
327 hid_warn(hdev
, "wacom_feature_mapping: "
328 "could not get HID_DG_CONTACTMAX, "
329 "defaulting to %d\n",
330 features
->touch_max
);
335 case HID_DG_INPUTMODE
:
336 /* Ignore if value index is out of bounds. */
337 if (usage
->usage_index
>= field
->report_count
) {
338 dev_err(&hdev
->dev
, "HID_DG_INPUTMODE out of range\n");
342 hid_data
->inputmode
= field
->report
->id
;
343 hid_data
->inputmode_index
= usage
->usage_index
;
346 case HID_UP_DIGITIZER
:
347 if (field
->report
->id
== 0x0B &&
348 (field
->application
== WACOM_HID_G9_PEN
||
349 field
->application
== WACOM_HID_G11_PEN
)) {
350 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
351 wacom
->wacom_wac
.mode_value
= 0;
355 case WACOM_HID_WD_DATAMODE
:
356 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
357 wacom
->wacom_wac
.mode_value
= 2;
360 case WACOM_HID_UP_G9
:
361 case WACOM_HID_UP_G11
:
362 if (field
->report
->id
== 0x03 &&
363 (field
->application
== WACOM_HID_G9_TOUCHSCREEN
||
364 field
->application
== WACOM_HID_G11_TOUCHSCREEN
)) {
365 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
366 wacom
->wacom_wac
.mode_value
= 0;
369 case WACOM_HID_WD_OFFSETLEFT
:
370 case WACOM_HID_WD_OFFSETTOP
:
371 case WACOM_HID_WD_OFFSETRIGHT
:
372 case WACOM_HID_WD_OFFSETBOTTOM
:
374 n
= hid_report_len(field
->report
);
375 data
= hid_alloc_report_buf(field
->report
, GFP_KERNEL
);
378 data
[0] = field
->report
->id
;
379 ret
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
380 data
, n
, WAC_CMD_RETRIES
);
382 ret
= hid_report_raw_event(hdev
, HID_FEATURE_REPORT
,
385 hid_warn(hdev
, "%s: could not retrieve sensor offsets\n",
394 * Interface Descriptor of wacom devices can be incomplete and
395 * inconsistent so wacom_features table is used to store stylus
396 * device's packet lengths, various maximum values, and tablet
397 * resolution based on product ID's.
399 * For devices that contain 2 interfaces, wacom_features table is
400 * inaccurate for the touch interface. Since the Interface Descriptor
401 * for touch interfaces has pretty complete data, this function exists
402 * to query tablet for this missing information instead of hard coding in
403 * an additional table.
405 * A typical Interface Descriptor for a stylus will contain a
406 * boot mouse application collection that is not of interest and this
407 * function will ignore it.
409 * It also contains a digitizer application collection that also is not
410 * of interest since any information it contains would be duplicate
411 * of what is in wacom_features. Usually it defines a report of an array
412 * of bytes that could be used as max length of the stylus packet returned.
413 * If it happens to define a Digitizer-Stylus Physical Collection then
414 * the X and Y logical values contain valid data but it is ignored.
416 * A typical Interface Descriptor for a touch interface will contain a
417 * Digitizer-Finger Physical Collection which will define both logical
418 * X/Y maximum as well as the physical size of tablet. Since touch
419 * interfaces haven't supported pressure or distance, this is enough
420 * information to override invalid values in the wacom_features table.
422 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
423 * data. We deal with them after returning from this function.
425 static void wacom_usage_mapping(struct hid_device
*hdev
,
426 struct hid_field
*field
, struct hid_usage
*usage
)
428 struct wacom
*wacom
= hid_get_drvdata(hdev
);
429 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
430 bool finger
= WACOM_FINGER_FIELD(field
);
431 bool pen
= WACOM_PEN_FIELD(field
);
432 unsigned equivalent_usage
= wacom_equivalent_usage(usage
->hid
);
435 * Requiring Stylus Usage will ignore boot mouse
436 * X/Y values and some cases of invalid Digitizer X/Y
437 * values commonly reported.
440 features
->device_type
|= WACOM_DEVICETYPE_PEN
;
442 features
->device_type
|= WACOM_DEVICETYPE_TOUCH
;
446 wacom_hid_usage_quirk(hdev
, field
, usage
);
448 switch (equivalent_usage
) {
450 features
->x_max
= field
->logical_maximum
;
452 features
->x_phy
= field
->physical_maximum
;
453 if ((features
->type
!= BAMBOO_PT
) &&
454 (features
->type
!= BAMBOO_TOUCH
)) {
455 features
->unit
= field
->unit
;
456 features
->unitExpo
= field
->unit_exponent
;
461 features
->y_max
= field
->logical_maximum
;
463 features
->y_phy
= field
->physical_maximum
;
464 if ((features
->type
!= BAMBOO_PT
) &&
465 (features
->type
!= BAMBOO_TOUCH
)) {
466 features
->unit
= field
->unit
;
467 features
->unitExpo
= field
->unit_exponent
;
471 case HID_DG_TIPPRESSURE
:
473 features
->pressure_max
= field
->logical_maximum
;
477 if (features
->type
== HID_GENERIC
)
478 wacom_wac_usage_mapping(hdev
, field
, usage
);
481 static void wacom_post_parse_hid(struct hid_device
*hdev
,
482 struct wacom_features
*features
)
484 struct wacom
*wacom
= hid_get_drvdata(hdev
);
485 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
487 if (features
->type
== HID_GENERIC
) {
488 /* Any last-minute generic device setup */
489 if (wacom_wac
->has_mode_change
) {
490 if (wacom_wac
->is_direct_mode
)
491 features
->device_type
|= WACOM_DEVICETYPE_DIRECT
;
493 features
->device_type
&= ~WACOM_DEVICETYPE_DIRECT
;
496 if (features
->touch_max
> 1) {
497 if (features
->device_type
& WACOM_DEVICETYPE_DIRECT
)
498 input_mt_init_slots(wacom_wac
->touch_input
,
499 wacom_wac
->features
.touch_max
,
502 input_mt_init_slots(wacom_wac
->touch_input
,
503 wacom_wac
->features
.touch_max
,
509 static void wacom_parse_hid(struct hid_device
*hdev
,
510 struct wacom_features
*features
)
512 struct hid_report_enum
*rep_enum
;
513 struct hid_report
*hreport
;
516 /* check features first */
517 rep_enum
= &hdev
->report_enum
[HID_FEATURE_REPORT
];
518 list_for_each_entry(hreport
, &rep_enum
->report_list
, list
) {
519 for (i
= 0; i
< hreport
->maxfield
; i
++) {
520 /* Ignore if report count is out of bounds. */
521 if (hreport
->field
[i
]->report_count
< 1)
524 for (j
= 0; j
< hreport
->field
[i
]->maxusage
; j
++) {
525 wacom_feature_mapping(hdev
, hreport
->field
[i
],
526 hreport
->field
[i
]->usage
+ j
);
531 /* now check the input usages */
532 rep_enum
= &hdev
->report_enum
[HID_INPUT_REPORT
];
533 list_for_each_entry(hreport
, &rep_enum
->report_list
, list
) {
535 if (!hreport
->maxfield
)
538 for (i
= 0; i
< hreport
->maxfield
; i
++)
539 for (j
= 0; j
< hreport
->field
[i
]->maxusage
; j
++)
540 wacom_usage_mapping(hdev
, hreport
->field
[i
],
541 hreport
->field
[i
]->usage
+ j
);
544 wacom_post_parse_hid(hdev
, features
);
547 static int wacom_hid_set_device_mode(struct hid_device
*hdev
)
549 struct wacom
*wacom
= hid_get_drvdata(hdev
);
550 struct hid_data
*hid_data
= &wacom
->wacom_wac
.hid_data
;
551 struct hid_report
*r
;
552 struct hid_report_enum
*re
;
554 if (hid_data
->inputmode
< 0)
557 re
= &(hdev
->report_enum
[HID_FEATURE_REPORT
]);
558 r
= re
->report_id_hash
[hid_data
->inputmode
];
560 r
->field
[0]->value
[hid_data
->inputmode_index
] = 2;
561 hid_hw_request(hdev
, r
, HID_REQ_SET_REPORT
);
566 static int wacom_set_device_mode(struct hid_device
*hdev
,
567 struct wacom_wac
*wacom_wac
)
570 struct hid_report
*r
;
571 struct hid_report_enum
*re
;
573 int error
= -ENOMEM
, limit
= 0;
575 if (wacom_wac
->mode_report
< 0)
578 re
= &(hdev
->report_enum
[HID_FEATURE_REPORT
]);
579 r
= re
->report_id_hash
[wacom_wac
->mode_report
];
583 rep_data
= hid_alloc_report_buf(r
, GFP_KERNEL
);
587 length
= hid_report_len(r
);
590 rep_data
[0] = wacom_wac
->mode_report
;
591 rep_data
[1] = wacom_wac
->mode_value
;
593 error
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
,
596 error
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
597 rep_data
, length
, 1);
598 } while (error
>= 0 &&
599 rep_data
[1] != wacom_wac
->mode_report
&&
600 limit
++ < WAC_MSG_RETRIES
);
604 return error
< 0 ? error
: 0;
607 static int wacom_bt_query_tablet_data(struct hid_device
*hdev
, u8 speed
,
608 struct wacom_features
*features
)
610 struct wacom
*wacom
= hid_get_drvdata(hdev
);
614 switch (features
->type
) {
618 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
, 2,
622 rep_data
[0] = speed
== 0 ? 0x05 : 0x06;
625 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
,
629 wacom
->wacom_wac
.bt_high_speed
= speed
;
635 * Note that if the raw queries fail, it's not a hard failure
636 * and it is safe to continue
638 hid_warn(hdev
, "failed to poke device, command %d, err %d\n",
643 wacom
->wacom_wac
.bt_features
&= ~0x20;
645 wacom
->wacom_wac
.bt_features
|= 0x20;
648 rep_data
[1] = wacom
->wacom_wac
.bt_features
;
650 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
, 2,
653 wacom
->wacom_wac
.bt_high_speed
= speed
;
661 * Switch the tablet into its most-capable mode. Wacom tablets are
662 * typically configured to power-up in a mode which sends mouse-like
663 * reports to the OS. To get absolute position, pressure data, etc.
664 * from the tablet, it is necessary to switch the tablet out of this
665 * mode and into one which sends the full range of tablet data.
667 static int _wacom_query_tablet_data(struct wacom
*wacom
)
669 struct hid_device
*hdev
= wacom
->hdev
;
670 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
671 struct wacom_features
*features
= &wacom_wac
->features
;
673 if (hdev
->bus
== BUS_BLUETOOTH
)
674 return wacom_bt_query_tablet_data(hdev
, 1, features
);
676 if (features
->type
!= HID_GENERIC
) {
677 if (features
->device_type
& WACOM_DEVICETYPE_TOUCH
) {
678 if (features
->type
> TABLETPC
) {
679 /* MT Tablet PC touch */
680 wacom_wac
->mode_report
= 3;
681 wacom_wac
->mode_value
= 4;
682 } else if (features
->type
== WACOM_24HDT
) {
683 wacom_wac
->mode_report
= 18;
684 wacom_wac
->mode_value
= 2;
685 } else if (features
->type
== WACOM_27QHDT
) {
686 wacom_wac
->mode_report
= 131;
687 wacom_wac
->mode_value
= 2;
688 } else if (features
->type
== BAMBOO_PAD
) {
689 wacom_wac
->mode_report
= 2;
690 wacom_wac
->mode_value
= 2;
692 } else if (features
->device_type
& WACOM_DEVICETYPE_PEN
) {
693 if (features
->type
<= BAMBOO_PT
) {
694 wacom_wac
->mode_report
= 2;
695 wacom_wac
->mode_value
= 2;
700 wacom_set_device_mode(hdev
, wacom_wac
);
702 if (features
->type
== HID_GENERIC
)
703 return wacom_hid_set_device_mode(hdev
);
708 static void wacom_retrieve_hid_descriptor(struct hid_device
*hdev
,
709 struct wacom_features
*features
)
711 struct wacom
*wacom
= hid_get_drvdata(hdev
);
712 struct usb_interface
*intf
= wacom
->intf
;
714 /* default features */
715 features
->x_fuzz
= 4;
716 features
->y_fuzz
= 4;
717 features
->pressure_fuzz
= 0;
718 features
->distance_fuzz
= 1;
719 features
->tilt_fuzz
= 1;
722 * The wireless device HID is basic and layout conflicts with
723 * other tablets (monitor and touch interface can look like pen).
724 * Skip the query for this type and modify defaults based on
727 if (features
->type
== WIRELESS
) {
728 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 0)
729 features
->device_type
= WACOM_DEVICETYPE_WL_MONITOR
;
731 features
->device_type
= WACOM_DEVICETYPE_NONE
;
735 wacom_parse_hid(hdev
, features
);
738 struct wacom_hdev_data
{
739 struct list_head list
;
741 struct hid_device
*dev
;
742 struct wacom_shared shared
;
745 static LIST_HEAD(wacom_udev_list
);
746 static DEFINE_MUTEX(wacom_udev_list_lock
);
748 static bool wacom_are_sibling(struct hid_device
*hdev
,
749 struct hid_device
*sibling
)
751 struct wacom
*wacom
= hid_get_drvdata(hdev
);
752 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
753 struct wacom
*sibling_wacom
= hid_get_drvdata(sibling
);
754 struct wacom_features
*sibling_features
= &sibling_wacom
->wacom_wac
.features
;
755 __u32 oVid
= features
->oVid
? features
->oVid
: hdev
->vendor
;
756 __u32 oPid
= features
->oPid
? features
->oPid
: hdev
->product
;
758 /* The defined oVid/oPid must match that of the sibling */
759 if (features
->oVid
!= HID_ANY_ID
&& sibling
->vendor
!= oVid
)
761 if (features
->oPid
!= HID_ANY_ID
&& sibling
->product
!= oPid
)
765 * Devices with the same VID/PID must share the same physical
766 * device path, while those with different VID/PID must share
767 * the same physical parent device path.
769 if (hdev
->vendor
== sibling
->vendor
&& hdev
->product
== sibling
->product
) {
770 if (!hid_compare_device_paths(hdev
, sibling
, '/'))
773 if (!hid_compare_device_paths(hdev
, sibling
, '.'))
777 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
778 if (features
->type
!= HID_GENERIC
)
782 * Direct-input devices may not be siblings of indirect-input
785 if ((features
->device_type
& WACOM_DEVICETYPE_DIRECT
) &&
786 !(sibling_features
->device_type
& WACOM_DEVICETYPE_DIRECT
))
790 * Indirect-input devices may not be siblings of direct-input
793 if (!(features
->device_type
& WACOM_DEVICETYPE_DIRECT
) &&
794 (sibling_features
->device_type
& WACOM_DEVICETYPE_DIRECT
))
797 /* Pen devices may only be siblings of touch devices */
798 if ((features
->device_type
& WACOM_DEVICETYPE_PEN
) &&
799 !(sibling_features
->device_type
& WACOM_DEVICETYPE_TOUCH
))
802 /* Touch devices may only be siblings of pen devices */
803 if ((features
->device_type
& WACOM_DEVICETYPE_TOUCH
) &&
804 !(sibling_features
->device_type
& WACOM_DEVICETYPE_PEN
))
808 * No reason could be found for these two devices to NOT be
809 * siblings, so there's a good chance they ARE siblings
814 static struct wacom_hdev_data
*wacom_get_hdev_data(struct hid_device
*hdev
)
816 struct wacom_hdev_data
*data
;
818 /* Try to find an already-probed interface from the same device */
819 list_for_each_entry(data
, &wacom_udev_list
, list
) {
820 if (hid_compare_device_paths(hdev
, data
->dev
, '/')) {
821 kref_get(&data
->kref
);
826 /* Fallback to finding devices that appear to be "siblings" */
827 list_for_each_entry(data
, &wacom_udev_list
, list
) {
828 if (wacom_are_sibling(hdev
, data
->dev
)) {
829 kref_get(&data
->kref
);
837 static void wacom_release_shared_data(struct kref
*kref
)
839 struct wacom_hdev_data
*data
=
840 container_of(kref
, struct wacom_hdev_data
, kref
);
842 mutex_lock(&wacom_udev_list_lock
);
843 list_del(&data
->list
);
844 mutex_unlock(&wacom_udev_list_lock
);
849 static void wacom_remove_shared_data(void *res
)
851 struct wacom
*wacom
= res
;
852 struct wacom_hdev_data
*data
;
853 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
855 if (wacom_wac
->shared
) {
856 data
= container_of(wacom_wac
->shared
, struct wacom_hdev_data
,
859 if (wacom_wac
->shared
->touch
== wacom
->hdev
)
860 wacom_wac
->shared
->touch
= NULL
;
861 else if (wacom_wac
->shared
->pen
== wacom
->hdev
)
862 wacom_wac
->shared
->pen
= NULL
;
864 kref_put(&data
->kref
, wacom_release_shared_data
);
865 wacom_wac
->shared
= NULL
;
869 static int wacom_add_shared_data(struct hid_device
*hdev
)
871 struct wacom
*wacom
= hid_get_drvdata(hdev
);
872 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
873 struct wacom_hdev_data
*data
;
876 mutex_lock(&wacom_udev_list_lock
);
878 data
= wacom_get_hdev_data(hdev
);
880 data
= kzalloc(sizeof(struct wacom_hdev_data
), GFP_KERNEL
);
886 kref_init(&data
->kref
);
888 list_add_tail(&data
->list
, &wacom_udev_list
);
891 wacom_wac
->shared
= &data
->shared
;
893 retval
= devm_add_action(&hdev
->dev
, wacom_remove_shared_data
, wacom
);
895 mutex_unlock(&wacom_udev_list_lock
);
896 wacom_remove_shared_data(wacom
);
900 if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_TOUCH
)
901 wacom_wac
->shared
->touch
= hdev
;
902 else if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_PEN
)
903 wacom_wac
->shared
->pen
= hdev
;
906 mutex_unlock(&wacom_udev_list_lock
);
910 static int wacom_led_control(struct wacom
*wacom
)
914 unsigned char report_id
= WAC_CMD_LED_CONTROL
;
917 if (!wacom
->led
.groups
)
920 if (wacom
->wacom_wac
.features
.type
== REMOTE
)
923 if (wacom
->wacom_wac
.pid
) { /* wireless connected */
924 report_id
= WAC_CMD_WL_LED_CONTROL
;
927 else if (wacom
->wacom_wac
.features
.type
== INTUOSP2_BT
) {
928 report_id
= WAC_CMD_WL_INTUOSP2
;
931 buf
= kzalloc(buf_size
, GFP_KERNEL
);
935 if (wacom
->wacom_wac
.features
.type
== HID_GENERIC
) {
936 buf
[0] = WAC_CMD_LED_CONTROL_GENERIC
;
937 buf
[1] = wacom
->led
.llv
;
938 buf
[2] = wacom
->led
.groups
[0].select
& 0x03;
940 } else if ((wacom
->wacom_wac
.features
.type
>= INTUOS5S
&&
941 wacom
->wacom_wac
.features
.type
<= INTUOSPL
)) {
943 * Touch Ring and crop mark LED luminance may take on
944 * one of four values:
945 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
947 int ring_led
= wacom
->led
.groups
[0].select
& 0x03;
948 int ring_lum
= (((wacom
->led
.llv
& 0x60) >> 5) - 1) & 0x03;
950 unsigned char led_bits
= (crop_lum
<< 4) | (ring_lum
<< 2) | (ring_led
);
953 if (wacom
->wacom_wac
.pid
) {
954 wacom_get_report(wacom
->hdev
, HID_FEATURE_REPORT
,
955 buf
, buf_size
, WAC_CMD_RETRIES
);
961 else if (wacom
->wacom_wac
.features
.type
== INTUOSP2_BT
) {
963 buf
[4] = 100; // Power Connection LED (ORANGE)
964 buf
[5] = 100; // BT Connection LED (BLUE)
965 buf
[6] = 100; // Paper Mode (RED?)
966 buf
[7] = 100; // Paper Mode (GREEN?)
967 buf
[8] = 100; // Paper Mode (BLUE?)
968 buf
[9] = wacom
->led
.llv
;
969 buf
[10] = wacom
->led
.groups
[0].select
& 0x03;
972 int led
= wacom
->led
.groups
[0].select
| 0x4;
974 if (wacom
->wacom_wac
.features
.type
== WACOM_21UX2
||
975 wacom
->wacom_wac
.features
.type
== WACOM_24HD
)
976 led
|= (wacom
->led
.groups
[1].select
<< 4) | 0x40;
980 buf
[2] = wacom
->led
.llv
;
981 buf
[3] = wacom
->led
.hlv
;
982 buf
[4] = wacom
->led
.img_lum
;
985 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, buf_size
,
992 static int wacom_led_putimage(struct wacom
*wacom
, int button_id
, u8 xfer_id
,
993 const unsigned len
, const void *img
)
997 const unsigned chunk_len
= len
/ 4; /* 4 chunks are needed to be sent */
999 buf
= kzalloc(chunk_len
+ 3 , GFP_KERNEL
);
1003 /* Send 'start' command */
1004 buf
[0] = WAC_CMD_ICON_START
;
1006 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, 2,
1012 buf
[1] = button_id
& 0x07;
1013 for (i
= 0; i
< 4; i
++) {
1015 memcpy(buf
+ 3, img
+ i
* chunk_len
, chunk_len
);
1017 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
,
1018 buf
, chunk_len
+ 3, WAC_CMD_RETRIES
);
1024 buf
[0] = WAC_CMD_ICON_START
;
1026 wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, 2,
1034 static ssize_t
wacom_led_select_store(struct device
*dev
, int set_id
,
1035 const char *buf
, size_t count
)
1037 struct hid_device
*hdev
= to_hid_device(dev
);
1038 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1042 err
= kstrtouint(buf
, 10, &id
);
1046 mutex_lock(&wacom
->lock
);
1048 wacom
->led
.groups
[set_id
].select
= id
& 0x3;
1049 err
= wacom_led_control(wacom
);
1051 mutex_unlock(&wacom
->lock
);
1053 return err
< 0 ? err
: count
;
1056 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
1057 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
1058 struct device_attribute *attr, const char *buf, size_t count) \
1060 return wacom_led_select_store(dev, SET_ID, buf, count); \
1062 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
1063 struct device_attribute *attr, char *buf) \
1065 struct hid_device *hdev = to_hid_device(dev);\
1066 struct wacom *wacom = hid_get_drvdata(hdev); \
1067 return scnprintf(buf, PAGE_SIZE, "%d\n", \
1068 wacom->led.groups[SET_ID].select); \
1070 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
1071 wacom_led##SET_ID##_select_show, \
1072 wacom_led##SET_ID##_select_store)
1074 DEVICE_LED_SELECT_ATTR(0);
1075 DEVICE_LED_SELECT_ATTR(1);
1077 static ssize_t
wacom_luminance_store(struct wacom
*wacom
, u8
*dest
,
1078 const char *buf
, size_t count
)
1083 err
= kstrtouint(buf
, 10, &value
);
1087 mutex_lock(&wacom
->lock
);
1089 *dest
= value
& 0x7f;
1090 err
= wacom_led_control(wacom
);
1092 mutex_unlock(&wacom
->lock
);
1094 return err
< 0 ? err
: count
;
1097 #define DEVICE_LUMINANCE_ATTR(name, field) \
1098 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
1099 struct device_attribute *attr, const char *buf, size_t count) \
1101 struct hid_device *hdev = to_hid_device(dev);\
1102 struct wacom *wacom = hid_get_drvdata(hdev); \
1104 return wacom_luminance_store(wacom, &wacom->led.field, \
1107 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
1108 struct device_attribute *attr, char *buf) \
1110 struct wacom *wacom = dev_get_drvdata(dev); \
1111 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
1113 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
1114 wacom_##name##_luminance_show, \
1115 wacom_##name##_luminance_store)
1117 DEVICE_LUMINANCE_ATTR(status0
, llv
);
1118 DEVICE_LUMINANCE_ATTR(status1
, hlv
);
1119 DEVICE_LUMINANCE_ATTR(buttons
, img_lum
);
1121 static ssize_t
wacom_button_image_store(struct device
*dev
, int button_id
,
1122 const char *buf
, size_t count
)
1124 struct hid_device
*hdev
= to_hid_device(dev
);
1125 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1130 if (hdev
->bus
== BUS_BLUETOOTH
) {
1132 xfer_id
= WAC_CMD_ICON_BT_XFER
;
1135 xfer_id
= WAC_CMD_ICON_XFER
;
1141 mutex_lock(&wacom
->lock
);
1143 err
= wacom_led_putimage(wacom
, button_id
, xfer_id
, len
, buf
);
1145 mutex_unlock(&wacom
->lock
);
1147 return err
< 0 ? err
: count
;
1150 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1151 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1152 struct device_attribute *attr, const char *buf, size_t count) \
1154 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1156 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
1157 NULL, wacom_btnimg##BUTTON_ID##_store)
1159 DEVICE_BTNIMG_ATTR(0);
1160 DEVICE_BTNIMG_ATTR(1);
1161 DEVICE_BTNIMG_ATTR(2);
1162 DEVICE_BTNIMG_ATTR(3);
1163 DEVICE_BTNIMG_ATTR(4);
1164 DEVICE_BTNIMG_ATTR(5);
1165 DEVICE_BTNIMG_ATTR(6);
1166 DEVICE_BTNIMG_ATTR(7);
1168 static struct attribute
*cintiq_led_attrs
[] = {
1169 &dev_attr_status_led0_select
.attr
,
1170 &dev_attr_status_led1_select
.attr
,
1174 static struct attribute_group cintiq_led_attr_group
= {
1175 .name
= "wacom_led",
1176 .attrs
= cintiq_led_attrs
,
1179 static struct attribute
*intuos4_led_attrs
[] = {
1180 &dev_attr_status0_luminance
.attr
,
1181 &dev_attr_status1_luminance
.attr
,
1182 &dev_attr_status_led0_select
.attr
,
1183 &dev_attr_buttons_luminance
.attr
,
1184 &dev_attr_button0_rawimg
.attr
,
1185 &dev_attr_button1_rawimg
.attr
,
1186 &dev_attr_button2_rawimg
.attr
,
1187 &dev_attr_button3_rawimg
.attr
,
1188 &dev_attr_button4_rawimg
.attr
,
1189 &dev_attr_button5_rawimg
.attr
,
1190 &dev_attr_button6_rawimg
.attr
,
1191 &dev_attr_button7_rawimg
.attr
,
1195 static struct attribute_group intuos4_led_attr_group
= {
1196 .name
= "wacom_led",
1197 .attrs
= intuos4_led_attrs
,
1200 static struct attribute
*intuos5_led_attrs
[] = {
1201 &dev_attr_status0_luminance
.attr
,
1202 &dev_attr_status_led0_select
.attr
,
1206 static struct attribute_group intuos5_led_attr_group
= {
1207 .name
= "wacom_led",
1208 .attrs
= intuos5_led_attrs
,
1211 static struct attribute
*generic_led_attrs
[] = {
1212 &dev_attr_status0_luminance
.attr
,
1213 &dev_attr_status_led0_select
.attr
,
1217 static struct attribute_group generic_led_attr_group
= {
1218 .name
= "wacom_led",
1219 .attrs
= generic_led_attrs
,
1222 struct wacom_sysfs_group_devres
{
1223 struct attribute_group
*group
;
1224 struct kobject
*root
;
1227 static void wacom_devm_sysfs_group_release(struct device
*dev
, void *res
)
1229 struct wacom_sysfs_group_devres
*devres
= res
;
1230 struct kobject
*kobj
= devres
->root
;
1232 dev_dbg(dev
, "%s: dropping reference to %s\n",
1233 __func__
, devres
->group
->name
);
1234 sysfs_remove_group(kobj
, devres
->group
);
1237 static int __wacom_devm_sysfs_create_group(struct wacom
*wacom
,
1238 struct kobject
*root
,
1239 struct attribute_group
*group
)
1241 struct wacom_sysfs_group_devres
*devres
;
1244 devres
= devres_alloc(wacom_devm_sysfs_group_release
,
1245 sizeof(struct wacom_sysfs_group_devres
),
1250 devres
->group
= group
;
1251 devres
->root
= root
;
1253 error
= sysfs_create_group(devres
->root
, group
);
1255 devres_free(devres
);
1259 devres_add(&wacom
->hdev
->dev
, devres
);
1264 static int wacom_devm_sysfs_create_group(struct wacom
*wacom
,
1265 struct attribute_group
*group
)
1267 return __wacom_devm_sysfs_create_group(wacom
, &wacom
->hdev
->dev
.kobj
,
1271 enum led_brightness
wacom_leds_brightness_get(struct wacom_led
*led
)
1273 struct wacom
*wacom
= led
->wacom
;
1275 if (wacom
->led
.max_hlv
)
1276 return led
->hlv
* LED_FULL
/ wacom
->led
.max_hlv
;
1278 if (wacom
->led
.max_llv
)
1279 return led
->llv
* LED_FULL
/ wacom
->led
.max_llv
;
1281 /* device doesn't support brightness tuning */
1285 static enum led_brightness
__wacom_led_brightness_get(struct led_classdev
*cdev
)
1287 struct wacom_led
*led
= container_of(cdev
, struct wacom_led
, cdev
);
1288 struct wacom
*wacom
= led
->wacom
;
1290 if (wacom
->led
.groups
[led
->group
].select
!= led
->id
)
1293 return wacom_leds_brightness_get(led
);
1296 static int wacom_led_brightness_set(struct led_classdev
*cdev
,
1297 enum led_brightness brightness
)
1299 struct wacom_led
*led
= container_of(cdev
, struct wacom_led
, cdev
);
1300 struct wacom
*wacom
= led
->wacom
;
1303 mutex_lock(&wacom
->lock
);
1305 if (!wacom
->led
.groups
|| (brightness
== LED_OFF
&&
1306 wacom
->led
.groups
[led
->group
].select
!= led
->id
)) {
1311 led
->llv
= wacom
->led
.llv
= wacom
->led
.max_llv
* brightness
/ LED_FULL
;
1312 led
->hlv
= wacom
->led
.hlv
= wacom
->led
.max_hlv
* brightness
/ LED_FULL
;
1314 wacom
->led
.groups
[led
->group
].select
= led
->id
;
1316 error
= wacom_led_control(wacom
);
1319 mutex_unlock(&wacom
->lock
);
1324 static void wacom_led_readonly_brightness_set(struct led_classdev
*cdev
,
1325 enum led_brightness brightness
)
1329 static int wacom_led_register_one(struct device
*dev
, struct wacom
*wacom
,
1330 struct wacom_led
*led
, unsigned int group
,
1331 unsigned int id
, bool read_only
)
1336 name
= devm_kasprintf(dev
, GFP_KERNEL
,
1345 led
->trigger
.name
= name
;
1346 error
= devm_led_trigger_register(dev
, &led
->trigger
);
1348 hid_err(wacom
->hdev
,
1349 "failed to register LED trigger %s: %d\n",
1350 led
->cdev
.name
, error
);
1358 led
->llv
= wacom
->led
.llv
;
1359 led
->hlv
= wacom
->led
.hlv
;
1360 led
->cdev
.name
= name
;
1361 led
->cdev
.max_brightness
= LED_FULL
;
1362 led
->cdev
.flags
= LED_HW_PLUGGABLE
;
1363 led
->cdev
.brightness_get
= __wacom_led_brightness_get
;
1365 led
->cdev
.brightness_set_blocking
= wacom_led_brightness_set
;
1366 led
->cdev
.default_trigger
= led
->cdev
.name
;
1368 led
->cdev
.brightness_set
= wacom_led_readonly_brightness_set
;
1371 error
= devm_led_classdev_register(dev
, &led
->cdev
);
1373 hid_err(wacom
->hdev
,
1374 "failed to register LED %s: %d\n",
1375 led
->cdev
.name
, error
);
1376 led
->cdev
.name
= NULL
;
1383 static void wacom_led_groups_release_one(void *data
)
1385 struct wacom_group_leds
*group
= data
;
1387 devres_release_group(group
->dev
, group
);
1390 static int wacom_led_groups_alloc_and_register_one(struct device
*dev
,
1391 struct wacom
*wacom
,
1392 int group_id
, int count
,
1395 struct wacom_led
*leds
;
1398 if (group_id
>= wacom
->led
.count
|| count
<= 0)
1401 if (!devres_open_group(dev
, &wacom
->led
.groups
[group_id
], GFP_KERNEL
))
1404 leds
= devm_kcalloc(dev
, count
, sizeof(struct wacom_led
), GFP_KERNEL
);
1410 wacom
->led
.groups
[group_id
].leds
= leds
;
1411 wacom
->led
.groups
[group_id
].count
= count
;
1413 for (i
= 0; i
< count
; i
++) {
1414 error
= wacom_led_register_one(dev
, wacom
, &leds
[i
],
1415 group_id
, i
, read_only
);
1420 wacom
->led
.groups
[group_id
].dev
= dev
;
1422 devres_close_group(dev
, &wacom
->led
.groups
[group_id
]);
1425 * There is a bug (?) in devm_led_classdev_register() in which its
1426 * increments the refcount of the parent. If the parent is an input
1427 * device, that means the ref count never reaches 0 when
1428 * devm_input_device_release() gets called.
1429 * This means that the LEDs are still there after disconnect.
1430 * Manually force the release of the group so that the leds are released
1431 * once we are done using them.
1433 error
= devm_add_action_or_reset(&wacom
->hdev
->dev
,
1434 wacom_led_groups_release_one
,
1435 &wacom
->led
.groups
[group_id
]);
1442 devres_release_group(dev
, &wacom
->led
.groups
[group_id
]);
1446 struct wacom_led
*wacom_led_find(struct wacom
*wacom
, unsigned int group_id
,
1449 struct wacom_group_leds
*group
;
1451 if (group_id
>= wacom
->led
.count
)
1454 group
= &wacom
->led
.groups
[group_id
];
1461 return &group
->leds
[id
];
1465 * wacom_led_next: gives the next available led with a wacom trigger.
1467 * returns the next available struct wacom_led which has its default trigger
1468 * or the current one if none is available.
1470 struct wacom_led
*wacom_led_next(struct wacom
*wacom
, struct wacom_led
*cur
)
1472 struct wacom_led
*next_led
;
1482 next_led
= wacom_led_find(wacom
, group
, ++next
);
1483 if (!next_led
|| next_led
== cur
)
1485 } while (next_led
->cdev
.trigger
!= &next_led
->trigger
);
1490 static void wacom_led_groups_release(void *data
)
1492 struct wacom
*wacom
= data
;
1494 wacom
->led
.groups
= NULL
;
1495 wacom
->led
.count
= 0;
1498 static int wacom_led_groups_allocate(struct wacom
*wacom
, int count
)
1500 struct device
*dev
= &wacom
->hdev
->dev
;
1501 struct wacom_group_leds
*groups
;
1504 groups
= devm_kcalloc(dev
, count
, sizeof(struct wacom_group_leds
),
1509 error
= devm_add_action_or_reset(dev
, wacom_led_groups_release
, wacom
);
1513 wacom
->led
.groups
= groups
;
1514 wacom
->led
.count
= count
;
1519 static int wacom_leds_alloc_and_register(struct wacom
*wacom
, int group_count
,
1520 int led_per_group
, bool read_only
)
1525 if (!wacom
->wacom_wac
.pad_input
)
1528 dev
= &wacom
->wacom_wac
.pad_input
->dev
;
1530 error
= wacom_led_groups_allocate(wacom
, group_count
);
1534 for (i
= 0; i
< group_count
; i
++) {
1535 error
= wacom_led_groups_alloc_and_register_one(dev
, wacom
, i
,
1545 int wacom_initialize_leds(struct wacom
*wacom
)
1549 if (!(wacom
->wacom_wac
.features
.device_type
& WACOM_DEVICETYPE_PAD
))
1552 /* Initialize default values */
1553 switch (wacom
->wacom_wac
.features
.type
) {
1555 if (!wacom
->generic_has_leds
)
1557 wacom
->led
.llv
= 100;
1558 wacom
->led
.max_llv
= 100;
1560 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1562 hid_err(wacom
->hdev
,
1563 "cannot create leds err: %d\n", error
);
1567 error
= wacom_devm_sysfs_create_group(wacom
,
1568 &generic_led_attr_group
);
1575 wacom
->led
.llv
= 10;
1576 wacom
->led
.hlv
= 20;
1577 wacom
->led
.max_llv
= 127;
1578 wacom
->led
.max_hlv
= 127;
1579 wacom
->led
.img_lum
= 10;
1581 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1583 hid_err(wacom
->hdev
,
1584 "cannot create leds err: %d\n", error
);
1588 error
= wacom_devm_sysfs_create_group(wacom
,
1589 &intuos4_led_attr_group
);
1596 wacom
->led
.img_lum
= 0;
1598 error
= wacom_leds_alloc_and_register(wacom
, 2, 4, false);
1600 hid_err(wacom
->hdev
,
1601 "cannot create leds err: %d\n", error
);
1605 error
= wacom_devm_sysfs_create_group(wacom
,
1606 &cintiq_led_attr_group
);
1615 wacom
->led
.llv
= 32;
1616 wacom
->led
.max_llv
= 96;
1618 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1620 hid_err(wacom
->hdev
,
1621 "cannot create leds err: %d\n", error
);
1625 error
= wacom_devm_sysfs_create_group(wacom
,
1626 &intuos5_led_attr_group
);
1630 wacom
->led
.llv
= 50;
1631 wacom
->led
.max_llv
= 100;
1632 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1634 hid_err(wacom
->hdev
,
1635 "cannot create leds err: %d\n", error
);
1641 wacom
->led
.llv
= 255;
1642 wacom
->led
.max_llv
= 255;
1643 error
= wacom_led_groups_allocate(wacom
, 5);
1645 hid_err(wacom
->hdev
,
1646 "cannot create leds err: %d\n", error
);
1656 hid_err(wacom
->hdev
,
1657 "cannot create sysfs group err: %d\n", error
);
1664 static void wacom_init_work(struct work_struct
*work
)
1666 struct wacom
*wacom
= container_of(work
, struct wacom
, init_work
.work
);
1668 _wacom_query_tablet_data(wacom
);
1669 wacom_led_control(wacom
);
1672 static void wacom_query_tablet_data(struct wacom
*wacom
)
1674 schedule_delayed_work(&wacom
->init_work
, msecs_to_jiffies(1000));
1677 static enum power_supply_property wacom_battery_props
[] = {
1678 POWER_SUPPLY_PROP_MODEL_NAME
,
1679 POWER_SUPPLY_PROP_PRESENT
,
1680 POWER_SUPPLY_PROP_STATUS
,
1681 POWER_SUPPLY_PROP_SCOPE
,
1682 POWER_SUPPLY_PROP_CAPACITY
1685 static int wacom_battery_get_property(struct power_supply
*psy
,
1686 enum power_supply_property psp
,
1687 union power_supply_propval
*val
)
1689 struct wacom_battery
*battery
= power_supply_get_drvdata(psy
);
1693 case POWER_SUPPLY_PROP_MODEL_NAME
:
1694 val
->strval
= battery
->wacom
->wacom_wac
.name
;
1696 case POWER_SUPPLY_PROP_PRESENT
:
1697 val
->intval
= battery
->bat_connected
;
1699 case POWER_SUPPLY_PROP_SCOPE
:
1700 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
1702 case POWER_SUPPLY_PROP_CAPACITY
:
1703 val
->intval
= battery
->battery_capacity
;
1705 case POWER_SUPPLY_PROP_STATUS
:
1706 if (battery
->bat_status
!= WACOM_POWER_SUPPLY_STATUS_AUTO
)
1707 val
->intval
= battery
->bat_status
;
1708 else if (battery
->bat_charging
)
1709 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
1710 else if (battery
->battery_capacity
== 100 &&
1711 battery
->ps_connected
)
1712 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
1713 else if (battery
->ps_connected
)
1714 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1716 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
1726 static int __wacom_initialize_battery(struct wacom
*wacom
,
1727 struct wacom_battery
*battery
)
1729 static atomic_t battery_no
= ATOMIC_INIT(0);
1730 struct device
*dev
= &wacom
->hdev
->dev
;
1731 struct power_supply_config psy_cfg
= { .drv_data
= battery
, };
1732 struct power_supply
*ps_bat
;
1733 struct power_supply_desc
*bat_desc
= &battery
->bat_desc
;
1737 if (!devres_open_group(dev
, bat_desc
, GFP_KERNEL
))
1740 battery
->wacom
= wacom
;
1742 n
= atomic_inc_return(&battery_no
) - 1;
1744 bat_desc
->properties
= wacom_battery_props
;
1745 bat_desc
->num_properties
= ARRAY_SIZE(wacom_battery_props
);
1746 bat_desc
->get_property
= wacom_battery_get_property
;
1747 sprintf(battery
->bat_name
, "wacom_battery_%ld", n
);
1748 bat_desc
->name
= battery
->bat_name
;
1749 bat_desc
->type
= POWER_SUPPLY_TYPE_USB
;
1750 bat_desc
->use_for_apm
= 0;
1752 ps_bat
= devm_power_supply_register(dev
, bat_desc
, &psy_cfg
);
1753 if (IS_ERR(ps_bat
)) {
1754 error
= PTR_ERR(ps_bat
);
1758 power_supply_powers(ps_bat
, &wacom
->hdev
->dev
);
1760 battery
->battery
= ps_bat
;
1762 devres_close_group(dev
, bat_desc
);
1766 devres_release_group(dev
, bat_desc
);
1770 static int wacom_initialize_battery(struct wacom
*wacom
)
1772 if (wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
)
1773 return __wacom_initialize_battery(wacom
, &wacom
->battery
);
1778 static void wacom_destroy_battery(struct wacom
*wacom
)
1780 if (wacom
->battery
.battery
) {
1781 devres_release_group(&wacom
->hdev
->dev
,
1782 &wacom
->battery
.bat_desc
);
1783 wacom
->battery
.battery
= NULL
;
1787 static ssize_t
wacom_show_speed(struct device
*dev
,
1788 struct device_attribute
1791 struct hid_device
*hdev
= to_hid_device(dev
);
1792 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1794 return snprintf(buf
, PAGE_SIZE
, "%i\n", wacom
->wacom_wac
.bt_high_speed
);
1797 static ssize_t
wacom_store_speed(struct device
*dev
,
1798 struct device_attribute
*attr
,
1799 const char *buf
, size_t count
)
1801 struct hid_device
*hdev
= to_hid_device(dev
);
1802 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1805 if (kstrtou8(buf
, 0, &new_speed
))
1808 if (new_speed
!= 0 && new_speed
!= 1)
1811 wacom_bt_query_tablet_data(hdev
, new_speed
, &wacom
->wacom_wac
.features
);
1816 static DEVICE_ATTR(speed
, DEV_ATTR_RW_PERM
,
1817 wacom_show_speed
, wacom_store_speed
);
1820 static ssize_t
wacom_show_remote_mode(struct kobject
*kobj
,
1821 struct kobj_attribute
*kattr
,
1822 char *buf
, int index
)
1824 struct device
*dev
= kobj_to_dev(kobj
->parent
);
1825 struct hid_device
*hdev
= to_hid_device(dev
);
1826 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1829 mode
= wacom
->led
.groups
[index
].select
;
1830 return sprintf(buf
, "%d\n", mode
< 3 ? mode
: -1);
1833 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1834 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1835 struct kobj_attribute *kattr, char *buf) \
1837 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1839 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1840 .attr = {.name = "remote_mode", \
1841 .mode = DEV_ATTR_RO_PERM}, \
1842 .show = wacom_show_remote##SET_ID##_mode, \
1844 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1845 &remote##SET_ID##_mode_attr.attr, \
1848 static struct attribute_group remote##SET_ID##_serial_group = { \
1850 .attrs = remote##SET_ID##_serial_attrs, \
1853 DEVICE_EKR_ATTR_GROUP(0);
1854 DEVICE_EKR_ATTR_GROUP(1);
1855 DEVICE_EKR_ATTR_GROUP(2);
1856 DEVICE_EKR_ATTR_GROUP(3);
1857 DEVICE_EKR_ATTR_GROUP(4);
1859 static int wacom_remote_create_attr_group(struct wacom
*wacom
, __u32 serial
,
1863 struct wacom_remote
*remote
= wacom
->remote
;
1865 remote
->remotes
[index
].group
.name
= devm_kasprintf(&wacom
->hdev
->dev
,
1868 if (!remote
->remotes
[index
].group
.name
)
1871 error
= __wacom_devm_sysfs_create_group(wacom
, remote
->remote_dir
,
1872 &remote
->remotes
[index
].group
);
1874 remote
->remotes
[index
].group
.name
= NULL
;
1875 hid_err(wacom
->hdev
,
1876 "cannot create sysfs group err: %d\n", error
);
1883 static int wacom_cmd_unpair_remote(struct wacom
*wacom
, unsigned char selector
)
1885 const size_t buf_size
= 2;
1889 buf
= kzalloc(buf_size
, GFP_KERNEL
);
1893 buf
[0] = WAC_CMD_DELETE_PAIRING
;
1896 retval
= wacom_set_report(wacom
->hdev
, HID_OUTPUT_REPORT
, buf
,
1897 buf_size
, WAC_CMD_RETRIES
);
1903 static ssize_t
wacom_store_unpair_remote(struct kobject
*kobj
,
1904 struct kobj_attribute
*attr
,
1905 const char *buf
, size_t count
)
1907 unsigned char selector
= 0;
1908 struct device
*dev
= kobj_to_dev(kobj
->parent
);
1909 struct hid_device
*hdev
= to_hid_device(dev
);
1910 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1913 if (!strncmp(buf
, "*\n", 2)) {
1914 selector
= WAC_CMD_UNPAIR_ALL
;
1916 hid_info(wacom
->hdev
, "remote: unrecognized unpair code: %s\n",
1921 mutex_lock(&wacom
->lock
);
1923 err
= wacom_cmd_unpair_remote(wacom
, selector
);
1924 mutex_unlock(&wacom
->lock
);
1926 return err
< 0 ? err
: count
;
1929 static struct kobj_attribute unpair_remote_attr
= {
1930 .attr
= {.name
= "unpair_remote", .mode
= 0200},
1931 .store
= wacom_store_unpair_remote
,
1934 static const struct attribute
*remote_unpair_attrs
[] = {
1935 &unpair_remote_attr
.attr
,
1939 static void wacom_remotes_destroy(void *data
)
1941 struct wacom
*wacom
= data
;
1942 struct wacom_remote
*remote
= wacom
->remote
;
1947 kobject_put(remote
->remote_dir
);
1948 kfifo_free(&remote
->remote_fifo
);
1949 wacom
->remote
= NULL
;
1952 static int wacom_initialize_remotes(struct wacom
*wacom
)
1955 struct wacom_remote
*remote
;
1958 if (wacom
->wacom_wac
.features
.type
!= REMOTE
)
1961 remote
= devm_kzalloc(&wacom
->hdev
->dev
, sizeof(*wacom
->remote
),
1966 wacom
->remote
= remote
;
1968 spin_lock_init(&remote
->remote_lock
);
1970 error
= kfifo_alloc(&remote
->remote_fifo
,
1971 5 * sizeof(struct wacom_remote_data
),
1974 hid_err(wacom
->hdev
, "failed allocating remote_fifo\n");
1978 remote
->remotes
[0].group
= remote0_serial_group
;
1979 remote
->remotes
[1].group
= remote1_serial_group
;
1980 remote
->remotes
[2].group
= remote2_serial_group
;
1981 remote
->remotes
[3].group
= remote3_serial_group
;
1982 remote
->remotes
[4].group
= remote4_serial_group
;
1984 remote
->remote_dir
= kobject_create_and_add("wacom_remote",
1985 &wacom
->hdev
->dev
.kobj
);
1986 if (!remote
->remote_dir
)
1989 error
= sysfs_create_files(remote
->remote_dir
, remote_unpair_attrs
);
1992 hid_err(wacom
->hdev
,
1993 "cannot create sysfs group err: %d\n", error
);
1997 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
1998 wacom
->led
.groups
[i
].select
= WACOM_STATUS_UNKNOWN
;
1999 remote
->remotes
[i
].serial
= 0;
2002 error
= devm_add_action_or_reset(&wacom
->hdev
->dev
,
2003 wacom_remotes_destroy
, wacom
);
2010 static struct input_dev
*wacom_allocate_input(struct wacom
*wacom
)
2012 struct input_dev
*input_dev
;
2013 struct hid_device
*hdev
= wacom
->hdev
;
2014 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
2016 input_dev
= devm_input_allocate_device(&hdev
->dev
);
2020 input_dev
->name
= wacom_wac
->features
.name
;
2021 input_dev
->phys
= hdev
->phys
;
2022 input_dev
->dev
.parent
= &hdev
->dev
;
2023 input_dev
->open
= wacom_open
;
2024 input_dev
->close
= wacom_close
;
2025 input_dev
->uniq
= hdev
->uniq
;
2026 input_dev
->id
.bustype
= hdev
->bus
;
2027 input_dev
->id
.vendor
= hdev
->vendor
;
2028 input_dev
->id
.product
= wacom_wac
->pid
? wacom_wac
->pid
: hdev
->product
;
2029 input_dev
->id
.version
= hdev
->version
;
2030 input_set_drvdata(input_dev
, wacom
);
2035 static int wacom_allocate_inputs(struct wacom
*wacom
)
2037 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
2039 wacom_wac
->pen_input
= wacom_allocate_input(wacom
);
2040 wacom_wac
->touch_input
= wacom_allocate_input(wacom
);
2041 wacom_wac
->pad_input
= wacom_allocate_input(wacom
);
2042 if (!wacom_wac
->pen_input
||
2043 !wacom_wac
->touch_input
||
2044 !wacom_wac
->pad_input
)
2047 wacom_wac
->pen_input
->name
= wacom_wac
->pen_name
;
2048 wacom_wac
->touch_input
->name
= wacom_wac
->touch_name
;
2049 wacom_wac
->pad_input
->name
= wacom_wac
->pad_name
;
2054 static int wacom_register_inputs(struct wacom
*wacom
)
2056 struct input_dev
*pen_input_dev
, *touch_input_dev
, *pad_input_dev
;
2057 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
2060 pen_input_dev
= wacom_wac
->pen_input
;
2061 touch_input_dev
= wacom_wac
->touch_input
;
2062 pad_input_dev
= wacom_wac
->pad_input
;
2064 if (!pen_input_dev
|| !touch_input_dev
|| !pad_input_dev
)
2067 error
= wacom_setup_pen_input_capabilities(pen_input_dev
, wacom_wac
);
2069 /* no pen in use on this interface */
2070 input_free_device(pen_input_dev
);
2071 wacom_wac
->pen_input
= NULL
;
2072 pen_input_dev
= NULL
;
2074 error
= input_register_device(pen_input_dev
);
2079 error
= wacom_setup_touch_input_capabilities(touch_input_dev
, wacom_wac
);
2081 /* no touch in use on this interface */
2082 input_free_device(touch_input_dev
);
2083 wacom_wac
->touch_input
= NULL
;
2084 touch_input_dev
= NULL
;
2086 error
= input_register_device(touch_input_dev
);
2091 error
= wacom_setup_pad_input_capabilities(pad_input_dev
, wacom_wac
);
2093 /* no pad in use on this interface */
2094 input_free_device(pad_input_dev
);
2095 wacom_wac
->pad_input
= NULL
;
2096 pad_input_dev
= NULL
;
2098 error
= input_register_device(pad_input_dev
);
2106 wacom_wac
->pad_input
= NULL
;
2107 wacom_wac
->touch_input
= NULL
;
2108 wacom_wac
->pen_input
= NULL
;
2113 * Not all devices report physical dimensions from HID.
2114 * Compute the default from hardcoded logical dimension
2115 * and resolution before driver overwrites them.
2117 static void wacom_set_default_phy(struct wacom_features
*features
)
2119 if (features
->x_resolution
) {
2120 features
->x_phy
= (features
->x_max
* 100) /
2121 features
->x_resolution
;
2122 features
->y_phy
= (features
->y_max
* 100) /
2123 features
->y_resolution
;
2127 static void wacom_calculate_res(struct wacom_features
*features
)
2129 /* set unit to "100th of a mm" for devices not reported by HID */
2130 if (!features
->unit
) {
2131 features
->unit
= 0x11;
2132 features
->unitExpo
= -3;
2135 features
->x_resolution
= wacom_calc_hid_res(features
->x_max
,
2138 features
->unitExpo
);
2139 features
->y_resolution
= wacom_calc_hid_res(features
->y_max
,
2142 features
->unitExpo
);
2145 void wacom_battery_work(struct work_struct
*work
)
2147 struct wacom
*wacom
= container_of(work
, struct wacom
, battery_work
);
2149 if ((wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
) &&
2150 !wacom
->battery
.battery
) {
2151 wacom_initialize_battery(wacom
);
2153 else if (!(wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
) &&
2154 wacom
->battery
.battery
) {
2155 wacom_destroy_battery(wacom
);
2159 static size_t wacom_compute_pktlen(struct hid_device
*hdev
)
2161 struct hid_report_enum
*report_enum
;
2162 struct hid_report
*report
;
2165 report_enum
= hdev
->report_enum
+ HID_INPUT_REPORT
;
2167 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
2168 size_t report_size
= hid_report_len(report
);
2169 if (report_size
> size
)
2176 static void wacom_update_name(struct wacom
*wacom
, const char *suffix
)
2178 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2179 struct wacom_features
*features
= &wacom_wac
->features
;
2180 char name
[WACOM_NAME_MAX
- 20]; /* Leave some room for suffixes */
2182 /* Generic devices name unspecified */
2183 if ((features
->type
== HID_GENERIC
) && !strcmp("Wacom HID", features
->name
)) {
2184 char *product_name
= wacom
->hdev
->name
;
2186 if (hid_is_using_ll_driver(wacom
->hdev
, &usb_hid_driver
)) {
2187 struct usb_interface
*intf
= to_usb_interface(wacom
->hdev
->dev
.parent
);
2188 struct usb_device
*dev
= interface_to_usbdev(intf
);
2189 product_name
= dev
->product
;
2192 if (wacom
->hdev
->bus
== BUS_I2C
) {
2193 snprintf(name
, sizeof(name
), "%s %X",
2194 features
->name
, wacom
->hdev
->product
);
2195 } else if (strstr(product_name
, "Wacom") ||
2196 strstr(product_name
, "wacom") ||
2197 strstr(product_name
, "WACOM")) {
2198 strlcpy(name
, product_name
, sizeof(name
));
2200 snprintf(name
, sizeof(name
), "Wacom %s", product_name
);
2203 /* strip out excess whitespaces */
2205 char *gap
= strstr(name
, " ");
2208 /* shift everything including the terminator */
2209 memmove(gap
, gap
+1, strlen(gap
));
2212 /* get rid of trailing whitespace */
2213 if (name
[strlen(name
)-1] == ' ')
2214 name
[strlen(name
)-1] = '\0';
2216 strlcpy(name
, features
->name
, sizeof(name
));
2219 snprintf(wacom_wac
->name
, sizeof(wacom_wac
->name
), "%s%s",
2222 /* Append the device type to the name */
2223 snprintf(wacom_wac
->pen_name
, sizeof(wacom_wac
->pen_name
),
2224 "%s%s Pen", name
, suffix
);
2225 snprintf(wacom_wac
->touch_name
, sizeof(wacom_wac
->touch_name
),
2226 "%s%s Finger", name
, suffix
);
2227 snprintf(wacom_wac
->pad_name
, sizeof(wacom_wac
->pad_name
),
2228 "%s%s Pad", name
, suffix
);
2231 static void wacom_release_resources(struct wacom
*wacom
)
2233 struct hid_device
*hdev
= wacom
->hdev
;
2235 if (!wacom
->resources
)
2238 devres_release_group(&hdev
->dev
, wacom
);
2240 wacom
->resources
= false;
2242 wacom
->wacom_wac
.pen_input
= NULL
;
2243 wacom
->wacom_wac
.touch_input
= NULL
;
2244 wacom
->wacom_wac
.pad_input
= NULL
;
2247 static void wacom_set_shared_values(struct wacom_wac
*wacom_wac
)
2249 if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_TOUCH
) {
2250 wacom_wac
->shared
->type
= wacom_wac
->features
.type
;
2251 wacom_wac
->shared
->touch_input
= wacom_wac
->touch_input
;
2254 if (wacom_wac
->has_mute_touch_switch
) {
2255 wacom_wac
->shared
->has_mute_touch_switch
= true;
2256 wacom_wac
->shared
->is_touch_on
= true;
2259 if (wacom_wac
->shared
->has_mute_touch_switch
&&
2260 wacom_wac
->shared
->touch_input
) {
2261 set_bit(EV_SW
, wacom_wac
->shared
->touch_input
->evbit
);
2262 input_set_capability(wacom_wac
->shared
->touch_input
, EV_SW
,
2267 static int wacom_parse_and_register(struct wacom
*wacom
, bool wireless
)
2269 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2270 struct wacom_features
*features
= &wacom_wac
->features
;
2271 struct hid_device
*hdev
= wacom
->hdev
;
2273 unsigned int connect_mask
= HID_CONNECT_HIDRAW
;
2275 features
->pktlen
= wacom_compute_pktlen(hdev
);
2276 if (features
->pktlen
> WACOM_PKGLEN_MAX
)
2279 if (!devres_open_group(&hdev
->dev
, wacom
, GFP_KERNEL
))
2282 wacom
->resources
= true;
2284 error
= wacom_allocate_inputs(wacom
);
2289 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2290 * into debug mode for the touch part.
2291 * We ignore the other interfaces.
2293 if (features
->type
== BAMBOO_PAD
) {
2294 if (features
->pktlen
== WACOM_PKGLEN_PENABLED
) {
2295 features
->type
= HID_GENERIC
;
2296 } else if ((features
->pktlen
!= WACOM_PKGLEN_BPAD_TOUCH
) &&
2297 (features
->pktlen
!= WACOM_PKGLEN_BPAD_TOUCH_USB
)) {
2303 /* set the default size in case we do not get them from hid */
2304 wacom_set_default_phy(features
);
2306 /* Retrieve the physical and logical size for touch devices */
2307 wacom_retrieve_hid_descriptor(hdev
, features
);
2308 wacom_setup_device_quirks(wacom
);
2310 if (features
->device_type
== WACOM_DEVICETYPE_NONE
&&
2311 features
->type
!= WIRELESS
) {
2312 error
= features
->type
== HID_GENERIC
? -ENODEV
: 0;
2314 dev_warn(&hdev
->dev
, "Unknown device_type for '%s'. %s.",
2316 error
? "Ignoring" : "Assuming pen");
2321 features
->device_type
|= WACOM_DEVICETYPE_PEN
;
2324 wacom_calculate_res(features
);
2326 wacom_update_name(wacom
, wireless
? " (WL)" : "");
2328 /* pen only Bamboo neither support touch nor pad */
2329 if ((features
->type
== BAMBOO_PEN
) &&
2330 ((features
->device_type
& WACOM_DEVICETYPE_TOUCH
) ||
2331 (features
->device_type
& WACOM_DEVICETYPE_PAD
))) {
2336 error
= wacom_add_shared_data(hdev
);
2340 if (!(features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
) &&
2341 (features
->quirks
& WACOM_QUIRK_BATTERY
)) {
2342 error
= wacom_initialize_battery(wacom
);
2347 error
= wacom_register_inputs(wacom
);
2351 if (wacom
->wacom_wac
.features
.device_type
& WACOM_DEVICETYPE_PAD
) {
2352 error
= wacom_initialize_leds(wacom
);
2356 error
= wacom_initialize_remotes(wacom
);
2361 if (features
->type
== HID_GENERIC
)
2362 connect_mask
|= HID_CONNECT_DRIVER
;
2364 /* Regular HID work starts now */
2365 error
= hid_hw_start(hdev
, connect_mask
);
2367 hid_err(hdev
, "hw start failed\n");
2372 /* Note that if query fails it is not a hard failure */
2373 wacom_query_tablet_data(wacom
);
2376 /* touch only Bamboo doesn't support pen */
2377 if ((features
->type
== BAMBOO_TOUCH
) &&
2378 (features
->device_type
& WACOM_DEVICETYPE_PEN
)) {
2379 cancel_delayed_work_sync(&wacom
->init_work
);
2380 _wacom_query_tablet_data(wacom
);
2385 if (features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
)
2386 error
= hid_hw_open(hdev
);
2388 wacom_set_shared_values(wacom_wac
);
2389 devres_close_group(&hdev
->dev
, wacom
);
2396 wacom_release_resources(wacom
);
2400 static void wacom_wireless_work(struct work_struct
*work
)
2402 struct wacom
*wacom
= container_of(work
, struct wacom
, wireless_work
);
2403 struct usb_device
*usbdev
= wacom
->usbdev
;
2404 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2405 struct hid_device
*hdev1
, *hdev2
;
2406 struct wacom
*wacom1
, *wacom2
;
2407 struct wacom_wac
*wacom_wac1
, *wacom_wac2
;
2411 * Regardless if this is a disconnect or a new tablet,
2412 * remove any existing input and battery devices.
2415 wacom_destroy_battery(wacom
);
2417 /* Stylus interface */
2418 hdev1
= usb_get_intfdata(usbdev
->config
->interface
[1]);
2419 wacom1
= hid_get_drvdata(hdev1
);
2420 wacom_wac1
= &(wacom1
->wacom_wac
);
2421 wacom_release_resources(wacom1
);
2423 /* Touch interface */
2424 hdev2
= usb_get_intfdata(usbdev
->config
->interface
[2]);
2425 wacom2
= hid_get_drvdata(hdev2
);
2426 wacom_wac2
= &(wacom2
->wacom_wac
);
2427 wacom_release_resources(wacom2
);
2429 if (wacom_wac
->pid
== 0) {
2430 hid_info(wacom
->hdev
, "wireless tablet disconnected\n");
2432 const struct hid_device_id
*id
= wacom_ids
;
2434 hid_info(wacom
->hdev
, "wireless tablet connected with PID %x\n",
2438 if (id
->vendor
== USB_VENDOR_ID_WACOM
&&
2439 id
->product
== wacom_wac
->pid
)
2445 hid_info(wacom
->hdev
, "ignoring unknown PID.\n");
2449 /* Stylus interface */
2450 wacom_wac1
->features
=
2451 *((struct wacom_features
*)id
->driver_data
);
2453 wacom_wac1
->pid
= wacom_wac
->pid
;
2455 error
= wacom_parse_and_register(wacom1
, true);
2459 /* Touch interface */
2460 if (wacom_wac1
->features
.touch_max
||
2461 (wacom_wac1
->features
.type
>= INTUOSHT
&&
2462 wacom_wac1
->features
.type
<= BAMBOO_PT
)) {
2463 wacom_wac2
->features
=
2464 *((struct wacom_features
*)id
->driver_data
);
2465 wacom_wac2
->pid
= wacom_wac
->pid
;
2467 error
= wacom_parse_and_register(wacom2
, true);
2472 strlcpy(wacom_wac
->name
, wacom_wac1
->name
,
2473 sizeof(wacom_wac
->name
));
2474 error
= wacom_initialize_battery(wacom
);
2482 wacom_release_resources(wacom1
);
2483 wacom_release_resources(wacom2
);
2487 static void wacom_remote_destroy_one(struct wacom
*wacom
, unsigned int index
)
2489 struct wacom_remote
*remote
= wacom
->remote
;
2490 u32 serial
= remote
->remotes
[index
].serial
;
2492 unsigned long flags
;
2494 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
2495 if (remote
->remotes
[i
].serial
== serial
) {
2497 spin_lock_irqsave(&remote
->remote_lock
, flags
);
2498 remote
->remotes
[i
].registered
= false;
2499 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2501 if (remote
->remotes
[i
].battery
.battery
)
2502 devres_release_group(&wacom
->hdev
->dev
,
2503 &remote
->remotes
[i
].battery
.bat_desc
);
2505 if (remote
->remotes
[i
].group
.name
)
2506 devres_release_group(&wacom
->hdev
->dev
,
2507 &remote
->remotes
[i
]);
2509 remote
->remotes
[i
].serial
= 0;
2510 remote
->remotes
[i
].group
.name
= NULL
;
2511 remote
->remotes
[i
].battery
.battery
= NULL
;
2512 wacom
->led
.groups
[i
].select
= WACOM_STATUS_UNKNOWN
;
2517 static int wacom_remote_create_one(struct wacom
*wacom
, u32 serial
,
2520 struct wacom_remote
*remote
= wacom
->remote
;
2521 struct device
*dev
= &wacom
->hdev
->dev
;
2524 /* A remote can pair more than once with an EKR,
2525 * check to make sure this serial isn't already paired.
2527 for (k
= 0; k
< WACOM_MAX_REMOTES
; k
++) {
2528 if (remote
->remotes
[k
].serial
== serial
)
2532 if (k
< WACOM_MAX_REMOTES
) {
2533 remote
->remotes
[index
].serial
= serial
;
2537 if (!devres_open_group(dev
, &remote
->remotes
[index
], GFP_KERNEL
))
2540 error
= wacom_remote_create_attr_group(wacom
, serial
, index
);
2544 remote
->remotes
[index
].input
= wacom_allocate_input(wacom
);
2545 if (!remote
->remotes
[index
].input
) {
2549 remote
->remotes
[index
].input
->uniq
= remote
->remotes
[index
].group
.name
;
2550 remote
->remotes
[index
].input
->name
= wacom
->wacom_wac
.pad_name
;
2552 if (!remote
->remotes
[index
].input
->name
) {
2557 error
= wacom_setup_pad_input_capabilities(remote
->remotes
[index
].input
,
2562 remote
->remotes
[index
].serial
= serial
;
2564 error
= input_register_device(remote
->remotes
[index
].input
);
2568 error
= wacom_led_groups_alloc_and_register_one(
2569 &remote
->remotes
[index
].input
->dev
,
2570 wacom
, index
, 3, true);
2574 remote
->remotes
[index
].registered
= true;
2576 devres_close_group(dev
, &remote
->remotes
[index
]);
2580 devres_release_group(dev
, &remote
->remotes
[index
]);
2581 remote
->remotes
[index
].serial
= 0;
2585 static int wacom_remote_attach_battery(struct wacom
*wacom
, int index
)
2587 struct wacom_remote
*remote
= wacom
->remote
;
2590 if (!remote
->remotes
[index
].registered
)
2593 if (remote
->remotes
[index
].battery
.battery
)
2596 if (wacom
->led
.groups
[index
].select
== WACOM_STATUS_UNKNOWN
)
2599 error
= __wacom_initialize_battery(wacom
,
2600 &wacom
->remote
->remotes
[index
].battery
);
2607 static void wacom_remote_work(struct work_struct
*work
)
2609 struct wacom
*wacom
= container_of(work
, struct wacom
, remote_work
);
2610 struct wacom_remote
*remote
= wacom
->remote
;
2611 struct wacom_remote_data data
;
2612 unsigned long flags
;
2617 spin_lock_irqsave(&remote
->remote_lock
, flags
);
2619 count
= kfifo_out(&remote
->remote_fifo
, &data
, sizeof(data
));
2621 if (count
!= sizeof(data
)) {
2622 hid_err(wacom
->hdev
,
2623 "workitem triggered without status available\n");
2624 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2628 if (!kfifo_is_empty(&remote
->remote_fifo
))
2629 wacom_schedule_work(&wacom
->wacom_wac
, WACOM_WORKER_REMOTE
);
2631 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2633 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
2634 serial
= data
.remote
[i
].serial
;
2635 if (data
.remote
[i
].connected
) {
2637 if (remote
->remotes
[i
].serial
== serial
) {
2638 wacom_remote_attach_battery(wacom
, i
);
2642 if (remote
->remotes
[i
].serial
)
2643 wacom_remote_destroy_one(wacom
, i
);
2645 wacom_remote_create_one(wacom
, serial
, i
);
2647 } else if (remote
->remotes
[i
].serial
) {
2648 wacom_remote_destroy_one(wacom
, i
);
2653 static void wacom_mode_change_work(struct work_struct
*work
)
2655 struct wacom
*wacom
= container_of(work
, struct wacom
, mode_change_work
);
2656 struct wacom_shared
*shared
= wacom
->wacom_wac
.shared
;
2657 struct wacom
*wacom1
= NULL
;
2658 struct wacom
*wacom2
= NULL
;
2659 bool is_direct
= wacom
->wacom_wac
.is_direct_mode
;
2663 wacom1
= hid_get_drvdata(shared
->pen
);
2664 wacom_release_resources(wacom1
);
2665 hid_hw_stop(wacom1
->hdev
);
2666 wacom1
->wacom_wac
.has_mode_change
= true;
2667 wacom1
->wacom_wac
.is_direct_mode
= is_direct
;
2670 if (shared
->touch
) {
2671 wacom2
= hid_get_drvdata(shared
->touch
);
2672 wacom_release_resources(wacom2
);
2673 hid_hw_stop(wacom2
->hdev
);
2674 wacom2
->wacom_wac
.has_mode_change
= true;
2675 wacom2
->wacom_wac
.is_direct_mode
= is_direct
;
2679 error
= wacom_parse_and_register(wacom1
, false);
2685 error
= wacom_parse_and_register(wacom2
, false);
2693 static int wacom_probe(struct hid_device
*hdev
,
2694 const struct hid_device_id
*id
)
2696 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
2697 struct usb_device
*dev
= interface_to_usbdev(intf
);
2698 struct wacom
*wacom
;
2699 struct wacom_wac
*wacom_wac
;
2700 struct wacom_features
*features
;
2703 if (!id
->driver_data
)
2706 hdev
->quirks
|= HID_QUIRK_NO_INIT_REPORTS
;
2708 /* hid-core sets this quirk for the boot interface */
2709 hdev
->quirks
&= ~HID_QUIRK_NOGET
;
2711 wacom
= devm_kzalloc(&hdev
->dev
, sizeof(struct wacom
), GFP_KERNEL
);
2715 hid_set_drvdata(hdev
, wacom
);
2718 wacom_wac
= &wacom
->wacom_wac
;
2719 wacom_wac
->features
= *((struct wacom_features
*)id
->driver_data
);
2720 features
= &wacom_wac
->features
;
2722 if (features
->check_for_hid_type
&& features
->hid_type
!= hdev
->type
)
2725 error
= kfifo_alloc(&wacom_wac
->pen_fifo
, WACOM_PKGLEN_MAX
, GFP_KERNEL
);
2729 wacom_wac
->hid_data
.inputmode
= -1;
2730 wacom_wac
->mode_report
= -1;
2732 wacom
->usbdev
= dev
;
2734 mutex_init(&wacom
->lock
);
2735 INIT_DELAYED_WORK(&wacom
->init_work
, wacom_init_work
);
2736 INIT_WORK(&wacom
->wireless_work
, wacom_wireless_work
);
2737 INIT_WORK(&wacom
->battery_work
, wacom_battery_work
);
2738 INIT_WORK(&wacom
->remote_work
, wacom_remote_work
);
2739 INIT_WORK(&wacom
->mode_change_work
, wacom_mode_change_work
);
2741 /* ask for the report descriptor to be loaded by HID */
2742 error
= hid_parse(hdev
);
2744 hid_err(hdev
, "parse failed\n");
2748 error
= wacom_parse_and_register(wacom
, false);
2752 if (hdev
->bus
== BUS_BLUETOOTH
) {
2753 error
= device_create_file(&hdev
->dev
, &dev_attr_speed
);
2756 "can't create sysfs speed attribute err: %d\n",
2763 static void wacom_remove(struct hid_device
*hdev
)
2765 struct wacom
*wacom
= hid_get_drvdata(hdev
);
2766 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2767 struct wacom_features
*features
= &wacom_wac
->features
;
2769 if (features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
)
2774 cancel_delayed_work_sync(&wacom
->init_work
);
2775 cancel_work_sync(&wacom
->wireless_work
);
2776 cancel_work_sync(&wacom
->battery_work
);
2777 cancel_work_sync(&wacom
->remote_work
);
2778 cancel_work_sync(&wacom
->mode_change_work
);
2779 if (hdev
->bus
== BUS_BLUETOOTH
)
2780 device_remove_file(&hdev
->dev
, &dev_attr_speed
);
2782 /* make sure we don't trigger the LEDs */
2783 wacom_led_groups_release(wacom
);
2785 if (wacom
->wacom_wac
.features
.type
!= REMOTE
)
2786 wacom_release_resources(wacom
);
2788 kfifo_free(&wacom_wac
->pen_fifo
);
2792 static int wacom_resume(struct hid_device
*hdev
)
2794 struct wacom
*wacom
= hid_get_drvdata(hdev
);
2796 mutex_lock(&wacom
->lock
);
2798 /* switch to wacom mode first */
2799 _wacom_query_tablet_data(wacom
);
2800 wacom_led_control(wacom
);
2802 mutex_unlock(&wacom
->lock
);
2807 static int wacom_reset_resume(struct hid_device
*hdev
)
2809 return wacom_resume(hdev
);
2811 #endif /* CONFIG_PM */
2813 static struct hid_driver wacom_driver
= {
2815 .id_table
= wacom_ids
,
2816 .probe
= wacom_probe
,
2817 .remove
= wacom_remove
,
2818 .report
= wacom_wac_report
,
2820 .resume
= wacom_resume
,
2821 .reset_resume
= wacom_reset_resume
,
2823 .raw_event
= wacom_raw_event
,
2825 module_hid_driver(wacom_driver
);
2827 MODULE_VERSION(DRIVER_VERSION
);
2828 MODULE_AUTHOR(DRIVER_AUTHOR
);
2829 MODULE_DESCRIPTION(DRIVER_DESC
);
2830 MODULE_LICENSE("GPL");