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 (wacom->wacom_wac.features.type == BOOTLOADER)
169 if (size > WACOM_PKGLEN_MAX)
172 if (wacom_wac_pen_serial_enforce(hdev, report, raw_data, size))
175 memcpy(wacom->wacom_wac.data, raw_data, size);
177 wacom_wac_irq(&wacom->wacom_wac, size);
182 static int wacom_open(struct input_dev *dev)
184 struct wacom *wacom = input_get_drvdata(dev);
186 return hid_hw_open(wacom->hdev);
189 static void wacom_close(struct input_dev *dev)
191 struct wacom *wacom = input_get_drvdata(dev);
194 * wacom->hdev should never be null, but surprisingly, I had the case
195 * once while unplugging the Wacom Wireless Receiver.
198 hid_hw_close(wacom->hdev);
202 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
204 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
205 unsigned unit, int exponent)
207 struct hid_field field = {
208 .logical_maximum = logical_extents,
209 .physical_maximum = physical_extents,
211 .unit_exponent = exponent,
214 return hidinput_calc_abs_res(&field, ABS_X);
217 static void wacom_hid_usage_quirk(struct hid_device *hdev,
218 struct hid_field *field, struct hid_usage *usage)
220 struct wacom *wacom = hid_get_drvdata(hdev);
221 struct wacom_features *features = &wacom->wacom_wac.features;
222 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
225 * The Dell Canvas 27 needs to be switched to its vendor-defined
226 * report to provide the best resolution.
228 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
229 hdev->product == 0x4200 &&
230 field->application == HID_UP_MSVENDOR) {
231 wacom->wacom_wac.mode_report = field->report->id;
232 wacom->wacom_wac.mode_value = 2;
236 * ISDv4 devices which predate HID's adoption of the
237 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
238 * position instead. We can accurately detect if a
239 * usage with that value should be HID_DG_BARRELSWITCH2
240 * based on the surrounding usages, which have remained
241 * constant across generations.
243 if (features->type == HID_GENERIC &&
244 usage->hid == 0x000D0000 &&
245 field->application == HID_DG_PEN &&
246 field->physical == HID_DG_STYLUS) {
247 int i = usage->usage_index;
249 if (i-4 >= 0 && i+1 < field->maxusage &&
250 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
251 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
252 field->usage[i-2].hid == HID_DG_ERASER &&
253 field->usage[i-1].hid == HID_DG_INVERT &&
254 field->usage[i+1].hid == HID_DG_INRANGE) {
255 usage->hid = HID_DG_BARRELSWITCH2;
259 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
260 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
261 hdev->product == 0x0358 &&
262 WACOM_PEN_FIELD(field) &&
263 equivalent_usage == HID_GD_Y) {
264 field->logical_maximum = 43200;
268 static void wacom_feature_mapping(struct hid_device *hdev,
269 struct hid_field *field, struct hid_usage *usage)
271 struct wacom *wacom = hid_get_drvdata(hdev);
272 struct wacom_features *features = &wacom->wacom_wac.features;
273 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
274 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
279 wacom_hid_usage_quirk(hdev, field, usage);
281 switch (equivalent_usage) {
282 case WACOM_HID_WD_TOUCH_RING_SETTING:
283 wacom->generic_has_leds = true;
285 case HID_DG_CONTACTMAX:
286 /* leave touch_max as is if predefined */
287 if (!features->touch_max) {
289 n = hid_report_len(field->report);
290 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
293 data[0] = field->report->id;
294 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
295 data, n, WAC_CMD_RETRIES);
296 if (ret == n && features->type == HID_GENERIC) {
297 ret = hid_report_raw_event(hdev,
298 HID_FEATURE_REPORT, data, n, 0);
299 } else if (ret == 2 && features->type != HID_GENERIC) {
300 features->touch_max = data[1];
302 features->touch_max = 16;
303 hid_warn(hdev, "wacom_feature_mapping: "
304 "could not get HID_DG_CONTACTMAX, "
305 "defaulting to %d\n",
306 features->touch_max);
311 case HID_DG_INPUTMODE:
312 /* Ignore if value index is out of bounds. */
313 if (usage->usage_index >= field->report_count) {
314 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
318 hid_data->inputmode = field->report->id;
319 hid_data->inputmode_index = usage->usage_index;
322 case HID_UP_DIGITIZER:
323 if (field->report->id == 0x0B &&
324 (field->application == WACOM_HID_G9_PEN ||
325 field->application == WACOM_HID_G11_PEN)) {
326 wacom->wacom_wac.mode_report = field->report->id;
327 wacom->wacom_wac.mode_value = 0;
331 case WACOM_HID_WD_DATAMODE:
332 wacom->wacom_wac.mode_report = field->report->id;
333 wacom->wacom_wac.mode_value = 2;
336 case WACOM_HID_UP_G9:
337 case WACOM_HID_UP_G11:
338 if (field->report->id == 0x03 &&
339 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
340 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
341 wacom->wacom_wac.mode_report = field->report->id;
342 wacom->wacom_wac.mode_value = 0;
345 case WACOM_HID_WD_OFFSETLEFT:
346 case WACOM_HID_WD_OFFSETTOP:
347 case WACOM_HID_WD_OFFSETRIGHT:
348 case WACOM_HID_WD_OFFSETBOTTOM:
350 n = hid_report_len(field->report);
351 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
354 data[0] = field->report->id;
355 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
356 data, n, WAC_CMD_RETRIES);
358 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
361 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
370 * Interface Descriptor of wacom devices can be incomplete and
371 * inconsistent so wacom_features table is used to store stylus
372 * device's packet lengths, various maximum values, and tablet
373 * resolution based on product ID's.
375 * For devices that contain 2 interfaces, wacom_features table is
376 * inaccurate for the touch interface. Since the Interface Descriptor
377 * for touch interfaces has pretty complete data, this function exists
378 * to query tablet for this missing information instead of hard coding in
379 * an additional table.
381 * A typical Interface Descriptor for a stylus will contain a
382 * boot mouse application collection that is not of interest and this
383 * function will ignore it.
385 * It also contains a digitizer application collection that also is not
386 * of interest since any information it contains would be duplicate
387 * of what is in wacom_features. Usually it defines a report of an array
388 * of bytes that could be used as max length of the stylus packet returned.
389 * If it happens to define a Digitizer-Stylus Physical Collection then
390 * the X and Y logical values contain valid data but it is ignored.
392 * A typical Interface Descriptor for a touch interface will contain a
393 * Digitizer-Finger Physical Collection which will define both logical
394 * X/Y maximum as well as the physical size of tablet. Since touch
395 * interfaces haven't supported pressure or distance, this is enough
396 * information to override invalid values in the wacom_features table.
398 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
399 * data. We deal with them after returning from this function.
401 static void wacom_usage_mapping(struct hid_device *hdev,
402 struct hid_field *field, struct hid_usage *usage)
404 struct wacom *wacom = hid_get_drvdata(hdev);
405 struct wacom_features *features = &wacom->wacom_wac.features;
406 bool finger = WACOM_FINGER_FIELD(field);
407 bool pen = WACOM_PEN_FIELD(field);
408 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
411 * Requiring Stylus Usage will ignore boot mouse
412 * X/Y values and some cases of invalid Digitizer X/Y
413 * values commonly reported.
416 features->device_type |= WACOM_DEVICETYPE_PEN;
418 features->device_type |= WACOM_DEVICETYPE_TOUCH;
422 wacom_hid_usage_quirk(hdev, field, usage);
424 switch (equivalent_usage) {
426 features->x_max = field->logical_maximum;
428 features->x_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;
437 features->y_max = field->logical_maximum;
439 features->y_phy = field->physical_maximum;
440 if ((features->type != BAMBOO_PT) &&
441 (features->type != BAMBOO_TOUCH)) {
442 features->unit = field->unit;
443 features->unitExpo = field->unit_exponent;
447 case HID_DG_TIPPRESSURE:
449 features->pressure_max = field->logical_maximum;
453 if (features->type == HID_GENERIC)
454 wacom_wac_usage_mapping(hdev, field, usage);
457 static void wacom_post_parse_hid(struct hid_device *hdev,
458 struct wacom_features *features)
460 struct wacom *wacom = hid_get_drvdata(hdev);
461 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
463 if (features->type == HID_GENERIC) {
464 /* Any last-minute generic device setup */
465 if (wacom_wac->has_mode_change) {
466 if (wacom_wac->is_direct_mode)
467 features->device_type |= WACOM_DEVICETYPE_DIRECT;
469 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
472 if (features->touch_max > 1) {
473 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
474 input_mt_init_slots(wacom_wac->touch_input,
475 wacom_wac->features.touch_max,
478 input_mt_init_slots(wacom_wac->touch_input,
479 wacom_wac->features.touch_max,
485 static void wacom_parse_hid(struct hid_device *hdev,
486 struct wacom_features *features)
488 struct hid_report_enum *rep_enum;
489 struct hid_report *hreport;
492 /* check features first */
493 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
494 list_for_each_entry(hreport, &rep_enum->report_list, list) {
495 for (i = 0; i < hreport->maxfield; i++) {
496 /* Ignore if report count is out of bounds. */
497 if (hreport->field[i]->report_count < 1)
500 for (j = 0; j < hreport->field[i]->maxusage; j++) {
501 wacom_feature_mapping(hdev, hreport->field[i],
502 hreport->field[i]->usage + j);
507 /* now check the input usages */
508 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
509 list_for_each_entry(hreport, &rep_enum->report_list, list) {
511 if (!hreport->maxfield)
514 for (i = 0; i < hreport->maxfield; i++)
515 for (j = 0; j < hreport->field[i]->maxusage; j++)
516 wacom_usage_mapping(hdev, hreport->field[i],
517 hreport->field[i]->usage + j);
520 wacom_post_parse_hid(hdev, features);
523 static int wacom_hid_set_device_mode(struct hid_device *hdev)
525 struct wacom *wacom = hid_get_drvdata(hdev);
526 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
527 struct hid_report *r;
528 struct hid_report_enum *re;
530 if (hid_data->inputmode < 0)
533 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
534 r = re->report_id_hash[hid_data->inputmode];
536 r->field[0]->value[hid_data->inputmode_index] = 2;
537 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
542 static int wacom_set_device_mode(struct hid_device *hdev,
543 struct wacom_wac *wacom_wac)
546 struct hid_report *r;
547 struct hid_report_enum *re;
549 int error = -ENOMEM, limit = 0;
551 if (wacom_wac->mode_report < 0)
554 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
555 r = re->report_id_hash[wacom_wac->mode_report];
559 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
563 length = hid_report_len(r);
566 rep_data[0] = wacom_wac->mode_report;
567 rep_data[1] = wacom_wac->mode_value;
569 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
572 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
573 rep_data, length, 1);
574 } while (error >= 0 &&
575 rep_data[1] != wacom_wac->mode_report &&
576 limit++ < WAC_MSG_RETRIES);
580 return error < 0 ? error : 0;
583 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
584 struct wacom_features *features)
586 struct wacom *wacom = hid_get_drvdata(hdev);
590 switch (features->type) {
594 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
598 rep_data[0] = speed == 0 ? 0x05 : 0x06;
601 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
605 wacom->wacom_wac.bt_high_speed = speed;
611 * Note that if the raw queries fail, it's not a hard failure
612 * and it is safe to continue
614 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
619 wacom->wacom_wac.bt_features &= ~0x20;
621 wacom->wacom_wac.bt_features |= 0x20;
624 rep_data[1] = wacom->wacom_wac.bt_features;
626 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
629 wacom->wacom_wac.bt_high_speed = speed;
637 * Switch the tablet into its most-capable mode. Wacom tablets are
638 * typically configured to power-up in a mode which sends mouse-like
639 * reports to the OS. To get absolute position, pressure data, etc.
640 * from the tablet, it is necessary to switch the tablet out of this
641 * mode and into one which sends the full range of tablet data.
643 static int _wacom_query_tablet_data(struct wacom *wacom)
645 struct hid_device *hdev = wacom->hdev;
646 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
647 struct wacom_features *features = &wacom_wac->features;
649 if (hdev->bus == BUS_BLUETOOTH)
650 return wacom_bt_query_tablet_data(hdev, 1, features);
652 if (features->type != HID_GENERIC) {
653 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
654 if (features->type > TABLETPC) {
655 /* MT Tablet PC touch */
656 wacom_wac->mode_report = 3;
657 wacom_wac->mode_value = 4;
658 } else if (features->type == WACOM_24HDT) {
659 wacom_wac->mode_report = 18;
660 wacom_wac->mode_value = 2;
661 } else if (features->type == WACOM_27QHDT) {
662 wacom_wac->mode_report = 131;
663 wacom_wac->mode_value = 2;
664 } else if (features->type == BAMBOO_PAD) {
665 wacom_wac->mode_report = 2;
666 wacom_wac->mode_value = 2;
668 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
669 if (features->type <= BAMBOO_PT) {
670 wacom_wac->mode_report = 2;
671 wacom_wac->mode_value = 2;
676 wacom_set_device_mode(hdev, wacom_wac);
678 if (features->type == HID_GENERIC)
679 return wacom_hid_set_device_mode(hdev);
684 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
685 struct wacom_features *features)
687 struct wacom *wacom = hid_get_drvdata(hdev);
688 struct usb_interface *intf = wacom->intf;
690 /* default features */
691 features->x_fuzz = 4;
692 features->y_fuzz = 4;
693 features->pressure_fuzz = 0;
694 features->distance_fuzz = 1;
695 features->tilt_fuzz = 1;
698 * The wireless device HID is basic and layout conflicts with
699 * other tablets (monitor and touch interface can look like pen).
700 * Skip the query for this type and modify defaults based on
703 if (features->type == WIRELESS && intf) {
704 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
705 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
707 features->device_type = WACOM_DEVICETYPE_NONE;
711 wacom_parse_hid(hdev, features);
714 struct wacom_hdev_data {
715 struct list_head list;
717 struct hid_device *dev;
718 struct wacom_shared shared;
721 static LIST_HEAD(wacom_udev_list);
722 static DEFINE_MUTEX(wacom_udev_list_lock);
724 static bool wacom_are_sibling(struct hid_device *hdev,
725 struct hid_device *sibling)
727 struct wacom *wacom = hid_get_drvdata(hdev);
728 struct wacom_features *features = &wacom->wacom_wac.features;
729 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
730 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
731 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
732 __u32 oPid = features->oPid ? features->oPid : hdev->product;
734 /* The defined oVid/oPid must match that of the sibling */
735 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
737 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
741 * Devices with the same VID/PID must share the same physical
742 * device path, while those with different VID/PID must share
743 * the same physical parent device path.
745 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
746 if (!hid_compare_device_paths(hdev, sibling, '/'))
749 if (!hid_compare_device_paths(hdev, sibling, '.'))
753 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
754 if (features->type != HID_GENERIC)
758 * Direct-input devices may not be siblings of indirect-input
761 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
762 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
766 * Indirect-input devices may not be siblings of direct-input
769 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
770 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
773 /* Pen devices may only be siblings of touch devices */
774 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
775 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
778 /* Touch devices may only be siblings of pen devices */
779 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
780 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
784 * No reason could be found for these two devices to NOT be
785 * siblings, so there's a good chance they ARE siblings
790 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
792 struct wacom_hdev_data *data;
794 /* Try to find an already-probed interface from the same device */
795 list_for_each_entry(data, &wacom_udev_list, list) {
796 if (hid_compare_device_paths(hdev, data->dev, '/')) {
797 kref_get(&data->kref);
802 /* Fallback to finding devices that appear to be "siblings" */
803 list_for_each_entry(data, &wacom_udev_list, list) {
804 if (wacom_are_sibling(hdev, data->dev)) {
805 kref_get(&data->kref);
813 static void wacom_release_shared_data(struct kref *kref)
815 struct wacom_hdev_data *data =
816 container_of(kref, struct wacom_hdev_data, kref);
818 mutex_lock(&wacom_udev_list_lock);
819 list_del(&data->list);
820 mutex_unlock(&wacom_udev_list_lock);
825 static void wacom_remove_shared_data(void *res)
827 struct wacom *wacom = res;
828 struct wacom_hdev_data *data;
829 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
831 if (wacom_wac->shared) {
832 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
835 if (wacom_wac->shared->touch == wacom->hdev)
836 wacom_wac->shared->touch = NULL;
837 else if (wacom_wac->shared->pen == wacom->hdev)
838 wacom_wac->shared->pen = NULL;
840 kref_put(&data->kref, wacom_release_shared_data);
841 wacom_wac->shared = NULL;
845 static int wacom_add_shared_data(struct hid_device *hdev)
847 struct wacom *wacom = hid_get_drvdata(hdev);
848 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
849 struct wacom_hdev_data *data;
852 mutex_lock(&wacom_udev_list_lock);
854 data = wacom_get_hdev_data(hdev);
856 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
862 kref_init(&data->kref);
864 list_add_tail(&data->list, &wacom_udev_list);
867 wacom_wac->shared = &data->shared;
869 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
871 mutex_unlock(&wacom_udev_list_lock);
872 wacom_remove_shared_data(wacom);
876 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
877 wacom_wac->shared->touch = hdev;
878 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
879 wacom_wac->shared->pen = hdev;
882 mutex_unlock(&wacom_udev_list_lock);
886 static int wacom_led_control(struct wacom *wacom)
890 unsigned char report_id = WAC_CMD_LED_CONTROL;
893 if (!wacom->led.groups)
896 if (wacom->wacom_wac.features.type == REMOTE)
899 if (wacom->wacom_wac.pid) { /* wireless connected */
900 report_id = WAC_CMD_WL_LED_CONTROL;
903 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
904 report_id = WAC_CMD_WL_INTUOSP2;
907 buf = kzalloc(buf_size, GFP_KERNEL);
911 if (wacom->wacom_wac.features.type == HID_GENERIC) {
912 buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
913 buf[1] = wacom->led.llv;
914 buf[2] = wacom->led.groups[0].select & 0x03;
916 } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
917 wacom->wacom_wac.features.type <= INTUOSPL)) {
919 * Touch Ring and crop mark LED luminance may take on
920 * one of four values:
921 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
923 int ring_led = wacom->led.groups[0].select & 0x03;
924 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
926 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
929 if (wacom->wacom_wac.pid) {
930 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
931 buf, buf_size, WAC_CMD_RETRIES);
937 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
939 buf[4] = 100; // Power Connection LED (ORANGE)
940 buf[5] = 100; // BT Connection LED (BLUE)
941 buf[6] = 100; // Paper Mode (RED?)
942 buf[7] = 100; // Paper Mode (GREEN?)
943 buf[8] = 100; // Paper Mode (BLUE?)
944 buf[9] = wacom->led.llv;
945 buf[10] = wacom->led.groups[0].select & 0x03;
948 int led = wacom->led.groups[0].select | 0x4;
950 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
951 wacom->wacom_wac.features.type == WACOM_24HD)
952 led |= (wacom->led.groups[1].select << 4) | 0x40;
956 buf[2] = wacom->led.llv;
957 buf[3] = wacom->led.hlv;
958 buf[4] = wacom->led.img_lum;
961 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
968 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
969 const unsigned len, const void *img)
973 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
975 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
979 /* Send 'start' command */
980 buf[0] = WAC_CMD_ICON_START;
982 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
988 buf[1] = button_id & 0x07;
989 for (i = 0; i < 4; i++) {
991 memcpy(buf + 3, img + i * chunk_len, chunk_len);
993 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
994 buf, chunk_len + 3, WAC_CMD_RETRIES);
1000 buf[0] = WAC_CMD_ICON_START;
1002 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1010 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
1011 const char *buf, size_t count)
1013 struct hid_device *hdev = to_hid_device(dev);
1014 struct wacom *wacom = hid_get_drvdata(hdev);
1018 err = kstrtouint(buf, 10, &id);
1022 mutex_lock(&wacom->lock);
1024 wacom->led.groups[set_id].select = id & 0x3;
1025 err = wacom_led_control(wacom);
1027 mutex_unlock(&wacom->lock);
1029 return err < 0 ? err : count;
1032 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
1033 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
1034 struct device_attribute *attr, const char *buf, size_t count) \
1036 return wacom_led_select_store(dev, SET_ID, buf, count); \
1038 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
1039 struct device_attribute *attr, char *buf) \
1041 struct hid_device *hdev = to_hid_device(dev);\
1042 struct wacom *wacom = hid_get_drvdata(hdev); \
1043 return scnprintf(buf, PAGE_SIZE, "%d\n", \
1044 wacom->led.groups[SET_ID].select); \
1046 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
1047 wacom_led##SET_ID##_select_show, \
1048 wacom_led##SET_ID##_select_store)
1050 DEVICE_LED_SELECT_ATTR(0);
1051 DEVICE_LED_SELECT_ATTR(1);
1053 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
1054 const char *buf, size_t count)
1059 err = kstrtouint(buf, 10, &value);
1063 mutex_lock(&wacom->lock);
1065 *dest = value & 0x7f;
1066 err = wacom_led_control(wacom);
1068 mutex_unlock(&wacom->lock);
1070 return err < 0 ? err : count;
1073 #define DEVICE_LUMINANCE_ATTR(name, field) \
1074 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
1075 struct device_attribute *attr, const char *buf, size_t count) \
1077 struct hid_device *hdev = to_hid_device(dev);\
1078 struct wacom *wacom = hid_get_drvdata(hdev); \
1080 return wacom_luminance_store(wacom, &wacom->led.field, \
1083 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
1084 struct device_attribute *attr, char *buf) \
1086 struct wacom *wacom = dev_get_drvdata(dev); \
1087 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
1089 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
1090 wacom_##name##_luminance_show, \
1091 wacom_##name##_luminance_store)
1093 DEVICE_LUMINANCE_ATTR(status0, llv);
1094 DEVICE_LUMINANCE_ATTR(status1, hlv);
1095 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
1097 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
1098 const char *buf, size_t count)
1100 struct hid_device *hdev = to_hid_device(dev);
1101 struct wacom *wacom = hid_get_drvdata(hdev);
1106 if (hdev->bus == BUS_BLUETOOTH) {
1108 xfer_id = WAC_CMD_ICON_BT_XFER;
1111 xfer_id = WAC_CMD_ICON_XFER;
1117 mutex_lock(&wacom->lock);
1119 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
1121 mutex_unlock(&wacom->lock);
1123 return err < 0 ? err : count;
1126 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1127 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1128 struct device_attribute *attr, const char *buf, size_t count) \
1130 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1132 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
1133 NULL, wacom_btnimg##BUTTON_ID##_store)
1135 DEVICE_BTNIMG_ATTR(0);
1136 DEVICE_BTNIMG_ATTR(1);
1137 DEVICE_BTNIMG_ATTR(2);
1138 DEVICE_BTNIMG_ATTR(3);
1139 DEVICE_BTNIMG_ATTR(4);
1140 DEVICE_BTNIMG_ATTR(5);
1141 DEVICE_BTNIMG_ATTR(6);
1142 DEVICE_BTNIMG_ATTR(7);
1144 static struct attribute *cintiq_led_attrs[] = {
1145 &dev_attr_status_led0_select.attr,
1146 &dev_attr_status_led1_select.attr,
1150 static struct attribute_group cintiq_led_attr_group = {
1151 .name = "wacom_led",
1152 .attrs = cintiq_led_attrs,
1155 static struct attribute *intuos4_led_attrs[] = {
1156 &dev_attr_status0_luminance.attr,
1157 &dev_attr_status1_luminance.attr,
1158 &dev_attr_status_led0_select.attr,
1159 &dev_attr_buttons_luminance.attr,
1160 &dev_attr_button0_rawimg.attr,
1161 &dev_attr_button1_rawimg.attr,
1162 &dev_attr_button2_rawimg.attr,
1163 &dev_attr_button3_rawimg.attr,
1164 &dev_attr_button4_rawimg.attr,
1165 &dev_attr_button5_rawimg.attr,
1166 &dev_attr_button6_rawimg.attr,
1167 &dev_attr_button7_rawimg.attr,
1171 static struct attribute_group intuos4_led_attr_group = {
1172 .name = "wacom_led",
1173 .attrs = intuos4_led_attrs,
1176 static struct attribute *intuos5_led_attrs[] = {
1177 &dev_attr_status0_luminance.attr,
1178 &dev_attr_status_led0_select.attr,
1182 static struct attribute_group intuos5_led_attr_group = {
1183 .name = "wacom_led",
1184 .attrs = intuos5_led_attrs,
1187 static struct attribute *generic_led_attrs[] = {
1188 &dev_attr_status0_luminance.attr,
1189 &dev_attr_status_led0_select.attr,
1193 static struct attribute_group generic_led_attr_group = {
1194 .name = "wacom_led",
1195 .attrs = generic_led_attrs,
1198 struct wacom_sysfs_group_devres {
1199 struct attribute_group *group;
1200 struct kobject *root;
1203 static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1205 struct wacom_sysfs_group_devres *devres = res;
1206 struct kobject *kobj = devres->root;
1208 dev_dbg(dev, "%s: dropping reference to %s\n",
1209 __func__, devres->group->name);
1210 sysfs_remove_group(kobj, devres->group);
1213 static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1214 struct kobject *root,
1215 struct attribute_group *group)
1217 struct wacom_sysfs_group_devres *devres;
1220 devres = devres_alloc(wacom_devm_sysfs_group_release,
1221 sizeof(struct wacom_sysfs_group_devres),
1226 devres->group = group;
1227 devres->root = root;
1229 error = sysfs_create_group(devres->root, group);
1231 devres_free(devres);
1235 devres_add(&wacom->hdev->dev, devres);
1240 static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1241 struct attribute_group *group)
1243 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1247 static void wacom_devm_kfifo_release(struct device *dev, void *res)
1249 struct kfifo_rec_ptr_2 *devres = res;
1254 static int wacom_devm_kfifo_alloc(struct wacom *wacom)
1256 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1257 struct kfifo_rec_ptr_2 *pen_fifo;
1260 pen_fifo = devres_alloc(wacom_devm_kfifo_release,
1261 sizeof(struct kfifo_rec_ptr_2),
1267 error = kfifo_alloc(pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
1269 devres_free(pen_fifo);
1273 devres_add(&wacom->hdev->dev, pen_fifo);
1274 wacom_wac->pen_fifo = pen_fifo;
1279 enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
1281 struct wacom *wacom = led->wacom;
1283 if (wacom->led.max_hlv)
1284 return led->hlv * LED_FULL / wacom->led.max_hlv;
1286 if (wacom->led.max_llv)
1287 return led->llv * LED_FULL / wacom->led.max_llv;
1289 /* device doesn't support brightness tuning */
1293 static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1295 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1296 struct wacom *wacom = led->wacom;
1298 if (wacom->led.groups[led->group].select != led->id)
1301 return wacom_leds_brightness_get(led);
1304 static int wacom_led_brightness_set(struct led_classdev *cdev,
1305 enum led_brightness brightness)
1307 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1308 struct wacom *wacom = led->wacom;
1311 mutex_lock(&wacom->lock);
1313 if (!wacom->led.groups || (brightness == LED_OFF &&
1314 wacom->led.groups[led->group].select != led->id)) {
1319 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1320 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1322 wacom->led.groups[led->group].select = led->id;
1324 error = wacom_led_control(wacom);
1327 mutex_unlock(&wacom->lock);
1332 static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1333 enum led_brightness brightness)
1337 static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1338 struct wacom_led *led, unsigned int group,
1339 unsigned int id, bool read_only)
1344 name = devm_kasprintf(dev, GFP_KERNEL,
1353 led->trigger.name = name;
1354 error = devm_led_trigger_register(dev, &led->trigger);
1356 hid_err(wacom->hdev,
1357 "failed to register LED trigger %s: %d\n",
1358 led->cdev.name, error);
1366 led->llv = wacom->led.llv;
1367 led->hlv = wacom->led.hlv;
1368 led->cdev.name = name;
1369 led->cdev.max_brightness = LED_FULL;
1370 led->cdev.flags = LED_HW_PLUGGABLE;
1371 led->cdev.brightness_get = __wacom_led_brightness_get;
1373 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
1374 led->cdev.default_trigger = led->cdev.name;
1376 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
1379 error = devm_led_classdev_register(dev, &led->cdev);
1381 hid_err(wacom->hdev,
1382 "failed to register LED %s: %d\n",
1383 led->cdev.name, error);
1384 led->cdev.name = NULL;
1391 static void wacom_led_groups_release_one(void *data)
1393 struct wacom_group_leds *group = data;
1395 devres_release_group(group->dev, group);
1398 static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1399 struct wacom *wacom,
1400 int group_id, int count,
1403 struct wacom_led *leds;
1406 if (group_id >= wacom->led.count || count <= 0)
1409 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1412 leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL);
1418 wacom->led.groups[group_id].leds = leds;
1419 wacom->led.groups[group_id].count = count;
1421 for (i = 0; i < count; i++) {
1422 error = wacom_led_register_one(dev, wacom, &leds[i],
1423 group_id, i, read_only);
1428 wacom->led.groups[group_id].dev = dev;
1430 devres_close_group(dev, &wacom->led.groups[group_id]);
1433 * There is a bug (?) in devm_led_classdev_register() in which its
1434 * increments the refcount of the parent. If the parent is an input
1435 * device, that means the ref count never reaches 0 when
1436 * devm_input_device_release() gets called.
1437 * This means that the LEDs are still there after disconnect.
1438 * Manually force the release of the group so that the leds are released
1439 * once we are done using them.
1441 error = devm_add_action_or_reset(&wacom->hdev->dev,
1442 wacom_led_groups_release_one,
1443 &wacom->led.groups[group_id]);
1450 devres_release_group(dev, &wacom->led.groups[group_id]);
1454 struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1457 struct wacom_group_leds *group;
1459 if (group_id >= wacom->led.count)
1462 group = &wacom->led.groups[group_id];
1469 return &group->leds[id];
1473 * wacom_led_next: gives the next available led with a wacom trigger.
1475 * returns the next available struct wacom_led which has its default trigger
1476 * or the current one if none is available.
1478 struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1480 struct wacom_led *next_led;
1490 next_led = wacom_led_find(wacom, group, ++next);
1491 if (!next_led || next_led == cur)
1493 } while (next_led->cdev.trigger != &next_led->trigger);
1498 static void wacom_led_groups_release(void *data)
1500 struct wacom *wacom = data;
1502 wacom->led.groups = NULL;
1503 wacom->led.count = 0;
1506 static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1508 struct device *dev = &wacom->hdev->dev;
1509 struct wacom_group_leds *groups;
1512 groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds),
1517 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1521 wacom->led.groups = groups;
1522 wacom->led.count = count;
1527 static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1528 int led_per_group, bool read_only)
1533 if (!wacom->wacom_wac.pad_input)
1536 dev = &wacom->wacom_wac.pad_input->dev;
1538 error = wacom_led_groups_allocate(wacom, group_count);
1542 for (i = 0; i < group_count; i++) {
1543 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1553 int wacom_initialize_leds(struct wacom *wacom)
1557 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1560 /* Initialize default values */
1561 switch (wacom->wacom_wac.features.type) {
1563 if (!wacom->generic_has_leds)
1565 wacom->led.llv = 100;
1566 wacom->led.max_llv = 100;
1568 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1570 hid_err(wacom->hdev,
1571 "cannot create leds err: %d\n", error);
1575 error = wacom_devm_sysfs_create_group(wacom,
1576 &generic_led_attr_group);
1583 wacom->led.llv = 10;
1584 wacom->led.hlv = 20;
1585 wacom->led.max_llv = 127;
1586 wacom->led.max_hlv = 127;
1587 wacom->led.img_lum = 10;
1589 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1591 hid_err(wacom->hdev,
1592 "cannot create leds err: %d\n", error);
1596 error = wacom_devm_sysfs_create_group(wacom,
1597 &intuos4_led_attr_group);
1604 wacom->led.img_lum = 0;
1606 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1608 hid_err(wacom->hdev,
1609 "cannot create leds err: %d\n", error);
1613 error = wacom_devm_sysfs_create_group(wacom,
1614 &cintiq_led_attr_group);
1623 wacom->led.llv = 32;
1624 wacom->led.max_llv = 96;
1626 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1628 hid_err(wacom->hdev,
1629 "cannot create leds err: %d\n", error);
1633 error = wacom_devm_sysfs_create_group(wacom,
1634 &intuos5_led_attr_group);
1638 wacom->led.llv = 50;
1639 wacom->led.max_llv = 100;
1640 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1642 hid_err(wacom->hdev,
1643 "cannot create leds err: %d\n", error);
1649 wacom->led.llv = 255;
1650 wacom->led.max_llv = 255;
1651 error = wacom_led_groups_allocate(wacom, 5);
1653 hid_err(wacom->hdev,
1654 "cannot create leds err: %d\n", error);
1664 hid_err(wacom->hdev,
1665 "cannot create sysfs group err: %d\n", error);
1672 static void wacom_init_work(struct work_struct *work)
1674 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1676 _wacom_query_tablet_data(wacom);
1677 wacom_led_control(wacom);
1680 static void wacom_query_tablet_data(struct wacom *wacom)
1682 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1685 static enum power_supply_property wacom_battery_props[] = {
1686 POWER_SUPPLY_PROP_MODEL_NAME,
1687 POWER_SUPPLY_PROP_PRESENT,
1688 POWER_SUPPLY_PROP_STATUS,
1689 POWER_SUPPLY_PROP_SCOPE,
1690 POWER_SUPPLY_PROP_CAPACITY
1693 static int wacom_battery_get_property(struct power_supply *psy,
1694 enum power_supply_property psp,
1695 union power_supply_propval *val)
1697 struct wacom_battery *battery = power_supply_get_drvdata(psy);
1701 case POWER_SUPPLY_PROP_MODEL_NAME:
1702 val->strval = battery->wacom->wacom_wac.name;
1704 case POWER_SUPPLY_PROP_PRESENT:
1705 val->intval = battery->bat_connected;
1707 case POWER_SUPPLY_PROP_SCOPE:
1708 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1710 case POWER_SUPPLY_PROP_CAPACITY:
1711 val->intval = battery->battery_capacity;
1713 case POWER_SUPPLY_PROP_STATUS:
1714 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1715 val->intval = battery->bat_status;
1716 else if (battery->bat_charging)
1717 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1718 else if (battery->battery_capacity == 100 &&
1719 battery->ps_connected)
1720 val->intval = POWER_SUPPLY_STATUS_FULL;
1721 else if (battery->ps_connected)
1722 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1724 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1734 static int __wacom_initialize_battery(struct wacom *wacom,
1735 struct wacom_battery *battery)
1737 static atomic_t battery_no = ATOMIC_INIT(0);
1738 struct device *dev = &wacom->hdev->dev;
1739 struct power_supply_config psy_cfg = { .drv_data = battery, };
1740 struct power_supply *ps_bat;
1741 struct power_supply_desc *bat_desc = &battery->bat_desc;
1745 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1748 battery->wacom = wacom;
1750 n = atomic_inc_return(&battery_no) - 1;
1752 bat_desc->properties = wacom_battery_props;
1753 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1754 bat_desc->get_property = wacom_battery_get_property;
1755 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1756 bat_desc->name = battery->bat_name;
1757 bat_desc->type = POWER_SUPPLY_TYPE_USB;
1758 bat_desc->use_for_apm = 0;
1760 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1761 if (IS_ERR(ps_bat)) {
1762 error = PTR_ERR(ps_bat);
1766 power_supply_powers(ps_bat, &wacom->hdev->dev);
1768 battery->battery = ps_bat;
1770 devres_close_group(dev, bat_desc);
1774 devres_release_group(dev, bat_desc);
1778 static int wacom_initialize_battery(struct wacom *wacom)
1780 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1781 return __wacom_initialize_battery(wacom, &wacom->battery);
1786 static void wacom_destroy_battery(struct wacom *wacom)
1788 if (wacom->battery.battery) {
1789 devres_release_group(&wacom->hdev->dev,
1790 &wacom->battery.bat_desc);
1791 wacom->battery.battery = NULL;
1795 static ssize_t wacom_show_speed(struct device *dev,
1796 struct device_attribute
1799 struct hid_device *hdev = to_hid_device(dev);
1800 struct wacom *wacom = hid_get_drvdata(hdev);
1802 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1805 static ssize_t wacom_store_speed(struct device *dev,
1806 struct device_attribute *attr,
1807 const char *buf, size_t count)
1809 struct hid_device *hdev = to_hid_device(dev);
1810 struct wacom *wacom = hid_get_drvdata(hdev);
1813 if (kstrtou8(buf, 0, &new_speed))
1816 if (new_speed != 0 && new_speed != 1)
1819 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1824 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1825 wacom_show_speed, wacom_store_speed);
1828 static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1829 struct kobj_attribute *kattr,
1830 char *buf, int index)
1832 struct device *dev = kobj_to_dev(kobj->parent);
1833 struct hid_device *hdev = to_hid_device(dev);
1834 struct wacom *wacom = hid_get_drvdata(hdev);
1837 mode = wacom->led.groups[index].select;
1838 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
1841 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1842 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1843 struct kobj_attribute *kattr, char *buf) \
1845 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1847 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1848 .attr = {.name = "remote_mode", \
1849 .mode = DEV_ATTR_RO_PERM}, \
1850 .show = wacom_show_remote##SET_ID##_mode, \
1852 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1853 &remote##SET_ID##_mode_attr.attr, \
1856 static struct attribute_group remote##SET_ID##_serial_group = { \
1858 .attrs = remote##SET_ID##_serial_attrs, \
1861 DEVICE_EKR_ATTR_GROUP(0);
1862 DEVICE_EKR_ATTR_GROUP(1);
1863 DEVICE_EKR_ATTR_GROUP(2);
1864 DEVICE_EKR_ATTR_GROUP(3);
1865 DEVICE_EKR_ATTR_GROUP(4);
1867 static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1871 struct wacom_remote *remote = wacom->remote;
1873 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1876 if (!remote->remotes[index].group.name)
1879 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1880 &remote->remotes[index].group);
1882 remote->remotes[index].group.name = NULL;
1883 hid_err(wacom->hdev,
1884 "cannot create sysfs group err: %d\n", error);
1891 static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1893 const size_t buf_size = 2;
1897 buf = kzalloc(buf_size, GFP_KERNEL);
1901 buf[0] = WAC_CMD_DELETE_PAIRING;
1904 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1905 buf_size, WAC_CMD_RETRIES);
1911 static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1912 struct kobj_attribute *attr,
1913 const char *buf, size_t count)
1915 unsigned char selector = 0;
1916 struct device *dev = kobj_to_dev(kobj->parent);
1917 struct hid_device *hdev = to_hid_device(dev);
1918 struct wacom *wacom = hid_get_drvdata(hdev);
1921 if (!strncmp(buf, "*\n", 2)) {
1922 selector = WAC_CMD_UNPAIR_ALL;
1924 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1929 mutex_lock(&wacom->lock);
1931 err = wacom_cmd_unpair_remote(wacom, selector);
1932 mutex_unlock(&wacom->lock);
1934 return err < 0 ? err : count;
1937 static struct kobj_attribute unpair_remote_attr = {
1938 .attr = {.name = "unpair_remote", .mode = 0200},
1939 .store = wacom_store_unpair_remote,
1942 static const struct attribute *remote_unpair_attrs[] = {
1943 &unpair_remote_attr.attr,
1947 static void wacom_remotes_destroy(void *data)
1949 struct wacom *wacom = data;
1950 struct wacom_remote *remote = wacom->remote;
1955 kobject_put(remote->remote_dir);
1956 kfifo_free(&remote->remote_fifo);
1957 wacom->remote = NULL;
1960 static int wacom_initialize_remotes(struct wacom *wacom)
1963 struct wacom_remote *remote;
1966 if (wacom->wacom_wac.features.type != REMOTE)
1969 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1974 wacom->remote = remote;
1976 spin_lock_init(&remote->remote_lock);
1978 error = kfifo_alloc(&remote->remote_fifo,
1979 5 * sizeof(struct wacom_remote_data),
1982 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1986 remote->remotes[0].group = remote0_serial_group;
1987 remote->remotes[1].group = remote1_serial_group;
1988 remote->remotes[2].group = remote2_serial_group;
1989 remote->remotes[3].group = remote3_serial_group;
1990 remote->remotes[4].group = remote4_serial_group;
1992 remote->remote_dir = kobject_create_and_add("wacom_remote",
1993 &wacom->hdev->dev.kobj);
1994 if (!remote->remote_dir)
1997 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
2000 hid_err(wacom->hdev,
2001 "cannot create sysfs group err: %d\n", error);
2005 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2006 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2007 remote->remotes[i].serial = 0;
2010 error = devm_add_action_or_reset(&wacom->hdev->dev,
2011 wacom_remotes_destroy, wacom);
2018 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
2020 struct input_dev *input_dev;
2021 struct hid_device *hdev = wacom->hdev;
2022 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2024 input_dev = devm_input_allocate_device(&hdev->dev);
2028 input_dev->name = wacom_wac->features.name;
2029 input_dev->phys = hdev->phys;
2030 input_dev->dev.parent = &hdev->dev;
2031 input_dev->open = wacom_open;
2032 input_dev->close = wacom_close;
2033 input_dev->uniq = hdev->uniq;
2034 input_dev->id.bustype = hdev->bus;
2035 input_dev->id.vendor = hdev->vendor;
2036 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
2037 input_dev->id.version = hdev->version;
2038 input_set_drvdata(input_dev, wacom);
2043 static int wacom_allocate_inputs(struct wacom *wacom)
2045 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2047 wacom_wac->pen_input = wacom_allocate_input(wacom);
2048 wacom_wac->touch_input = wacom_allocate_input(wacom);
2049 wacom_wac->pad_input = wacom_allocate_input(wacom);
2050 if (!wacom_wac->pen_input ||
2051 !wacom_wac->touch_input ||
2052 !wacom_wac->pad_input)
2055 wacom_wac->pen_input->name = wacom_wac->pen_name;
2056 wacom_wac->touch_input->name = wacom_wac->touch_name;
2057 wacom_wac->pad_input->name = wacom_wac->pad_name;
2062 static int wacom_register_inputs(struct wacom *wacom)
2064 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2065 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2068 pen_input_dev = wacom_wac->pen_input;
2069 touch_input_dev = wacom_wac->touch_input;
2070 pad_input_dev = wacom_wac->pad_input;
2072 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
2075 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
2077 /* no pen in use on this interface */
2078 input_free_device(pen_input_dev);
2079 wacom_wac->pen_input = NULL;
2080 pen_input_dev = NULL;
2082 error = input_register_device(pen_input_dev);
2087 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
2089 /* no touch in use on this interface */
2090 input_free_device(touch_input_dev);
2091 wacom_wac->touch_input = NULL;
2092 touch_input_dev = NULL;
2094 error = input_register_device(touch_input_dev);
2099 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2101 /* no pad events using this interface */
2102 input_free_device(pad_input_dev);
2103 wacom_wac->pad_input = NULL;
2104 pad_input_dev = NULL;
2106 error = input_register_device(pad_input_dev);
2114 wacom_wac->pad_input = NULL;
2115 wacom_wac->touch_input = NULL;
2116 wacom_wac->pen_input = NULL;
2121 * Not all devices report physical dimensions from HID.
2122 * Compute the default from hardcoded logical dimension
2123 * and resolution before driver overwrites them.
2125 static void wacom_set_default_phy(struct wacom_features *features)
2127 if (features->x_resolution) {
2128 features->x_phy = (features->x_max * 100) /
2129 features->x_resolution;
2130 features->y_phy = (features->y_max * 100) /
2131 features->y_resolution;
2135 static void wacom_calculate_res(struct wacom_features *features)
2137 /* set unit to "100th of a mm" for devices not reported by HID */
2138 if (!features->unit) {
2139 features->unit = 0x11;
2140 features->unitExpo = -3;
2143 features->x_resolution = wacom_calc_hid_res(features->x_max,
2146 features->unitExpo);
2147 features->y_resolution = wacom_calc_hid_res(features->y_max,
2150 features->unitExpo);
2153 void wacom_battery_work(struct work_struct *work)
2155 struct wacom *wacom = container_of(work, struct wacom, battery_work);
2157 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2158 !wacom->battery.battery) {
2159 wacom_initialize_battery(wacom);
2161 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2162 wacom->battery.battery) {
2163 wacom_destroy_battery(wacom);
2167 static size_t wacom_compute_pktlen(struct hid_device *hdev)
2169 struct hid_report_enum *report_enum;
2170 struct hid_report *report;
2173 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2175 list_for_each_entry(report, &report_enum->report_list, list) {
2176 size_t report_size = hid_report_len(report);
2177 if (report_size > size)
2184 static void wacom_update_name(struct wacom *wacom, const char *suffix)
2186 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2187 struct wacom_features *features = &wacom_wac->features;
2188 char name[WACOM_NAME_MAX - 20]; /* Leave some room for suffixes */
2190 /* Generic devices name unspecified */
2191 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
2192 char *product_name = wacom->hdev->name;
2194 if (hid_is_usb(wacom->hdev)) {
2195 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2196 struct usb_device *dev = interface_to_usbdev(intf);
2197 product_name = dev->product;
2200 if (wacom->hdev->bus == BUS_I2C) {
2201 snprintf(name, sizeof(name), "%s %X",
2202 features->name, wacom->hdev->product);
2203 } else if (strstr(product_name, "Wacom") ||
2204 strstr(product_name, "wacom") ||
2205 strstr(product_name, "WACOM")) {
2206 strlcpy(name, product_name, sizeof(name));
2208 snprintf(name, sizeof(name), "Wacom %s", product_name);
2211 /* strip out excess whitespaces */
2213 char *gap = strstr(name, " ");
2216 /* shift everything including the terminator */
2217 memmove(gap, gap+1, strlen(gap));
2220 /* get rid of trailing whitespace */
2221 if (name[strlen(name)-1] == ' ')
2222 name[strlen(name)-1] = '\0';
2224 strlcpy(name, features->name, sizeof(name));
2227 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2230 /* Append the device type to the name */
2231 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
2232 "%s%s Pen", name, suffix);
2233 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
2234 "%s%s Finger", name, suffix);
2235 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
2236 "%s%s Pad", name, suffix);
2239 static void wacom_release_resources(struct wacom *wacom)
2241 struct hid_device *hdev = wacom->hdev;
2243 if (!wacom->resources)
2246 devres_release_group(&hdev->dev, wacom);
2248 wacom->resources = false;
2250 wacom->wacom_wac.pen_input = NULL;
2251 wacom->wacom_wac.touch_input = NULL;
2252 wacom->wacom_wac.pad_input = NULL;
2255 static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2257 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2258 wacom_wac->shared->type = wacom_wac->features.type;
2259 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2262 if (wacom_wac->has_mute_touch_switch) {
2263 wacom_wac->shared->has_mute_touch_switch = true;
2264 wacom_wac->shared->is_touch_on = true;
2267 if (wacom_wac->shared->has_mute_touch_switch &&
2268 wacom_wac->shared->touch_input) {
2269 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2270 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2275 static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
2277 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2278 struct wacom_features *features = &wacom_wac->features;
2279 struct hid_device *hdev = wacom->hdev;
2281 unsigned int connect_mask = HID_CONNECT_HIDRAW;
2283 features->pktlen = wacom_compute_pktlen(hdev);
2284 if (features->pktlen > WACOM_PKGLEN_MAX)
2287 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2290 wacom->resources = true;
2292 error = wacom_allocate_inputs(wacom);
2297 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2298 * into debug mode for the touch part.
2299 * We ignore the other interfaces.
2301 if (features->type == BAMBOO_PAD) {
2302 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2303 features->type = HID_GENERIC;
2304 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2305 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2311 /* set the default size in case we do not get them from hid */
2312 wacom_set_default_phy(features);
2314 /* Retrieve the physical and logical size for touch devices */
2315 wacom_retrieve_hid_descriptor(hdev, features);
2316 wacom_setup_device_quirks(wacom);
2318 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2319 features->type != WIRELESS) {
2320 error = features->type == HID_GENERIC ? -ENODEV : 0;
2322 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
2324 error ? "Ignoring" : "Assuming pen");
2329 features->device_type |= WACOM_DEVICETYPE_PEN;
2332 wacom_calculate_res(features);
2334 wacom_update_name(wacom, wireless ? " (WL)" : "");
2336 /* pen only Bamboo neither support touch nor pad */
2337 if ((features->type == BAMBOO_PEN) &&
2338 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2339 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2344 error = wacom_add_shared_data(hdev);
2348 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2349 (features->quirks & WACOM_QUIRK_BATTERY)) {
2350 error = wacom_initialize_battery(wacom);
2355 error = wacom_register_inputs(wacom);
2359 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
2360 error = wacom_initialize_leds(wacom);
2364 error = wacom_initialize_remotes(wacom);
2369 if (features->type == HID_GENERIC)
2370 connect_mask |= HID_CONNECT_DRIVER;
2372 /* Regular HID work starts now */
2373 error = hid_hw_start(hdev, connect_mask);
2375 hid_err(hdev, "hw start failed\n");
2380 /* Note that if query fails it is not a hard failure */
2381 wacom_query_tablet_data(wacom);
2384 /* touch only Bamboo doesn't support pen */
2385 if ((features->type == BAMBOO_TOUCH) &&
2386 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2387 cancel_delayed_work_sync(&wacom->init_work);
2388 _wacom_query_tablet_data(wacom);
2393 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR) {
2394 error = hid_hw_open(hdev);
2396 hid_err(hdev, "hw open failed\n");
2401 wacom_set_shared_values(wacom_wac);
2402 devres_close_group(&hdev->dev, wacom);
2409 wacom_release_resources(wacom);
2413 static void wacom_wireless_work(struct work_struct *work)
2415 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2416 struct usb_device *usbdev = wacom->usbdev;
2417 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2418 struct hid_device *hdev1, *hdev2;
2419 struct wacom *wacom1, *wacom2;
2420 struct wacom_wac *wacom_wac1, *wacom_wac2;
2424 * Regardless if this is a disconnect or a new tablet,
2425 * remove any existing input and battery devices.
2428 wacom_destroy_battery(wacom);
2433 /* Stylus interface */
2434 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2435 wacom1 = hid_get_drvdata(hdev1);
2436 wacom_wac1 = &(wacom1->wacom_wac);
2437 wacom_release_resources(wacom1);
2439 /* Touch interface */
2440 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2441 wacom2 = hid_get_drvdata(hdev2);
2442 wacom_wac2 = &(wacom2->wacom_wac);
2443 wacom_release_resources(wacom2);
2445 if (wacom_wac->pid == 0) {
2446 hid_info(wacom->hdev, "wireless tablet disconnected\n");
2448 const struct hid_device_id *id = wacom_ids;
2450 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2454 if (id->vendor == USB_VENDOR_ID_WACOM &&
2455 id->product == wacom_wac->pid)
2461 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2465 /* Stylus interface */
2466 wacom_wac1->features =
2467 *((struct wacom_features *)id->driver_data);
2469 wacom_wac1->pid = wacom_wac->pid;
2471 error = wacom_parse_and_register(wacom1, true);
2475 /* Touch interface */
2476 if (wacom_wac1->features.touch_max ||
2477 (wacom_wac1->features.type >= INTUOSHT &&
2478 wacom_wac1->features.type <= BAMBOO_PT)) {
2479 wacom_wac2->features =
2480 *((struct wacom_features *)id->driver_data);
2481 wacom_wac2->pid = wacom_wac->pid;
2483 error = wacom_parse_and_register(wacom2, true);
2488 strlcpy(wacom_wac->name, wacom_wac1->name,
2489 sizeof(wacom_wac->name));
2490 error = wacom_initialize_battery(wacom);
2498 wacom_release_resources(wacom1);
2499 wacom_release_resources(wacom2);
2503 static void wacom_remote_destroy_battery(struct wacom *wacom, int index)
2505 struct wacom_remote *remote = wacom->remote;
2507 if (remote->remotes[index].battery.battery) {
2508 devres_release_group(&wacom->hdev->dev,
2509 &remote->remotes[index].battery.bat_desc);
2510 remote->remotes[index].battery.battery = NULL;
2511 remote->remotes[index].active_time = 0;
2515 static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2517 struct wacom_remote *remote = wacom->remote;
2518 u32 serial = remote->remotes[index].serial;
2520 unsigned long flags;
2522 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2523 if (remote->remotes[i].serial == serial) {
2525 spin_lock_irqsave(&remote->remote_lock, flags);
2526 remote->remotes[i].registered = false;
2527 spin_unlock_irqrestore(&remote->remote_lock, flags);
2529 wacom_remote_destroy_battery(wacom, i);
2531 if (remote->remotes[i].group.name)
2532 devres_release_group(&wacom->hdev->dev,
2533 &remote->remotes[i]);
2535 remote->remotes[i].serial = 0;
2536 remote->remotes[i].group.name = NULL;
2537 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2542 static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2545 struct wacom_remote *remote = wacom->remote;
2546 struct device *dev = &wacom->hdev->dev;
2549 /* A remote can pair more than once with an EKR,
2550 * check to make sure this serial isn't already paired.
2552 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2553 if (remote->remotes[k].serial == serial)
2557 if (k < WACOM_MAX_REMOTES) {
2558 remote->remotes[index].serial = serial;
2562 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2565 error = wacom_remote_create_attr_group(wacom, serial, index);
2569 remote->remotes[index].input = wacom_allocate_input(wacom);
2570 if (!remote->remotes[index].input) {
2574 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2575 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2577 if (!remote->remotes[index].input->name) {
2582 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2587 remote->remotes[index].serial = serial;
2589 error = input_register_device(remote->remotes[index].input);
2593 error = wacom_led_groups_alloc_and_register_one(
2594 &remote->remotes[index].input->dev,
2595 wacom, index, 3, true);
2599 remote->remotes[index].registered = true;
2601 devres_close_group(dev, &remote->remotes[index]);
2605 devres_release_group(dev, &remote->remotes[index]);
2606 remote->remotes[index].serial = 0;
2610 static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2612 struct wacom_remote *remote = wacom->remote;
2615 if (!remote->remotes[index].registered)
2618 if (remote->remotes[index].battery.battery)
2621 if (!remote->remotes[index].active_time)
2624 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2627 error = __wacom_initialize_battery(wacom,
2628 &wacom->remote->remotes[index].battery);
2635 static void wacom_remote_work(struct work_struct *work)
2637 struct wacom *wacom = container_of(work, struct wacom, remote_work);
2638 struct wacom_remote *remote = wacom->remote;
2639 ktime_t kt = ktime_get();
2640 struct wacom_remote_data data;
2641 unsigned long flags;
2646 spin_lock_irqsave(&remote->remote_lock, flags);
2648 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2650 if (count != sizeof(data)) {
2651 hid_err(wacom->hdev,
2652 "workitem triggered without status available\n");
2653 spin_unlock_irqrestore(&remote->remote_lock, flags);
2657 if (!kfifo_is_empty(&remote->remote_fifo))
2658 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2660 spin_unlock_irqrestore(&remote->remote_lock, flags);
2662 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2663 serial = data.remote[i].serial;
2664 if (data.remote[i].connected) {
2666 if (kt - remote->remotes[i].active_time > WACOM_REMOTE_BATTERY_TIMEOUT
2667 && remote->remotes[i].active_time != 0)
2668 wacom_remote_destroy_battery(wacom, i);
2670 if (remote->remotes[i].serial == serial) {
2671 wacom_remote_attach_battery(wacom, i);
2675 if (remote->remotes[i].serial)
2676 wacom_remote_destroy_one(wacom, i);
2678 wacom_remote_create_one(wacom, serial, i);
2680 } else if (remote->remotes[i].serial) {
2681 wacom_remote_destroy_one(wacom, i);
2686 static void wacom_mode_change_work(struct work_struct *work)
2688 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2689 struct wacom_shared *shared = wacom->wacom_wac.shared;
2690 struct wacom *wacom1 = NULL;
2691 struct wacom *wacom2 = NULL;
2692 bool is_direct = wacom->wacom_wac.is_direct_mode;
2696 wacom1 = hid_get_drvdata(shared->pen);
2697 wacom_release_resources(wacom1);
2698 hid_hw_stop(wacom1->hdev);
2699 wacom1->wacom_wac.has_mode_change = true;
2700 wacom1->wacom_wac.is_direct_mode = is_direct;
2703 if (shared->touch) {
2704 wacom2 = hid_get_drvdata(shared->touch);
2705 wacom_release_resources(wacom2);
2706 hid_hw_stop(wacom2->hdev);
2707 wacom2->wacom_wac.has_mode_change = true;
2708 wacom2->wacom_wac.is_direct_mode = is_direct;
2712 error = wacom_parse_and_register(wacom1, false);
2718 error = wacom_parse_and_register(wacom2, false);
2726 static int wacom_probe(struct hid_device *hdev,
2727 const struct hid_device_id *id)
2729 struct wacom *wacom;
2730 struct wacom_wac *wacom_wac;
2731 struct wacom_features *features;
2734 if (!id->driver_data)
2737 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2739 /* hid-core sets this quirk for the boot interface */
2740 hdev->quirks &= ~HID_QUIRK_NOGET;
2742 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2746 hid_set_drvdata(hdev, wacom);
2749 wacom_wac = &wacom->wacom_wac;
2750 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2751 features = &wacom_wac->features;
2753 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2758 error = wacom_devm_kfifo_alloc(wacom);
2762 wacom_wac->hid_data.inputmode = -1;
2763 wacom_wac->mode_report = -1;
2765 if (hid_is_usb(hdev)) {
2766 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2767 struct usb_device *dev = interface_to_usbdev(intf);
2769 wacom->usbdev = dev;
2773 mutex_init(&wacom->lock);
2774 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
2775 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2776 INIT_WORK(&wacom->battery_work, wacom_battery_work);
2777 INIT_WORK(&wacom->remote_work, wacom_remote_work);
2778 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
2779 timer_setup(&wacom->idleprox_timer, &wacom_idleprox_timeout, TIMER_DEFERRABLE);
2781 /* ask for the report descriptor to be loaded by HID */
2782 error = hid_parse(hdev);
2784 hid_err(hdev, "parse failed\n");
2788 if (features->type == BOOTLOADER) {
2789 hid_warn(hdev, "Using device in hidraw-only mode");
2790 return hid_hw_start(hdev, HID_CONNECT_HIDRAW);
2793 error = wacom_parse_and_register(wacom, false);
2797 if (hdev->bus == BUS_BLUETOOTH) {
2798 error = device_create_file(&hdev->dev, &dev_attr_speed);
2801 "can't create sysfs speed attribute err: %d\n",
2808 hid_set_drvdata(hdev, NULL);
2812 static void wacom_remove(struct hid_device *hdev)
2814 struct wacom *wacom = hid_get_drvdata(hdev);
2815 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2816 struct wacom_features *features = &wacom_wac->features;
2818 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2823 cancel_delayed_work_sync(&wacom->init_work);
2824 cancel_work_sync(&wacom->wireless_work);
2825 cancel_work_sync(&wacom->battery_work);
2826 cancel_work_sync(&wacom->remote_work);
2827 cancel_work_sync(&wacom->mode_change_work);
2828 del_timer_sync(&wacom->idleprox_timer);
2829 if (hdev->bus == BUS_BLUETOOTH)
2830 device_remove_file(&hdev->dev, &dev_attr_speed);
2832 /* make sure we don't trigger the LEDs */
2833 wacom_led_groups_release(wacom);
2835 if (wacom->wacom_wac.features.type != REMOTE)
2836 wacom_release_resources(wacom);
2838 hid_set_drvdata(hdev, NULL);
2842 static int wacom_resume(struct hid_device *hdev)
2844 struct wacom *wacom = hid_get_drvdata(hdev);
2846 mutex_lock(&wacom->lock);
2848 /* switch to wacom mode first */
2849 _wacom_query_tablet_data(wacom);
2850 wacom_led_control(wacom);
2852 mutex_unlock(&wacom->lock);
2857 static int wacom_reset_resume(struct hid_device *hdev)
2859 return wacom_resume(hdev);
2861 #endif /* CONFIG_PM */
2863 static struct hid_driver wacom_driver = {
2865 .id_table = wacom_ids,
2866 .probe = wacom_probe,
2867 .remove = wacom_remove,
2868 .report = wacom_wac_report,
2870 .resume = wacom_resume,
2871 .reset_resume = wacom_reset_resume,
2873 .raw_event = wacom_raw_event,
2875 module_hid_driver(wacom_driver);
2877 MODULE_VERSION(DRIVER_VERSION);
2878 MODULE_AUTHOR(DRIVER_AUTHOR);
2879 MODULE_DESCRIPTION(DRIVER_DESC);
2880 MODULE_LICENSE("GPL");