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_feature_mapping(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 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
219 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
224 switch (equivalent_usage) {
225 case HID_DG_CONTACTMAX:
226 /* leave touch_max as is if predefined */
227 if (!features->touch_max) {
229 data = kzalloc(2, GFP_KERNEL);
232 data[0] = field->report->id;
233 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
234 data, 2, WAC_CMD_RETRIES);
236 features->touch_max = data[1];
238 features->touch_max = 16;
239 hid_warn(hdev, "wacom_feature_mapping: "
240 "could not get HID_DG_CONTACTMAX, "
241 "defaulting to %d\n",
242 features->touch_max);
247 case HID_DG_INPUTMODE:
248 /* Ignore if value index is out of bounds. */
249 if (usage->usage_index >= field->report_count) {
250 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
254 hid_data->inputmode = field->report->id;
255 hid_data->inputmode_index = usage->usage_index;
258 case HID_UP_DIGITIZER:
259 if (field->report->id == 0x0B &&
260 (field->application == WACOM_HID_G9_PEN ||
261 field->application == WACOM_HID_G11_PEN)) {
262 wacom->wacom_wac.mode_report = field->report->id;
263 wacom->wacom_wac.mode_value = 0;
267 case WACOM_HID_WD_DATAMODE:
268 wacom->wacom_wac.mode_report = field->report->id;
269 wacom->wacom_wac.mode_value = 2;
272 case WACOM_HID_UP_G9:
273 case WACOM_HID_UP_G11:
274 if (field->report->id == 0x03 &&
275 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
276 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
277 wacom->wacom_wac.mode_report = field->report->id;
278 wacom->wacom_wac.mode_value = 0;
281 case WACOM_HID_WD_OFFSETLEFT:
282 case WACOM_HID_WD_OFFSETTOP:
283 case WACOM_HID_WD_OFFSETRIGHT:
284 case WACOM_HID_WD_OFFSETBOTTOM:
286 n = hid_report_len(field->report);
287 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
290 data[0] = field->report->id;
291 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
292 data, n, WAC_CMD_RETRIES);
294 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
297 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
304 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
305 hdev->product == 0x4200 /* Dell Canvas 27 */ &&
306 field->application == HID_UP_MSVENDOR) {
307 wacom->wacom_wac.mode_report = field->report->id;
308 wacom->wacom_wac.mode_value = 2;
313 * Interface Descriptor of wacom devices can be incomplete and
314 * inconsistent so wacom_features table is used to store stylus
315 * device's packet lengths, various maximum values, and tablet
316 * resolution based on product ID's.
318 * For devices that contain 2 interfaces, wacom_features table is
319 * inaccurate for the touch interface. Since the Interface Descriptor
320 * for touch interfaces has pretty complete data, this function exists
321 * to query tablet for this missing information instead of hard coding in
322 * an additional table.
324 * A typical Interface Descriptor for a stylus will contain a
325 * boot mouse application collection that is not of interest and this
326 * function will ignore it.
328 * It also contains a digitizer application collection that also is not
329 * of interest since any information it contains would be duplicate
330 * of what is in wacom_features. Usually it defines a report of an array
331 * of bytes that could be used as max length of the stylus packet returned.
332 * If it happens to define a Digitizer-Stylus Physical Collection then
333 * the X and Y logical values contain valid data but it is ignored.
335 * A typical Interface Descriptor for a touch interface will contain a
336 * Digitizer-Finger Physical Collection which will define both logical
337 * X/Y maximum as well as the physical size of tablet. Since touch
338 * interfaces haven't supported pressure or distance, this is enough
339 * information to override invalid values in the wacom_features table.
341 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
342 * data. We deal with them after returning from this function.
344 static void wacom_usage_mapping(struct hid_device *hdev,
345 struct hid_field *field, struct hid_usage *usage)
347 struct wacom *wacom = hid_get_drvdata(hdev);
348 struct wacom_features *features = &wacom->wacom_wac.features;
349 bool finger = WACOM_FINGER_FIELD(field);
350 bool pen = WACOM_PEN_FIELD(field);
353 * Requiring Stylus Usage will ignore boot mouse
354 * X/Y values and some cases of invalid Digitizer X/Y
355 * values commonly reported.
358 features->device_type |= WACOM_DEVICETYPE_PEN;
360 features->device_type |= WACOM_DEVICETYPE_TOUCH;
365 * Bamboo models do not support HID_DG_CONTACTMAX.
366 * And, Bamboo Pen only descriptor contains touch.
368 if (features->type > BAMBOO_PT) {
369 /* ISDv4 touch devices at least supports one touch point */
370 if (finger && !features->touch_max)
371 features->touch_max = 1;
375 * ISDv4 devices which predate HID's adoption of the
376 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
377 * position instead. We can accurately detect if a
378 * usage with that value should be HID_DG_BARRELSWITCH2
379 * based on the surrounding usages, which have remained
380 * constant across generations.
382 if (features->type == HID_GENERIC &&
383 usage->hid == 0x000D0000 &&
384 field->application == HID_DG_PEN &&
385 field->physical == HID_DG_STYLUS) {
386 int i = usage->usage_index;
388 if (i-4 >= 0 && i+1 < field->maxusage &&
389 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
390 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
391 field->usage[i-2].hid == HID_DG_ERASER &&
392 field->usage[i-1].hid == HID_DG_INVERT &&
393 field->usage[i+1].hid == HID_DG_INRANGE) {
394 usage->hid = HID_DG_BARRELSWITCH2;
398 switch (usage->hid) {
400 features->x_max = field->logical_maximum;
402 features->x_phy = field->physical_maximum;
403 if ((features->type != BAMBOO_PT) &&
404 (features->type != BAMBOO_TOUCH)) {
405 features->unit = field->unit;
406 features->unitExpo = field->unit_exponent;
411 features->y_max = field->logical_maximum;
413 features->y_phy = field->physical_maximum;
414 if ((features->type != BAMBOO_PT) &&
415 (features->type != BAMBOO_TOUCH)) {
416 features->unit = field->unit;
417 features->unitExpo = field->unit_exponent;
421 case HID_DG_TIPPRESSURE:
423 features->pressure_max = field->logical_maximum;
427 if (features->type == HID_GENERIC)
428 wacom_wac_usage_mapping(hdev, field, usage);
431 static void wacom_post_parse_hid(struct hid_device *hdev,
432 struct wacom_features *features)
434 struct wacom *wacom = hid_get_drvdata(hdev);
435 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
437 if (features->type == HID_GENERIC) {
438 /* Any last-minute generic device setup */
439 if (wacom_wac->has_mode_change) {
440 if (wacom_wac->is_direct_mode)
441 features->device_type |= WACOM_DEVICETYPE_DIRECT;
443 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
446 if (features->touch_max > 1) {
447 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
448 input_mt_init_slots(wacom_wac->touch_input,
449 wacom_wac->features.touch_max,
452 input_mt_init_slots(wacom_wac->touch_input,
453 wacom_wac->features.touch_max,
459 static void wacom_parse_hid(struct hid_device *hdev,
460 struct wacom_features *features)
462 struct hid_report_enum *rep_enum;
463 struct hid_report *hreport;
466 /* check features first */
467 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
468 list_for_each_entry(hreport, &rep_enum->report_list, list) {
469 for (i = 0; i < hreport->maxfield; i++) {
470 /* Ignore if report count is out of bounds. */
471 if (hreport->field[i]->report_count < 1)
474 for (j = 0; j < hreport->field[i]->maxusage; j++) {
475 wacom_feature_mapping(hdev, hreport->field[i],
476 hreport->field[i]->usage + j);
481 /* now check the input usages */
482 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
483 list_for_each_entry(hreport, &rep_enum->report_list, list) {
485 if (!hreport->maxfield)
488 for (i = 0; i < hreport->maxfield; i++)
489 for (j = 0; j < hreport->field[i]->maxusage; j++)
490 wacom_usage_mapping(hdev, hreport->field[i],
491 hreport->field[i]->usage + j);
494 wacom_post_parse_hid(hdev, features);
497 static int wacom_hid_set_device_mode(struct hid_device *hdev)
499 struct wacom *wacom = hid_get_drvdata(hdev);
500 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
501 struct hid_report *r;
502 struct hid_report_enum *re;
504 if (hid_data->inputmode < 0)
507 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
508 r = re->report_id_hash[hid_data->inputmode];
510 r->field[0]->value[hid_data->inputmode_index] = 2;
511 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
516 static int wacom_set_device_mode(struct hid_device *hdev,
517 struct wacom_wac *wacom_wac)
520 struct hid_report *r;
521 struct hid_report_enum *re;
523 int error = -ENOMEM, limit = 0;
525 if (wacom_wac->mode_report < 0)
528 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
529 r = re->report_id_hash[wacom_wac->mode_report];
533 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
537 length = hid_report_len(r);
540 rep_data[0] = wacom_wac->mode_report;
541 rep_data[1] = wacom_wac->mode_value;
543 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
546 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
547 rep_data, length, 1);
548 } while (error >= 0 &&
549 rep_data[1] != wacom_wac->mode_report &&
550 limit++ < WAC_MSG_RETRIES);
554 return error < 0 ? error : 0;
557 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
558 struct wacom_features *features)
560 struct wacom *wacom = hid_get_drvdata(hdev);
564 switch (features->type) {
568 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
572 rep_data[0] = speed == 0 ? 0x05 : 0x06;
575 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
579 wacom->wacom_wac.bt_high_speed = speed;
585 * Note that if the raw queries fail, it's not a hard failure
586 * and it is safe to continue
588 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
593 wacom->wacom_wac.bt_features &= ~0x20;
595 wacom->wacom_wac.bt_features |= 0x20;
598 rep_data[1] = wacom->wacom_wac.bt_features;
600 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
603 wacom->wacom_wac.bt_high_speed = speed;
611 * Switch the tablet into its most-capable mode. Wacom tablets are
612 * typically configured to power-up in a mode which sends mouse-like
613 * reports to the OS. To get absolute position, pressure data, etc.
614 * from the tablet, it is necessary to switch the tablet out of this
615 * mode and into one which sends the full range of tablet data.
617 static int _wacom_query_tablet_data(struct wacom *wacom)
619 struct hid_device *hdev = wacom->hdev;
620 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
621 struct wacom_features *features = &wacom_wac->features;
623 if (hdev->bus == BUS_BLUETOOTH)
624 return wacom_bt_query_tablet_data(hdev, 1, features);
626 if (features->type != HID_GENERIC) {
627 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
628 if (features->type > TABLETPC) {
629 /* MT Tablet PC touch */
630 wacom_wac->mode_report = 3;
631 wacom_wac->mode_value = 4;
632 } else if (features->type == WACOM_24HDT) {
633 wacom_wac->mode_report = 18;
634 wacom_wac->mode_value = 2;
635 } else if (features->type == WACOM_27QHDT) {
636 wacom_wac->mode_report = 131;
637 wacom_wac->mode_value = 2;
638 } else if (features->type == BAMBOO_PAD) {
639 wacom_wac->mode_report = 2;
640 wacom_wac->mode_value = 2;
642 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
643 if (features->type <= BAMBOO_PT) {
644 wacom_wac->mode_report = 2;
645 wacom_wac->mode_value = 2;
650 wacom_set_device_mode(hdev, wacom_wac);
652 if (features->type == HID_GENERIC)
653 return wacom_hid_set_device_mode(hdev);
658 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
659 struct wacom_features *features)
661 struct wacom *wacom = hid_get_drvdata(hdev);
662 struct usb_interface *intf = wacom->intf;
664 /* default features */
665 features->x_fuzz = 4;
666 features->y_fuzz = 4;
667 features->pressure_fuzz = 0;
668 features->distance_fuzz = 1;
669 features->tilt_fuzz = 1;
672 * The wireless device HID is basic and layout conflicts with
673 * other tablets (monitor and touch interface can look like pen).
674 * Skip the query for this type and modify defaults based on
677 if (features->type == WIRELESS) {
678 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
679 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
681 features->device_type = WACOM_DEVICETYPE_NONE;
685 wacom_parse_hid(hdev, features);
688 struct wacom_hdev_data {
689 struct list_head list;
691 struct hid_device *dev;
692 struct wacom_shared shared;
695 static LIST_HEAD(wacom_udev_list);
696 static DEFINE_MUTEX(wacom_udev_list_lock);
698 static bool compare_device_paths(struct hid_device *hdev_a,
699 struct hid_device *hdev_b, char separator)
701 int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
702 int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
704 if (n1 != n2 || n1 <= 0 || n2 <= 0)
707 return !strncmp(hdev_a->phys, hdev_b->phys, n1);
710 static bool wacom_are_sibling(struct hid_device *hdev,
711 struct hid_device *sibling)
713 struct wacom *wacom = hid_get_drvdata(hdev);
714 struct wacom_features *features = &wacom->wacom_wac.features;
715 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
716 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
717 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
718 __u32 oPid = features->oPid ? features->oPid : hdev->product;
720 /* The defined oVid/oPid must match that of the sibling */
721 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
723 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
727 * Devices with the same VID/PID must share the same physical
728 * device path, while those with different VID/PID must share
729 * the same physical parent device path.
731 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
732 if (!compare_device_paths(hdev, sibling, '/'))
735 if (!compare_device_paths(hdev, sibling, '.'))
739 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
740 if (features->type != HID_GENERIC)
744 * Direct-input devices may not be siblings of indirect-input
747 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
748 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
752 * Indirect-input devices may not be siblings of direct-input
755 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
756 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
759 /* Pen devices may only be siblings of touch devices */
760 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
761 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
764 /* Touch devices may only be siblings of pen devices */
765 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
766 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
770 * No reason could be found for these two devices to NOT be
771 * siblings, so there's a good chance they ARE siblings
776 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
778 struct wacom_hdev_data *data;
780 /* Try to find an already-probed interface from the same device */
781 list_for_each_entry(data, &wacom_udev_list, list) {
782 if (compare_device_paths(hdev, data->dev, '/')) {
783 kref_get(&data->kref);
788 /* Fallback to finding devices that appear to be "siblings" */
789 list_for_each_entry(data, &wacom_udev_list, list) {
790 if (wacom_are_sibling(hdev, data->dev)) {
791 kref_get(&data->kref);
799 static void wacom_release_shared_data(struct kref *kref)
801 struct wacom_hdev_data *data =
802 container_of(kref, struct wacom_hdev_data, kref);
804 mutex_lock(&wacom_udev_list_lock);
805 list_del(&data->list);
806 mutex_unlock(&wacom_udev_list_lock);
811 static void wacom_remove_shared_data(void *res)
813 struct wacom *wacom = res;
814 struct wacom_hdev_data *data;
815 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
817 if (wacom_wac->shared) {
818 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
821 if (wacom_wac->shared->touch == wacom->hdev)
822 wacom_wac->shared->touch = NULL;
823 else if (wacom_wac->shared->pen == wacom->hdev)
824 wacom_wac->shared->pen = NULL;
826 kref_put(&data->kref, wacom_release_shared_data);
827 wacom_wac->shared = NULL;
831 static int wacom_add_shared_data(struct hid_device *hdev)
833 struct wacom *wacom = hid_get_drvdata(hdev);
834 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
835 struct wacom_hdev_data *data;
838 mutex_lock(&wacom_udev_list_lock);
840 data = wacom_get_hdev_data(hdev);
842 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
848 kref_init(&data->kref);
850 list_add_tail(&data->list, &wacom_udev_list);
853 wacom_wac->shared = &data->shared;
855 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
857 mutex_unlock(&wacom_udev_list_lock);
858 wacom_remove_shared_data(wacom);
862 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
863 wacom_wac->shared->touch = hdev;
864 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
865 wacom_wac->shared->pen = hdev;
868 mutex_unlock(&wacom_udev_list_lock);
872 static int wacom_led_control(struct wacom *wacom)
876 unsigned char report_id = WAC_CMD_LED_CONTROL;
879 if (!wacom->led.groups)
882 if (wacom->wacom_wac.features.type == REMOTE)
885 if (wacom->wacom_wac.pid) { /* wireless connected */
886 report_id = WAC_CMD_WL_LED_CONTROL;
889 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
890 report_id = WAC_CMD_WL_INTUOSP2;
893 buf = kzalloc(buf_size, GFP_KERNEL);
897 if (wacom->wacom_wac.features.type == HID_GENERIC) {
898 buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
899 buf[1] = wacom->led.llv;
900 buf[2] = wacom->led.groups[0].select & 0x03;
902 } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
903 wacom->wacom_wac.features.type <= INTUOSPL)) {
905 * Touch Ring and crop mark LED luminance may take on
906 * one of four values:
907 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
909 int ring_led = wacom->led.groups[0].select & 0x03;
910 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
912 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
915 if (wacom->wacom_wac.pid) {
916 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
917 buf, buf_size, WAC_CMD_RETRIES);
923 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
925 buf[4] = 100; // Power Connection LED (ORANGE)
926 buf[5] = 100; // BT Connection LED (BLUE)
927 buf[6] = 100; // Paper Mode (RED?)
928 buf[7] = 100; // Paper Mode (GREEN?)
929 buf[8] = 100; // Paper Mode (BLUE?)
930 buf[9] = wacom->led.llv;
931 buf[10] = wacom->led.groups[0].select & 0x03;
934 int led = wacom->led.groups[0].select | 0x4;
936 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
937 wacom->wacom_wac.features.type == WACOM_24HD)
938 led |= (wacom->led.groups[1].select << 4) | 0x40;
942 buf[2] = wacom->led.llv;
943 buf[3] = wacom->led.hlv;
944 buf[4] = wacom->led.img_lum;
947 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
954 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
955 const unsigned len, const void *img)
959 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
961 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
965 /* Send 'start' command */
966 buf[0] = WAC_CMD_ICON_START;
968 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
974 buf[1] = button_id & 0x07;
975 for (i = 0; i < 4; i++) {
977 memcpy(buf + 3, img + i * chunk_len, chunk_len);
979 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
980 buf, chunk_len + 3, WAC_CMD_RETRIES);
986 buf[0] = WAC_CMD_ICON_START;
988 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
996 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
997 const char *buf, size_t count)
999 struct hid_device *hdev = to_hid_device(dev);
1000 struct wacom *wacom = hid_get_drvdata(hdev);
1004 err = kstrtouint(buf, 10, &id);
1008 mutex_lock(&wacom->lock);
1010 wacom->led.groups[set_id].select = id & 0x3;
1011 err = wacom_led_control(wacom);
1013 mutex_unlock(&wacom->lock);
1015 return err < 0 ? err : count;
1018 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
1019 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
1020 struct device_attribute *attr, const char *buf, size_t count) \
1022 return wacom_led_select_store(dev, SET_ID, buf, count); \
1024 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
1025 struct device_attribute *attr, char *buf) \
1027 struct hid_device *hdev = to_hid_device(dev);\
1028 struct wacom *wacom = hid_get_drvdata(hdev); \
1029 return scnprintf(buf, PAGE_SIZE, "%d\n", \
1030 wacom->led.groups[SET_ID].select); \
1032 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
1033 wacom_led##SET_ID##_select_show, \
1034 wacom_led##SET_ID##_select_store)
1036 DEVICE_LED_SELECT_ATTR(0);
1037 DEVICE_LED_SELECT_ATTR(1);
1039 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
1040 const char *buf, size_t count)
1045 err = kstrtouint(buf, 10, &value);
1049 mutex_lock(&wacom->lock);
1051 *dest = value & 0x7f;
1052 err = wacom_led_control(wacom);
1054 mutex_unlock(&wacom->lock);
1056 return err < 0 ? err : count;
1059 #define DEVICE_LUMINANCE_ATTR(name, field) \
1060 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
1061 struct device_attribute *attr, const char *buf, size_t count) \
1063 struct hid_device *hdev = to_hid_device(dev);\
1064 struct wacom *wacom = hid_get_drvdata(hdev); \
1066 return wacom_luminance_store(wacom, &wacom->led.field, \
1069 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
1070 struct device_attribute *attr, char *buf) \
1072 struct wacom *wacom = dev_get_drvdata(dev); \
1073 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
1075 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
1076 wacom_##name##_luminance_show, \
1077 wacom_##name##_luminance_store)
1079 DEVICE_LUMINANCE_ATTR(status0, llv);
1080 DEVICE_LUMINANCE_ATTR(status1, hlv);
1081 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
1083 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
1084 const char *buf, size_t count)
1086 struct hid_device *hdev = to_hid_device(dev);
1087 struct wacom *wacom = hid_get_drvdata(hdev);
1092 if (hdev->bus == BUS_BLUETOOTH) {
1094 xfer_id = WAC_CMD_ICON_BT_XFER;
1097 xfer_id = WAC_CMD_ICON_XFER;
1103 mutex_lock(&wacom->lock);
1105 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
1107 mutex_unlock(&wacom->lock);
1109 return err < 0 ? err : count;
1112 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1113 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1114 struct device_attribute *attr, const char *buf, size_t count) \
1116 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1118 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
1119 NULL, wacom_btnimg##BUTTON_ID##_store)
1121 DEVICE_BTNIMG_ATTR(0);
1122 DEVICE_BTNIMG_ATTR(1);
1123 DEVICE_BTNIMG_ATTR(2);
1124 DEVICE_BTNIMG_ATTR(3);
1125 DEVICE_BTNIMG_ATTR(4);
1126 DEVICE_BTNIMG_ATTR(5);
1127 DEVICE_BTNIMG_ATTR(6);
1128 DEVICE_BTNIMG_ATTR(7);
1130 static struct attribute *cintiq_led_attrs[] = {
1131 &dev_attr_status_led0_select.attr,
1132 &dev_attr_status_led1_select.attr,
1136 static struct attribute_group cintiq_led_attr_group = {
1137 .name = "wacom_led",
1138 .attrs = cintiq_led_attrs,
1141 static struct attribute *intuos4_led_attrs[] = {
1142 &dev_attr_status0_luminance.attr,
1143 &dev_attr_status1_luminance.attr,
1144 &dev_attr_status_led0_select.attr,
1145 &dev_attr_buttons_luminance.attr,
1146 &dev_attr_button0_rawimg.attr,
1147 &dev_attr_button1_rawimg.attr,
1148 &dev_attr_button2_rawimg.attr,
1149 &dev_attr_button3_rawimg.attr,
1150 &dev_attr_button4_rawimg.attr,
1151 &dev_attr_button5_rawimg.attr,
1152 &dev_attr_button6_rawimg.attr,
1153 &dev_attr_button7_rawimg.attr,
1157 static struct attribute_group intuos4_led_attr_group = {
1158 .name = "wacom_led",
1159 .attrs = intuos4_led_attrs,
1162 static struct attribute *intuos5_led_attrs[] = {
1163 &dev_attr_status0_luminance.attr,
1164 &dev_attr_status_led0_select.attr,
1168 static struct attribute_group intuos5_led_attr_group = {
1169 .name = "wacom_led",
1170 .attrs = intuos5_led_attrs,
1173 static struct attribute *generic_led_attrs[] = {
1174 &dev_attr_status0_luminance.attr,
1175 &dev_attr_status_led0_select.attr,
1179 static struct attribute_group generic_led_attr_group = {
1180 .name = "wacom_led",
1181 .attrs = generic_led_attrs,
1184 struct wacom_sysfs_group_devres {
1185 struct attribute_group *group;
1186 struct kobject *root;
1189 static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1191 struct wacom_sysfs_group_devres *devres = res;
1192 struct kobject *kobj = devres->root;
1194 dev_dbg(dev, "%s: dropping reference to %s\n",
1195 __func__, devres->group->name);
1196 sysfs_remove_group(kobj, devres->group);
1199 static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1200 struct kobject *root,
1201 struct attribute_group *group)
1203 struct wacom_sysfs_group_devres *devres;
1206 devres = devres_alloc(wacom_devm_sysfs_group_release,
1207 sizeof(struct wacom_sysfs_group_devres),
1212 devres->group = group;
1213 devres->root = root;
1215 error = sysfs_create_group(devres->root, group);
1217 devres_free(devres);
1221 devres_add(&wacom->hdev->dev, devres);
1226 static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1227 struct attribute_group *group)
1229 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1233 enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
1235 struct wacom *wacom = led->wacom;
1237 if (wacom->led.max_hlv)
1238 return led->hlv * LED_FULL / wacom->led.max_hlv;
1240 if (wacom->led.max_llv)
1241 return led->llv * LED_FULL / wacom->led.max_llv;
1243 /* device doesn't support brightness tuning */
1247 static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1249 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1250 struct wacom *wacom = led->wacom;
1252 if (wacom->led.groups[led->group].select != led->id)
1255 return wacom_leds_brightness_get(led);
1258 static int wacom_led_brightness_set(struct led_classdev *cdev,
1259 enum led_brightness brightness)
1261 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1262 struct wacom *wacom = led->wacom;
1265 mutex_lock(&wacom->lock);
1267 if (!wacom->led.groups || (brightness == LED_OFF &&
1268 wacom->led.groups[led->group].select != led->id)) {
1273 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1274 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1276 wacom->led.groups[led->group].select = led->id;
1278 error = wacom_led_control(wacom);
1281 mutex_unlock(&wacom->lock);
1286 static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1287 enum led_brightness brightness)
1291 static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1292 struct wacom_led *led, unsigned int group,
1293 unsigned int id, bool read_only)
1298 name = devm_kasprintf(dev, GFP_KERNEL,
1307 led->trigger.name = name;
1308 error = devm_led_trigger_register(dev, &led->trigger);
1310 hid_err(wacom->hdev,
1311 "failed to register LED trigger %s: %d\n",
1312 led->cdev.name, error);
1320 led->llv = wacom->led.llv;
1321 led->hlv = wacom->led.hlv;
1322 led->cdev.name = name;
1323 led->cdev.max_brightness = LED_FULL;
1324 led->cdev.flags = LED_HW_PLUGGABLE;
1325 led->cdev.brightness_get = __wacom_led_brightness_get;
1327 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
1328 led->cdev.default_trigger = led->cdev.name;
1330 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
1333 error = devm_led_classdev_register(dev, &led->cdev);
1335 hid_err(wacom->hdev,
1336 "failed to register LED %s: %d\n",
1337 led->cdev.name, error);
1338 led->cdev.name = NULL;
1345 static void wacom_led_groups_release_one(void *data)
1347 struct wacom_group_leds *group = data;
1349 devres_release_group(group->dev, group);
1352 static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1353 struct wacom *wacom,
1354 int group_id, int count,
1357 struct wacom_led *leds;
1360 if (group_id >= wacom->led.count || count <= 0)
1363 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1366 leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
1372 wacom->led.groups[group_id].leds = leds;
1373 wacom->led.groups[group_id].count = count;
1375 for (i = 0; i < count; i++) {
1376 error = wacom_led_register_one(dev, wacom, &leds[i],
1377 group_id, i, read_only);
1382 wacom->led.groups[group_id].dev = dev;
1384 devres_close_group(dev, &wacom->led.groups[group_id]);
1387 * There is a bug (?) in devm_led_classdev_register() in which its
1388 * increments the refcount of the parent. If the parent is an input
1389 * device, that means the ref count never reaches 0 when
1390 * devm_input_device_release() gets called.
1391 * This means that the LEDs are still there after disconnect.
1392 * Manually force the release of the group so that the leds are released
1393 * once we are done using them.
1395 error = devm_add_action_or_reset(&wacom->hdev->dev,
1396 wacom_led_groups_release_one,
1397 &wacom->led.groups[group_id]);
1404 devres_release_group(dev, &wacom->led.groups[group_id]);
1408 struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1411 struct wacom_group_leds *group;
1413 if (group_id >= wacom->led.count)
1416 group = &wacom->led.groups[group_id];
1423 return &group->leds[id];
1427 * wacom_led_next: gives the next available led with a wacom trigger.
1429 * returns the next available struct wacom_led which has its default trigger
1430 * or the current one if none is available.
1432 struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1434 struct wacom_led *next_led;
1444 next_led = wacom_led_find(wacom, group, ++next);
1445 if (!next_led || next_led == cur)
1447 } while (next_led->cdev.trigger != &next_led->trigger);
1452 static void wacom_led_groups_release(void *data)
1454 struct wacom *wacom = data;
1456 wacom->led.groups = NULL;
1457 wacom->led.count = 0;
1460 static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1462 struct device *dev = &wacom->hdev->dev;
1463 struct wacom_group_leds *groups;
1466 groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
1471 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1475 wacom->led.groups = groups;
1476 wacom->led.count = count;
1481 static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1482 int led_per_group, bool read_only)
1487 if (!wacom->wacom_wac.pad_input)
1490 dev = &wacom->wacom_wac.pad_input->dev;
1492 error = wacom_led_groups_allocate(wacom, group_count);
1496 for (i = 0; i < group_count; i++) {
1497 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1507 int wacom_initialize_leds(struct wacom *wacom)
1511 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1514 /* Initialize default values */
1515 switch (wacom->wacom_wac.features.type) {
1517 if (!wacom->generic_has_leds)
1519 wacom->led.llv = 100;
1520 wacom->led.max_llv = 100;
1522 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1524 hid_err(wacom->hdev,
1525 "cannot create leds err: %d\n", error);
1529 error = wacom_devm_sysfs_create_group(wacom,
1530 &generic_led_attr_group);
1537 wacom->led.llv = 10;
1538 wacom->led.hlv = 20;
1539 wacom->led.max_llv = 127;
1540 wacom->led.max_hlv = 127;
1541 wacom->led.img_lum = 10;
1543 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1545 hid_err(wacom->hdev,
1546 "cannot create leds err: %d\n", error);
1550 error = wacom_devm_sysfs_create_group(wacom,
1551 &intuos4_led_attr_group);
1558 wacom->led.img_lum = 0;
1560 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1562 hid_err(wacom->hdev,
1563 "cannot create leds err: %d\n", error);
1567 error = wacom_devm_sysfs_create_group(wacom,
1568 &cintiq_led_attr_group);
1577 wacom->led.llv = 32;
1578 wacom->led.max_llv = 96;
1580 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1582 hid_err(wacom->hdev,
1583 "cannot create leds err: %d\n", error);
1587 error = wacom_devm_sysfs_create_group(wacom,
1588 &intuos5_led_attr_group);
1592 wacom->led.llv = 50;
1593 wacom->led.max_llv = 100;
1594 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1596 hid_err(wacom->hdev,
1597 "cannot create leds err: %d\n", error);
1603 wacom->led.llv = 255;
1604 wacom->led.max_llv = 255;
1605 error = wacom_led_groups_allocate(wacom, 5);
1607 hid_err(wacom->hdev,
1608 "cannot create leds err: %d\n", error);
1618 hid_err(wacom->hdev,
1619 "cannot create sysfs group err: %d\n", error);
1626 static void wacom_init_work(struct work_struct *work)
1628 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1630 _wacom_query_tablet_data(wacom);
1631 wacom_led_control(wacom);
1634 static void wacom_query_tablet_data(struct wacom *wacom)
1636 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1639 static enum power_supply_property wacom_battery_props[] = {
1640 POWER_SUPPLY_PROP_MODEL_NAME,
1641 POWER_SUPPLY_PROP_PRESENT,
1642 POWER_SUPPLY_PROP_STATUS,
1643 POWER_SUPPLY_PROP_SCOPE,
1644 POWER_SUPPLY_PROP_CAPACITY
1647 static int wacom_battery_get_property(struct power_supply *psy,
1648 enum power_supply_property psp,
1649 union power_supply_propval *val)
1651 struct wacom_battery *battery = power_supply_get_drvdata(psy);
1655 case POWER_SUPPLY_PROP_MODEL_NAME:
1656 val->strval = battery->wacom->wacom_wac.name;
1658 case POWER_SUPPLY_PROP_PRESENT:
1659 val->intval = battery->bat_connected;
1661 case POWER_SUPPLY_PROP_SCOPE:
1662 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1664 case POWER_SUPPLY_PROP_CAPACITY:
1665 val->intval = battery->battery_capacity;
1667 case POWER_SUPPLY_PROP_STATUS:
1668 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1669 val->intval = battery->bat_status;
1670 else if (battery->bat_charging)
1671 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1672 else if (battery->battery_capacity == 100 &&
1673 battery->ps_connected)
1674 val->intval = POWER_SUPPLY_STATUS_FULL;
1675 else if (battery->ps_connected)
1676 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1678 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1688 static int __wacom_initialize_battery(struct wacom *wacom,
1689 struct wacom_battery *battery)
1691 static atomic_t battery_no = ATOMIC_INIT(0);
1692 struct device *dev = &wacom->hdev->dev;
1693 struct power_supply_config psy_cfg = { .drv_data = battery, };
1694 struct power_supply *ps_bat;
1695 struct power_supply_desc *bat_desc = &battery->bat_desc;
1699 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1702 battery->wacom = wacom;
1704 n = atomic_inc_return(&battery_no) - 1;
1706 bat_desc->properties = wacom_battery_props;
1707 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1708 bat_desc->get_property = wacom_battery_get_property;
1709 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1710 bat_desc->name = battery->bat_name;
1711 bat_desc->type = POWER_SUPPLY_TYPE_USB;
1712 bat_desc->use_for_apm = 0;
1714 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1715 if (IS_ERR(ps_bat)) {
1716 error = PTR_ERR(ps_bat);
1720 power_supply_powers(ps_bat, &wacom->hdev->dev);
1722 battery->battery = ps_bat;
1724 devres_close_group(dev, bat_desc);
1728 devres_release_group(dev, bat_desc);
1732 static int wacom_initialize_battery(struct wacom *wacom)
1734 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1735 return __wacom_initialize_battery(wacom, &wacom->battery);
1740 static void wacom_destroy_battery(struct wacom *wacom)
1742 if (wacom->battery.battery) {
1743 devres_release_group(&wacom->hdev->dev,
1744 &wacom->battery.bat_desc);
1745 wacom->battery.battery = NULL;
1749 static ssize_t wacom_show_speed(struct device *dev,
1750 struct device_attribute
1753 struct hid_device *hdev = to_hid_device(dev);
1754 struct wacom *wacom = hid_get_drvdata(hdev);
1756 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1759 static ssize_t wacom_store_speed(struct device *dev,
1760 struct device_attribute *attr,
1761 const char *buf, size_t count)
1763 struct hid_device *hdev = to_hid_device(dev);
1764 struct wacom *wacom = hid_get_drvdata(hdev);
1767 if (kstrtou8(buf, 0, &new_speed))
1770 if (new_speed != 0 && new_speed != 1)
1773 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1778 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1779 wacom_show_speed, wacom_store_speed);
1782 static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1783 struct kobj_attribute *kattr,
1784 char *buf, int index)
1786 struct device *dev = kobj_to_dev(kobj->parent);
1787 struct hid_device *hdev = to_hid_device(dev);
1788 struct wacom *wacom = hid_get_drvdata(hdev);
1791 mode = wacom->led.groups[index].select;
1792 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
1795 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1796 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1797 struct kobj_attribute *kattr, char *buf) \
1799 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1801 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1802 .attr = {.name = "remote_mode", \
1803 .mode = DEV_ATTR_RO_PERM}, \
1804 .show = wacom_show_remote##SET_ID##_mode, \
1806 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1807 &remote##SET_ID##_mode_attr.attr, \
1810 static struct attribute_group remote##SET_ID##_serial_group = { \
1812 .attrs = remote##SET_ID##_serial_attrs, \
1815 DEVICE_EKR_ATTR_GROUP(0);
1816 DEVICE_EKR_ATTR_GROUP(1);
1817 DEVICE_EKR_ATTR_GROUP(2);
1818 DEVICE_EKR_ATTR_GROUP(3);
1819 DEVICE_EKR_ATTR_GROUP(4);
1821 static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1825 struct wacom_remote *remote = wacom->remote;
1827 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1830 if (!remote->remotes[index].group.name)
1833 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1834 &remote->remotes[index].group);
1836 remote->remotes[index].group.name = NULL;
1837 hid_err(wacom->hdev,
1838 "cannot create sysfs group err: %d\n", error);
1845 static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1847 const size_t buf_size = 2;
1851 buf = kzalloc(buf_size, GFP_KERNEL);
1855 buf[0] = WAC_CMD_DELETE_PAIRING;
1858 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1859 buf_size, WAC_CMD_RETRIES);
1865 static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1866 struct kobj_attribute *attr,
1867 const char *buf, size_t count)
1869 unsigned char selector = 0;
1870 struct device *dev = kobj_to_dev(kobj->parent);
1871 struct hid_device *hdev = to_hid_device(dev);
1872 struct wacom *wacom = hid_get_drvdata(hdev);
1875 if (!strncmp(buf, "*\n", 2)) {
1876 selector = WAC_CMD_UNPAIR_ALL;
1878 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1883 mutex_lock(&wacom->lock);
1885 err = wacom_cmd_unpair_remote(wacom, selector);
1886 mutex_unlock(&wacom->lock);
1888 return err < 0 ? err : count;
1891 static struct kobj_attribute unpair_remote_attr = {
1892 .attr = {.name = "unpair_remote", .mode = 0200},
1893 .store = wacom_store_unpair_remote,
1896 static const struct attribute *remote_unpair_attrs[] = {
1897 &unpair_remote_attr.attr,
1901 static void wacom_remotes_destroy(void *data)
1903 struct wacom *wacom = data;
1904 struct wacom_remote *remote = wacom->remote;
1909 kobject_put(remote->remote_dir);
1910 kfifo_free(&remote->remote_fifo);
1911 wacom->remote = NULL;
1914 static int wacom_initialize_remotes(struct wacom *wacom)
1917 struct wacom_remote *remote;
1920 if (wacom->wacom_wac.features.type != REMOTE)
1923 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1928 wacom->remote = remote;
1930 spin_lock_init(&remote->remote_lock);
1932 error = kfifo_alloc(&remote->remote_fifo,
1933 5 * sizeof(struct wacom_remote_data),
1936 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1940 remote->remotes[0].group = remote0_serial_group;
1941 remote->remotes[1].group = remote1_serial_group;
1942 remote->remotes[2].group = remote2_serial_group;
1943 remote->remotes[3].group = remote3_serial_group;
1944 remote->remotes[4].group = remote4_serial_group;
1946 remote->remote_dir = kobject_create_and_add("wacom_remote",
1947 &wacom->hdev->dev.kobj);
1948 if (!remote->remote_dir)
1951 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
1954 hid_err(wacom->hdev,
1955 "cannot create sysfs group err: %d\n", error);
1959 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1960 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
1961 remote->remotes[i].serial = 0;
1964 error = devm_add_action_or_reset(&wacom->hdev->dev,
1965 wacom_remotes_destroy, wacom);
1972 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1974 struct input_dev *input_dev;
1975 struct hid_device *hdev = wacom->hdev;
1976 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1978 input_dev = devm_input_allocate_device(&hdev->dev);
1982 input_dev->name = wacom_wac->features.name;
1983 input_dev->phys = hdev->phys;
1984 input_dev->dev.parent = &hdev->dev;
1985 input_dev->open = wacom_open;
1986 input_dev->close = wacom_close;
1987 input_dev->uniq = hdev->uniq;
1988 input_dev->id.bustype = hdev->bus;
1989 input_dev->id.vendor = hdev->vendor;
1990 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1991 input_dev->id.version = hdev->version;
1992 input_set_drvdata(input_dev, wacom);
1997 static int wacom_allocate_inputs(struct wacom *wacom)
1999 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2001 wacom_wac->pen_input = wacom_allocate_input(wacom);
2002 wacom_wac->touch_input = wacom_allocate_input(wacom);
2003 wacom_wac->pad_input = wacom_allocate_input(wacom);
2004 if (!wacom_wac->pen_input ||
2005 !wacom_wac->touch_input ||
2006 !wacom_wac->pad_input)
2009 wacom_wac->pen_input->name = wacom_wac->pen_name;
2010 wacom_wac->touch_input->name = wacom_wac->touch_name;
2011 wacom_wac->pad_input->name = wacom_wac->pad_name;
2016 static int wacom_register_inputs(struct wacom *wacom)
2018 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2019 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2022 pen_input_dev = wacom_wac->pen_input;
2023 touch_input_dev = wacom_wac->touch_input;
2024 pad_input_dev = wacom_wac->pad_input;
2026 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
2029 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
2031 /* no pen in use on this interface */
2032 input_free_device(pen_input_dev);
2033 wacom_wac->pen_input = NULL;
2034 pen_input_dev = NULL;
2036 error = input_register_device(pen_input_dev);
2041 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
2043 /* no touch in use on this interface */
2044 input_free_device(touch_input_dev);
2045 wacom_wac->touch_input = NULL;
2046 touch_input_dev = NULL;
2048 error = input_register_device(touch_input_dev);
2053 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2055 /* no pad in use on this interface */
2056 input_free_device(pad_input_dev);
2057 wacom_wac->pad_input = NULL;
2058 pad_input_dev = NULL;
2060 error = input_register_device(pad_input_dev);
2068 wacom_wac->pad_input = NULL;
2069 wacom_wac->touch_input = NULL;
2070 wacom_wac->pen_input = NULL;
2075 * Not all devices report physical dimensions from HID.
2076 * Compute the default from hardcoded logical dimension
2077 * and resolution before driver overwrites them.
2079 static void wacom_set_default_phy(struct wacom_features *features)
2081 if (features->x_resolution) {
2082 features->x_phy = (features->x_max * 100) /
2083 features->x_resolution;
2084 features->y_phy = (features->y_max * 100) /
2085 features->y_resolution;
2089 static void wacom_calculate_res(struct wacom_features *features)
2091 /* set unit to "100th of a mm" for devices not reported by HID */
2092 if (!features->unit) {
2093 features->unit = 0x11;
2094 features->unitExpo = -3;
2097 features->x_resolution = wacom_calc_hid_res(features->x_max,
2100 features->unitExpo);
2101 features->y_resolution = wacom_calc_hid_res(features->y_max,
2104 features->unitExpo);
2107 void wacom_battery_work(struct work_struct *work)
2109 struct wacom *wacom = container_of(work, struct wacom, battery_work);
2111 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2112 !wacom->battery.battery) {
2113 wacom_initialize_battery(wacom);
2115 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2116 wacom->battery.battery) {
2117 wacom_destroy_battery(wacom);
2121 static size_t wacom_compute_pktlen(struct hid_device *hdev)
2123 struct hid_report_enum *report_enum;
2124 struct hid_report *report;
2127 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2129 list_for_each_entry(report, &report_enum->report_list, list) {
2130 size_t report_size = hid_report_len(report);
2131 if (report_size > size)
2138 static void wacom_update_name(struct wacom *wacom, const char *suffix)
2140 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2141 struct wacom_features *features = &wacom_wac->features;
2142 char name[WACOM_NAME_MAX];
2144 /* Generic devices name unspecified */
2145 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
2146 char *product_name = wacom->hdev->name;
2148 if (hid_is_using_ll_driver(wacom->hdev, &usb_hid_driver)) {
2149 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2150 struct usb_device *dev = interface_to_usbdev(intf);
2151 product_name = dev->product;
2154 if (wacom->hdev->bus == BUS_I2C) {
2155 snprintf(name, sizeof(name), "%s %X",
2156 features->name, wacom->hdev->product);
2157 } else if (strstr(product_name, "Wacom") ||
2158 strstr(product_name, "wacom") ||
2159 strstr(product_name, "WACOM")) {
2160 strlcpy(name, product_name, sizeof(name));
2162 snprintf(name, sizeof(name), "Wacom %s", product_name);
2165 /* strip out excess whitespaces */
2167 char *gap = strstr(name, " ");
2170 /* shift everything including the terminator */
2171 memmove(gap, gap+1, strlen(gap));
2174 /* get rid of trailing whitespace */
2175 if (name[strlen(name)-1] == ' ')
2176 name[strlen(name)-1] = '\0';
2178 strlcpy(name, features->name, sizeof(name));
2181 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2184 /* Append the device type to the name */
2185 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
2186 "%s%s Pen", name, suffix);
2187 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
2188 "%s%s Finger", name, suffix);
2189 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
2190 "%s%s Pad", name, suffix);
2193 static void wacom_release_resources(struct wacom *wacom)
2195 struct hid_device *hdev = wacom->hdev;
2197 if (!wacom->resources)
2200 devres_release_group(&hdev->dev, wacom);
2202 wacom->resources = false;
2204 wacom->wacom_wac.pen_input = NULL;
2205 wacom->wacom_wac.touch_input = NULL;
2206 wacom->wacom_wac.pad_input = NULL;
2209 static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2211 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2212 wacom_wac->shared->type = wacom_wac->features.type;
2213 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2216 if (wacom_wac->has_mute_touch_switch) {
2217 wacom_wac->shared->has_mute_touch_switch = true;
2218 wacom_wac->shared->is_touch_on = true;
2221 if (wacom_wac->shared->has_mute_touch_switch &&
2222 wacom_wac->shared->touch_input) {
2223 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2224 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2229 static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
2231 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2232 struct wacom_features *features = &wacom_wac->features;
2233 struct hid_device *hdev = wacom->hdev;
2235 unsigned int connect_mask = HID_CONNECT_HIDRAW;
2237 features->pktlen = wacom_compute_pktlen(hdev);
2238 if (features->pktlen > WACOM_PKGLEN_MAX)
2241 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2244 wacom->resources = true;
2246 error = wacom_allocate_inputs(wacom);
2251 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2252 * into debug mode for the touch part.
2253 * We ignore the other interfaces.
2255 if (features->type == BAMBOO_PAD) {
2256 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2257 features->type = HID_GENERIC;
2258 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2259 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2265 /* set the default size in case we do not get them from hid */
2266 wacom_set_default_phy(features);
2268 /* Retrieve the physical and logical size for touch devices */
2269 wacom_retrieve_hid_descriptor(hdev, features);
2270 wacom_setup_device_quirks(wacom);
2272 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2273 features->type != WIRELESS) {
2274 error = features->type == HID_GENERIC ? -ENODEV : 0;
2276 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
2278 error ? "Ignoring" : "Assuming pen");
2283 features->device_type |= WACOM_DEVICETYPE_PEN;
2286 wacom_calculate_res(features);
2288 wacom_update_name(wacom, wireless ? " (WL)" : "");
2290 /* pen only Bamboo neither support touch nor pad */
2291 if ((features->type == BAMBOO_PEN) &&
2292 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2293 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2298 error = wacom_add_shared_data(hdev);
2302 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2303 (features->quirks & WACOM_QUIRK_BATTERY)) {
2304 error = wacom_initialize_battery(wacom);
2309 error = wacom_register_inputs(wacom);
2313 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
2314 error = wacom_initialize_leds(wacom);
2318 error = wacom_initialize_remotes(wacom);
2323 if (features->type == HID_GENERIC)
2324 connect_mask |= HID_CONNECT_DRIVER;
2326 /* Regular HID work starts now */
2327 error = hid_hw_start(hdev, connect_mask);
2329 hid_err(hdev, "hw start failed\n");
2334 /* Note that if query fails it is not a hard failure */
2335 wacom_query_tablet_data(wacom);
2338 /* touch only Bamboo doesn't support pen */
2339 if ((features->type == BAMBOO_TOUCH) &&
2340 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2341 cancel_delayed_work_sync(&wacom->init_work);
2342 _wacom_query_tablet_data(wacom);
2347 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2348 error = hid_hw_open(hdev);
2350 wacom_set_shared_values(wacom_wac);
2351 devres_close_group(&hdev->dev, wacom);
2358 wacom_release_resources(wacom);
2362 static void wacom_wireless_work(struct work_struct *work)
2364 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2365 struct usb_device *usbdev = wacom->usbdev;
2366 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2367 struct hid_device *hdev1, *hdev2;
2368 struct wacom *wacom1, *wacom2;
2369 struct wacom_wac *wacom_wac1, *wacom_wac2;
2373 * Regardless if this is a disconnect or a new tablet,
2374 * remove any existing input and battery devices.
2377 wacom_destroy_battery(wacom);
2379 /* Stylus interface */
2380 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2381 wacom1 = hid_get_drvdata(hdev1);
2382 wacom_wac1 = &(wacom1->wacom_wac);
2383 wacom_release_resources(wacom1);
2385 /* Touch interface */
2386 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2387 wacom2 = hid_get_drvdata(hdev2);
2388 wacom_wac2 = &(wacom2->wacom_wac);
2389 wacom_release_resources(wacom2);
2391 if (wacom_wac->pid == 0) {
2392 hid_info(wacom->hdev, "wireless tablet disconnected\n");
2394 const struct hid_device_id *id = wacom_ids;
2396 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2400 if (id->vendor == USB_VENDOR_ID_WACOM &&
2401 id->product == wacom_wac->pid)
2407 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2411 /* Stylus interface */
2412 wacom_wac1->features =
2413 *((struct wacom_features *)id->driver_data);
2415 wacom_wac1->pid = wacom_wac->pid;
2417 error = wacom_parse_and_register(wacom1, true);
2421 /* Touch interface */
2422 if (wacom_wac1->features.touch_max ||
2423 (wacom_wac1->features.type >= INTUOSHT &&
2424 wacom_wac1->features.type <= BAMBOO_PT)) {
2425 wacom_wac2->features =
2426 *((struct wacom_features *)id->driver_data);
2427 wacom_wac2->pid = wacom_wac->pid;
2429 error = wacom_parse_and_register(wacom2, true);
2434 strlcpy(wacom_wac->name, wacom_wac1->name,
2435 sizeof(wacom_wac->name));
2436 error = wacom_initialize_battery(wacom);
2444 wacom_release_resources(wacom1);
2445 wacom_release_resources(wacom2);
2449 static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2451 struct wacom_remote *remote = wacom->remote;
2452 u32 serial = remote->remotes[index].serial;
2454 unsigned long flags;
2456 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2457 if (remote->remotes[i].serial == serial) {
2459 spin_lock_irqsave(&remote->remote_lock, flags);
2460 remote->remotes[i].registered = false;
2461 spin_unlock_irqrestore(&remote->remote_lock, flags);
2463 if (remote->remotes[i].battery.battery)
2464 devres_release_group(&wacom->hdev->dev,
2465 &remote->remotes[i].battery.bat_desc);
2467 if (remote->remotes[i].group.name)
2468 devres_release_group(&wacom->hdev->dev,
2469 &remote->remotes[i]);
2471 remote->remotes[i].serial = 0;
2472 remote->remotes[i].group.name = NULL;
2473 remote->remotes[i].battery.battery = NULL;
2474 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2479 static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2482 struct wacom_remote *remote = wacom->remote;
2483 struct device *dev = &wacom->hdev->dev;
2486 /* A remote can pair more than once with an EKR,
2487 * check to make sure this serial isn't already paired.
2489 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2490 if (remote->remotes[k].serial == serial)
2494 if (k < WACOM_MAX_REMOTES) {
2495 remote->remotes[index].serial = serial;
2499 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2502 error = wacom_remote_create_attr_group(wacom, serial, index);
2506 remote->remotes[index].input = wacom_allocate_input(wacom);
2507 if (!remote->remotes[index].input) {
2511 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2512 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2514 if (!remote->remotes[index].input->name) {
2519 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2524 remote->remotes[index].serial = serial;
2526 error = input_register_device(remote->remotes[index].input);
2530 error = wacom_led_groups_alloc_and_register_one(
2531 &remote->remotes[index].input->dev,
2532 wacom, index, 3, true);
2536 remote->remotes[index].registered = true;
2538 devres_close_group(dev, &remote->remotes[index]);
2542 devres_release_group(dev, &remote->remotes[index]);
2543 remote->remotes[index].serial = 0;
2547 static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2549 struct wacom_remote *remote = wacom->remote;
2552 if (!remote->remotes[index].registered)
2555 if (remote->remotes[index].battery.battery)
2558 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2561 error = __wacom_initialize_battery(wacom,
2562 &wacom->remote->remotes[index].battery);
2569 static void wacom_remote_work(struct work_struct *work)
2571 struct wacom *wacom = container_of(work, struct wacom, remote_work);
2572 struct wacom_remote *remote = wacom->remote;
2573 struct wacom_remote_data data;
2574 unsigned long flags;
2579 spin_lock_irqsave(&remote->remote_lock, flags);
2581 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2583 if (count != sizeof(data)) {
2584 hid_err(wacom->hdev,
2585 "workitem triggered without status available\n");
2586 spin_unlock_irqrestore(&remote->remote_lock, flags);
2590 if (!kfifo_is_empty(&remote->remote_fifo))
2591 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2593 spin_unlock_irqrestore(&remote->remote_lock, flags);
2595 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2596 serial = data.remote[i].serial;
2597 if (data.remote[i].connected) {
2599 if (remote->remotes[i].serial == serial) {
2600 wacom_remote_attach_battery(wacom, i);
2604 if (remote->remotes[i].serial)
2605 wacom_remote_destroy_one(wacom, i);
2607 wacom_remote_create_one(wacom, serial, i);
2609 } else if (remote->remotes[i].serial) {
2610 wacom_remote_destroy_one(wacom, i);
2615 static void wacom_mode_change_work(struct work_struct *work)
2617 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2618 struct wacom_shared *shared = wacom->wacom_wac.shared;
2619 struct wacom *wacom1 = NULL;
2620 struct wacom *wacom2 = NULL;
2621 bool is_direct = wacom->wacom_wac.is_direct_mode;
2625 wacom1 = hid_get_drvdata(shared->pen);
2626 wacom_release_resources(wacom1);
2627 hid_hw_stop(wacom1->hdev);
2628 wacom1->wacom_wac.has_mode_change = true;
2629 wacom1->wacom_wac.is_direct_mode = is_direct;
2632 if (shared->touch) {
2633 wacom2 = hid_get_drvdata(shared->touch);
2634 wacom_release_resources(wacom2);
2635 hid_hw_stop(wacom2->hdev);
2636 wacom2->wacom_wac.has_mode_change = true;
2637 wacom2->wacom_wac.is_direct_mode = is_direct;
2641 error = wacom_parse_and_register(wacom1, false);
2647 error = wacom_parse_and_register(wacom2, false);
2655 static int wacom_probe(struct hid_device *hdev,
2656 const struct hid_device_id *id)
2658 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2659 struct usb_device *dev = interface_to_usbdev(intf);
2660 struct wacom *wacom;
2661 struct wacom_wac *wacom_wac;
2662 struct wacom_features *features;
2665 if (!id->driver_data)
2668 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2670 /* hid-core sets this quirk for the boot interface */
2671 hdev->quirks &= ~HID_QUIRK_NOGET;
2673 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2677 hid_set_drvdata(hdev, wacom);
2680 wacom_wac = &wacom->wacom_wac;
2681 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2682 features = &wacom_wac->features;
2684 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2689 error = kfifo_alloc(&wacom_wac->pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
2693 wacom_wac->hid_data.inputmode = -1;
2694 wacom_wac->mode_report = -1;
2696 wacom->usbdev = dev;
2698 mutex_init(&wacom->lock);
2699 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
2700 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2701 INIT_WORK(&wacom->battery_work, wacom_battery_work);
2702 INIT_WORK(&wacom->remote_work, wacom_remote_work);
2703 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
2705 /* ask for the report descriptor to be loaded by HID */
2706 error = hid_parse(hdev);
2708 hid_err(hdev, "parse failed\n");
2712 error = wacom_parse_and_register(wacom, false);
2716 if (hdev->bus == BUS_BLUETOOTH) {
2717 error = device_create_file(&hdev->dev, &dev_attr_speed);
2720 "can't create sysfs speed attribute err: %d\n",
2727 hid_set_drvdata(hdev, NULL);
2731 static void wacom_remove(struct hid_device *hdev)
2733 struct wacom *wacom = hid_get_drvdata(hdev);
2734 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2735 struct wacom_features *features = &wacom_wac->features;
2737 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2742 cancel_delayed_work_sync(&wacom->init_work);
2743 cancel_work_sync(&wacom->wireless_work);
2744 cancel_work_sync(&wacom->battery_work);
2745 cancel_work_sync(&wacom->remote_work);
2746 cancel_work_sync(&wacom->mode_change_work);
2747 if (hdev->bus == BUS_BLUETOOTH)
2748 device_remove_file(&hdev->dev, &dev_attr_speed);
2750 /* make sure we don't trigger the LEDs */
2751 wacom_led_groups_release(wacom);
2753 if (wacom->wacom_wac.features.type != REMOTE)
2754 wacom_release_resources(wacom);
2756 kfifo_free(&wacom_wac->pen_fifo);
2758 hid_set_drvdata(hdev, NULL);
2762 static int wacom_resume(struct hid_device *hdev)
2764 struct wacom *wacom = hid_get_drvdata(hdev);
2766 mutex_lock(&wacom->lock);
2768 /* switch to wacom mode first */
2769 _wacom_query_tablet_data(wacom);
2770 wacom_led_control(wacom);
2772 mutex_unlock(&wacom->lock);
2777 static int wacom_reset_resume(struct hid_device *hdev)
2779 return wacom_resume(hdev);
2781 #endif /* CONFIG_PM */
2783 static struct hid_driver wacom_driver = {
2785 .id_table = wacom_ids,
2786 .probe = wacom_probe,
2787 .remove = wacom_remove,
2788 .report = wacom_wac_report,
2790 .resume = wacom_resume,
2791 .reset_resume = wacom_reset_resume,
2793 .raw_event = wacom_raw_event,
2795 module_hid_driver(wacom_driver);
2797 MODULE_VERSION(DRIVER_VERSION);
2798 MODULE_AUTHOR(DRIVER_AUTHOR);
2799 MODULE_DESCRIPTION(DRIVER_DESC);
2800 MODULE_LICENSE("GPL");