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 void wacom_wac_queue_insert(struct hid_device
*hdev
,
60 struct kfifo_rec_ptr_2
*fifo
,
61 u8
*raw_data
, int size
)
65 while (kfifo_avail(fifo
) < size
) {
67 hid_warn(hdev
, "%s: kfifo has filled, starting to drop events\n", __func__
);
73 kfifo_in(fifo
, raw_data
, size
);
76 static void wacom_wac_queue_flush(struct hid_device
*hdev
,
77 struct kfifo_rec_ptr_2
*fifo
)
79 while (!kfifo_is_empty(fifo
)) {
80 u8 buf
[WACOM_PKGLEN_MAX
];
84 size
= kfifo_out(fifo
, buf
, sizeof(buf
));
85 err
= hid_report_raw_event(hdev
, HID_INPUT_REPORT
, buf
, size
, false);
87 hid_warn(hdev
, "%s: unable to flush event due to error %d\n",
93 static int wacom_wac_pen_serial_enforce(struct hid_device
*hdev
,
94 struct hid_report
*report
, u8
*raw_data
, int report_size
)
96 struct wacom
*wacom
= hid_get_drvdata(hdev
);
97 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
98 struct wacom_features
*features
= &wacom_wac
->features
;
103 if (wacom_wac
->serial
[0] || !(features
->quirks
& WACOM_QUIRK_TOOLSERIAL
))
106 /* Queue events which have invalid tool type or serial number */
107 for (i
= 0; i
< report
->maxfield
; i
++) {
108 for (j
= 0; j
< report
->field
[i
]->maxusage
; j
++) {
109 struct hid_field
*field
= report
->field
[i
];
110 struct hid_usage
*usage
= &field
->usage
[j
];
111 unsigned int equivalent_usage
= wacom_equivalent_usage(usage
->hid
);
116 if (equivalent_usage
!= HID_DG_INRANGE
&&
117 equivalent_usage
!= HID_DG_TOOLSERIALNUMBER
&&
118 equivalent_usage
!= WACOM_HID_WD_SERIALHI
&&
119 equivalent_usage
!= WACOM_HID_WD_TOOLTYPE
)
122 offset
= field
->report_offset
;
123 size
= field
->report_size
;
124 value
= hid_field_extract(hdev
, raw_data
+1, offset
+ j
* size
, size
);
126 /* If we go out of range, we need to flush the queue ASAP */
127 if (equivalent_usage
== HID_DG_INRANGE
)
132 switch (equivalent_usage
) {
133 case HID_DG_TOOLSERIALNUMBER
:
134 wacom_wac
->serial
[0] = value
;
137 case WACOM_HID_WD_SERIALHI
:
138 wacom_wac
->serial
[0] |= ((__u64
)value
) << 32;
141 case WACOM_HID_WD_TOOLTYPE
:
142 wacom_wac
->id
[0] = value
;
153 wacom_wac_queue_flush(hdev
, &wacom_wac
->pen_fifo
);
155 wacom_wac_queue_insert(hdev
, &wacom_wac
->pen_fifo
,
156 raw_data
, report_size
);
158 return insert
&& !flush
;
161 static int wacom_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
162 u8
*raw_data
, int size
)
164 struct wacom
*wacom
= hid_get_drvdata(hdev
);
166 if (size
> WACOM_PKGLEN_MAX
)
169 if (wacom_wac_pen_serial_enforce(hdev
, report
, raw_data
, size
))
172 memcpy(wacom
->wacom_wac
.data
, raw_data
, size
);
174 wacom_wac_irq(&wacom
->wacom_wac
, size
);
179 static int wacom_open(struct input_dev
*dev
)
181 struct wacom
*wacom
= input_get_drvdata(dev
);
183 return hid_hw_open(wacom
->hdev
);
186 static void wacom_close(struct input_dev
*dev
)
188 struct wacom
*wacom
= input_get_drvdata(dev
);
191 * wacom->hdev should never be null, but surprisingly, I had the case
192 * once while unplugging the Wacom Wireless Receiver.
195 hid_hw_close(wacom
->hdev
);
199 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
201 static int wacom_calc_hid_res(int logical_extents
, int physical_extents
,
202 unsigned unit
, int exponent
)
204 struct hid_field field
= {
205 .logical_maximum
= logical_extents
,
206 .physical_maximum
= physical_extents
,
208 .unit_exponent
= exponent
,
211 return hidinput_calc_abs_res(&field
, ABS_X
);
214 static void wacom_hid_usage_quirk(struct hid_device
*hdev
,
215 struct hid_field
*field
, struct hid_usage
*usage
)
217 struct wacom
*wacom
= hid_get_drvdata(hdev
);
218 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
219 unsigned int equivalent_usage
= wacom_equivalent_usage(usage
->hid
);
222 * The Dell Canvas 27 needs to be switched to its vendor-defined
223 * report to provide the best resolution.
225 if (hdev
->vendor
== USB_VENDOR_ID_WACOM
&&
226 hdev
->product
== 0x4200 &&
227 field
->application
== HID_UP_MSVENDOR
) {
228 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
229 wacom
->wacom_wac
.mode_value
= 2;
233 * ISDv4 devices which predate HID's adoption of the
234 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
235 * position instead. We can accurately detect if a
236 * usage with that value should be HID_DG_BARRELSWITCH2
237 * based on the surrounding usages, which have remained
238 * constant across generations.
240 if (features
->type
== HID_GENERIC
&&
241 usage
->hid
== 0x000D0000 &&
242 field
->application
== HID_DG_PEN
&&
243 field
->physical
== HID_DG_STYLUS
) {
244 int i
= usage
->usage_index
;
246 if (i
-4 >= 0 && i
+1 < field
->maxusage
&&
247 field
->usage
[i
-4].hid
== HID_DG_TIPSWITCH
&&
248 field
->usage
[i
-3].hid
== HID_DG_BARRELSWITCH
&&
249 field
->usage
[i
-2].hid
== HID_DG_ERASER
&&
250 field
->usage
[i
-1].hid
== HID_DG_INVERT
&&
251 field
->usage
[i
+1].hid
== HID_DG_INRANGE
) {
252 usage
->hid
= HID_DG_BARRELSWITCH2
;
256 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
257 if (hdev
->vendor
== USB_VENDOR_ID_WACOM
&&
258 hdev
->product
== 0x0358 &&
259 WACOM_PEN_FIELD(field
) &&
260 equivalent_usage
== HID_GD_Y
) {
261 field
->logical_maximum
= 43200;
265 static void wacom_feature_mapping(struct hid_device
*hdev
,
266 struct hid_field
*field
, struct hid_usage
*usage
)
268 struct wacom
*wacom
= hid_get_drvdata(hdev
);
269 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
270 struct hid_data
*hid_data
= &wacom
->wacom_wac
.hid_data
;
271 unsigned int equivalent_usage
= wacom_equivalent_usage(usage
->hid
);
276 wacom_hid_usage_quirk(hdev
, field
, usage
);
278 switch (equivalent_usage
) {
279 case WACOM_HID_WD_TOUCH_RING_SETTING
:
280 wacom
->generic_has_leds
= true;
282 case HID_DG_CONTACTMAX
:
283 /* leave touch_max as is if predefined */
284 if (!features
->touch_max
) {
286 n
= hid_report_len(field
->report
);
287 data
= hid_alloc_report_buf(field
->report
, GFP_KERNEL
);
290 data
[0] = field
->report
->id
;
291 ret
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
292 data
, n
, WAC_CMD_RETRIES
);
293 if (ret
== n
&& features
->type
== HID_GENERIC
) {
294 ret
= hid_report_raw_event(hdev
,
295 HID_FEATURE_REPORT
, data
, n
, 0);
296 } else if (ret
== 2 && features
->type
!= HID_GENERIC
) {
297 features
->touch_max
= data
[1];
299 features
->touch_max
= 16;
300 hid_warn(hdev
, "wacom_feature_mapping: "
301 "could not get HID_DG_CONTACTMAX, "
302 "defaulting to %d\n",
303 features
->touch_max
);
308 case HID_DG_INPUTMODE
:
309 /* Ignore if value index is out of bounds. */
310 if (usage
->usage_index
>= field
->report_count
) {
311 dev_err(&hdev
->dev
, "HID_DG_INPUTMODE out of range\n");
315 hid_data
->inputmode
= field
->report
->id
;
316 hid_data
->inputmode_index
= usage
->usage_index
;
319 case HID_UP_DIGITIZER
:
320 if (field
->report
->id
== 0x0B &&
321 (field
->application
== WACOM_HID_G9_PEN
||
322 field
->application
== WACOM_HID_G11_PEN
)) {
323 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
324 wacom
->wacom_wac
.mode_value
= 0;
328 case WACOM_HID_WD_DATAMODE
:
329 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
330 wacom
->wacom_wac
.mode_value
= 2;
333 case WACOM_HID_UP_G9
:
334 case WACOM_HID_UP_G11
:
335 if (field
->report
->id
== 0x03 &&
336 (field
->application
== WACOM_HID_G9_TOUCHSCREEN
||
337 field
->application
== WACOM_HID_G11_TOUCHSCREEN
)) {
338 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
339 wacom
->wacom_wac
.mode_value
= 0;
342 case WACOM_HID_WD_OFFSETLEFT
:
343 case WACOM_HID_WD_OFFSETTOP
:
344 case WACOM_HID_WD_OFFSETRIGHT
:
345 case WACOM_HID_WD_OFFSETBOTTOM
:
347 n
= hid_report_len(field
->report
);
348 data
= hid_alloc_report_buf(field
->report
, GFP_KERNEL
);
351 data
[0] = field
->report
->id
;
352 ret
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
353 data
, n
, WAC_CMD_RETRIES
);
355 ret
= hid_report_raw_event(hdev
, HID_FEATURE_REPORT
,
358 hid_warn(hdev
, "%s: could not retrieve sensor offsets\n",
367 * Interface Descriptor of wacom devices can be incomplete and
368 * inconsistent so wacom_features table is used to store stylus
369 * device's packet lengths, various maximum values, and tablet
370 * resolution based on product ID's.
372 * For devices that contain 2 interfaces, wacom_features table is
373 * inaccurate for the touch interface. Since the Interface Descriptor
374 * for touch interfaces has pretty complete data, this function exists
375 * to query tablet for this missing information instead of hard coding in
376 * an additional table.
378 * A typical Interface Descriptor for a stylus will contain a
379 * boot mouse application collection that is not of interest and this
380 * function will ignore it.
382 * It also contains a digitizer application collection that also is not
383 * of interest since any information it contains would be duplicate
384 * of what is in wacom_features. Usually it defines a report of an array
385 * of bytes that could be used as max length of the stylus packet returned.
386 * If it happens to define a Digitizer-Stylus Physical Collection then
387 * the X and Y logical values contain valid data but it is ignored.
389 * A typical Interface Descriptor for a touch interface will contain a
390 * Digitizer-Finger Physical Collection which will define both logical
391 * X/Y maximum as well as the physical size of tablet. Since touch
392 * interfaces haven't supported pressure or distance, this is enough
393 * information to override invalid values in the wacom_features table.
395 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
396 * data. We deal with them after returning from this function.
398 static void wacom_usage_mapping(struct hid_device
*hdev
,
399 struct hid_field
*field
, struct hid_usage
*usage
)
401 struct wacom
*wacom
= hid_get_drvdata(hdev
);
402 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
403 bool finger
= WACOM_FINGER_FIELD(field
);
404 bool pen
= WACOM_PEN_FIELD(field
);
405 unsigned equivalent_usage
= wacom_equivalent_usage(usage
->hid
);
408 * Requiring Stylus Usage will ignore boot mouse
409 * X/Y values and some cases of invalid Digitizer X/Y
410 * values commonly reported.
413 features
->device_type
|= WACOM_DEVICETYPE_PEN
;
415 features
->device_type
|= WACOM_DEVICETYPE_TOUCH
;
419 wacom_hid_usage_quirk(hdev
, field
, usage
);
421 switch (equivalent_usage
) {
423 features
->x_max
= field
->logical_maximum
;
425 features
->x_phy
= field
->physical_maximum
;
426 if ((features
->type
!= BAMBOO_PT
) &&
427 (features
->type
!= BAMBOO_TOUCH
)) {
428 features
->unit
= field
->unit
;
429 features
->unitExpo
= field
->unit_exponent
;
434 features
->y_max
= field
->logical_maximum
;
436 features
->y_phy
= field
->physical_maximum
;
437 if ((features
->type
!= BAMBOO_PT
) &&
438 (features
->type
!= BAMBOO_TOUCH
)) {
439 features
->unit
= field
->unit
;
440 features
->unitExpo
= field
->unit_exponent
;
444 case HID_DG_TIPPRESSURE
:
446 features
->pressure_max
= field
->logical_maximum
;
450 if (features
->type
== HID_GENERIC
)
451 wacom_wac_usage_mapping(hdev
, field
, usage
);
454 static void wacom_post_parse_hid(struct hid_device
*hdev
,
455 struct wacom_features
*features
)
457 struct wacom
*wacom
= hid_get_drvdata(hdev
);
458 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
460 if (features
->type
== HID_GENERIC
) {
461 /* Any last-minute generic device setup */
462 if (wacom_wac
->has_mode_change
) {
463 if (wacom_wac
->is_direct_mode
)
464 features
->device_type
|= WACOM_DEVICETYPE_DIRECT
;
466 features
->device_type
&= ~WACOM_DEVICETYPE_DIRECT
;
469 if (features
->touch_max
> 1) {
470 if (features
->device_type
& WACOM_DEVICETYPE_DIRECT
)
471 input_mt_init_slots(wacom_wac
->touch_input
,
472 wacom_wac
->features
.touch_max
,
475 input_mt_init_slots(wacom_wac
->touch_input
,
476 wacom_wac
->features
.touch_max
,
482 static void wacom_parse_hid(struct hid_device
*hdev
,
483 struct wacom_features
*features
)
485 struct hid_report_enum
*rep_enum
;
486 struct hid_report
*hreport
;
489 /* check features first */
490 rep_enum
= &hdev
->report_enum
[HID_FEATURE_REPORT
];
491 list_for_each_entry(hreport
, &rep_enum
->report_list
, list
) {
492 for (i
= 0; i
< hreport
->maxfield
; i
++) {
493 /* Ignore if report count is out of bounds. */
494 if (hreport
->field
[i
]->report_count
< 1)
497 for (j
= 0; j
< hreport
->field
[i
]->maxusage
; j
++) {
498 wacom_feature_mapping(hdev
, hreport
->field
[i
],
499 hreport
->field
[i
]->usage
+ j
);
504 /* now check the input usages */
505 rep_enum
= &hdev
->report_enum
[HID_INPUT_REPORT
];
506 list_for_each_entry(hreport
, &rep_enum
->report_list
, list
) {
508 if (!hreport
->maxfield
)
511 for (i
= 0; i
< hreport
->maxfield
; i
++)
512 for (j
= 0; j
< hreport
->field
[i
]->maxusage
; j
++)
513 wacom_usage_mapping(hdev
, hreport
->field
[i
],
514 hreport
->field
[i
]->usage
+ j
);
517 wacom_post_parse_hid(hdev
, features
);
520 static int wacom_hid_set_device_mode(struct hid_device
*hdev
)
522 struct wacom
*wacom
= hid_get_drvdata(hdev
);
523 struct hid_data
*hid_data
= &wacom
->wacom_wac
.hid_data
;
524 struct hid_report
*r
;
525 struct hid_report_enum
*re
;
527 if (hid_data
->inputmode
< 0)
530 re
= &(hdev
->report_enum
[HID_FEATURE_REPORT
]);
531 r
= re
->report_id_hash
[hid_data
->inputmode
];
533 r
->field
[0]->value
[hid_data
->inputmode_index
] = 2;
534 hid_hw_request(hdev
, r
, HID_REQ_SET_REPORT
);
539 static int wacom_set_device_mode(struct hid_device
*hdev
,
540 struct wacom_wac
*wacom_wac
)
543 struct hid_report
*r
;
544 struct hid_report_enum
*re
;
546 int error
= -ENOMEM
, limit
= 0;
548 if (wacom_wac
->mode_report
< 0)
551 re
= &(hdev
->report_enum
[HID_FEATURE_REPORT
]);
552 r
= re
->report_id_hash
[wacom_wac
->mode_report
];
556 rep_data
= hid_alloc_report_buf(r
, GFP_KERNEL
);
560 length
= hid_report_len(r
);
563 rep_data
[0] = wacom_wac
->mode_report
;
564 rep_data
[1] = wacom_wac
->mode_value
;
566 error
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
,
569 error
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
570 rep_data
, length
, 1);
571 } while (error
>= 0 &&
572 rep_data
[1] != wacom_wac
->mode_report
&&
573 limit
++ < WAC_MSG_RETRIES
);
577 return error
< 0 ? error
: 0;
580 static int wacom_bt_query_tablet_data(struct hid_device
*hdev
, u8 speed
,
581 struct wacom_features
*features
)
583 struct wacom
*wacom
= hid_get_drvdata(hdev
);
587 switch (features
->type
) {
591 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
, 2,
595 rep_data
[0] = speed
== 0 ? 0x05 : 0x06;
598 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
,
602 wacom
->wacom_wac
.bt_high_speed
= speed
;
608 * Note that if the raw queries fail, it's not a hard failure
609 * and it is safe to continue
611 hid_warn(hdev
, "failed to poke device, command %d, err %d\n",
616 wacom
->wacom_wac
.bt_features
&= ~0x20;
618 wacom
->wacom_wac
.bt_features
|= 0x20;
621 rep_data
[1] = wacom
->wacom_wac
.bt_features
;
623 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
, 2,
626 wacom
->wacom_wac
.bt_high_speed
= speed
;
634 * Switch the tablet into its most-capable mode. Wacom tablets are
635 * typically configured to power-up in a mode which sends mouse-like
636 * reports to the OS. To get absolute position, pressure data, etc.
637 * from the tablet, it is necessary to switch the tablet out of this
638 * mode and into one which sends the full range of tablet data.
640 static int _wacom_query_tablet_data(struct wacom
*wacom
)
642 struct hid_device
*hdev
= wacom
->hdev
;
643 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
644 struct wacom_features
*features
= &wacom_wac
->features
;
646 if (hdev
->bus
== BUS_BLUETOOTH
)
647 return wacom_bt_query_tablet_data(hdev
, 1, features
);
649 if (features
->type
!= HID_GENERIC
) {
650 if (features
->device_type
& WACOM_DEVICETYPE_TOUCH
) {
651 if (features
->type
> TABLETPC
) {
652 /* MT Tablet PC touch */
653 wacom_wac
->mode_report
= 3;
654 wacom_wac
->mode_value
= 4;
655 } else if (features
->type
== WACOM_24HDT
) {
656 wacom_wac
->mode_report
= 18;
657 wacom_wac
->mode_value
= 2;
658 } else if (features
->type
== WACOM_27QHDT
) {
659 wacom_wac
->mode_report
= 131;
660 wacom_wac
->mode_value
= 2;
661 } else if (features
->type
== BAMBOO_PAD
) {
662 wacom_wac
->mode_report
= 2;
663 wacom_wac
->mode_value
= 2;
665 } else if (features
->device_type
& WACOM_DEVICETYPE_PEN
) {
666 if (features
->type
<= BAMBOO_PT
) {
667 wacom_wac
->mode_report
= 2;
668 wacom_wac
->mode_value
= 2;
673 wacom_set_device_mode(hdev
, wacom_wac
);
675 if (features
->type
== HID_GENERIC
)
676 return wacom_hid_set_device_mode(hdev
);
681 static void wacom_retrieve_hid_descriptor(struct hid_device
*hdev
,
682 struct wacom_features
*features
)
684 struct wacom
*wacom
= hid_get_drvdata(hdev
);
685 struct usb_interface
*intf
= wacom
->intf
;
687 /* default features */
688 features
->x_fuzz
= 4;
689 features
->y_fuzz
= 4;
690 features
->pressure_fuzz
= 0;
691 features
->distance_fuzz
= 1;
692 features
->tilt_fuzz
= 1;
695 * The wireless device HID is basic and layout conflicts with
696 * other tablets (monitor and touch interface can look like pen).
697 * Skip the query for this type and modify defaults based on
700 if (features
->type
== WIRELESS
) {
701 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 0)
702 features
->device_type
= WACOM_DEVICETYPE_WL_MONITOR
;
704 features
->device_type
= WACOM_DEVICETYPE_NONE
;
708 wacom_parse_hid(hdev
, features
);
711 struct wacom_hdev_data
{
712 struct list_head list
;
714 struct hid_device
*dev
;
715 struct wacom_shared shared
;
718 static LIST_HEAD(wacom_udev_list
);
719 static DEFINE_MUTEX(wacom_udev_list_lock
);
721 static bool wacom_are_sibling(struct hid_device
*hdev
,
722 struct hid_device
*sibling
)
724 struct wacom
*wacom
= hid_get_drvdata(hdev
);
725 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
726 struct wacom
*sibling_wacom
= hid_get_drvdata(sibling
);
727 struct wacom_features
*sibling_features
= &sibling_wacom
->wacom_wac
.features
;
728 __u32 oVid
= features
->oVid
? features
->oVid
: hdev
->vendor
;
729 __u32 oPid
= features
->oPid
? features
->oPid
: hdev
->product
;
731 /* The defined oVid/oPid must match that of the sibling */
732 if (features
->oVid
!= HID_ANY_ID
&& sibling
->vendor
!= oVid
)
734 if (features
->oPid
!= HID_ANY_ID
&& sibling
->product
!= oPid
)
738 * Devices with the same VID/PID must share the same physical
739 * device path, while those with different VID/PID must share
740 * the same physical parent device path.
742 if (hdev
->vendor
== sibling
->vendor
&& hdev
->product
== sibling
->product
) {
743 if (!hid_compare_device_paths(hdev
, sibling
, '/'))
746 if (!hid_compare_device_paths(hdev
, sibling
, '.'))
750 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
751 if (features
->type
!= HID_GENERIC
)
755 * Direct-input devices may not be siblings of indirect-input
758 if ((features
->device_type
& WACOM_DEVICETYPE_DIRECT
) &&
759 !(sibling_features
->device_type
& WACOM_DEVICETYPE_DIRECT
))
763 * Indirect-input devices may not be siblings of direct-input
766 if (!(features
->device_type
& WACOM_DEVICETYPE_DIRECT
) &&
767 (sibling_features
->device_type
& WACOM_DEVICETYPE_DIRECT
))
770 /* Pen devices may only be siblings of touch devices */
771 if ((features
->device_type
& WACOM_DEVICETYPE_PEN
) &&
772 !(sibling_features
->device_type
& WACOM_DEVICETYPE_TOUCH
))
775 /* Touch devices may only be siblings of pen devices */
776 if ((features
->device_type
& WACOM_DEVICETYPE_TOUCH
) &&
777 !(sibling_features
->device_type
& WACOM_DEVICETYPE_PEN
))
781 * No reason could be found for these two devices to NOT be
782 * siblings, so there's a good chance they ARE siblings
787 static struct wacom_hdev_data
*wacom_get_hdev_data(struct hid_device
*hdev
)
789 struct wacom_hdev_data
*data
;
791 /* Try to find an already-probed interface from the same device */
792 list_for_each_entry(data
, &wacom_udev_list
, list
) {
793 if (hid_compare_device_paths(hdev
, data
->dev
, '/')) {
794 kref_get(&data
->kref
);
799 /* Fallback to finding devices that appear to be "siblings" */
800 list_for_each_entry(data
, &wacom_udev_list
, list
) {
801 if (wacom_are_sibling(hdev
, data
->dev
)) {
802 kref_get(&data
->kref
);
810 static void wacom_release_shared_data(struct kref
*kref
)
812 struct wacom_hdev_data
*data
=
813 container_of(kref
, struct wacom_hdev_data
, kref
);
815 mutex_lock(&wacom_udev_list_lock
);
816 list_del(&data
->list
);
817 mutex_unlock(&wacom_udev_list_lock
);
822 static void wacom_remove_shared_data(void *res
)
824 struct wacom
*wacom
= res
;
825 struct wacom_hdev_data
*data
;
826 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
828 if (wacom_wac
->shared
) {
829 data
= container_of(wacom_wac
->shared
, struct wacom_hdev_data
,
832 if (wacom_wac
->shared
->touch
== wacom
->hdev
)
833 wacom_wac
->shared
->touch
= NULL
;
834 else if (wacom_wac
->shared
->pen
== wacom
->hdev
)
835 wacom_wac
->shared
->pen
= NULL
;
837 kref_put(&data
->kref
, wacom_release_shared_data
);
838 wacom_wac
->shared
= NULL
;
842 static int wacom_add_shared_data(struct hid_device
*hdev
)
844 struct wacom
*wacom
= hid_get_drvdata(hdev
);
845 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
846 struct wacom_hdev_data
*data
;
849 mutex_lock(&wacom_udev_list_lock
);
851 data
= wacom_get_hdev_data(hdev
);
853 data
= kzalloc(sizeof(struct wacom_hdev_data
), GFP_KERNEL
);
859 kref_init(&data
->kref
);
861 list_add_tail(&data
->list
, &wacom_udev_list
);
864 wacom_wac
->shared
= &data
->shared
;
866 retval
= devm_add_action(&hdev
->dev
, wacom_remove_shared_data
, wacom
);
868 mutex_unlock(&wacom_udev_list_lock
);
869 wacom_remove_shared_data(wacom
);
873 if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_TOUCH
)
874 wacom_wac
->shared
->touch
= hdev
;
875 else if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_PEN
)
876 wacom_wac
->shared
->pen
= hdev
;
879 mutex_unlock(&wacom_udev_list_lock
);
883 static int wacom_led_control(struct wacom
*wacom
)
887 unsigned char report_id
= WAC_CMD_LED_CONTROL
;
890 if (!wacom
->led
.groups
)
893 if (wacom
->wacom_wac
.features
.type
== REMOTE
)
896 if (wacom
->wacom_wac
.pid
) { /* wireless connected */
897 report_id
= WAC_CMD_WL_LED_CONTROL
;
900 else if (wacom
->wacom_wac
.features
.type
== INTUOSP2_BT
) {
901 report_id
= WAC_CMD_WL_INTUOSP2
;
904 buf
= kzalloc(buf_size
, GFP_KERNEL
);
908 if (wacom
->wacom_wac
.features
.type
== HID_GENERIC
) {
909 buf
[0] = WAC_CMD_LED_CONTROL_GENERIC
;
910 buf
[1] = wacom
->led
.llv
;
911 buf
[2] = wacom
->led
.groups
[0].select
& 0x03;
913 } else if ((wacom
->wacom_wac
.features
.type
>= INTUOS5S
&&
914 wacom
->wacom_wac
.features
.type
<= INTUOSPL
)) {
916 * Touch Ring and crop mark LED luminance may take on
917 * one of four values:
918 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
920 int ring_led
= wacom
->led
.groups
[0].select
& 0x03;
921 int ring_lum
= (((wacom
->led
.llv
& 0x60) >> 5) - 1) & 0x03;
923 unsigned char led_bits
= (crop_lum
<< 4) | (ring_lum
<< 2) | (ring_led
);
926 if (wacom
->wacom_wac
.pid
) {
927 wacom_get_report(wacom
->hdev
, HID_FEATURE_REPORT
,
928 buf
, buf_size
, WAC_CMD_RETRIES
);
934 else if (wacom
->wacom_wac
.features
.type
== INTUOSP2_BT
) {
936 buf
[4] = 100; // Power Connection LED (ORANGE)
937 buf
[5] = 100; // BT Connection LED (BLUE)
938 buf
[6] = 100; // Paper Mode (RED?)
939 buf
[7] = 100; // Paper Mode (GREEN?)
940 buf
[8] = 100; // Paper Mode (BLUE?)
941 buf
[9] = wacom
->led
.llv
;
942 buf
[10] = wacom
->led
.groups
[0].select
& 0x03;
945 int led
= wacom
->led
.groups
[0].select
| 0x4;
947 if (wacom
->wacom_wac
.features
.type
== WACOM_21UX2
||
948 wacom
->wacom_wac
.features
.type
== WACOM_24HD
)
949 led
|= (wacom
->led
.groups
[1].select
<< 4) | 0x40;
953 buf
[2] = wacom
->led
.llv
;
954 buf
[3] = wacom
->led
.hlv
;
955 buf
[4] = wacom
->led
.img_lum
;
958 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, buf_size
,
965 static int wacom_led_putimage(struct wacom
*wacom
, int button_id
, u8 xfer_id
,
966 const unsigned len
, const void *img
)
970 const unsigned chunk_len
= len
/ 4; /* 4 chunks are needed to be sent */
972 buf
= kzalloc(chunk_len
+ 3 , GFP_KERNEL
);
976 /* Send 'start' command */
977 buf
[0] = WAC_CMD_ICON_START
;
979 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, 2,
985 buf
[1] = button_id
& 0x07;
986 for (i
= 0; i
< 4; i
++) {
988 memcpy(buf
+ 3, img
+ i
* chunk_len
, chunk_len
);
990 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
,
991 buf
, chunk_len
+ 3, WAC_CMD_RETRIES
);
997 buf
[0] = WAC_CMD_ICON_START
;
999 wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, 2,
1007 static ssize_t
wacom_led_select_store(struct device
*dev
, int set_id
,
1008 const char *buf
, size_t count
)
1010 struct hid_device
*hdev
= to_hid_device(dev
);
1011 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1015 err
= kstrtouint(buf
, 10, &id
);
1019 mutex_lock(&wacom
->lock
);
1021 wacom
->led
.groups
[set_id
].select
= id
& 0x3;
1022 err
= wacom_led_control(wacom
);
1024 mutex_unlock(&wacom
->lock
);
1026 return err
< 0 ? err
: count
;
1029 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
1030 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
1031 struct device_attribute *attr, const char *buf, size_t count) \
1033 return wacom_led_select_store(dev, SET_ID, buf, count); \
1035 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
1036 struct device_attribute *attr, char *buf) \
1038 struct hid_device *hdev = to_hid_device(dev);\
1039 struct wacom *wacom = hid_get_drvdata(hdev); \
1040 return scnprintf(buf, PAGE_SIZE, "%d\n", \
1041 wacom->led.groups[SET_ID].select); \
1043 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
1044 wacom_led##SET_ID##_select_show, \
1045 wacom_led##SET_ID##_select_store)
1047 DEVICE_LED_SELECT_ATTR(0);
1048 DEVICE_LED_SELECT_ATTR(1);
1050 static ssize_t
wacom_luminance_store(struct wacom
*wacom
, u8
*dest
,
1051 const char *buf
, size_t count
)
1056 err
= kstrtouint(buf
, 10, &value
);
1060 mutex_lock(&wacom
->lock
);
1062 *dest
= value
& 0x7f;
1063 err
= wacom_led_control(wacom
);
1065 mutex_unlock(&wacom
->lock
);
1067 return err
< 0 ? err
: count
;
1070 #define DEVICE_LUMINANCE_ATTR(name, field) \
1071 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
1072 struct device_attribute *attr, const char *buf, size_t count) \
1074 struct hid_device *hdev = to_hid_device(dev);\
1075 struct wacom *wacom = hid_get_drvdata(hdev); \
1077 return wacom_luminance_store(wacom, &wacom->led.field, \
1080 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
1081 struct device_attribute *attr, char *buf) \
1083 struct wacom *wacom = dev_get_drvdata(dev); \
1084 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
1086 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
1087 wacom_##name##_luminance_show, \
1088 wacom_##name##_luminance_store)
1090 DEVICE_LUMINANCE_ATTR(status0
, llv
);
1091 DEVICE_LUMINANCE_ATTR(status1
, hlv
);
1092 DEVICE_LUMINANCE_ATTR(buttons
, img_lum
);
1094 static ssize_t
wacom_button_image_store(struct device
*dev
, int button_id
,
1095 const char *buf
, size_t count
)
1097 struct hid_device
*hdev
= to_hid_device(dev
);
1098 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1103 if (hdev
->bus
== BUS_BLUETOOTH
) {
1105 xfer_id
= WAC_CMD_ICON_BT_XFER
;
1108 xfer_id
= WAC_CMD_ICON_XFER
;
1114 mutex_lock(&wacom
->lock
);
1116 err
= wacom_led_putimage(wacom
, button_id
, xfer_id
, len
, buf
);
1118 mutex_unlock(&wacom
->lock
);
1120 return err
< 0 ? err
: count
;
1123 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1124 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1125 struct device_attribute *attr, const char *buf, size_t count) \
1127 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1129 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
1130 NULL, wacom_btnimg##BUTTON_ID##_store)
1132 DEVICE_BTNIMG_ATTR(0);
1133 DEVICE_BTNIMG_ATTR(1);
1134 DEVICE_BTNIMG_ATTR(2);
1135 DEVICE_BTNIMG_ATTR(3);
1136 DEVICE_BTNIMG_ATTR(4);
1137 DEVICE_BTNIMG_ATTR(5);
1138 DEVICE_BTNIMG_ATTR(6);
1139 DEVICE_BTNIMG_ATTR(7);
1141 static struct attribute
*cintiq_led_attrs
[] = {
1142 &dev_attr_status_led0_select
.attr
,
1143 &dev_attr_status_led1_select
.attr
,
1147 static struct attribute_group cintiq_led_attr_group
= {
1148 .name
= "wacom_led",
1149 .attrs
= cintiq_led_attrs
,
1152 static struct attribute
*intuos4_led_attrs
[] = {
1153 &dev_attr_status0_luminance
.attr
,
1154 &dev_attr_status1_luminance
.attr
,
1155 &dev_attr_status_led0_select
.attr
,
1156 &dev_attr_buttons_luminance
.attr
,
1157 &dev_attr_button0_rawimg
.attr
,
1158 &dev_attr_button1_rawimg
.attr
,
1159 &dev_attr_button2_rawimg
.attr
,
1160 &dev_attr_button3_rawimg
.attr
,
1161 &dev_attr_button4_rawimg
.attr
,
1162 &dev_attr_button5_rawimg
.attr
,
1163 &dev_attr_button6_rawimg
.attr
,
1164 &dev_attr_button7_rawimg
.attr
,
1168 static struct attribute_group intuos4_led_attr_group
= {
1169 .name
= "wacom_led",
1170 .attrs
= intuos4_led_attrs
,
1173 static struct attribute
*intuos5_led_attrs
[] = {
1174 &dev_attr_status0_luminance
.attr
,
1175 &dev_attr_status_led0_select
.attr
,
1179 static struct attribute_group intuos5_led_attr_group
= {
1180 .name
= "wacom_led",
1181 .attrs
= intuos5_led_attrs
,
1184 static struct attribute
*generic_led_attrs
[] = {
1185 &dev_attr_status0_luminance
.attr
,
1186 &dev_attr_status_led0_select
.attr
,
1190 static struct attribute_group generic_led_attr_group
= {
1191 .name
= "wacom_led",
1192 .attrs
= generic_led_attrs
,
1195 struct wacom_sysfs_group_devres
{
1196 struct attribute_group
*group
;
1197 struct kobject
*root
;
1200 static void wacom_devm_sysfs_group_release(struct device
*dev
, void *res
)
1202 struct wacom_sysfs_group_devres
*devres
= res
;
1203 struct kobject
*kobj
= devres
->root
;
1205 dev_dbg(dev
, "%s: dropping reference to %s\n",
1206 __func__
, devres
->group
->name
);
1207 sysfs_remove_group(kobj
, devres
->group
);
1210 static int __wacom_devm_sysfs_create_group(struct wacom
*wacom
,
1211 struct kobject
*root
,
1212 struct attribute_group
*group
)
1214 struct wacom_sysfs_group_devres
*devres
;
1217 devres
= devres_alloc(wacom_devm_sysfs_group_release
,
1218 sizeof(struct wacom_sysfs_group_devres
),
1223 devres
->group
= group
;
1224 devres
->root
= root
;
1226 error
= sysfs_create_group(devres
->root
, group
);
1228 devres_free(devres
);
1232 devres_add(&wacom
->hdev
->dev
, devres
);
1237 static int wacom_devm_sysfs_create_group(struct wacom
*wacom
,
1238 struct attribute_group
*group
)
1240 return __wacom_devm_sysfs_create_group(wacom
, &wacom
->hdev
->dev
.kobj
,
1244 enum led_brightness
wacom_leds_brightness_get(struct wacom_led
*led
)
1246 struct wacom
*wacom
= led
->wacom
;
1248 if (wacom
->led
.max_hlv
)
1249 return led
->hlv
* LED_FULL
/ wacom
->led
.max_hlv
;
1251 if (wacom
->led
.max_llv
)
1252 return led
->llv
* LED_FULL
/ wacom
->led
.max_llv
;
1254 /* device doesn't support brightness tuning */
1258 static enum led_brightness
__wacom_led_brightness_get(struct led_classdev
*cdev
)
1260 struct wacom_led
*led
= container_of(cdev
, struct wacom_led
, cdev
);
1261 struct wacom
*wacom
= led
->wacom
;
1263 if (wacom
->led
.groups
[led
->group
].select
!= led
->id
)
1266 return wacom_leds_brightness_get(led
);
1269 static int wacom_led_brightness_set(struct led_classdev
*cdev
,
1270 enum led_brightness brightness
)
1272 struct wacom_led
*led
= container_of(cdev
, struct wacom_led
, cdev
);
1273 struct wacom
*wacom
= led
->wacom
;
1276 mutex_lock(&wacom
->lock
);
1278 if (!wacom
->led
.groups
|| (brightness
== LED_OFF
&&
1279 wacom
->led
.groups
[led
->group
].select
!= led
->id
)) {
1284 led
->llv
= wacom
->led
.llv
= wacom
->led
.max_llv
* brightness
/ LED_FULL
;
1285 led
->hlv
= wacom
->led
.hlv
= wacom
->led
.max_hlv
* brightness
/ LED_FULL
;
1287 wacom
->led
.groups
[led
->group
].select
= led
->id
;
1289 error
= wacom_led_control(wacom
);
1292 mutex_unlock(&wacom
->lock
);
1297 static void wacom_led_readonly_brightness_set(struct led_classdev
*cdev
,
1298 enum led_brightness brightness
)
1302 static int wacom_led_register_one(struct device
*dev
, struct wacom
*wacom
,
1303 struct wacom_led
*led
, unsigned int group
,
1304 unsigned int id
, bool read_only
)
1309 name
= devm_kasprintf(dev
, GFP_KERNEL
,
1318 led
->trigger
.name
= name
;
1319 error
= devm_led_trigger_register(dev
, &led
->trigger
);
1321 hid_err(wacom
->hdev
,
1322 "failed to register LED trigger %s: %d\n",
1323 led
->cdev
.name
, error
);
1331 led
->llv
= wacom
->led
.llv
;
1332 led
->hlv
= wacom
->led
.hlv
;
1333 led
->cdev
.name
= name
;
1334 led
->cdev
.max_brightness
= LED_FULL
;
1335 led
->cdev
.flags
= LED_HW_PLUGGABLE
;
1336 led
->cdev
.brightness_get
= __wacom_led_brightness_get
;
1338 led
->cdev
.brightness_set_blocking
= wacom_led_brightness_set
;
1339 led
->cdev
.default_trigger
= led
->cdev
.name
;
1341 led
->cdev
.brightness_set
= wacom_led_readonly_brightness_set
;
1344 error
= devm_led_classdev_register(dev
, &led
->cdev
);
1346 hid_err(wacom
->hdev
,
1347 "failed to register LED %s: %d\n",
1348 led
->cdev
.name
, error
);
1349 led
->cdev
.name
= NULL
;
1356 static void wacom_led_groups_release_one(void *data
)
1358 struct wacom_group_leds
*group
= data
;
1360 devres_release_group(group
->dev
, group
);
1363 static int wacom_led_groups_alloc_and_register_one(struct device
*dev
,
1364 struct wacom
*wacom
,
1365 int group_id
, int count
,
1368 struct wacom_led
*leds
;
1371 if (group_id
>= wacom
->led
.count
|| count
<= 0)
1374 if (!devres_open_group(dev
, &wacom
->led
.groups
[group_id
], GFP_KERNEL
))
1377 leds
= devm_kcalloc(dev
, count
, sizeof(struct wacom_led
), GFP_KERNEL
);
1383 wacom
->led
.groups
[group_id
].leds
= leds
;
1384 wacom
->led
.groups
[group_id
].count
= count
;
1386 for (i
= 0; i
< count
; i
++) {
1387 error
= wacom_led_register_one(dev
, wacom
, &leds
[i
],
1388 group_id
, i
, read_only
);
1393 wacom
->led
.groups
[group_id
].dev
= dev
;
1395 devres_close_group(dev
, &wacom
->led
.groups
[group_id
]);
1398 * There is a bug (?) in devm_led_classdev_register() in which its
1399 * increments the refcount of the parent. If the parent is an input
1400 * device, that means the ref count never reaches 0 when
1401 * devm_input_device_release() gets called.
1402 * This means that the LEDs are still there after disconnect.
1403 * Manually force the release of the group so that the leds are released
1404 * once we are done using them.
1406 error
= devm_add_action_or_reset(&wacom
->hdev
->dev
,
1407 wacom_led_groups_release_one
,
1408 &wacom
->led
.groups
[group_id
]);
1415 devres_release_group(dev
, &wacom
->led
.groups
[group_id
]);
1419 struct wacom_led
*wacom_led_find(struct wacom
*wacom
, unsigned int group_id
,
1422 struct wacom_group_leds
*group
;
1424 if (group_id
>= wacom
->led
.count
)
1427 group
= &wacom
->led
.groups
[group_id
];
1434 return &group
->leds
[id
];
1438 * wacom_led_next: gives the next available led with a wacom trigger.
1440 * returns the next available struct wacom_led which has its default trigger
1441 * or the current one if none is available.
1443 struct wacom_led
*wacom_led_next(struct wacom
*wacom
, struct wacom_led
*cur
)
1445 struct wacom_led
*next_led
;
1455 next_led
= wacom_led_find(wacom
, group
, ++next
);
1456 if (!next_led
|| next_led
== cur
)
1458 } while (next_led
->cdev
.trigger
!= &next_led
->trigger
);
1463 static void wacom_led_groups_release(void *data
)
1465 struct wacom
*wacom
= data
;
1467 wacom
->led
.groups
= NULL
;
1468 wacom
->led
.count
= 0;
1471 static int wacom_led_groups_allocate(struct wacom
*wacom
, int count
)
1473 struct device
*dev
= &wacom
->hdev
->dev
;
1474 struct wacom_group_leds
*groups
;
1477 groups
= devm_kcalloc(dev
, count
, sizeof(struct wacom_group_leds
),
1482 error
= devm_add_action_or_reset(dev
, wacom_led_groups_release
, wacom
);
1486 wacom
->led
.groups
= groups
;
1487 wacom
->led
.count
= count
;
1492 static int wacom_leds_alloc_and_register(struct wacom
*wacom
, int group_count
,
1493 int led_per_group
, bool read_only
)
1498 if (!wacom
->wacom_wac
.pad_input
)
1501 dev
= &wacom
->wacom_wac
.pad_input
->dev
;
1503 error
= wacom_led_groups_allocate(wacom
, group_count
);
1507 for (i
= 0; i
< group_count
; i
++) {
1508 error
= wacom_led_groups_alloc_and_register_one(dev
, wacom
, i
,
1518 int wacom_initialize_leds(struct wacom
*wacom
)
1522 if (!(wacom
->wacom_wac
.features
.device_type
& WACOM_DEVICETYPE_PAD
))
1525 /* Initialize default values */
1526 switch (wacom
->wacom_wac
.features
.type
) {
1528 if (!wacom
->generic_has_leds
)
1530 wacom
->led
.llv
= 100;
1531 wacom
->led
.max_llv
= 100;
1533 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1535 hid_err(wacom
->hdev
,
1536 "cannot create leds err: %d\n", error
);
1540 error
= wacom_devm_sysfs_create_group(wacom
,
1541 &generic_led_attr_group
);
1548 wacom
->led
.llv
= 10;
1549 wacom
->led
.hlv
= 20;
1550 wacom
->led
.max_llv
= 127;
1551 wacom
->led
.max_hlv
= 127;
1552 wacom
->led
.img_lum
= 10;
1554 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1556 hid_err(wacom
->hdev
,
1557 "cannot create leds err: %d\n", error
);
1561 error
= wacom_devm_sysfs_create_group(wacom
,
1562 &intuos4_led_attr_group
);
1569 wacom
->led
.img_lum
= 0;
1571 error
= wacom_leds_alloc_and_register(wacom
, 2, 4, false);
1573 hid_err(wacom
->hdev
,
1574 "cannot create leds err: %d\n", error
);
1578 error
= wacom_devm_sysfs_create_group(wacom
,
1579 &cintiq_led_attr_group
);
1588 wacom
->led
.llv
= 32;
1589 wacom
->led
.max_llv
= 96;
1591 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1593 hid_err(wacom
->hdev
,
1594 "cannot create leds err: %d\n", error
);
1598 error
= wacom_devm_sysfs_create_group(wacom
,
1599 &intuos5_led_attr_group
);
1603 wacom
->led
.llv
= 50;
1604 wacom
->led
.max_llv
= 100;
1605 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1607 hid_err(wacom
->hdev
,
1608 "cannot create leds err: %d\n", error
);
1614 wacom
->led
.llv
= 255;
1615 wacom
->led
.max_llv
= 255;
1616 error
= wacom_led_groups_allocate(wacom
, 5);
1618 hid_err(wacom
->hdev
,
1619 "cannot create leds err: %d\n", error
);
1629 hid_err(wacom
->hdev
,
1630 "cannot create sysfs group err: %d\n", error
);
1637 static void wacom_init_work(struct work_struct
*work
)
1639 struct wacom
*wacom
= container_of(work
, struct wacom
, init_work
.work
);
1641 _wacom_query_tablet_data(wacom
);
1642 wacom_led_control(wacom
);
1645 static void wacom_query_tablet_data(struct wacom
*wacom
)
1647 schedule_delayed_work(&wacom
->init_work
, msecs_to_jiffies(1000));
1650 static enum power_supply_property wacom_battery_props
[] = {
1651 POWER_SUPPLY_PROP_MODEL_NAME
,
1652 POWER_SUPPLY_PROP_PRESENT
,
1653 POWER_SUPPLY_PROP_STATUS
,
1654 POWER_SUPPLY_PROP_SCOPE
,
1655 POWER_SUPPLY_PROP_CAPACITY
1658 static int wacom_battery_get_property(struct power_supply
*psy
,
1659 enum power_supply_property psp
,
1660 union power_supply_propval
*val
)
1662 struct wacom_battery
*battery
= power_supply_get_drvdata(psy
);
1666 case POWER_SUPPLY_PROP_MODEL_NAME
:
1667 val
->strval
= battery
->wacom
->wacom_wac
.name
;
1669 case POWER_SUPPLY_PROP_PRESENT
:
1670 val
->intval
= battery
->bat_connected
;
1672 case POWER_SUPPLY_PROP_SCOPE
:
1673 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
1675 case POWER_SUPPLY_PROP_CAPACITY
:
1676 val
->intval
= battery
->battery_capacity
;
1678 case POWER_SUPPLY_PROP_STATUS
:
1679 if (battery
->bat_status
!= WACOM_POWER_SUPPLY_STATUS_AUTO
)
1680 val
->intval
= battery
->bat_status
;
1681 else if (battery
->bat_charging
)
1682 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
1683 else if (battery
->battery_capacity
== 100 &&
1684 battery
->ps_connected
)
1685 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
1686 else if (battery
->ps_connected
)
1687 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1689 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
1699 static int __wacom_initialize_battery(struct wacom
*wacom
,
1700 struct wacom_battery
*battery
)
1702 static atomic_t battery_no
= ATOMIC_INIT(0);
1703 struct device
*dev
= &wacom
->hdev
->dev
;
1704 struct power_supply_config psy_cfg
= { .drv_data
= battery
, };
1705 struct power_supply
*ps_bat
;
1706 struct power_supply_desc
*bat_desc
= &battery
->bat_desc
;
1710 if (!devres_open_group(dev
, bat_desc
, GFP_KERNEL
))
1713 battery
->wacom
= wacom
;
1715 n
= atomic_inc_return(&battery_no
) - 1;
1717 bat_desc
->properties
= wacom_battery_props
;
1718 bat_desc
->num_properties
= ARRAY_SIZE(wacom_battery_props
);
1719 bat_desc
->get_property
= wacom_battery_get_property
;
1720 sprintf(battery
->bat_name
, "wacom_battery_%ld", n
);
1721 bat_desc
->name
= battery
->bat_name
;
1722 bat_desc
->type
= POWER_SUPPLY_TYPE_USB
;
1723 bat_desc
->use_for_apm
= 0;
1725 ps_bat
= devm_power_supply_register(dev
, bat_desc
, &psy_cfg
);
1726 if (IS_ERR(ps_bat
)) {
1727 error
= PTR_ERR(ps_bat
);
1731 power_supply_powers(ps_bat
, &wacom
->hdev
->dev
);
1733 battery
->battery
= ps_bat
;
1735 devres_close_group(dev
, bat_desc
);
1739 devres_release_group(dev
, bat_desc
);
1743 static int wacom_initialize_battery(struct wacom
*wacom
)
1745 if (wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
)
1746 return __wacom_initialize_battery(wacom
, &wacom
->battery
);
1751 static void wacom_destroy_battery(struct wacom
*wacom
)
1753 if (wacom
->battery
.battery
) {
1754 devres_release_group(&wacom
->hdev
->dev
,
1755 &wacom
->battery
.bat_desc
);
1756 wacom
->battery
.battery
= NULL
;
1760 static ssize_t
wacom_show_speed(struct device
*dev
,
1761 struct device_attribute
1764 struct hid_device
*hdev
= to_hid_device(dev
);
1765 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1767 return snprintf(buf
, PAGE_SIZE
, "%i\n", wacom
->wacom_wac
.bt_high_speed
);
1770 static ssize_t
wacom_store_speed(struct device
*dev
,
1771 struct device_attribute
*attr
,
1772 const char *buf
, size_t count
)
1774 struct hid_device
*hdev
= to_hid_device(dev
);
1775 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1778 if (kstrtou8(buf
, 0, &new_speed
))
1781 if (new_speed
!= 0 && new_speed
!= 1)
1784 wacom_bt_query_tablet_data(hdev
, new_speed
, &wacom
->wacom_wac
.features
);
1789 static DEVICE_ATTR(speed
, DEV_ATTR_RW_PERM
,
1790 wacom_show_speed
, wacom_store_speed
);
1793 static ssize_t
wacom_show_remote_mode(struct kobject
*kobj
,
1794 struct kobj_attribute
*kattr
,
1795 char *buf
, int index
)
1797 struct device
*dev
= kobj_to_dev(kobj
->parent
);
1798 struct hid_device
*hdev
= to_hid_device(dev
);
1799 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1802 mode
= wacom
->led
.groups
[index
].select
;
1803 return sprintf(buf
, "%d\n", mode
< 3 ? mode
: -1);
1806 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1807 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1808 struct kobj_attribute *kattr, char *buf) \
1810 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1812 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1813 .attr = {.name = "remote_mode", \
1814 .mode = DEV_ATTR_RO_PERM}, \
1815 .show = wacom_show_remote##SET_ID##_mode, \
1817 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1818 &remote##SET_ID##_mode_attr.attr, \
1821 static struct attribute_group remote##SET_ID##_serial_group = { \
1823 .attrs = remote##SET_ID##_serial_attrs, \
1826 DEVICE_EKR_ATTR_GROUP(0);
1827 DEVICE_EKR_ATTR_GROUP(1);
1828 DEVICE_EKR_ATTR_GROUP(2);
1829 DEVICE_EKR_ATTR_GROUP(3);
1830 DEVICE_EKR_ATTR_GROUP(4);
1832 static int wacom_remote_create_attr_group(struct wacom
*wacom
, __u32 serial
,
1836 struct wacom_remote
*remote
= wacom
->remote
;
1838 remote
->remotes
[index
].group
.name
= devm_kasprintf(&wacom
->hdev
->dev
,
1841 if (!remote
->remotes
[index
].group
.name
)
1844 error
= __wacom_devm_sysfs_create_group(wacom
, remote
->remote_dir
,
1845 &remote
->remotes
[index
].group
);
1847 remote
->remotes
[index
].group
.name
= NULL
;
1848 hid_err(wacom
->hdev
,
1849 "cannot create sysfs group err: %d\n", error
);
1856 static int wacom_cmd_unpair_remote(struct wacom
*wacom
, unsigned char selector
)
1858 const size_t buf_size
= 2;
1862 buf
= kzalloc(buf_size
, GFP_KERNEL
);
1866 buf
[0] = WAC_CMD_DELETE_PAIRING
;
1869 retval
= wacom_set_report(wacom
->hdev
, HID_OUTPUT_REPORT
, buf
,
1870 buf_size
, WAC_CMD_RETRIES
);
1876 static ssize_t
wacom_store_unpair_remote(struct kobject
*kobj
,
1877 struct kobj_attribute
*attr
,
1878 const char *buf
, size_t count
)
1880 unsigned char selector
= 0;
1881 struct device
*dev
= kobj_to_dev(kobj
->parent
);
1882 struct hid_device
*hdev
= to_hid_device(dev
);
1883 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1886 if (!strncmp(buf
, "*\n", 2)) {
1887 selector
= WAC_CMD_UNPAIR_ALL
;
1889 hid_info(wacom
->hdev
, "remote: unrecognized unpair code: %s\n",
1894 mutex_lock(&wacom
->lock
);
1896 err
= wacom_cmd_unpair_remote(wacom
, selector
);
1897 mutex_unlock(&wacom
->lock
);
1899 return err
< 0 ? err
: count
;
1902 static struct kobj_attribute unpair_remote_attr
= {
1903 .attr
= {.name
= "unpair_remote", .mode
= 0200},
1904 .store
= wacom_store_unpair_remote
,
1907 static const struct attribute
*remote_unpair_attrs
[] = {
1908 &unpair_remote_attr
.attr
,
1912 static void wacom_remotes_destroy(void *data
)
1914 struct wacom
*wacom
= data
;
1915 struct wacom_remote
*remote
= wacom
->remote
;
1920 kobject_put(remote
->remote_dir
);
1921 kfifo_free(&remote
->remote_fifo
);
1922 wacom
->remote
= NULL
;
1925 static int wacom_initialize_remotes(struct wacom
*wacom
)
1928 struct wacom_remote
*remote
;
1931 if (wacom
->wacom_wac
.features
.type
!= REMOTE
)
1934 remote
= devm_kzalloc(&wacom
->hdev
->dev
, sizeof(*wacom
->remote
),
1939 wacom
->remote
= remote
;
1941 spin_lock_init(&remote
->remote_lock
);
1943 error
= kfifo_alloc(&remote
->remote_fifo
,
1944 5 * sizeof(struct wacom_remote_data
),
1947 hid_err(wacom
->hdev
, "failed allocating remote_fifo\n");
1951 remote
->remotes
[0].group
= remote0_serial_group
;
1952 remote
->remotes
[1].group
= remote1_serial_group
;
1953 remote
->remotes
[2].group
= remote2_serial_group
;
1954 remote
->remotes
[3].group
= remote3_serial_group
;
1955 remote
->remotes
[4].group
= remote4_serial_group
;
1957 remote
->remote_dir
= kobject_create_and_add("wacom_remote",
1958 &wacom
->hdev
->dev
.kobj
);
1959 if (!remote
->remote_dir
)
1962 error
= sysfs_create_files(remote
->remote_dir
, remote_unpair_attrs
);
1965 hid_err(wacom
->hdev
,
1966 "cannot create sysfs group err: %d\n", error
);
1970 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
1971 wacom
->led
.groups
[i
].select
= WACOM_STATUS_UNKNOWN
;
1972 remote
->remotes
[i
].serial
= 0;
1975 error
= devm_add_action_or_reset(&wacom
->hdev
->dev
,
1976 wacom_remotes_destroy
, wacom
);
1983 static struct input_dev
*wacom_allocate_input(struct wacom
*wacom
)
1985 struct input_dev
*input_dev
;
1986 struct hid_device
*hdev
= wacom
->hdev
;
1987 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1989 input_dev
= devm_input_allocate_device(&hdev
->dev
);
1993 input_dev
->name
= wacom_wac
->features
.name
;
1994 input_dev
->phys
= hdev
->phys
;
1995 input_dev
->dev
.parent
= &hdev
->dev
;
1996 input_dev
->open
= wacom_open
;
1997 input_dev
->close
= wacom_close
;
1998 input_dev
->uniq
= hdev
->uniq
;
1999 input_dev
->id
.bustype
= hdev
->bus
;
2000 input_dev
->id
.vendor
= hdev
->vendor
;
2001 input_dev
->id
.product
= wacom_wac
->pid
? wacom_wac
->pid
: hdev
->product
;
2002 input_dev
->id
.version
= hdev
->version
;
2003 input_set_drvdata(input_dev
, wacom
);
2008 static int wacom_allocate_inputs(struct wacom
*wacom
)
2010 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
2012 wacom_wac
->pen_input
= wacom_allocate_input(wacom
);
2013 wacom_wac
->touch_input
= wacom_allocate_input(wacom
);
2014 wacom_wac
->pad_input
= wacom_allocate_input(wacom
);
2015 if (!wacom_wac
->pen_input
||
2016 !wacom_wac
->touch_input
||
2017 !wacom_wac
->pad_input
)
2020 wacom_wac
->pen_input
->name
= wacom_wac
->pen_name
;
2021 wacom_wac
->touch_input
->name
= wacom_wac
->touch_name
;
2022 wacom_wac
->pad_input
->name
= wacom_wac
->pad_name
;
2027 static int wacom_register_inputs(struct wacom
*wacom
)
2029 struct input_dev
*pen_input_dev
, *touch_input_dev
, *pad_input_dev
;
2030 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
2033 pen_input_dev
= wacom_wac
->pen_input
;
2034 touch_input_dev
= wacom_wac
->touch_input
;
2035 pad_input_dev
= wacom_wac
->pad_input
;
2037 if (!pen_input_dev
|| !touch_input_dev
|| !pad_input_dev
)
2040 error
= wacom_setup_pen_input_capabilities(pen_input_dev
, wacom_wac
);
2042 /* no pen in use on this interface */
2043 input_free_device(pen_input_dev
);
2044 wacom_wac
->pen_input
= NULL
;
2045 pen_input_dev
= NULL
;
2047 error
= input_register_device(pen_input_dev
);
2052 error
= wacom_setup_touch_input_capabilities(touch_input_dev
, wacom_wac
);
2054 /* no touch in use on this interface */
2055 input_free_device(touch_input_dev
);
2056 wacom_wac
->touch_input
= NULL
;
2057 touch_input_dev
= NULL
;
2059 error
= input_register_device(touch_input_dev
);
2064 error
= wacom_setup_pad_input_capabilities(pad_input_dev
, wacom_wac
);
2066 /* no pad in use on this interface */
2067 input_free_device(pad_input_dev
);
2068 wacom_wac
->pad_input
= NULL
;
2069 pad_input_dev
= NULL
;
2071 error
= input_register_device(pad_input_dev
);
2079 wacom_wac
->pad_input
= NULL
;
2080 wacom_wac
->touch_input
= NULL
;
2081 wacom_wac
->pen_input
= NULL
;
2086 * Not all devices report physical dimensions from HID.
2087 * Compute the default from hardcoded logical dimension
2088 * and resolution before driver overwrites them.
2090 static void wacom_set_default_phy(struct wacom_features
*features
)
2092 if (features
->x_resolution
) {
2093 features
->x_phy
= (features
->x_max
* 100) /
2094 features
->x_resolution
;
2095 features
->y_phy
= (features
->y_max
* 100) /
2096 features
->y_resolution
;
2100 static void wacom_calculate_res(struct wacom_features
*features
)
2102 /* set unit to "100th of a mm" for devices not reported by HID */
2103 if (!features
->unit
) {
2104 features
->unit
= 0x11;
2105 features
->unitExpo
= -3;
2108 features
->x_resolution
= wacom_calc_hid_res(features
->x_max
,
2111 features
->unitExpo
);
2112 features
->y_resolution
= wacom_calc_hid_res(features
->y_max
,
2115 features
->unitExpo
);
2118 void wacom_battery_work(struct work_struct
*work
)
2120 struct wacom
*wacom
= container_of(work
, struct wacom
, battery_work
);
2122 if ((wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
) &&
2123 !wacom
->battery
.battery
) {
2124 wacom_initialize_battery(wacom
);
2126 else if (!(wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
) &&
2127 wacom
->battery
.battery
) {
2128 wacom_destroy_battery(wacom
);
2132 static size_t wacom_compute_pktlen(struct hid_device
*hdev
)
2134 struct hid_report_enum
*report_enum
;
2135 struct hid_report
*report
;
2138 report_enum
= hdev
->report_enum
+ HID_INPUT_REPORT
;
2140 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
2141 size_t report_size
= hid_report_len(report
);
2142 if (report_size
> size
)
2149 static void wacom_update_name(struct wacom
*wacom
, const char *suffix
)
2151 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2152 struct wacom_features
*features
= &wacom_wac
->features
;
2153 char name
[WACOM_NAME_MAX
- 20]; /* Leave some room for suffixes */
2155 /* Generic devices name unspecified */
2156 if ((features
->type
== HID_GENERIC
) && !strcmp("Wacom HID", features
->name
)) {
2157 char *product_name
= wacom
->hdev
->name
;
2159 if (hid_is_using_ll_driver(wacom
->hdev
, &usb_hid_driver
)) {
2160 struct usb_interface
*intf
= to_usb_interface(wacom
->hdev
->dev
.parent
);
2161 struct usb_device
*dev
= interface_to_usbdev(intf
);
2162 product_name
= dev
->product
;
2165 if (wacom
->hdev
->bus
== BUS_I2C
) {
2166 snprintf(name
, sizeof(name
), "%s %X",
2167 features
->name
, wacom
->hdev
->product
);
2168 } else if (strstr(product_name
, "Wacom") ||
2169 strstr(product_name
, "wacom") ||
2170 strstr(product_name
, "WACOM")) {
2171 strlcpy(name
, product_name
, sizeof(name
));
2173 snprintf(name
, sizeof(name
), "Wacom %s", product_name
);
2176 /* strip out excess whitespaces */
2178 char *gap
= strstr(name
, " ");
2181 /* shift everything including the terminator */
2182 memmove(gap
, gap
+1, strlen(gap
));
2185 /* get rid of trailing whitespace */
2186 if (name
[strlen(name
)-1] == ' ')
2187 name
[strlen(name
)-1] = '\0';
2189 strlcpy(name
, features
->name
, sizeof(name
));
2192 snprintf(wacom_wac
->name
, sizeof(wacom_wac
->name
), "%s%s",
2195 /* Append the device type to the name */
2196 snprintf(wacom_wac
->pen_name
, sizeof(wacom_wac
->pen_name
),
2197 "%s%s Pen", name
, suffix
);
2198 snprintf(wacom_wac
->touch_name
, sizeof(wacom_wac
->touch_name
),
2199 "%s%s Finger", name
, suffix
);
2200 snprintf(wacom_wac
->pad_name
, sizeof(wacom_wac
->pad_name
),
2201 "%s%s Pad", name
, suffix
);
2204 static void wacom_release_resources(struct wacom
*wacom
)
2206 struct hid_device
*hdev
= wacom
->hdev
;
2208 if (!wacom
->resources
)
2211 devres_release_group(&hdev
->dev
, wacom
);
2213 wacom
->resources
= false;
2215 wacom
->wacom_wac
.pen_input
= NULL
;
2216 wacom
->wacom_wac
.touch_input
= NULL
;
2217 wacom
->wacom_wac
.pad_input
= NULL
;
2220 static void wacom_set_shared_values(struct wacom_wac
*wacom_wac
)
2222 if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_TOUCH
) {
2223 wacom_wac
->shared
->type
= wacom_wac
->features
.type
;
2224 wacom_wac
->shared
->touch_input
= wacom_wac
->touch_input
;
2227 if (wacom_wac
->has_mute_touch_switch
) {
2228 wacom_wac
->shared
->has_mute_touch_switch
= true;
2229 wacom_wac
->shared
->is_touch_on
= true;
2232 if (wacom_wac
->shared
->has_mute_touch_switch
&&
2233 wacom_wac
->shared
->touch_input
) {
2234 set_bit(EV_SW
, wacom_wac
->shared
->touch_input
->evbit
);
2235 input_set_capability(wacom_wac
->shared
->touch_input
, EV_SW
,
2240 static int wacom_parse_and_register(struct wacom
*wacom
, bool wireless
)
2242 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2243 struct wacom_features
*features
= &wacom_wac
->features
;
2244 struct hid_device
*hdev
= wacom
->hdev
;
2246 unsigned int connect_mask
= HID_CONNECT_HIDRAW
;
2248 features
->pktlen
= wacom_compute_pktlen(hdev
);
2249 if (features
->pktlen
> WACOM_PKGLEN_MAX
)
2252 if (!devres_open_group(&hdev
->dev
, wacom
, GFP_KERNEL
))
2255 wacom
->resources
= true;
2257 error
= wacom_allocate_inputs(wacom
);
2262 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2263 * into debug mode for the touch part.
2264 * We ignore the other interfaces.
2266 if (features
->type
== BAMBOO_PAD
) {
2267 if (features
->pktlen
== WACOM_PKGLEN_PENABLED
) {
2268 features
->type
= HID_GENERIC
;
2269 } else if ((features
->pktlen
!= WACOM_PKGLEN_BPAD_TOUCH
) &&
2270 (features
->pktlen
!= WACOM_PKGLEN_BPAD_TOUCH_USB
)) {
2276 /* set the default size in case we do not get them from hid */
2277 wacom_set_default_phy(features
);
2279 /* Retrieve the physical and logical size for touch devices */
2280 wacom_retrieve_hid_descriptor(hdev
, features
);
2281 wacom_setup_device_quirks(wacom
);
2283 if (features
->device_type
== WACOM_DEVICETYPE_NONE
&&
2284 features
->type
!= WIRELESS
) {
2285 error
= features
->type
== HID_GENERIC
? -ENODEV
: 0;
2287 dev_warn(&hdev
->dev
, "Unknown device_type for '%s'. %s.",
2289 error
? "Ignoring" : "Assuming pen");
2294 features
->device_type
|= WACOM_DEVICETYPE_PEN
;
2297 wacom_calculate_res(features
);
2299 wacom_update_name(wacom
, wireless
? " (WL)" : "");
2301 /* pen only Bamboo neither support touch nor pad */
2302 if ((features
->type
== BAMBOO_PEN
) &&
2303 ((features
->device_type
& WACOM_DEVICETYPE_TOUCH
) ||
2304 (features
->device_type
& WACOM_DEVICETYPE_PAD
))) {
2309 error
= wacom_add_shared_data(hdev
);
2313 if (!(features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
) &&
2314 (features
->quirks
& WACOM_QUIRK_BATTERY
)) {
2315 error
= wacom_initialize_battery(wacom
);
2320 error
= wacom_register_inputs(wacom
);
2324 if (wacom
->wacom_wac
.features
.device_type
& WACOM_DEVICETYPE_PAD
) {
2325 error
= wacom_initialize_leds(wacom
);
2329 error
= wacom_initialize_remotes(wacom
);
2334 if (features
->type
== HID_GENERIC
)
2335 connect_mask
|= HID_CONNECT_DRIVER
;
2337 /* Regular HID work starts now */
2338 error
= hid_hw_start(hdev
, connect_mask
);
2340 hid_err(hdev
, "hw start failed\n");
2345 /* Note that if query fails it is not a hard failure */
2346 wacom_query_tablet_data(wacom
);
2349 /* touch only Bamboo doesn't support pen */
2350 if ((features
->type
== BAMBOO_TOUCH
) &&
2351 (features
->device_type
& WACOM_DEVICETYPE_PEN
)) {
2352 cancel_delayed_work_sync(&wacom
->init_work
);
2353 _wacom_query_tablet_data(wacom
);
2358 if (features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
)
2359 error
= hid_hw_open(hdev
);
2361 wacom_set_shared_values(wacom_wac
);
2362 devres_close_group(&hdev
->dev
, wacom
);
2369 wacom_release_resources(wacom
);
2373 static void wacom_wireless_work(struct work_struct
*work
)
2375 struct wacom
*wacom
= container_of(work
, struct wacom
, wireless_work
);
2376 struct usb_device
*usbdev
= wacom
->usbdev
;
2377 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2378 struct hid_device
*hdev1
, *hdev2
;
2379 struct wacom
*wacom1
, *wacom2
;
2380 struct wacom_wac
*wacom_wac1
, *wacom_wac2
;
2384 * Regardless if this is a disconnect or a new tablet,
2385 * remove any existing input and battery devices.
2388 wacom_destroy_battery(wacom
);
2390 /* Stylus interface */
2391 hdev1
= usb_get_intfdata(usbdev
->config
->interface
[1]);
2392 wacom1
= hid_get_drvdata(hdev1
);
2393 wacom_wac1
= &(wacom1
->wacom_wac
);
2394 wacom_release_resources(wacom1
);
2396 /* Touch interface */
2397 hdev2
= usb_get_intfdata(usbdev
->config
->interface
[2]);
2398 wacom2
= hid_get_drvdata(hdev2
);
2399 wacom_wac2
= &(wacom2
->wacom_wac
);
2400 wacom_release_resources(wacom2
);
2402 if (wacom_wac
->pid
== 0) {
2403 hid_info(wacom
->hdev
, "wireless tablet disconnected\n");
2405 const struct hid_device_id
*id
= wacom_ids
;
2407 hid_info(wacom
->hdev
, "wireless tablet connected with PID %x\n",
2411 if (id
->vendor
== USB_VENDOR_ID_WACOM
&&
2412 id
->product
== wacom_wac
->pid
)
2418 hid_info(wacom
->hdev
, "ignoring unknown PID.\n");
2422 /* Stylus interface */
2423 wacom_wac1
->features
=
2424 *((struct wacom_features
*)id
->driver_data
);
2426 wacom_wac1
->pid
= wacom_wac
->pid
;
2428 error
= wacom_parse_and_register(wacom1
, true);
2432 /* Touch interface */
2433 if (wacom_wac1
->features
.touch_max
||
2434 (wacom_wac1
->features
.type
>= INTUOSHT
&&
2435 wacom_wac1
->features
.type
<= BAMBOO_PT
)) {
2436 wacom_wac2
->features
=
2437 *((struct wacom_features
*)id
->driver_data
);
2438 wacom_wac2
->pid
= wacom_wac
->pid
;
2440 error
= wacom_parse_and_register(wacom2
, true);
2445 strlcpy(wacom_wac
->name
, wacom_wac1
->name
,
2446 sizeof(wacom_wac
->name
));
2447 error
= wacom_initialize_battery(wacom
);
2455 wacom_release_resources(wacom1
);
2456 wacom_release_resources(wacom2
);
2460 static void wacom_remote_destroy_one(struct wacom
*wacom
, unsigned int index
)
2462 struct wacom_remote
*remote
= wacom
->remote
;
2463 u32 serial
= remote
->remotes
[index
].serial
;
2465 unsigned long flags
;
2467 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
2468 if (remote
->remotes
[i
].serial
== serial
) {
2470 spin_lock_irqsave(&remote
->remote_lock
, flags
);
2471 remote
->remotes
[i
].registered
= false;
2472 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2474 if (remote
->remotes
[i
].battery
.battery
)
2475 devres_release_group(&wacom
->hdev
->dev
,
2476 &remote
->remotes
[i
].battery
.bat_desc
);
2478 if (remote
->remotes
[i
].group
.name
)
2479 devres_release_group(&wacom
->hdev
->dev
,
2480 &remote
->remotes
[i
]);
2482 remote
->remotes
[i
].serial
= 0;
2483 remote
->remotes
[i
].group
.name
= NULL
;
2484 remote
->remotes
[i
].battery
.battery
= NULL
;
2485 wacom
->led
.groups
[i
].select
= WACOM_STATUS_UNKNOWN
;
2490 static int wacom_remote_create_one(struct wacom
*wacom
, u32 serial
,
2493 struct wacom_remote
*remote
= wacom
->remote
;
2494 struct device
*dev
= &wacom
->hdev
->dev
;
2497 /* A remote can pair more than once with an EKR,
2498 * check to make sure this serial isn't already paired.
2500 for (k
= 0; k
< WACOM_MAX_REMOTES
; k
++) {
2501 if (remote
->remotes
[k
].serial
== serial
)
2505 if (k
< WACOM_MAX_REMOTES
) {
2506 remote
->remotes
[index
].serial
= serial
;
2510 if (!devres_open_group(dev
, &remote
->remotes
[index
], GFP_KERNEL
))
2513 error
= wacom_remote_create_attr_group(wacom
, serial
, index
);
2517 remote
->remotes
[index
].input
= wacom_allocate_input(wacom
);
2518 if (!remote
->remotes
[index
].input
) {
2522 remote
->remotes
[index
].input
->uniq
= remote
->remotes
[index
].group
.name
;
2523 remote
->remotes
[index
].input
->name
= wacom
->wacom_wac
.pad_name
;
2525 if (!remote
->remotes
[index
].input
->name
) {
2530 error
= wacom_setup_pad_input_capabilities(remote
->remotes
[index
].input
,
2535 remote
->remotes
[index
].serial
= serial
;
2537 error
= input_register_device(remote
->remotes
[index
].input
);
2541 error
= wacom_led_groups_alloc_and_register_one(
2542 &remote
->remotes
[index
].input
->dev
,
2543 wacom
, index
, 3, true);
2547 remote
->remotes
[index
].registered
= true;
2549 devres_close_group(dev
, &remote
->remotes
[index
]);
2553 devres_release_group(dev
, &remote
->remotes
[index
]);
2554 remote
->remotes
[index
].serial
= 0;
2558 static int wacom_remote_attach_battery(struct wacom
*wacom
, int index
)
2560 struct wacom_remote
*remote
= wacom
->remote
;
2563 if (!remote
->remotes
[index
].registered
)
2566 if (remote
->remotes
[index
].battery
.battery
)
2569 if (wacom
->led
.groups
[index
].select
== WACOM_STATUS_UNKNOWN
)
2572 error
= __wacom_initialize_battery(wacom
,
2573 &wacom
->remote
->remotes
[index
].battery
);
2580 static void wacom_remote_work(struct work_struct
*work
)
2582 struct wacom
*wacom
= container_of(work
, struct wacom
, remote_work
);
2583 struct wacom_remote
*remote
= wacom
->remote
;
2584 struct wacom_remote_data data
;
2585 unsigned long flags
;
2590 spin_lock_irqsave(&remote
->remote_lock
, flags
);
2592 count
= kfifo_out(&remote
->remote_fifo
, &data
, sizeof(data
));
2594 if (count
!= sizeof(data
)) {
2595 hid_err(wacom
->hdev
,
2596 "workitem triggered without status available\n");
2597 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2601 if (!kfifo_is_empty(&remote
->remote_fifo
))
2602 wacom_schedule_work(&wacom
->wacom_wac
, WACOM_WORKER_REMOTE
);
2604 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2606 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
2607 serial
= data
.remote
[i
].serial
;
2608 if (data
.remote
[i
].connected
) {
2610 if (remote
->remotes
[i
].serial
== serial
) {
2611 wacom_remote_attach_battery(wacom
, i
);
2615 if (remote
->remotes
[i
].serial
)
2616 wacom_remote_destroy_one(wacom
, i
);
2618 wacom_remote_create_one(wacom
, serial
, i
);
2620 } else if (remote
->remotes
[i
].serial
) {
2621 wacom_remote_destroy_one(wacom
, i
);
2626 static void wacom_mode_change_work(struct work_struct
*work
)
2628 struct wacom
*wacom
= container_of(work
, struct wacom
, mode_change_work
);
2629 struct wacom_shared
*shared
= wacom
->wacom_wac
.shared
;
2630 struct wacom
*wacom1
= NULL
;
2631 struct wacom
*wacom2
= NULL
;
2632 bool is_direct
= wacom
->wacom_wac
.is_direct_mode
;
2636 wacom1
= hid_get_drvdata(shared
->pen
);
2637 wacom_release_resources(wacom1
);
2638 hid_hw_stop(wacom1
->hdev
);
2639 wacom1
->wacom_wac
.has_mode_change
= true;
2640 wacom1
->wacom_wac
.is_direct_mode
= is_direct
;
2643 if (shared
->touch
) {
2644 wacom2
= hid_get_drvdata(shared
->touch
);
2645 wacom_release_resources(wacom2
);
2646 hid_hw_stop(wacom2
->hdev
);
2647 wacom2
->wacom_wac
.has_mode_change
= true;
2648 wacom2
->wacom_wac
.is_direct_mode
= is_direct
;
2652 error
= wacom_parse_and_register(wacom1
, false);
2658 error
= wacom_parse_and_register(wacom2
, false);
2666 static int wacom_probe(struct hid_device
*hdev
,
2667 const struct hid_device_id
*id
)
2669 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
2670 struct usb_device
*dev
= interface_to_usbdev(intf
);
2671 struct wacom
*wacom
;
2672 struct wacom_wac
*wacom_wac
;
2673 struct wacom_features
*features
;
2676 if (!id
->driver_data
)
2679 hdev
->quirks
|= HID_QUIRK_NO_INIT_REPORTS
;
2681 /* hid-core sets this quirk for the boot interface */
2682 hdev
->quirks
&= ~HID_QUIRK_NOGET
;
2684 wacom
= devm_kzalloc(&hdev
->dev
, sizeof(struct wacom
), GFP_KERNEL
);
2688 hid_set_drvdata(hdev
, wacom
);
2691 wacom_wac
= &wacom
->wacom_wac
;
2692 wacom_wac
->features
= *((struct wacom_features
*)id
->driver_data
);
2693 features
= &wacom_wac
->features
;
2695 if (features
->check_for_hid_type
&& features
->hid_type
!= hdev
->type
) {
2700 error
= kfifo_alloc(&wacom_wac
->pen_fifo
, WACOM_PKGLEN_MAX
, GFP_KERNEL
);
2704 wacom_wac
->hid_data
.inputmode
= -1;
2705 wacom_wac
->mode_report
= -1;
2707 wacom
->usbdev
= dev
;
2709 mutex_init(&wacom
->lock
);
2710 INIT_DELAYED_WORK(&wacom
->init_work
, wacom_init_work
);
2711 INIT_WORK(&wacom
->wireless_work
, wacom_wireless_work
);
2712 INIT_WORK(&wacom
->battery_work
, wacom_battery_work
);
2713 INIT_WORK(&wacom
->remote_work
, wacom_remote_work
);
2714 INIT_WORK(&wacom
->mode_change_work
, wacom_mode_change_work
);
2716 /* ask for the report descriptor to be loaded by HID */
2717 error
= hid_parse(hdev
);
2719 hid_err(hdev
, "parse failed\n");
2723 error
= wacom_parse_and_register(wacom
, false);
2727 if (hdev
->bus
== BUS_BLUETOOTH
) {
2728 error
= device_create_file(&hdev
->dev
, &dev_attr_speed
);
2731 "can't create sysfs speed attribute err: %d\n",
2738 hid_set_drvdata(hdev
, NULL
);
2742 static void wacom_remove(struct hid_device
*hdev
)
2744 struct wacom
*wacom
= hid_get_drvdata(hdev
);
2745 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2746 struct wacom_features
*features
= &wacom_wac
->features
;
2748 if (features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
)
2753 cancel_delayed_work_sync(&wacom
->init_work
);
2754 cancel_work_sync(&wacom
->wireless_work
);
2755 cancel_work_sync(&wacom
->battery_work
);
2756 cancel_work_sync(&wacom
->remote_work
);
2757 cancel_work_sync(&wacom
->mode_change_work
);
2758 if (hdev
->bus
== BUS_BLUETOOTH
)
2759 device_remove_file(&hdev
->dev
, &dev_attr_speed
);
2761 /* make sure we don't trigger the LEDs */
2762 wacom_led_groups_release(wacom
);
2764 if (wacom
->wacom_wac
.features
.type
!= REMOTE
)
2765 wacom_release_resources(wacom
);
2767 kfifo_free(&wacom_wac
->pen_fifo
);
2769 hid_set_drvdata(hdev
, NULL
);
2773 static int wacom_resume(struct hid_device
*hdev
)
2775 struct wacom
*wacom
= hid_get_drvdata(hdev
);
2777 mutex_lock(&wacom
->lock
);
2779 /* switch to wacom mode first */
2780 _wacom_query_tablet_data(wacom
);
2781 wacom_led_control(wacom
);
2783 mutex_unlock(&wacom
->lock
);
2788 static int wacom_reset_resume(struct hid_device
*hdev
)
2790 return wacom_resume(hdev
);
2792 #endif /* CONFIG_PM */
2794 static struct hid_driver wacom_driver
= {
2796 .id_table
= wacom_ids
,
2797 .probe
= wacom_probe
,
2798 .remove
= wacom_remove
,
2799 .report
= wacom_wac_report
,
2801 .resume
= wacom_resume
,
2802 .reset_resume
= wacom_reset_resume
,
2804 .raw_event
= wacom_raw_event
,
2806 module_hid_driver(wacom_driver
);
2808 MODULE_VERSION(DRIVER_VERSION
);
2809 MODULE_AUTHOR(DRIVER_AUTHOR
);
2810 MODULE_DESCRIPTION(DRIVER_DESC
);
2811 MODULE_LICENSE("GPL");