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 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
, raw_data
, size
);
157 return insert
&& !flush
;
160 static int wacom_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
161 u8
*raw_data
, int size
)
163 struct wacom
*wacom
= hid_get_drvdata(hdev
);
165 if (size
> WACOM_PKGLEN_MAX
)
168 if (wacom_wac_pen_serial_enforce(hdev
, report
, raw_data
, size
))
171 memcpy(wacom
->wacom_wac
.data
, raw_data
, size
);
173 wacom_wac_irq(&wacom
->wacom_wac
, size
);
178 static int wacom_open(struct input_dev
*dev
)
180 struct wacom
*wacom
= input_get_drvdata(dev
);
182 return hid_hw_open(wacom
->hdev
);
185 static void wacom_close(struct input_dev
*dev
)
187 struct wacom
*wacom
= input_get_drvdata(dev
);
190 * wacom->hdev should never be null, but surprisingly, I had the case
191 * once while unplugging the Wacom Wireless Receiver.
194 hid_hw_close(wacom
->hdev
);
198 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
200 static int wacom_calc_hid_res(int logical_extents
, int physical_extents
,
201 unsigned unit
, int exponent
)
203 struct hid_field field
= {
204 .logical_maximum
= logical_extents
,
205 .physical_maximum
= physical_extents
,
207 .unit_exponent
= exponent
,
210 return hidinput_calc_abs_res(&field
, ABS_X
);
213 static void wacom_hid_usage_quirk(struct hid_device
*hdev
,
214 struct hid_field
*field
, struct hid_usage
*usage
)
216 struct wacom
*wacom
= hid_get_drvdata(hdev
);
217 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
218 unsigned int equivalent_usage
= wacom_equivalent_usage(usage
->hid
);
221 * The Dell Canvas 27 needs to be switched to its vendor-defined
222 * report to provide the best resolution.
224 if (hdev
->vendor
== USB_VENDOR_ID_WACOM
&&
225 hdev
->product
== 0x4200 &&
226 field
->application
== HID_UP_MSVENDOR
) {
227 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
228 wacom
->wacom_wac
.mode_value
= 2;
232 * ISDv4 devices which predate HID's adoption of the
233 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
234 * position instead. We can accurately detect if a
235 * usage with that value should be HID_DG_BARRELSWITCH2
236 * based on the surrounding usages, which have remained
237 * constant across generations.
239 if (features
->type
== HID_GENERIC
&&
240 usage
->hid
== 0x000D0000 &&
241 field
->application
== HID_DG_PEN
&&
242 field
->physical
== HID_DG_STYLUS
) {
243 int i
= usage
->usage_index
;
245 if (i
-4 >= 0 && i
+1 < field
->maxusage
&&
246 field
->usage
[i
-4].hid
== HID_DG_TIPSWITCH
&&
247 field
->usage
[i
-3].hid
== HID_DG_BARRELSWITCH
&&
248 field
->usage
[i
-2].hid
== HID_DG_ERASER
&&
249 field
->usage
[i
-1].hid
== HID_DG_INVERT
&&
250 field
->usage
[i
+1].hid
== HID_DG_INRANGE
) {
251 usage
->hid
= HID_DG_BARRELSWITCH2
;
255 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
256 if (hdev
->vendor
== USB_VENDOR_ID_WACOM
&&
257 hdev
->product
== 0x0358 &&
258 WACOM_PEN_FIELD(field
) &&
259 equivalent_usage
== HID_GD_Y
) {
260 field
->logical_maximum
= 43200;
264 static void wacom_feature_mapping(struct hid_device
*hdev
,
265 struct hid_field
*field
, struct hid_usage
*usage
)
267 struct wacom
*wacom
= hid_get_drvdata(hdev
);
268 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
269 struct hid_data
*hid_data
= &wacom
->wacom_wac
.hid_data
;
270 unsigned int equivalent_usage
= wacom_equivalent_usage(usage
->hid
);
275 wacom_hid_usage_quirk(hdev
, field
, usage
);
277 switch (equivalent_usage
) {
278 case HID_DG_CONTACTMAX
:
279 /* leave touch_max as is if predefined */
280 if (!features
->touch_max
) {
282 data
= kzalloc(2, GFP_KERNEL
);
285 data
[0] = field
->report
->id
;
286 ret
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
287 data
, 2, WAC_CMD_RETRIES
);
289 features
->touch_max
= data
[1];
291 features
->touch_max
= 16;
292 hid_warn(hdev
, "wacom_feature_mapping: "
293 "could not get HID_DG_CONTACTMAX, "
294 "defaulting to %d\n",
295 features
->touch_max
);
300 case HID_DG_INPUTMODE
:
301 /* Ignore if value index is out of bounds. */
302 if (usage
->usage_index
>= field
->report_count
) {
303 dev_err(&hdev
->dev
, "HID_DG_INPUTMODE out of range\n");
307 hid_data
->inputmode
= field
->report
->id
;
308 hid_data
->inputmode_index
= usage
->usage_index
;
311 case HID_UP_DIGITIZER
:
312 if (field
->report
->id
== 0x0B &&
313 (field
->application
== WACOM_HID_G9_PEN
||
314 field
->application
== WACOM_HID_G11_PEN
)) {
315 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
316 wacom
->wacom_wac
.mode_value
= 0;
320 case WACOM_HID_WD_DATAMODE
:
321 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
322 wacom
->wacom_wac
.mode_value
= 2;
325 case WACOM_HID_UP_G9
:
326 case WACOM_HID_UP_G11
:
327 if (field
->report
->id
== 0x03 &&
328 (field
->application
== WACOM_HID_G9_TOUCHSCREEN
||
329 field
->application
== WACOM_HID_G11_TOUCHSCREEN
)) {
330 wacom
->wacom_wac
.mode_report
= field
->report
->id
;
331 wacom
->wacom_wac
.mode_value
= 0;
334 case WACOM_HID_WD_OFFSETLEFT
:
335 case WACOM_HID_WD_OFFSETTOP
:
336 case WACOM_HID_WD_OFFSETRIGHT
:
337 case WACOM_HID_WD_OFFSETBOTTOM
:
339 n
= hid_report_len(field
->report
);
340 data
= hid_alloc_report_buf(field
->report
, GFP_KERNEL
);
343 data
[0] = field
->report
->id
;
344 ret
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
345 data
, n
, WAC_CMD_RETRIES
);
347 ret
= hid_report_raw_event(hdev
, HID_FEATURE_REPORT
,
350 hid_warn(hdev
, "%s: could not retrieve sensor offsets\n",
359 * Interface Descriptor of wacom devices can be incomplete and
360 * inconsistent so wacom_features table is used to store stylus
361 * device's packet lengths, various maximum values, and tablet
362 * resolution based on product ID's.
364 * For devices that contain 2 interfaces, wacom_features table is
365 * inaccurate for the touch interface. Since the Interface Descriptor
366 * for touch interfaces has pretty complete data, this function exists
367 * to query tablet for this missing information instead of hard coding in
368 * an additional table.
370 * A typical Interface Descriptor for a stylus will contain a
371 * boot mouse application collection that is not of interest and this
372 * function will ignore it.
374 * It also contains a digitizer application collection that also is not
375 * of interest since any information it contains would be duplicate
376 * of what is in wacom_features. Usually it defines a report of an array
377 * of bytes that could be used as max length of the stylus packet returned.
378 * If it happens to define a Digitizer-Stylus Physical Collection then
379 * the X and Y logical values contain valid data but it is ignored.
381 * A typical Interface Descriptor for a touch interface will contain a
382 * Digitizer-Finger Physical Collection which will define both logical
383 * X/Y maximum as well as the physical size of tablet. Since touch
384 * interfaces haven't supported pressure or distance, this is enough
385 * information to override invalid values in the wacom_features table.
387 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
388 * data. We deal with them after returning from this function.
390 static void wacom_usage_mapping(struct hid_device
*hdev
,
391 struct hid_field
*field
, struct hid_usage
*usage
)
393 struct wacom
*wacom
= hid_get_drvdata(hdev
);
394 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
395 bool finger
= WACOM_FINGER_FIELD(field
);
396 bool pen
= WACOM_PEN_FIELD(field
);
397 unsigned equivalent_usage
= wacom_equivalent_usage(usage
->hid
);
400 * Requiring Stylus Usage will ignore boot mouse
401 * X/Y values and some cases of invalid Digitizer X/Y
402 * values commonly reported.
405 features
->device_type
|= WACOM_DEVICETYPE_PEN
;
407 features
->device_type
|= WACOM_DEVICETYPE_TOUCH
;
411 wacom_hid_usage_quirk(hdev
, field
, usage
);
413 switch (equivalent_usage
) {
415 features
->x_max
= field
->logical_maximum
;
417 features
->x_phy
= field
->physical_maximum
;
418 if ((features
->type
!= BAMBOO_PT
) &&
419 (features
->type
!= BAMBOO_TOUCH
)) {
420 features
->unit
= field
->unit
;
421 features
->unitExpo
= field
->unit_exponent
;
426 features
->y_max
= field
->logical_maximum
;
428 features
->y_phy
= field
->physical_maximum
;
429 if ((features
->type
!= BAMBOO_PT
) &&
430 (features
->type
!= BAMBOO_TOUCH
)) {
431 features
->unit
= field
->unit
;
432 features
->unitExpo
= field
->unit_exponent
;
436 case HID_DG_TIPPRESSURE
:
438 features
->pressure_max
= field
->logical_maximum
;
442 if (features
->type
== HID_GENERIC
)
443 wacom_wac_usage_mapping(hdev
, field
, usage
);
446 static void wacom_post_parse_hid(struct hid_device
*hdev
,
447 struct wacom_features
*features
)
449 struct wacom
*wacom
= hid_get_drvdata(hdev
);
450 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
452 if (features
->type
== HID_GENERIC
) {
453 /* Any last-minute generic device setup */
454 if (wacom_wac
->has_mode_change
) {
455 if (wacom_wac
->is_direct_mode
)
456 features
->device_type
|= WACOM_DEVICETYPE_DIRECT
;
458 features
->device_type
&= ~WACOM_DEVICETYPE_DIRECT
;
461 if (features
->touch_max
> 1) {
462 if (features
->device_type
& WACOM_DEVICETYPE_DIRECT
)
463 input_mt_init_slots(wacom_wac
->touch_input
,
464 wacom_wac
->features
.touch_max
,
467 input_mt_init_slots(wacom_wac
->touch_input
,
468 wacom_wac
->features
.touch_max
,
474 static void wacom_parse_hid(struct hid_device
*hdev
,
475 struct wacom_features
*features
)
477 struct hid_report_enum
*rep_enum
;
478 struct hid_report
*hreport
;
481 /* check features first */
482 rep_enum
= &hdev
->report_enum
[HID_FEATURE_REPORT
];
483 list_for_each_entry(hreport
, &rep_enum
->report_list
, list
) {
484 for (i
= 0; i
< hreport
->maxfield
; i
++) {
485 /* Ignore if report count is out of bounds. */
486 if (hreport
->field
[i
]->report_count
< 1)
489 for (j
= 0; j
< hreport
->field
[i
]->maxusage
; j
++) {
490 wacom_feature_mapping(hdev
, hreport
->field
[i
],
491 hreport
->field
[i
]->usage
+ j
);
496 /* now check the input usages */
497 rep_enum
= &hdev
->report_enum
[HID_INPUT_REPORT
];
498 list_for_each_entry(hreport
, &rep_enum
->report_list
, list
) {
500 if (!hreport
->maxfield
)
503 for (i
= 0; i
< hreport
->maxfield
; i
++)
504 for (j
= 0; j
< hreport
->field
[i
]->maxusage
; j
++)
505 wacom_usage_mapping(hdev
, hreport
->field
[i
],
506 hreport
->field
[i
]->usage
+ j
);
509 wacom_post_parse_hid(hdev
, features
);
512 static int wacom_hid_set_device_mode(struct hid_device
*hdev
)
514 struct wacom
*wacom
= hid_get_drvdata(hdev
);
515 struct hid_data
*hid_data
= &wacom
->wacom_wac
.hid_data
;
516 struct hid_report
*r
;
517 struct hid_report_enum
*re
;
519 if (hid_data
->inputmode
< 0)
522 re
= &(hdev
->report_enum
[HID_FEATURE_REPORT
]);
523 r
= re
->report_id_hash
[hid_data
->inputmode
];
525 r
->field
[0]->value
[hid_data
->inputmode_index
] = 2;
526 hid_hw_request(hdev
, r
, HID_REQ_SET_REPORT
);
531 static int wacom_set_device_mode(struct hid_device
*hdev
,
532 struct wacom_wac
*wacom_wac
)
535 struct hid_report
*r
;
536 struct hid_report_enum
*re
;
538 int error
= -ENOMEM
, limit
= 0;
540 if (wacom_wac
->mode_report
< 0)
543 re
= &(hdev
->report_enum
[HID_FEATURE_REPORT
]);
544 r
= re
->report_id_hash
[wacom_wac
->mode_report
];
548 rep_data
= hid_alloc_report_buf(r
, GFP_KERNEL
);
552 length
= hid_report_len(r
);
555 rep_data
[0] = wacom_wac
->mode_report
;
556 rep_data
[1] = wacom_wac
->mode_value
;
558 error
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
,
561 error
= wacom_get_report(hdev
, HID_FEATURE_REPORT
,
562 rep_data
, length
, 1);
563 } while (error
>= 0 &&
564 rep_data
[1] != wacom_wac
->mode_report
&&
565 limit
++ < WAC_MSG_RETRIES
);
569 return error
< 0 ? error
: 0;
572 static int wacom_bt_query_tablet_data(struct hid_device
*hdev
, u8 speed
,
573 struct wacom_features
*features
)
575 struct wacom
*wacom
= hid_get_drvdata(hdev
);
579 switch (features
->type
) {
583 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
, 2,
587 rep_data
[0] = speed
== 0 ? 0x05 : 0x06;
590 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
,
594 wacom
->wacom_wac
.bt_high_speed
= speed
;
600 * Note that if the raw queries fail, it's not a hard failure
601 * and it is safe to continue
603 hid_warn(hdev
, "failed to poke device, command %d, err %d\n",
608 wacom
->wacom_wac
.bt_features
&= ~0x20;
610 wacom
->wacom_wac
.bt_features
|= 0x20;
613 rep_data
[1] = wacom
->wacom_wac
.bt_features
;
615 ret
= wacom_set_report(hdev
, HID_FEATURE_REPORT
, rep_data
, 2,
618 wacom
->wacom_wac
.bt_high_speed
= speed
;
626 * Switch the tablet into its most-capable mode. Wacom tablets are
627 * typically configured to power-up in a mode which sends mouse-like
628 * reports to the OS. To get absolute position, pressure data, etc.
629 * from the tablet, it is necessary to switch the tablet out of this
630 * mode and into one which sends the full range of tablet data.
632 static int _wacom_query_tablet_data(struct wacom
*wacom
)
634 struct hid_device
*hdev
= wacom
->hdev
;
635 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
636 struct wacom_features
*features
= &wacom_wac
->features
;
638 if (hdev
->bus
== BUS_BLUETOOTH
)
639 return wacom_bt_query_tablet_data(hdev
, 1, features
);
641 if (features
->type
!= HID_GENERIC
) {
642 if (features
->device_type
& WACOM_DEVICETYPE_TOUCH
) {
643 if (features
->type
> TABLETPC
) {
644 /* MT Tablet PC touch */
645 wacom_wac
->mode_report
= 3;
646 wacom_wac
->mode_value
= 4;
647 } else if (features
->type
== WACOM_24HDT
) {
648 wacom_wac
->mode_report
= 18;
649 wacom_wac
->mode_value
= 2;
650 } else if (features
->type
== WACOM_27QHDT
) {
651 wacom_wac
->mode_report
= 131;
652 wacom_wac
->mode_value
= 2;
653 } else if (features
->type
== BAMBOO_PAD
) {
654 wacom_wac
->mode_report
= 2;
655 wacom_wac
->mode_value
= 2;
657 } else if (features
->device_type
& WACOM_DEVICETYPE_PEN
) {
658 if (features
->type
<= BAMBOO_PT
) {
659 wacom_wac
->mode_report
= 2;
660 wacom_wac
->mode_value
= 2;
665 wacom_set_device_mode(hdev
, wacom_wac
);
667 if (features
->type
== HID_GENERIC
)
668 return wacom_hid_set_device_mode(hdev
);
673 static void wacom_retrieve_hid_descriptor(struct hid_device
*hdev
,
674 struct wacom_features
*features
)
676 struct wacom
*wacom
= hid_get_drvdata(hdev
);
677 struct usb_interface
*intf
= wacom
->intf
;
679 /* default features */
680 features
->x_fuzz
= 4;
681 features
->y_fuzz
= 4;
682 features
->pressure_fuzz
= 0;
683 features
->distance_fuzz
= 1;
684 features
->tilt_fuzz
= 1;
687 * The wireless device HID is basic and layout conflicts with
688 * other tablets (monitor and touch interface can look like pen).
689 * Skip the query for this type and modify defaults based on
692 if (features
->type
== WIRELESS
) {
693 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 0)
694 features
->device_type
= WACOM_DEVICETYPE_WL_MONITOR
;
696 features
->device_type
= WACOM_DEVICETYPE_NONE
;
700 wacom_parse_hid(hdev
, features
);
703 struct wacom_hdev_data
{
704 struct list_head list
;
706 struct hid_device
*dev
;
707 struct wacom_shared shared
;
710 static LIST_HEAD(wacom_udev_list
);
711 static DEFINE_MUTEX(wacom_udev_list_lock
);
713 static bool wacom_are_sibling(struct hid_device
*hdev
,
714 struct hid_device
*sibling
)
716 struct wacom
*wacom
= hid_get_drvdata(hdev
);
717 struct wacom_features
*features
= &wacom
->wacom_wac
.features
;
718 struct wacom
*sibling_wacom
= hid_get_drvdata(sibling
);
719 struct wacom_features
*sibling_features
= &sibling_wacom
->wacom_wac
.features
;
720 __u32 oVid
= features
->oVid
? features
->oVid
: hdev
->vendor
;
721 __u32 oPid
= features
->oPid
? features
->oPid
: hdev
->product
;
723 /* The defined oVid/oPid must match that of the sibling */
724 if (features
->oVid
!= HID_ANY_ID
&& sibling
->vendor
!= oVid
)
726 if (features
->oPid
!= HID_ANY_ID
&& sibling
->product
!= oPid
)
730 * Devices with the same VID/PID must share the same physical
731 * device path, while those with different VID/PID must share
732 * the same physical parent device path.
734 if (hdev
->vendor
== sibling
->vendor
&& hdev
->product
== sibling
->product
) {
735 if (!hid_compare_device_paths(hdev
, sibling
, '/'))
738 if (!hid_compare_device_paths(hdev
, sibling
, '.'))
742 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
743 if (features
->type
!= HID_GENERIC
)
747 * Direct-input devices may not be siblings of indirect-input
750 if ((features
->device_type
& WACOM_DEVICETYPE_DIRECT
) &&
751 !(sibling_features
->device_type
& WACOM_DEVICETYPE_DIRECT
))
755 * Indirect-input devices may not be siblings of direct-input
758 if (!(features
->device_type
& WACOM_DEVICETYPE_DIRECT
) &&
759 (sibling_features
->device_type
& WACOM_DEVICETYPE_DIRECT
))
762 /* Pen devices may only be siblings of touch devices */
763 if ((features
->device_type
& WACOM_DEVICETYPE_PEN
) &&
764 !(sibling_features
->device_type
& WACOM_DEVICETYPE_TOUCH
))
767 /* Touch devices may only be siblings of pen devices */
768 if ((features
->device_type
& WACOM_DEVICETYPE_TOUCH
) &&
769 !(sibling_features
->device_type
& WACOM_DEVICETYPE_PEN
))
773 * No reason could be found for these two devices to NOT be
774 * siblings, so there's a good chance they ARE siblings
779 static struct wacom_hdev_data
*wacom_get_hdev_data(struct hid_device
*hdev
)
781 struct wacom_hdev_data
*data
;
783 /* Try to find an already-probed interface from the same device */
784 list_for_each_entry(data
, &wacom_udev_list
, list
) {
785 if (hid_compare_device_paths(hdev
, data
->dev
, '/')) {
786 kref_get(&data
->kref
);
791 /* Fallback to finding devices that appear to be "siblings" */
792 list_for_each_entry(data
, &wacom_udev_list
, list
) {
793 if (wacom_are_sibling(hdev
, data
->dev
)) {
794 kref_get(&data
->kref
);
802 static void wacom_release_shared_data(struct kref
*kref
)
804 struct wacom_hdev_data
*data
=
805 container_of(kref
, struct wacom_hdev_data
, kref
);
807 mutex_lock(&wacom_udev_list_lock
);
808 list_del(&data
->list
);
809 mutex_unlock(&wacom_udev_list_lock
);
814 static void wacom_remove_shared_data(void *res
)
816 struct wacom
*wacom
= res
;
817 struct wacom_hdev_data
*data
;
818 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
820 if (wacom_wac
->shared
) {
821 data
= container_of(wacom_wac
->shared
, struct wacom_hdev_data
,
824 if (wacom_wac
->shared
->touch
== wacom
->hdev
)
825 wacom_wac
->shared
->touch
= NULL
;
826 else if (wacom_wac
->shared
->pen
== wacom
->hdev
)
827 wacom_wac
->shared
->pen
= NULL
;
829 kref_put(&data
->kref
, wacom_release_shared_data
);
830 wacom_wac
->shared
= NULL
;
834 static int wacom_add_shared_data(struct hid_device
*hdev
)
836 struct wacom
*wacom
= hid_get_drvdata(hdev
);
837 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
838 struct wacom_hdev_data
*data
;
841 mutex_lock(&wacom_udev_list_lock
);
843 data
= wacom_get_hdev_data(hdev
);
845 data
= kzalloc(sizeof(struct wacom_hdev_data
), GFP_KERNEL
);
851 kref_init(&data
->kref
);
853 list_add_tail(&data
->list
, &wacom_udev_list
);
856 wacom_wac
->shared
= &data
->shared
;
858 retval
= devm_add_action(&hdev
->dev
, wacom_remove_shared_data
, wacom
);
860 mutex_unlock(&wacom_udev_list_lock
);
861 wacom_remove_shared_data(wacom
);
865 if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_TOUCH
)
866 wacom_wac
->shared
->touch
= hdev
;
867 else if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_PEN
)
868 wacom_wac
->shared
->pen
= hdev
;
871 mutex_unlock(&wacom_udev_list_lock
);
875 static int wacom_led_control(struct wacom
*wacom
)
879 unsigned char report_id
= WAC_CMD_LED_CONTROL
;
882 if (!wacom
->led
.groups
)
885 if (wacom
->wacom_wac
.features
.type
== REMOTE
)
888 if (wacom
->wacom_wac
.pid
) { /* wireless connected */
889 report_id
= WAC_CMD_WL_LED_CONTROL
;
892 else if (wacom
->wacom_wac
.features
.type
== INTUOSP2_BT
) {
893 report_id
= WAC_CMD_WL_INTUOSP2
;
896 buf
= kzalloc(buf_size
, GFP_KERNEL
);
900 if (wacom
->wacom_wac
.features
.type
== HID_GENERIC
) {
901 buf
[0] = WAC_CMD_LED_CONTROL_GENERIC
;
902 buf
[1] = wacom
->led
.llv
;
903 buf
[2] = wacom
->led
.groups
[0].select
& 0x03;
905 } else if ((wacom
->wacom_wac
.features
.type
>= INTUOS5S
&&
906 wacom
->wacom_wac
.features
.type
<= INTUOSPL
)) {
908 * Touch Ring and crop mark LED luminance may take on
909 * one of four values:
910 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
912 int ring_led
= wacom
->led
.groups
[0].select
& 0x03;
913 int ring_lum
= (((wacom
->led
.llv
& 0x60) >> 5) - 1) & 0x03;
915 unsigned char led_bits
= (crop_lum
<< 4) | (ring_lum
<< 2) | (ring_led
);
918 if (wacom
->wacom_wac
.pid
) {
919 wacom_get_report(wacom
->hdev
, HID_FEATURE_REPORT
,
920 buf
, buf_size
, WAC_CMD_RETRIES
);
926 else if (wacom
->wacom_wac
.features
.type
== INTUOSP2_BT
) {
928 buf
[4] = 100; // Power Connection LED (ORANGE)
929 buf
[5] = 100; // BT Connection LED (BLUE)
930 buf
[6] = 100; // Paper Mode (RED?)
931 buf
[7] = 100; // Paper Mode (GREEN?)
932 buf
[8] = 100; // Paper Mode (BLUE?)
933 buf
[9] = wacom
->led
.llv
;
934 buf
[10] = wacom
->led
.groups
[0].select
& 0x03;
937 int led
= wacom
->led
.groups
[0].select
| 0x4;
939 if (wacom
->wacom_wac
.features
.type
== WACOM_21UX2
||
940 wacom
->wacom_wac
.features
.type
== WACOM_24HD
)
941 led
|= (wacom
->led
.groups
[1].select
<< 4) | 0x40;
945 buf
[2] = wacom
->led
.llv
;
946 buf
[3] = wacom
->led
.hlv
;
947 buf
[4] = wacom
->led
.img_lum
;
950 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, buf_size
,
957 static int wacom_led_putimage(struct wacom
*wacom
, int button_id
, u8 xfer_id
,
958 const unsigned len
, const void *img
)
962 const unsigned chunk_len
= len
/ 4; /* 4 chunks are needed to be sent */
964 buf
= kzalloc(chunk_len
+ 3 , GFP_KERNEL
);
968 /* Send 'start' command */
969 buf
[0] = WAC_CMD_ICON_START
;
971 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, 2,
977 buf
[1] = button_id
& 0x07;
978 for (i
= 0; i
< 4; i
++) {
980 memcpy(buf
+ 3, img
+ i
* chunk_len
, chunk_len
);
982 retval
= wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
,
983 buf
, chunk_len
+ 3, WAC_CMD_RETRIES
);
989 buf
[0] = WAC_CMD_ICON_START
;
991 wacom_set_report(wacom
->hdev
, HID_FEATURE_REPORT
, buf
, 2,
999 static ssize_t
wacom_led_select_store(struct device
*dev
, int set_id
,
1000 const char *buf
, size_t count
)
1002 struct hid_device
*hdev
= to_hid_device(dev
);
1003 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1007 err
= kstrtouint(buf
, 10, &id
);
1011 mutex_lock(&wacom
->lock
);
1013 wacom
->led
.groups
[set_id
].select
= id
& 0x3;
1014 err
= wacom_led_control(wacom
);
1016 mutex_unlock(&wacom
->lock
);
1018 return err
< 0 ? err
: count
;
1021 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
1022 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
1023 struct device_attribute *attr, const char *buf, size_t count) \
1025 return wacom_led_select_store(dev, SET_ID, buf, count); \
1027 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
1028 struct device_attribute *attr, char *buf) \
1030 struct hid_device *hdev = to_hid_device(dev);\
1031 struct wacom *wacom = hid_get_drvdata(hdev); \
1032 return scnprintf(buf, PAGE_SIZE, "%d\n", \
1033 wacom->led.groups[SET_ID].select); \
1035 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
1036 wacom_led##SET_ID##_select_show, \
1037 wacom_led##SET_ID##_select_store)
1039 DEVICE_LED_SELECT_ATTR(0);
1040 DEVICE_LED_SELECT_ATTR(1);
1042 static ssize_t
wacom_luminance_store(struct wacom
*wacom
, u8
*dest
,
1043 const char *buf
, size_t count
)
1048 err
= kstrtouint(buf
, 10, &value
);
1052 mutex_lock(&wacom
->lock
);
1054 *dest
= value
& 0x7f;
1055 err
= wacom_led_control(wacom
);
1057 mutex_unlock(&wacom
->lock
);
1059 return err
< 0 ? err
: count
;
1062 #define DEVICE_LUMINANCE_ATTR(name, field) \
1063 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
1064 struct device_attribute *attr, const char *buf, size_t count) \
1066 struct hid_device *hdev = to_hid_device(dev);\
1067 struct wacom *wacom = hid_get_drvdata(hdev); \
1069 return wacom_luminance_store(wacom, &wacom->led.field, \
1072 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
1073 struct device_attribute *attr, char *buf) \
1075 struct wacom *wacom = dev_get_drvdata(dev); \
1076 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
1078 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
1079 wacom_##name##_luminance_show, \
1080 wacom_##name##_luminance_store)
1082 DEVICE_LUMINANCE_ATTR(status0
, llv
);
1083 DEVICE_LUMINANCE_ATTR(status1
, hlv
);
1084 DEVICE_LUMINANCE_ATTR(buttons
, img_lum
);
1086 static ssize_t
wacom_button_image_store(struct device
*dev
, int button_id
,
1087 const char *buf
, size_t count
)
1089 struct hid_device
*hdev
= to_hid_device(dev
);
1090 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1095 if (hdev
->bus
== BUS_BLUETOOTH
) {
1097 xfer_id
= WAC_CMD_ICON_BT_XFER
;
1100 xfer_id
= WAC_CMD_ICON_XFER
;
1106 mutex_lock(&wacom
->lock
);
1108 err
= wacom_led_putimage(wacom
, button_id
, xfer_id
, len
, buf
);
1110 mutex_unlock(&wacom
->lock
);
1112 return err
< 0 ? err
: count
;
1115 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1116 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1117 struct device_attribute *attr, const char *buf, size_t count) \
1119 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1121 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
1122 NULL, wacom_btnimg##BUTTON_ID##_store)
1124 DEVICE_BTNIMG_ATTR(0);
1125 DEVICE_BTNIMG_ATTR(1);
1126 DEVICE_BTNIMG_ATTR(2);
1127 DEVICE_BTNIMG_ATTR(3);
1128 DEVICE_BTNIMG_ATTR(4);
1129 DEVICE_BTNIMG_ATTR(5);
1130 DEVICE_BTNIMG_ATTR(6);
1131 DEVICE_BTNIMG_ATTR(7);
1133 static struct attribute
*cintiq_led_attrs
[] = {
1134 &dev_attr_status_led0_select
.attr
,
1135 &dev_attr_status_led1_select
.attr
,
1139 static struct attribute_group cintiq_led_attr_group
= {
1140 .name
= "wacom_led",
1141 .attrs
= cintiq_led_attrs
,
1144 static struct attribute
*intuos4_led_attrs
[] = {
1145 &dev_attr_status0_luminance
.attr
,
1146 &dev_attr_status1_luminance
.attr
,
1147 &dev_attr_status_led0_select
.attr
,
1148 &dev_attr_buttons_luminance
.attr
,
1149 &dev_attr_button0_rawimg
.attr
,
1150 &dev_attr_button1_rawimg
.attr
,
1151 &dev_attr_button2_rawimg
.attr
,
1152 &dev_attr_button3_rawimg
.attr
,
1153 &dev_attr_button4_rawimg
.attr
,
1154 &dev_attr_button5_rawimg
.attr
,
1155 &dev_attr_button6_rawimg
.attr
,
1156 &dev_attr_button7_rawimg
.attr
,
1160 static struct attribute_group intuos4_led_attr_group
= {
1161 .name
= "wacom_led",
1162 .attrs
= intuos4_led_attrs
,
1165 static struct attribute
*intuos5_led_attrs
[] = {
1166 &dev_attr_status0_luminance
.attr
,
1167 &dev_attr_status_led0_select
.attr
,
1171 static struct attribute_group intuos5_led_attr_group
= {
1172 .name
= "wacom_led",
1173 .attrs
= intuos5_led_attrs
,
1176 static struct attribute
*generic_led_attrs
[] = {
1177 &dev_attr_status0_luminance
.attr
,
1178 &dev_attr_status_led0_select
.attr
,
1182 static struct attribute_group generic_led_attr_group
= {
1183 .name
= "wacom_led",
1184 .attrs
= generic_led_attrs
,
1187 struct wacom_sysfs_group_devres
{
1188 struct attribute_group
*group
;
1189 struct kobject
*root
;
1192 static void wacom_devm_sysfs_group_release(struct device
*dev
, void *res
)
1194 struct wacom_sysfs_group_devres
*devres
= res
;
1195 struct kobject
*kobj
= devres
->root
;
1197 dev_dbg(dev
, "%s: dropping reference to %s\n",
1198 __func__
, devres
->group
->name
);
1199 sysfs_remove_group(kobj
, devres
->group
);
1202 static int __wacom_devm_sysfs_create_group(struct wacom
*wacom
,
1203 struct kobject
*root
,
1204 struct attribute_group
*group
)
1206 struct wacom_sysfs_group_devres
*devres
;
1209 devres
= devres_alloc(wacom_devm_sysfs_group_release
,
1210 sizeof(struct wacom_sysfs_group_devres
),
1215 devres
->group
= group
;
1216 devres
->root
= root
;
1218 error
= sysfs_create_group(devres
->root
, group
);
1220 devres_free(devres
);
1224 devres_add(&wacom
->hdev
->dev
, devres
);
1229 static int wacom_devm_sysfs_create_group(struct wacom
*wacom
,
1230 struct attribute_group
*group
)
1232 return __wacom_devm_sysfs_create_group(wacom
, &wacom
->hdev
->dev
.kobj
,
1236 enum led_brightness
wacom_leds_brightness_get(struct wacom_led
*led
)
1238 struct wacom
*wacom
= led
->wacom
;
1240 if (wacom
->led
.max_hlv
)
1241 return led
->hlv
* LED_FULL
/ wacom
->led
.max_hlv
;
1243 if (wacom
->led
.max_llv
)
1244 return led
->llv
* LED_FULL
/ wacom
->led
.max_llv
;
1246 /* device doesn't support brightness tuning */
1250 static enum led_brightness
__wacom_led_brightness_get(struct led_classdev
*cdev
)
1252 struct wacom_led
*led
= container_of(cdev
, struct wacom_led
, cdev
);
1253 struct wacom
*wacom
= led
->wacom
;
1255 if (wacom
->led
.groups
[led
->group
].select
!= led
->id
)
1258 return wacom_leds_brightness_get(led
);
1261 static int wacom_led_brightness_set(struct led_classdev
*cdev
,
1262 enum led_brightness brightness
)
1264 struct wacom_led
*led
= container_of(cdev
, struct wacom_led
, cdev
);
1265 struct wacom
*wacom
= led
->wacom
;
1268 mutex_lock(&wacom
->lock
);
1270 if (!wacom
->led
.groups
|| (brightness
== LED_OFF
&&
1271 wacom
->led
.groups
[led
->group
].select
!= led
->id
)) {
1276 led
->llv
= wacom
->led
.llv
= wacom
->led
.max_llv
* brightness
/ LED_FULL
;
1277 led
->hlv
= wacom
->led
.hlv
= wacom
->led
.max_hlv
* brightness
/ LED_FULL
;
1279 wacom
->led
.groups
[led
->group
].select
= led
->id
;
1281 error
= wacom_led_control(wacom
);
1284 mutex_unlock(&wacom
->lock
);
1289 static void wacom_led_readonly_brightness_set(struct led_classdev
*cdev
,
1290 enum led_brightness brightness
)
1294 static int wacom_led_register_one(struct device
*dev
, struct wacom
*wacom
,
1295 struct wacom_led
*led
, unsigned int group
,
1296 unsigned int id
, bool read_only
)
1301 name
= devm_kasprintf(dev
, GFP_KERNEL
,
1310 led
->trigger
.name
= name
;
1311 error
= devm_led_trigger_register(dev
, &led
->trigger
);
1313 hid_err(wacom
->hdev
,
1314 "failed to register LED trigger %s: %d\n",
1315 led
->cdev
.name
, error
);
1323 led
->llv
= wacom
->led
.llv
;
1324 led
->hlv
= wacom
->led
.hlv
;
1325 led
->cdev
.name
= name
;
1326 led
->cdev
.max_brightness
= LED_FULL
;
1327 led
->cdev
.flags
= LED_HW_PLUGGABLE
;
1328 led
->cdev
.brightness_get
= __wacom_led_brightness_get
;
1330 led
->cdev
.brightness_set_blocking
= wacom_led_brightness_set
;
1331 led
->cdev
.default_trigger
= led
->cdev
.name
;
1333 led
->cdev
.brightness_set
= wacom_led_readonly_brightness_set
;
1336 error
= devm_led_classdev_register(dev
, &led
->cdev
);
1338 hid_err(wacom
->hdev
,
1339 "failed to register LED %s: %d\n",
1340 led
->cdev
.name
, error
);
1341 led
->cdev
.name
= NULL
;
1348 static void wacom_led_groups_release_one(void *data
)
1350 struct wacom_group_leds
*group
= data
;
1352 devres_release_group(group
->dev
, group
);
1355 static int wacom_led_groups_alloc_and_register_one(struct device
*dev
,
1356 struct wacom
*wacom
,
1357 int group_id
, int count
,
1360 struct wacom_led
*leds
;
1363 if (group_id
>= wacom
->led
.count
|| count
<= 0)
1366 if (!devres_open_group(dev
, &wacom
->led
.groups
[group_id
], GFP_KERNEL
))
1369 leds
= devm_kcalloc(dev
, count
, sizeof(struct wacom_led
), GFP_KERNEL
);
1375 wacom
->led
.groups
[group_id
].leds
= leds
;
1376 wacom
->led
.groups
[group_id
].count
= count
;
1378 for (i
= 0; i
< count
; i
++) {
1379 error
= wacom_led_register_one(dev
, wacom
, &leds
[i
],
1380 group_id
, i
, read_only
);
1385 wacom
->led
.groups
[group_id
].dev
= dev
;
1387 devres_close_group(dev
, &wacom
->led
.groups
[group_id
]);
1390 * There is a bug (?) in devm_led_classdev_register() in which its
1391 * increments the refcount of the parent. If the parent is an input
1392 * device, that means the ref count never reaches 0 when
1393 * devm_input_device_release() gets called.
1394 * This means that the LEDs are still there after disconnect.
1395 * Manually force the release of the group so that the leds are released
1396 * once we are done using them.
1398 error
= devm_add_action_or_reset(&wacom
->hdev
->dev
,
1399 wacom_led_groups_release_one
,
1400 &wacom
->led
.groups
[group_id
]);
1407 devres_release_group(dev
, &wacom
->led
.groups
[group_id
]);
1411 struct wacom_led
*wacom_led_find(struct wacom
*wacom
, unsigned int group_id
,
1414 struct wacom_group_leds
*group
;
1416 if (group_id
>= wacom
->led
.count
)
1419 group
= &wacom
->led
.groups
[group_id
];
1426 return &group
->leds
[id
];
1430 * wacom_led_next: gives the next available led with a wacom trigger.
1432 * returns the next available struct wacom_led which has its default trigger
1433 * or the current one if none is available.
1435 struct wacom_led
*wacom_led_next(struct wacom
*wacom
, struct wacom_led
*cur
)
1437 struct wacom_led
*next_led
;
1447 next_led
= wacom_led_find(wacom
, group
, ++next
);
1448 if (!next_led
|| next_led
== cur
)
1450 } while (next_led
->cdev
.trigger
!= &next_led
->trigger
);
1455 static void wacom_led_groups_release(void *data
)
1457 struct wacom
*wacom
= data
;
1459 wacom
->led
.groups
= NULL
;
1460 wacom
->led
.count
= 0;
1463 static int wacom_led_groups_allocate(struct wacom
*wacom
, int count
)
1465 struct device
*dev
= &wacom
->hdev
->dev
;
1466 struct wacom_group_leds
*groups
;
1469 groups
= devm_kcalloc(dev
, count
, sizeof(struct wacom_group_leds
),
1474 error
= devm_add_action_or_reset(dev
, wacom_led_groups_release
, wacom
);
1478 wacom
->led
.groups
= groups
;
1479 wacom
->led
.count
= count
;
1484 static int wacom_leds_alloc_and_register(struct wacom
*wacom
, int group_count
,
1485 int led_per_group
, bool read_only
)
1490 if (!wacom
->wacom_wac
.pad_input
)
1493 dev
= &wacom
->wacom_wac
.pad_input
->dev
;
1495 error
= wacom_led_groups_allocate(wacom
, group_count
);
1499 for (i
= 0; i
< group_count
; i
++) {
1500 error
= wacom_led_groups_alloc_and_register_one(dev
, wacom
, i
,
1510 int wacom_initialize_leds(struct wacom
*wacom
)
1514 if (!(wacom
->wacom_wac
.features
.device_type
& WACOM_DEVICETYPE_PAD
))
1517 /* Initialize default values */
1518 switch (wacom
->wacom_wac
.features
.type
) {
1520 if (!wacom
->generic_has_leds
)
1522 wacom
->led
.llv
= 100;
1523 wacom
->led
.max_llv
= 100;
1525 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1527 hid_err(wacom
->hdev
,
1528 "cannot create leds err: %d\n", error
);
1532 error
= wacom_devm_sysfs_create_group(wacom
,
1533 &generic_led_attr_group
);
1540 wacom
->led
.llv
= 10;
1541 wacom
->led
.hlv
= 20;
1542 wacom
->led
.max_llv
= 127;
1543 wacom
->led
.max_hlv
= 127;
1544 wacom
->led
.img_lum
= 10;
1546 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1548 hid_err(wacom
->hdev
,
1549 "cannot create leds err: %d\n", error
);
1553 error
= wacom_devm_sysfs_create_group(wacom
,
1554 &intuos4_led_attr_group
);
1561 wacom
->led
.img_lum
= 0;
1563 error
= wacom_leds_alloc_and_register(wacom
, 2, 4, false);
1565 hid_err(wacom
->hdev
,
1566 "cannot create leds err: %d\n", error
);
1570 error
= wacom_devm_sysfs_create_group(wacom
,
1571 &cintiq_led_attr_group
);
1580 wacom
->led
.llv
= 32;
1581 wacom
->led
.max_llv
= 96;
1583 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1585 hid_err(wacom
->hdev
,
1586 "cannot create leds err: %d\n", error
);
1590 error
= wacom_devm_sysfs_create_group(wacom
,
1591 &intuos5_led_attr_group
);
1595 wacom
->led
.llv
= 50;
1596 wacom
->led
.max_llv
= 100;
1597 error
= wacom_leds_alloc_and_register(wacom
, 1, 4, false);
1599 hid_err(wacom
->hdev
,
1600 "cannot create leds err: %d\n", error
);
1606 wacom
->led
.llv
= 255;
1607 wacom
->led
.max_llv
= 255;
1608 error
= wacom_led_groups_allocate(wacom
, 5);
1610 hid_err(wacom
->hdev
,
1611 "cannot create leds err: %d\n", error
);
1621 hid_err(wacom
->hdev
,
1622 "cannot create sysfs group err: %d\n", error
);
1629 static void wacom_init_work(struct work_struct
*work
)
1631 struct wacom
*wacom
= container_of(work
, struct wacom
, init_work
.work
);
1633 _wacom_query_tablet_data(wacom
);
1634 wacom_led_control(wacom
);
1637 static void wacom_query_tablet_data(struct wacom
*wacom
)
1639 schedule_delayed_work(&wacom
->init_work
, msecs_to_jiffies(1000));
1642 static enum power_supply_property wacom_battery_props
[] = {
1643 POWER_SUPPLY_PROP_MODEL_NAME
,
1644 POWER_SUPPLY_PROP_PRESENT
,
1645 POWER_SUPPLY_PROP_STATUS
,
1646 POWER_SUPPLY_PROP_SCOPE
,
1647 POWER_SUPPLY_PROP_CAPACITY
1650 static int wacom_battery_get_property(struct power_supply
*psy
,
1651 enum power_supply_property psp
,
1652 union power_supply_propval
*val
)
1654 struct wacom_battery
*battery
= power_supply_get_drvdata(psy
);
1658 case POWER_SUPPLY_PROP_MODEL_NAME
:
1659 val
->strval
= battery
->wacom
->wacom_wac
.name
;
1661 case POWER_SUPPLY_PROP_PRESENT
:
1662 val
->intval
= battery
->bat_connected
;
1664 case POWER_SUPPLY_PROP_SCOPE
:
1665 val
->intval
= POWER_SUPPLY_SCOPE_DEVICE
;
1667 case POWER_SUPPLY_PROP_CAPACITY
:
1668 val
->intval
= battery
->battery_capacity
;
1670 case POWER_SUPPLY_PROP_STATUS
:
1671 if (battery
->bat_status
!= WACOM_POWER_SUPPLY_STATUS_AUTO
)
1672 val
->intval
= battery
->bat_status
;
1673 else if (battery
->bat_charging
)
1674 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
1675 else if (battery
->battery_capacity
== 100 &&
1676 battery
->ps_connected
)
1677 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
1678 else if (battery
->ps_connected
)
1679 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1681 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
1691 static int __wacom_initialize_battery(struct wacom
*wacom
,
1692 struct wacom_battery
*battery
)
1694 static atomic_t battery_no
= ATOMIC_INIT(0);
1695 struct device
*dev
= &wacom
->hdev
->dev
;
1696 struct power_supply_config psy_cfg
= { .drv_data
= battery
, };
1697 struct power_supply
*ps_bat
;
1698 struct power_supply_desc
*bat_desc
= &battery
->bat_desc
;
1702 if (!devres_open_group(dev
, bat_desc
, GFP_KERNEL
))
1705 battery
->wacom
= wacom
;
1707 n
= atomic_inc_return(&battery_no
) - 1;
1709 bat_desc
->properties
= wacom_battery_props
;
1710 bat_desc
->num_properties
= ARRAY_SIZE(wacom_battery_props
);
1711 bat_desc
->get_property
= wacom_battery_get_property
;
1712 sprintf(battery
->bat_name
, "wacom_battery_%ld", n
);
1713 bat_desc
->name
= battery
->bat_name
;
1714 bat_desc
->type
= POWER_SUPPLY_TYPE_USB
;
1715 bat_desc
->use_for_apm
= 0;
1717 ps_bat
= devm_power_supply_register(dev
, bat_desc
, &psy_cfg
);
1718 if (IS_ERR(ps_bat
)) {
1719 error
= PTR_ERR(ps_bat
);
1723 power_supply_powers(ps_bat
, &wacom
->hdev
->dev
);
1725 battery
->battery
= ps_bat
;
1727 devres_close_group(dev
, bat_desc
);
1731 devres_release_group(dev
, bat_desc
);
1735 static int wacom_initialize_battery(struct wacom
*wacom
)
1737 if (wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
)
1738 return __wacom_initialize_battery(wacom
, &wacom
->battery
);
1743 static void wacom_destroy_battery(struct wacom
*wacom
)
1745 if (wacom
->battery
.battery
) {
1746 devres_release_group(&wacom
->hdev
->dev
,
1747 &wacom
->battery
.bat_desc
);
1748 wacom
->battery
.battery
= NULL
;
1752 static ssize_t
wacom_show_speed(struct device
*dev
,
1753 struct device_attribute
1756 struct hid_device
*hdev
= to_hid_device(dev
);
1757 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1759 return snprintf(buf
, PAGE_SIZE
, "%i\n", wacom
->wacom_wac
.bt_high_speed
);
1762 static ssize_t
wacom_store_speed(struct device
*dev
,
1763 struct device_attribute
*attr
,
1764 const char *buf
, size_t count
)
1766 struct hid_device
*hdev
= to_hid_device(dev
);
1767 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1770 if (kstrtou8(buf
, 0, &new_speed
))
1773 if (new_speed
!= 0 && new_speed
!= 1)
1776 wacom_bt_query_tablet_data(hdev
, new_speed
, &wacom
->wacom_wac
.features
);
1781 static DEVICE_ATTR(speed
, DEV_ATTR_RW_PERM
,
1782 wacom_show_speed
, wacom_store_speed
);
1785 static ssize_t
wacom_show_remote_mode(struct kobject
*kobj
,
1786 struct kobj_attribute
*kattr
,
1787 char *buf
, int index
)
1789 struct device
*dev
= kobj_to_dev(kobj
->parent
);
1790 struct hid_device
*hdev
= to_hid_device(dev
);
1791 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1794 mode
= wacom
->led
.groups
[index
].select
;
1795 return sprintf(buf
, "%d\n", mode
< 3 ? mode
: -1);
1798 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1799 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1800 struct kobj_attribute *kattr, char *buf) \
1802 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1804 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1805 .attr = {.name = "remote_mode", \
1806 .mode = DEV_ATTR_RO_PERM}, \
1807 .show = wacom_show_remote##SET_ID##_mode, \
1809 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1810 &remote##SET_ID##_mode_attr.attr, \
1813 static struct attribute_group remote##SET_ID##_serial_group = { \
1815 .attrs = remote##SET_ID##_serial_attrs, \
1818 DEVICE_EKR_ATTR_GROUP(0);
1819 DEVICE_EKR_ATTR_GROUP(1);
1820 DEVICE_EKR_ATTR_GROUP(2);
1821 DEVICE_EKR_ATTR_GROUP(3);
1822 DEVICE_EKR_ATTR_GROUP(4);
1824 static int wacom_remote_create_attr_group(struct wacom
*wacom
, __u32 serial
,
1828 struct wacom_remote
*remote
= wacom
->remote
;
1830 remote
->remotes
[index
].group
.name
= devm_kasprintf(&wacom
->hdev
->dev
,
1833 if (!remote
->remotes
[index
].group
.name
)
1836 error
= __wacom_devm_sysfs_create_group(wacom
, remote
->remote_dir
,
1837 &remote
->remotes
[index
].group
);
1839 remote
->remotes
[index
].group
.name
= NULL
;
1840 hid_err(wacom
->hdev
,
1841 "cannot create sysfs group err: %d\n", error
);
1848 static int wacom_cmd_unpair_remote(struct wacom
*wacom
, unsigned char selector
)
1850 const size_t buf_size
= 2;
1854 buf
= kzalloc(buf_size
, GFP_KERNEL
);
1858 buf
[0] = WAC_CMD_DELETE_PAIRING
;
1861 retval
= wacom_set_report(wacom
->hdev
, HID_OUTPUT_REPORT
, buf
,
1862 buf_size
, WAC_CMD_RETRIES
);
1868 static ssize_t
wacom_store_unpair_remote(struct kobject
*kobj
,
1869 struct kobj_attribute
*attr
,
1870 const char *buf
, size_t count
)
1872 unsigned char selector
= 0;
1873 struct device
*dev
= kobj_to_dev(kobj
->parent
);
1874 struct hid_device
*hdev
= to_hid_device(dev
);
1875 struct wacom
*wacom
= hid_get_drvdata(hdev
);
1878 if (!strncmp(buf
, "*\n", 2)) {
1879 selector
= WAC_CMD_UNPAIR_ALL
;
1881 hid_info(wacom
->hdev
, "remote: unrecognized unpair code: %s\n",
1886 mutex_lock(&wacom
->lock
);
1888 err
= wacom_cmd_unpair_remote(wacom
, selector
);
1889 mutex_unlock(&wacom
->lock
);
1891 return err
< 0 ? err
: count
;
1894 static struct kobj_attribute unpair_remote_attr
= {
1895 .attr
= {.name
= "unpair_remote", .mode
= 0200},
1896 .store
= wacom_store_unpair_remote
,
1899 static const struct attribute
*remote_unpair_attrs
[] = {
1900 &unpair_remote_attr
.attr
,
1904 static void wacom_remotes_destroy(void *data
)
1906 struct wacom
*wacom
= data
;
1907 struct wacom_remote
*remote
= wacom
->remote
;
1912 kobject_put(remote
->remote_dir
);
1913 kfifo_free(&remote
->remote_fifo
);
1914 wacom
->remote
= NULL
;
1917 static int wacom_initialize_remotes(struct wacom
*wacom
)
1920 struct wacom_remote
*remote
;
1923 if (wacom
->wacom_wac
.features
.type
!= REMOTE
)
1926 remote
= devm_kzalloc(&wacom
->hdev
->dev
, sizeof(*wacom
->remote
),
1931 wacom
->remote
= remote
;
1933 spin_lock_init(&remote
->remote_lock
);
1935 error
= kfifo_alloc(&remote
->remote_fifo
,
1936 5 * sizeof(struct wacom_remote_data
),
1939 hid_err(wacom
->hdev
, "failed allocating remote_fifo\n");
1943 remote
->remotes
[0].group
= remote0_serial_group
;
1944 remote
->remotes
[1].group
= remote1_serial_group
;
1945 remote
->remotes
[2].group
= remote2_serial_group
;
1946 remote
->remotes
[3].group
= remote3_serial_group
;
1947 remote
->remotes
[4].group
= remote4_serial_group
;
1949 remote
->remote_dir
= kobject_create_and_add("wacom_remote",
1950 &wacom
->hdev
->dev
.kobj
);
1951 if (!remote
->remote_dir
)
1954 error
= sysfs_create_files(remote
->remote_dir
, remote_unpair_attrs
);
1957 hid_err(wacom
->hdev
,
1958 "cannot create sysfs group err: %d\n", error
);
1962 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
1963 wacom
->led
.groups
[i
].select
= WACOM_STATUS_UNKNOWN
;
1964 remote
->remotes
[i
].serial
= 0;
1967 error
= devm_add_action_or_reset(&wacom
->hdev
->dev
,
1968 wacom_remotes_destroy
, wacom
);
1975 static struct input_dev
*wacom_allocate_input(struct wacom
*wacom
)
1977 struct input_dev
*input_dev
;
1978 struct hid_device
*hdev
= wacom
->hdev
;
1979 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
1981 input_dev
= devm_input_allocate_device(&hdev
->dev
);
1985 input_dev
->name
= wacom_wac
->features
.name
;
1986 input_dev
->phys
= hdev
->phys
;
1987 input_dev
->dev
.parent
= &hdev
->dev
;
1988 input_dev
->open
= wacom_open
;
1989 input_dev
->close
= wacom_close
;
1990 input_dev
->uniq
= hdev
->uniq
;
1991 input_dev
->id
.bustype
= hdev
->bus
;
1992 input_dev
->id
.vendor
= hdev
->vendor
;
1993 input_dev
->id
.product
= wacom_wac
->pid
? wacom_wac
->pid
: hdev
->product
;
1994 input_dev
->id
.version
= hdev
->version
;
1995 input_set_drvdata(input_dev
, wacom
);
2000 static int wacom_allocate_inputs(struct wacom
*wacom
)
2002 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
2004 wacom_wac
->pen_input
= wacom_allocate_input(wacom
);
2005 wacom_wac
->touch_input
= wacom_allocate_input(wacom
);
2006 wacom_wac
->pad_input
= wacom_allocate_input(wacom
);
2007 if (!wacom_wac
->pen_input
||
2008 !wacom_wac
->touch_input
||
2009 !wacom_wac
->pad_input
)
2012 wacom_wac
->pen_input
->name
= wacom_wac
->pen_name
;
2013 wacom_wac
->touch_input
->name
= wacom_wac
->touch_name
;
2014 wacom_wac
->pad_input
->name
= wacom_wac
->pad_name
;
2019 static int wacom_register_inputs(struct wacom
*wacom
)
2021 struct input_dev
*pen_input_dev
, *touch_input_dev
, *pad_input_dev
;
2022 struct wacom_wac
*wacom_wac
= &(wacom
->wacom_wac
);
2025 pen_input_dev
= wacom_wac
->pen_input
;
2026 touch_input_dev
= wacom_wac
->touch_input
;
2027 pad_input_dev
= wacom_wac
->pad_input
;
2029 if (!pen_input_dev
|| !touch_input_dev
|| !pad_input_dev
)
2032 error
= wacom_setup_pen_input_capabilities(pen_input_dev
, wacom_wac
);
2034 /* no pen in use on this interface */
2035 input_free_device(pen_input_dev
);
2036 wacom_wac
->pen_input
= NULL
;
2037 pen_input_dev
= NULL
;
2039 error
= input_register_device(pen_input_dev
);
2044 error
= wacom_setup_touch_input_capabilities(touch_input_dev
, wacom_wac
);
2046 /* no touch in use on this interface */
2047 input_free_device(touch_input_dev
);
2048 wacom_wac
->touch_input
= NULL
;
2049 touch_input_dev
= NULL
;
2051 error
= input_register_device(touch_input_dev
);
2056 error
= wacom_setup_pad_input_capabilities(pad_input_dev
, wacom_wac
);
2058 /* no pad in use on this interface */
2059 input_free_device(pad_input_dev
);
2060 wacom_wac
->pad_input
= NULL
;
2061 pad_input_dev
= NULL
;
2063 error
= input_register_device(pad_input_dev
);
2071 wacom_wac
->pad_input
= NULL
;
2072 wacom_wac
->touch_input
= NULL
;
2073 wacom_wac
->pen_input
= NULL
;
2078 * Not all devices report physical dimensions from HID.
2079 * Compute the default from hardcoded logical dimension
2080 * and resolution before driver overwrites them.
2082 static void wacom_set_default_phy(struct wacom_features
*features
)
2084 if (features
->x_resolution
) {
2085 features
->x_phy
= (features
->x_max
* 100) /
2086 features
->x_resolution
;
2087 features
->y_phy
= (features
->y_max
* 100) /
2088 features
->y_resolution
;
2092 static void wacom_calculate_res(struct wacom_features
*features
)
2094 /* set unit to "100th of a mm" for devices not reported by HID */
2095 if (!features
->unit
) {
2096 features
->unit
= 0x11;
2097 features
->unitExpo
= -3;
2100 features
->x_resolution
= wacom_calc_hid_res(features
->x_max
,
2103 features
->unitExpo
);
2104 features
->y_resolution
= wacom_calc_hid_res(features
->y_max
,
2107 features
->unitExpo
);
2110 void wacom_battery_work(struct work_struct
*work
)
2112 struct wacom
*wacom
= container_of(work
, struct wacom
, battery_work
);
2114 if ((wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
) &&
2115 !wacom
->battery
.battery
) {
2116 wacom_initialize_battery(wacom
);
2118 else if (!(wacom
->wacom_wac
.features
.quirks
& WACOM_QUIRK_BATTERY
) &&
2119 wacom
->battery
.battery
) {
2120 wacom_destroy_battery(wacom
);
2124 static size_t wacom_compute_pktlen(struct hid_device
*hdev
)
2126 struct hid_report_enum
*report_enum
;
2127 struct hid_report
*report
;
2130 report_enum
= hdev
->report_enum
+ HID_INPUT_REPORT
;
2132 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
2133 size_t report_size
= hid_report_len(report
);
2134 if (report_size
> size
)
2141 static void wacom_update_name(struct wacom
*wacom
, const char *suffix
)
2143 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2144 struct wacom_features
*features
= &wacom_wac
->features
;
2145 char name
[WACOM_NAME_MAX
];
2147 /* Generic devices name unspecified */
2148 if ((features
->type
== HID_GENERIC
) && !strcmp("Wacom HID", features
->name
)) {
2149 char *product_name
= wacom
->hdev
->name
;
2151 if (hid_is_using_ll_driver(wacom
->hdev
, &usb_hid_driver
)) {
2152 struct usb_interface
*intf
= to_usb_interface(wacom
->hdev
->dev
.parent
);
2153 struct usb_device
*dev
= interface_to_usbdev(intf
);
2154 product_name
= dev
->product
;
2157 if (wacom
->hdev
->bus
== BUS_I2C
) {
2158 snprintf(name
, sizeof(name
), "%s %X",
2159 features
->name
, wacom
->hdev
->product
);
2160 } else if (strstr(product_name
, "Wacom") ||
2161 strstr(product_name
, "wacom") ||
2162 strstr(product_name
, "WACOM")) {
2163 strlcpy(name
, product_name
, sizeof(name
));
2165 snprintf(name
, sizeof(name
), "Wacom %s", product_name
);
2168 /* strip out excess whitespaces */
2170 char *gap
= strstr(name
, " ");
2173 /* shift everything including the terminator */
2174 memmove(gap
, gap
+1, strlen(gap
));
2177 /* get rid of trailing whitespace */
2178 if (name
[strlen(name
)-1] == ' ')
2179 name
[strlen(name
)-1] = '\0';
2181 strlcpy(name
, features
->name
, sizeof(name
));
2184 snprintf(wacom_wac
->name
, sizeof(wacom_wac
->name
), "%s%s",
2187 /* Append the device type to the name */
2188 snprintf(wacom_wac
->pen_name
, sizeof(wacom_wac
->pen_name
),
2189 "%s%s Pen", name
, suffix
);
2190 snprintf(wacom_wac
->touch_name
, sizeof(wacom_wac
->touch_name
),
2191 "%s%s Finger", name
, suffix
);
2192 snprintf(wacom_wac
->pad_name
, sizeof(wacom_wac
->pad_name
),
2193 "%s%s Pad", name
, suffix
);
2196 static void wacom_release_resources(struct wacom
*wacom
)
2198 struct hid_device
*hdev
= wacom
->hdev
;
2200 if (!wacom
->resources
)
2203 devres_release_group(&hdev
->dev
, wacom
);
2205 wacom
->resources
= false;
2207 wacom
->wacom_wac
.pen_input
= NULL
;
2208 wacom
->wacom_wac
.touch_input
= NULL
;
2209 wacom
->wacom_wac
.pad_input
= NULL
;
2212 static void wacom_set_shared_values(struct wacom_wac
*wacom_wac
)
2214 if (wacom_wac
->features
.device_type
& WACOM_DEVICETYPE_TOUCH
) {
2215 wacom_wac
->shared
->type
= wacom_wac
->features
.type
;
2216 wacom_wac
->shared
->touch_input
= wacom_wac
->touch_input
;
2219 if (wacom_wac
->has_mute_touch_switch
) {
2220 wacom_wac
->shared
->has_mute_touch_switch
= true;
2221 wacom_wac
->shared
->is_touch_on
= true;
2224 if (wacom_wac
->shared
->has_mute_touch_switch
&&
2225 wacom_wac
->shared
->touch_input
) {
2226 set_bit(EV_SW
, wacom_wac
->shared
->touch_input
->evbit
);
2227 input_set_capability(wacom_wac
->shared
->touch_input
, EV_SW
,
2232 static int wacom_parse_and_register(struct wacom
*wacom
, bool wireless
)
2234 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2235 struct wacom_features
*features
= &wacom_wac
->features
;
2236 struct hid_device
*hdev
= wacom
->hdev
;
2238 unsigned int connect_mask
= HID_CONNECT_HIDRAW
;
2240 features
->pktlen
= wacom_compute_pktlen(hdev
);
2241 if (features
->pktlen
> WACOM_PKGLEN_MAX
)
2244 if (!devres_open_group(&hdev
->dev
, wacom
, GFP_KERNEL
))
2247 wacom
->resources
= true;
2249 error
= wacom_allocate_inputs(wacom
);
2254 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2255 * into debug mode for the touch part.
2256 * We ignore the other interfaces.
2258 if (features
->type
== BAMBOO_PAD
) {
2259 if (features
->pktlen
== WACOM_PKGLEN_PENABLED
) {
2260 features
->type
= HID_GENERIC
;
2261 } else if ((features
->pktlen
!= WACOM_PKGLEN_BPAD_TOUCH
) &&
2262 (features
->pktlen
!= WACOM_PKGLEN_BPAD_TOUCH_USB
)) {
2268 /* set the default size in case we do not get them from hid */
2269 wacom_set_default_phy(features
);
2271 /* Retrieve the physical and logical size for touch devices */
2272 wacom_retrieve_hid_descriptor(hdev
, features
);
2273 wacom_setup_device_quirks(wacom
);
2275 if (features
->device_type
== WACOM_DEVICETYPE_NONE
&&
2276 features
->type
!= WIRELESS
) {
2277 error
= features
->type
== HID_GENERIC
? -ENODEV
: 0;
2279 dev_warn(&hdev
->dev
, "Unknown device_type for '%s'. %s.",
2281 error
? "Ignoring" : "Assuming pen");
2286 features
->device_type
|= WACOM_DEVICETYPE_PEN
;
2289 wacom_calculate_res(features
);
2291 wacom_update_name(wacom
, wireless
? " (WL)" : "");
2293 /* pen only Bamboo neither support touch nor pad */
2294 if ((features
->type
== BAMBOO_PEN
) &&
2295 ((features
->device_type
& WACOM_DEVICETYPE_TOUCH
) ||
2296 (features
->device_type
& WACOM_DEVICETYPE_PAD
))) {
2301 error
= wacom_add_shared_data(hdev
);
2305 if (!(features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
) &&
2306 (features
->quirks
& WACOM_QUIRK_BATTERY
)) {
2307 error
= wacom_initialize_battery(wacom
);
2312 error
= wacom_register_inputs(wacom
);
2316 if (wacom
->wacom_wac
.features
.device_type
& WACOM_DEVICETYPE_PAD
) {
2317 error
= wacom_initialize_leds(wacom
);
2321 error
= wacom_initialize_remotes(wacom
);
2326 if (features
->type
== HID_GENERIC
)
2327 connect_mask
|= HID_CONNECT_DRIVER
;
2329 /* Regular HID work starts now */
2330 error
= hid_hw_start(hdev
, connect_mask
);
2332 hid_err(hdev
, "hw start failed\n");
2337 /* Note that if query fails it is not a hard failure */
2338 wacom_query_tablet_data(wacom
);
2341 /* touch only Bamboo doesn't support pen */
2342 if ((features
->type
== BAMBOO_TOUCH
) &&
2343 (features
->device_type
& WACOM_DEVICETYPE_PEN
)) {
2344 cancel_delayed_work_sync(&wacom
->init_work
);
2345 _wacom_query_tablet_data(wacom
);
2350 if (features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
)
2351 error
= hid_hw_open(hdev
);
2353 wacom_set_shared_values(wacom_wac
);
2354 devres_close_group(&hdev
->dev
, wacom
);
2361 wacom_release_resources(wacom
);
2365 static void wacom_wireless_work(struct work_struct
*work
)
2367 struct wacom
*wacom
= container_of(work
, struct wacom
, wireless_work
);
2368 struct usb_device
*usbdev
= wacom
->usbdev
;
2369 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2370 struct hid_device
*hdev1
, *hdev2
;
2371 struct wacom
*wacom1
, *wacom2
;
2372 struct wacom_wac
*wacom_wac1
, *wacom_wac2
;
2376 * Regardless if this is a disconnect or a new tablet,
2377 * remove any existing input and battery devices.
2380 wacom_destroy_battery(wacom
);
2382 /* Stylus interface */
2383 hdev1
= usb_get_intfdata(usbdev
->config
->interface
[1]);
2384 wacom1
= hid_get_drvdata(hdev1
);
2385 wacom_wac1
= &(wacom1
->wacom_wac
);
2386 wacom_release_resources(wacom1
);
2388 /* Touch interface */
2389 hdev2
= usb_get_intfdata(usbdev
->config
->interface
[2]);
2390 wacom2
= hid_get_drvdata(hdev2
);
2391 wacom_wac2
= &(wacom2
->wacom_wac
);
2392 wacom_release_resources(wacom2
);
2394 if (wacom_wac
->pid
== 0) {
2395 hid_info(wacom
->hdev
, "wireless tablet disconnected\n");
2397 const struct hid_device_id
*id
= wacom_ids
;
2399 hid_info(wacom
->hdev
, "wireless tablet connected with PID %x\n",
2403 if (id
->vendor
== USB_VENDOR_ID_WACOM
&&
2404 id
->product
== wacom_wac
->pid
)
2410 hid_info(wacom
->hdev
, "ignoring unknown PID.\n");
2414 /* Stylus interface */
2415 wacom_wac1
->features
=
2416 *((struct wacom_features
*)id
->driver_data
);
2418 wacom_wac1
->pid
= wacom_wac
->pid
;
2420 error
= wacom_parse_and_register(wacom1
, true);
2424 /* Touch interface */
2425 if (wacom_wac1
->features
.touch_max
||
2426 (wacom_wac1
->features
.type
>= INTUOSHT
&&
2427 wacom_wac1
->features
.type
<= BAMBOO_PT
)) {
2428 wacom_wac2
->features
=
2429 *((struct wacom_features
*)id
->driver_data
);
2430 wacom_wac2
->pid
= wacom_wac
->pid
;
2432 error
= wacom_parse_and_register(wacom2
, true);
2437 strlcpy(wacom_wac
->name
, wacom_wac1
->name
,
2438 sizeof(wacom_wac
->name
));
2439 error
= wacom_initialize_battery(wacom
);
2447 wacom_release_resources(wacom1
);
2448 wacom_release_resources(wacom2
);
2452 static void wacom_remote_destroy_one(struct wacom
*wacom
, unsigned int index
)
2454 struct wacom_remote
*remote
= wacom
->remote
;
2455 u32 serial
= remote
->remotes
[index
].serial
;
2457 unsigned long flags
;
2459 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
2460 if (remote
->remotes
[i
].serial
== serial
) {
2462 spin_lock_irqsave(&remote
->remote_lock
, flags
);
2463 remote
->remotes
[i
].registered
= false;
2464 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2466 if (remote
->remotes
[i
].battery
.battery
)
2467 devres_release_group(&wacom
->hdev
->dev
,
2468 &remote
->remotes
[i
].battery
.bat_desc
);
2470 if (remote
->remotes
[i
].group
.name
)
2471 devres_release_group(&wacom
->hdev
->dev
,
2472 &remote
->remotes
[i
]);
2474 remote
->remotes
[i
].serial
= 0;
2475 remote
->remotes
[i
].group
.name
= NULL
;
2476 remote
->remotes
[i
].battery
.battery
= NULL
;
2477 wacom
->led
.groups
[i
].select
= WACOM_STATUS_UNKNOWN
;
2482 static int wacom_remote_create_one(struct wacom
*wacom
, u32 serial
,
2485 struct wacom_remote
*remote
= wacom
->remote
;
2486 struct device
*dev
= &wacom
->hdev
->dev
;
2489 /* A remote can pair more than once with an EKR,
2490 * check to make sure this serial isn't already paired.
2492 for (k
= 0; k
< WACOM_MAX_REMOTES
; k
++) {
2493 if (remote
->remotes
[k
].serial
== serial
)
2497 if (k
< WACOM_MAX_REMOTES
) {
2498 remote
->remotes
[index
].serial
= serial
;
2502 if (!devres_open_group(dev
, &remote
->remotes
[index
], GFP_KERNEL
))
2505 error
= wacom_remote_create_attr_group(wacom
, serial
, index
);
2509 remote
->remotes
[index
].input
= wacom_allocate_input(wacom
);
2510 if (!remote
->remotes
[index
].input
) {
2514 remote
->remotes
[index
].input
->uniq
= remote
->remotes
[index
].group
.name
;
2515 remote
->remotes
[index
].input
->name
= wacom
->wacom_wac
.pad_name
;
2517 if (!remote
->remotes
[index
].input
->name
) {
2522 error
= wacom_setup_pad_input_capabilities(remote
->remotes
[index
].input
,
2527 remote
->remotes
[index
].serial
= serial
;
2529 error
= input_register_device(remote
->remotes
[index
].input
);
2533 error
= wacom_led_groups_alloc_and_register_one(
2534 &remote
->remotes
[index
].input
->dev
,
2535 wacom
, index
, 3, true);
2539 remote
->remotes
[index
].registered
= true;
2541 devres_close_group(dev
, &remote
->remotes
[index
]);
2545 devres_release_group(dev
, &remote
->remotes
[index
]);
2546 remote
->remotes
[index
].serial
= 0;
2550 static int wacom_remote_attach_battery(struct wacom
*wacom
, int index
)
2552 struct wacom_remote
*remote
= wacom
->remote
;
2555 if (!remote
->remotes
[index
].registered
)
2558 if (remote
->remotes
[index
].battery
.battery
)
2561 if (wacom
->led
.groups
[index
].select
== WACOM_STATUS_UNKNOWN
)
2564 error
= __wacom_initialize_battery(wacom
,
2565 &wacom
->remote
->remotes
[index
].battery
);
2572 static void wacom_remote_work(struct work_struct
*work
)
2574 struct wacom
*wacom
= container_of(work
, struct wacom
, remote_work
);
2575 struct wacom_remote
*remote
= wacom
->remote
;
2576 struct wacom_remote_data data
;
2577 unsigned long flags
;
2582 spin_lock_irqsave(&remote
->remote_lock
, flags
);
2584 count
= kfifo_out(&remote
->remote_fifo
, &data
, sizeof(data
));
2586 if (count
!= sizeof(data
)) {
2587 hid_err(wacom
->hdev
,
2588 "workitem triggered without status available\n");
2589 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2593 if (!kfifo_is_empty(&remote
->remote_fifo
))
2594 wacom_schedule_work(&wacom
->wacom_wac
, WACOM_WORKER_REMOTE
);
2596 spin_unlock_irqrestore(&remote
->remote_lock
, flags
);
2598 for (i
= 0; i
< WACOM_MAX_REMOTES
; i
++) {
2599 serial
= data
.remote
[i
].serial
;
2600 if (data
.remote
[i
].connected
) {
2602 if (remote
->remotes
[i
].serial
== serial
) {
2603 wacom_remote_attach_battery(wacom
, i
);
2607 if (remote
->remotes
[i
].serial
)
2608 wacom_remote_destroy_one(wacom
, i
);
2610 wacom_remote_create_one(wacom
, serial
, i
);
2612 } else if (remote
->remotes
[i
].serial
) {
2613 wacom_remote_destroy_one(wacom
, i
);
2618 static void wacom_mode_change_work(struct work_struct
*work
)
2620 struct wacom
*wacom
= container_of(work
, struct wacom
, mode_change_work
);
2621 struct wacom_shared
*shared
= wacom
->wacom_wac
.shared
;
2622 struct wacom
*wacom1
= NULL
;
2623 struct wacom
*wacom2
= NULL
;
2624 bool is_direct
= wacom
->wacom_wac
.is_direct_mode
;
2628 wacom1
= hid_get_drvdata(shared
->pen
);
2629 wacom_release_resources(wacom1
);
2630 hid_hw_stop(wacom1
->hdev
);
2631 wacom1
->wacom_wac
.has_mode_change
= true;
2632 wacom1
->wacom_wac
.is_direct_mode
= is_direct
;
2635 if (shared
->touch
) {
2636 wacom2
= hid_get_drvdata(shared
->touch
);
2637 wacom_release_resources(wacom2
);
2638 hid_hw_stop(wacom2
->hdev
);
2639 wacom2
->wacom_wac
.has_mode_change
= true;
2640 wacom2
->wacom_wac
.is_direct_mode
= is_direct
;
2644 error
= wacom_parse_and_register(wacom1
, false);
2650 error
= wacom_parse_and_register(wacom2
, false);
2658 static int wacom_probe(struct hid_device
*hdev
,
2659 const struct hid_device_id
*id
)
2661 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
2662 struct usb_device
*dev
= interface_to_usbdev(intf
);
2663 struct wacom
*wacom
;
2664 struct wacom_wac
*wacom_wac
;
2665 struct wacom_features
*features
;
2668 if (!id
->driver_data
)
2671 hdev
->quirks
|= HID_QUIRK_NO_INIT_REPORTS
;
2673 /* hid-core sets this quirk for the boot interface */
2674 hdev
->quirks
&= ~HID_QUIRK_NOGET
;
2676 wacom
= devm_kzalloc(&hdev
->dev
, sizeof(struct wacom
), GFP_KERNEL
);
2680 hid_set_drvdata(hdev
, wacom
);
2683 wacom_wac
= &wacom
->wacom_wac
;
2684 wacom_wac
->features
= *((struct wacom_features
*)id
->driver_data
);
2685 features
= &wacom_wac
->features
;
2687 if (features
->check_for_hid_type
&& features
->hid_type
!= hdev
->type
) {
2692 error
= kfifo_alloc(&wacom_wac
->pen_fifo
, WACOM_PKGLEN_MAX
, GFP_KERNEL
);
2696 wacom_wac
->hid_data
.inputmode
= -1;
2697 wacom_wac
->mode_report
= -1;
2699 wacom
->usbdev
= dev
;
2701 mutex_init(&wacom
->lock
);
2702 INIT_DELAYED_WORK(&wacom
->init_work
, wacom_init_work
);
2703 INIT_WORK(&wacom
->wireless_work
, wacom_wireless_work
);
2704 INIT_WORK(&wacom
->battery_work
, wacom_battery_work
);
2705 INIT_WORK(&wacom
->remote_work
, wacom_remote_work
);
2706 INIT_WORK(&wacom
->mode_change_work
, wacom_mode_change_work
);
2708 /* ask for the report descriptor to be loaded by HID */
2709 error
= hid_parse(hdev
);
2711 hid_err(hdev
, "parse failed\n");
2715 error
= wacom_parse_and_register(wacom
, false);
2719 if (hdev
->bus
== BUS_BLUETOOTH
) {
2720 error
= device_create_file(&hdev
->dev
, &dev_attr_speed
);
2723 "can't create sysfs speed attribute err: %d\n",
2730 hid_set_drvdata(hdev
, NULL
);
2734 static void wacom_remove(struct hid_device
*hdev
)
2736 struct wacom
*wacom
= hid_get_drvdata(hdev
);
2737 struct wacom_wac
*wacom_wac
= &wacom
->wacom_wac
;
2738 struct wacom_features
*features
= &wacom_wac
->features
;
2740 if (features
->device_type
& WACOM_DEVICETYPE_WL_MONITOR
)
2745 cancel_delayed_work_sync(&wacom
->init_work
);
2746 cancel_work_sync(&wacom
->wireless_work
);
2747 cancel_work_sync(&wacom
->battery_work
);
2748 cancel_work_sync(&wacom
->remote_work
);
2749 cancel_work_sync(&wacom
->mode_change_work
);
2750 if (hdev
->bus
== BUS_BLUETOOTH
)
2751 device_remove_file(&hdev
->dev
, &dev_attr_speed
);
2753 /* make sure we don't trigger the LEDs */
2754 wacom_led_groups_release(wacom
);
2756 if (wacom
->wacom_wac
.features
.type
!= REMOTE
)
2757 wacom_release_resources(wacom
);
2759 kfifo_free(&wacom_wac
->pen_fifo
);
2761 hid_set_drvdata(hdev
, NULL
);
2765 static int wacom_resume(struct hid_device
*hdev
)
2767 struct wacom
*wacom
= hid_get_drvdata(hdev
);
2769 mutex_lock(&wacom
->lock
);
2771 /* switch to wacom mode first */
2772 _wacom_query_tablet_data(wacom
);
2773 wacom_led_control(wacom
);
2775 mutex_unlock(&wacom
->lock
);
2780 static int wacom_reset_resume(struct hid_device
*hdev
)
2782 return wacom_resume(hdev
);
2784 #endif /* CONFIG_PM */
2786 static struct hid_driver wacom_driver
= {
2788 .id_table
= wacom_ids
,
2789 .probe
= wacom_probe
,
2790 .remove
= wacom_remove
,
2791 .report
= wacom_wac_report
,
2793 .resume
= wacom_resume
,
2794 .reset_resume
= wacom_reset_resume
,
2796 .raw_event
= wacom_raw_event
,
2798 module_hid_driver(wacom_driver
);
2800 MODULE_VERSION(DRIVER_VERSION
);
2801 MODULE_AUTHOR(DRIVER_AUTHOR
);
2802 MODULE_DESCRIPTION(DRIVER_DESC
);
2803 MODULE_LICENSE("GPL");