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 int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
60 u8 *raw_data, int size)
62 struct wacom *wacom = hid_get_drvdata(hdev);
64 if (wacom->wacom_wac.features.type == BOOTLOADER)
67 if (size > WACOM_PKGLEN_MAX)
70 memcpy(wacom->wacom_wac.data, raw_data, size);
72 wacom_wac_irq(&wacom->wacom_wac, size);
77 static int wacom_open(struct input_dev *dev)
79 struct wacom *wacom = input_get_drvdata(dev);
81 return hid_hw_open(wacom->hdev);
84 static void wacom_close(struct input_dev *dev)
86 struct wacom *wacom = input_get_drvdata(dev);
89 * wacom->hdev should never be null, but surprisingly, I had the case
90 * once while unplugging the Wacom Wireless Receiver.
93 hid_hw_close(wacom->hdev);
97 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
99 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
100 unsigned unit, int exponent)
102 struct hid_field field = {
103 .logical_maximum = logical_extents,
104 .physical_maximum = physical_extents,
106 .unit_exponent = exponent,
109 return hidinput_calc_abs_res(&field, ABS_X);
112 static void wacom_feature_mapping(struct hid_device *hdev,
113 struct hid_field *field, struct hid_usage *usage)
115 struct wacom *wacom = hid_get_drvdata(hdev);
116 struct wacom_features *features = &wacom->wacom_wac.features;
117 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
118 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
123 switch (equivalent_usage) {
124 case WACOM_HID_WD_TOUCH_RING_SETTING:
125 wacom->generic_has_leds = true;
127 case HID_DG_CONTACTMAX:
128 /* leave touch_max as is if predefined */
129 if (!features->touch_max) {
131 n = hid_report_len(field->report);
132 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
135 data[0] = field->report->id;
136 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
137 data, n, WAC_CMD_RETRIES);
138 if (ret == n && features->type == HID_GENERIC) {
139 ret = hid_report_raw_event(hdev,
140 HID_FEATURE_REPORT, data, n, 0);
141 } else if (ret == 2 && features->type != HID_GENERIC) {
142 features->touch_max = data[1];
144 features->touch_max = 16;
145 hid_warn(hdev, "wacom_feature_mapping: "
146 "could not get HID_DG_CONTACTMAX, "
147 "defaulting to %d\n",
148 features->touch_max);
153 case HID_DG_INPUTMODE:
154 /* Ignore if value index is out of bounds. */
155 if (usage->usage_index >= field->report_count) {
156 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
160 hid_data->inputmode = field->report->id;
161 hid_data->inputmode_index = usage->usage_index;
164 case HID_UP_DIGITIZER:
165 if (field->report->id == 0x0B &&
166 (field->application == WACOM_HID_G9_PEN ||
167 field->application == WACOM_HID_G11_PEN)) {
168 wacom->wacom_wac.mode_report = field->report->id;
169 wacom->wacom_wac.mode_value = 0;
173 case WACOM_HID_WD_DATAMODE:
174 wacom->wacom_wac.mode_report = field->report->id;
175 wacom->wacom_wac.mode_value = 2;
178 case WACOM_HID_UP_G9:
179 case WACOM_HID_UP_G11:
180 if (field->report->id == 0x03 &&
181 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
182 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
183 wacom->wacom_wac.mode_report = field->report->id;
184 wacom->wacom_wac.mode_value = 0;
187 case WACOM_HID_WD_OFFSETLEFT:
188 case WACOM_HID_WD_OFFSETTOP:
189 case WACOM_HID_WD_OFFSETRIGHT:
190 case WACOM_HID_WD_OFFSETBOTTOM:
192 n = hid_report_len(field->report);
193 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
196 data[0] = field->report->id;
197 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
198 data, n, WAC_CMD_RETRIES);
200 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
203 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
212 * Interface Descriptor of wacom devices can be incomplete and
213 * inconsistent so wacom_features table is used to store stylus
214 * device's packet lengths, various maximum values, and tablet
215 * resolution based on product ID's.
217 * For devices that contain 2 interfaces, wacom_features table is
218 * inaccurate for the touch interface. Since the Interface Descriptor
219 * for touch interfaces has pretty complete data, this function exists
220 * to query tablet for this missing information instead of hard coding in
221 * an additional table.
223 * A typical Interface Descriptor for a stylus will contain a
224 * boot mouse application collection that is not of interest and this
225 * function will ignore it.
227 * It also contains a digitizer application collection that also is not
228 * of interest since any information it contains would be duplicate
229 * of what is in wacom_features. Usually it defines a report of an array
230 * of bytes that could be used as max length of the stylus packet returned.
231 * If it happens to define a Digitizer-Stylus Physical Collection then
232 * the X and Y logical values contain valid data but it is ignored.
234 * A typical Interface Descriptor for a touch interface will contain a
235 * Digitizer-Finger Physical Collection which will define both logical
236 * X/Y maximum as well as the physical size of tablet. Since touch
237 * interfaces haven't supported pressure or distance, this is enough
238 * information to override invalid values in the wacom_features table.
240 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
241 * data. We deal with them after returning from this function.
243 static void wacom_usage_mapping(struct hid_device *hdev,
244 struct hid_field *field, struct hid_usage *usage)
246 struct wacom *wacom = hid_get_drvdata(hdev);
247 struct wacom_features *features = &wacom->wacom_wac.features;
248 bool finger = WACOM_FINGER_FIELD(field);
249 bool pen = WACOM_PEN_FIELD(field);
252 * Requiring Stylus Usage will ignore boot mouse
253 * X/Y values and some cases of invalid Digitizer X/Y
254 * values commonly reported.
257 features->device_type |= WACOM_DEVICETYPE_PEN;
259 features->device_type |= WACOM_DEVICETYPE_TOUCH;
264 * Bamboo models do not support HID_DG_CONTACTMAX.
265 * And, Bamboo Pen only descriptor contains touch.
267 if (features->type > BAMBOO_PT) {
268 /* ISDv4 touch devices at least supports one touch point */
269 if (finger && !features->touch_max)
270 features->touch_max = 1;
274 * ISDv4 devices which predate HID's adoption of the
275 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
276 * position instead. We can accurately detect if a
277 * usage with that value should be HID_DG_BARRELSWITCH2
278 * based on the surrounding usages, which have remained
279 * constant across generations.
281 if (features->type == HID_GENERIC &&
282 usage->hid == 0x000D0000 &&
283 field->application == HID_DG_PEN &&
284 field->physical == HID_DG_STYLUS) {
285 int i = usage->usage_index;
287 if (i-4 >= 0 && i+1 < field->maxusage &&
288 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
289 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
290 field->usage[i-2].hid == HID_DG_ERASER &&
291 field->usage[i-1].hid == HID_DG_INVERT &&
292 field->usage[i+1].hid == HID_DG_INRANGE) {
293 usage->hid = HID_DG_BARRELSWITCH2;
297 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
298 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
299 hdev->product == 0x0358 &&
300 WACOM_PEN_FIELD(field) &&
301 wacom_equivalent_usage(usage->hid) == HID_GD_Y) {
302 field->logical_maximum = 43200;
305 switch (usage->hid) {
307 features->x_max = field->logical_maximum;
309 features->x_phy = field->physical_maximum;
310 if ((features->type != BAMBOO_PT) &&
311 (features->type != BAMBOO_TOUCH)) {
312 features->unit = field->unit;
313 features->unitExpo = field->unit_exponent;
318 features->y_max = field->logical_maximum;
320 features->y_phy = field->physical_maximum;
321 if ((features->type != BAMBOO_PT) &&
322 (features->type != BAMBOO_TOUCH)) {
323 features->unit = field->unit;
324 features->unitExpo = field->unit_exponent;
328 case HID_DG_TIPPRESSURE:
330 features->pressure_max = field->logical_maximum;
334 if (features->type == HID_GENERIC)
335 wacom_wac_usage_mapping(hdev, field, usage);
338 static void wacom_post_parse_hid(struct hid_device *hdev,
339 struct wacom_features *features)
341 struct wacom *wacom = hid_get_drvdata(hdev);
342 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
344 if (features->type == HID_GENERIC) {
345 /* Any last-minute generic device setup */
346 if (wacom_wac->has_mode_change) {
347 if (wacom_wac->is_direct_mode)
348 features->device_type |= WACOM_DEVICETYPE_DIRECT;
350 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
353 if (features->touch_max > 1) {
354 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
355 input_mt_init_slots(wacom_wac->touch_input,
356 wacom_wac->features.touch_max,
359 input_mt_init_slots(wacom_wac->touch_input,
360 wacom_wac->features.touch_max,
366 static void wacom_parse_hid(struct hid_device *hdev,
367 struct wacom_features *features)
369 struct hid_report_enum *rep_enum;
370 struct hid_report *hreport;
373 /* check features first */
374 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
375 list_for_each_entry(hreport, &rep_enum->report_list, list) {
376 for (i = 0; i < hreport->maxfield; i++) {
377 /* Ignore if report count is out of bounds. */
378 if (hreport->field[i]->report_count < 1)
381 for (j = 0; j < hreport->field[i]->maxusage; j++) {
382 wacom_feature_mapping(hdev, hreport->field[i],
383 hreport->field[i]->usage + j);
388 /* now check the input usages */
389 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
390 list_for_each_entry(hreport, &rep_enum->report_list, list) {
392 if (!hreport->maxfield)
395 for (i = 0; i < hreport->maxfield; i++)
396 for (j = 0; j < hreport->field[i]->maxusage; j++)
397 wacom_usage_mapping(hdev, hreport->field[i],
398 hreport->field[i]->usage + j);
401 wacom_post_parse_hid(hdev, features);
404 static int wacom_hid_set_device_mode(struct hid_device *hdev)
406 struct wacom *wacom = hid_get_drvdata(hdev);
407 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
408 struct hid_report *r;
409 struct hid_report_enum *re;
411 if (hid_data->inputmode < 0)
414 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
415 r = re->report_id_hash[hid_data->inputmode];
417 r->field[0]->value[hid_data->inputmode_index] = 2;
418 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
423 static int wacom_set_device_mode(struct hid_device *hdev,
424 struct wacom_wac *wacom_wac)
427 struct hid_report *r;
428 struct hid_report_enum *re;
430 int error = -ENOMEM, limit = 0;
432 if (wacom_wac->mode_report < 0)
435 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
436 r = re->report_id_hash[wacom_wac->mode_report];
440 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
444 length = hid_report_len(r);
447 rep_data[0] = wacom_wac->mode_report;
448 rep_data[1] = wacom_wac->mode_value;
450 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
453 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
454 rep_data, length, 1);
455 } while (error >= 0 &&
456 rep_data[1] != wacom_wac->mode_report &&
457 limit++ < WAC_MSG_RETRIES);
461 return error < 0 ? error : 0;
464 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
465 struct wacom_features *features)
467 struct wacom *wacom = hid_get_drvdata(hdev);
471 switch (features->type) {
475 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
479 rep_data[0] = speed == 0 ? 0x05 : 0x06;
482 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
486 wacom->wacom_wac.bt_high_speed = speed;
492 * Note that if the raw queries fail, it's not a hard failure
493 * and it is safe to continue
495 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
500 wacom->wacom_wac.bt_features &= ~0x20;
502 wacom->wacom_wac.bt_features |= 0x20;
505 rep_data[1] = wacom->wacom_wac.bt_features;
507 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
510 wacom->wacom_wac.bt_high_speed = speed;
518 * Switch the tablet into its most-capable mode. Wacom tablets are
519 * typically configured to power-up in a mode which sends mouse-like
520 * reports to the OS. To get absolute position, pressure data, etc.
521 * from the tablet, it is necessary to switch the tablet out of this
522 * mode and into one which sends the full range of tablet data.
524 static int _wacom_query_tablet_data(struct wacom *wacom)
526 struct hid_device *hdev = wacom->hdev;
527 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
528 struct wacom_features *features = &wacom_wac->features;
530 if (hdev->bus == BUS_BLUETOOTH)
531 return wacom_bt_query_tablet_data(hdev, 1, features);
533 if (features->type != HID_GENERIC) {
534 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
535 if (features->type > TABLETPC) {
536 /* MT Tablet PC touch */
537 wacom_wac->mode_report = 3;
538 wacom_wac->mode_value = 4;
539 } else if (features->type == WACOM_24HDT) {
540 wacom_wac->mode_report = 18;
541 wacom_wac->mode_value = 2;
542 } else if (features->type == WACOM_27QHDT) {
543 wacom_wac->mode_report = 131;
544 wacom_wac->mode_value = 2;
545 } else if (features->type == BAMBOO_PAD) {
546 wacom_wac->mode_report = 2;
547 wacom_wac->mode_value = 2;
549 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
550 if (features->type <= BAMBOO_PT) {
551 wacom_wac->mode_report = 2;
552 wacom_wac->mode_value = 2;
557 wacom_set_device_mode(hdev, wacom_wac);
559 if (features->type == HID_GENERIC)
560 return wacom_hid_set_device_mode(hdev);
565 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
566 struct wacom_features *features)
568 struct wacom *wacom = hid_get_drvdata(hdev);
569 struct usb_interface *intf = wacom->intf;
571 /* default features */
572 features->x_fuzz = 4;
573 features->y_fuzz = 4;
574 features->pressure_fuzz = 0;
575 features->distance_fuzz = 1;
576 features->tilt_fuzz = 1;
579 * The wireless device HID is basic and layout conflicts with
580 * other tablets (monitor and touch interface can look like pen).
581 * Skip the query for this type and modify defaults based on
584 if (features->type == WIRELESS && intf) {
585 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
586 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
588 features->device_type = WACOM_DEVICETYPE_NONE;
592 wacom_parse_hid(hdev, features);
595 struct wacom_hdev_data {
596 struct list_head list;
598 struct hid_device *dev;
599 struct wacom_shared shared;
602 static LIST_HEAD(wacom_udev_list);
603 static DEFINE_MUTEX(wacom_udev_list_lock);
605 static bool compare_device_paths(struct hid_device *hdev_a,
606 struct hid_device *hdev_b, char separator)
608 int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
609 int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
611 if (n1 != n2 || n1 <= 0 || n2 <= 0)
614 return !strncmp(hdev_a->phys, hdev_b->phys, n1);
617 static bool wacom_are_sibling(struct hid_device *hdev,
618 struct hid_device *sibling)
620 struct wacom *wacom = hid_get_drvdata(hdev);
621 struct wacom_features *features = &wacom->wacom_wac.features;
622 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
623 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
624 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
625 __u32 oPid = features->oPid ? features->oPid : hdev->product;
627 /* The defined oVid/oPid must match that of the sibling */
628 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
630 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
634 * Devices with the same VID/PID must share the same physical
635 * device path, while those with different VID/PID must share
636 * the same physical parent device path.
638 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
639 if (!compare_device_paths(hdev, sibling, '/'))
642 if (!compare_device_paths(hdev, sibling, '.'))
646 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
647 if (features->type != HID_GENERIC)
651 * Direct-input devices may not be siblings of indirect-input
654 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
655 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
659 * Indirect-input devices may not be siblings of direct-input
662 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
663 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
666 /* Pen devices may only be siblings of touch devices */
667 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
668 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
671 /* Touch devices may only be siblings of pen devices */
672 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
673 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
677 * No reason could be found for these two devices to NOT be
678 * siblings, so there's a good chance they ARE siblings
683 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
685 struct wacom_hdev_data *data;
687 /* Try to find an already-probed interface from the same device */
688 list_for_each_entry(data, &wacom_udev_list, list) {
689 if (compare_device_paths(hdev, data->dev, '/')) {
690 kref_get(&data->kref);
695 /* Fallback to finding devices that appear to be "siblings" */
696 list_for_each_entry(data, &wacom_udev_list, list) {
697 if (wacom_are_sibling(hdev, data->dev)) {
698 kref_get(&data->kref);
706 static void wacom_release_shared_data(struct kref *kref)
708 struct wacom_hdev_data *data =
709 container_of(kref, struct wacom_hdev_data, kref);
711 mutex_lock(&wacom_udev_list_lock);
712 list_del(&data->list);
713 mutex_unlock(&wacom_udev_list_lock);
718 static void wacom_remove_shared_data(void *res)
720 struct wacom *wacom = res;
721 struct wacom_hdev_data *data;
722 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
724 if (wacom_wac->shared) {
725 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
728 if (wacom_wac->shared->touch == wacom->hdev)
729 wacom_wac->shared->touch = NULL;
730 else if (wacom_wac->shared->pen == wacom->hdev)
731 wacom_wac->shared->pen = NULL;
733 kref_put(&data->kref, wacom_release_shared_data);
734 wacom_wac->shared = NULL;
738 static int wacom_add_shared_data(struct hid_device *hdev)
740 struct wacom *wacom = hid_get_drvdata(hdev);
741 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
742 struct wacom_hdev_data *data;
745 mutex_lock(&wacom_udev_list_lock);
747 data = wacom_get_hdev_data(hdev);
749 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
755 kref_init(&data->kref);
757 list_add_tail(&data->list, &wacom_udev_list);
760 wacom_wac->shared = &data->shared;
762 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
764 mutex_unlock(&wacom_udev_list_lock);
765 wacom_remove_shared_data(wacom);
769 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
770 wacom_wac->shared->touch = hdev;
771 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
772 wacom_wac->shared->pen = hdev;
775 mutex_unlock(&wacom_udev_list_lock);
779 static int wacom_led_control(struct wacom *wacom)
783 unsigned char report_id = WAC_CMD_LED_CONTROL;
786 if (!wacom->led.groups)
789 if (wacom->wacom_wac.features.type == REMOTE)
792 if (wacom->wacom_wac.pid) { /* wireless connected */
793 report_id = WAC_CMD_WL_LED_CONTROL;
796 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
797 report_id = WAC_CMD_WL_INTUOSP2;
800 buf = kzalloc(buf_size, GFP_KERNEL);
804 if (wacom->wacom_wac.features.type == HID_GENERIC) {
805 buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
806 buf[1] = wacom->led.llv;
807 buf[2] = wacom->led.groups[0].select & 0x03;
809 } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
810 wacom->wacom_wac.features.type <= INTUOSPL)) {
812 * Touch Ring and crop mark LED luminance may take on
813 * one of four values:
814 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
816 int ring_led = wacom->led.groups[0].select & 0x03;
817 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
819 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
822 if (wacom->wacom_wac.pid) {
823 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
824 buf, buf_size, WAC_CMD_RETRIES);
830 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
832 buf[4] = 100; // Power Connection LED (ORANGE)
833 buf[5] = 100; // BT Connection LED (BLUE)
834 buf[6] = 100; // Paper Mode (RED?)
835 buf[7] = 100; // Paper Mode (GREEN?)
836 buf[8] = 100; // Paper Mode (BLUE?)
837 buf[9] = wacom->led.llv;
838 buf[10] = wacom->led.groups[0].select & 0x03;
841 int led = wacom->led.groups[0].select | 0x4;
843 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
844 wacom->wacom_wac.features.type == WACOM_24HD)
845 led |= (wacom->led.groups[1].select << 4) | 0x40;
849 buf[2] = wacom->led.llv;
850 buf[3] = wacom->led.hlv;
851 buf[4] = wacom->led.img_lum;
854 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
861 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
862 const unsigned len, const void *img)
866 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
868 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
872 /* Send 'start' command */
873 buf[0] = WAC_CMD_ICON_START;
875 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
881 buf[1] = button_id & 0x07;
882 for (i = 0; i < 4; i++) {
884 memcpy(buf + 3, img + i * chunk_len, chunk_len);
886 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
887 buf, chunk_len + 3, WAC_CMD_RETRIES);
893 buf[0] = WAC_CMD_ICON_START;
895 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
903 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
904 const char *buf, size_t count)
906 struct hid_device *hdev = to_hid_device(dev);
907 struct wacom *wacom = hid_get_drvdata(hdev);
911 err = kstrtouint(buf, 10, &id);
915 mutex_lock(&wacom->lock);
917 wacom->led.groups[set_id].select = id & 0x3;
918 err = wacom_led_control(wacom);
920 mutex_unlock(&wacom->lock);
922 return err < 0 ? err : count;
925 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
926 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
927 struct device_attribute *attr, const char *buf, size_t count) \
929 return wacom_led_select_store(dev, SET_ID, buf, count); \
931 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
932 struct device_attribute *attr, char *buf) \
934 struct hid_device *hdev = to_hid_device(dev);\
935 struct wacom *wacom = hid_get_drvdata(hdev); \
936 return scnprintf(buf, PAGE_SIZE, "%d\n", \
937 wacom->led.groups[SET_ID].select); \
939 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
940 wacom_led##SET_ID##_select_show, \
941 wacom_led##SET_ID##_select_store)
943 DEVICE_LED_SELECT_ATTR(0);
944 DEVICE_LED_SELECT_ATTR(1);
946 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
947 const char *buf, size_t count)
952 err = kstrtouint(buf, 10, &value);
956 mutex_lock(&wacom->lock);
958 *dest = value & 0x7f;
959 err = wacom_led_control(wacom);
961 mutex_unlock(&wacom->lock);
963 return err < 0 ? err : count;
966 #define DEVICE_LUMINANCE_ATTR(name, field) \
967 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
968 struct device_attribute *attr, const char *buf, size_t count) \
970 struct hid_device *hdev = to_hid_device(dev);\
971 struct wacom *wacom = hid_get_drvdata(hdev); \
973 return wacom_luminance_store(wacom, &wacom->led.field, \
976 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
977 struct device_attribute *attr, char *buf) \
979 struct wacom *wacom = dev_get_drvdata(dev); \
980 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
982 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
983 wacom_##name##_luminance_show, \
984 wacom_##name##_luminance_store)
986 DEVICE_LUMINANCE_ATTR(status0, llv);
987 DEVICE_LUMINANCE_ATTR(status1, hlv);
988 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
990 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
991 const char *buf, size_t count)
993 struct hid_device *hdev = to_hid_device(dev);
994 struct wacom *wacom = hid_get_drvdata(hdev);
999 if (hdev->bus == BUS_BLUETOOTH) {
1001 xfer_id = WAC_CMD_ICON_BT_XFER;
1004 xfer_id = WAC_CMD_ICON_XFER;
1010 mutex_lock(&wacom->lock);
1012 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
1014 mutex_unlock(&wacom->lock);
1016 return err < 0 ? err : count;
1019 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1020 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1021 struct device_attribute *attr, const char *buf, size_t count) \
1023 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1025 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
1026 NULL, wacom_btnimg##BUTTON_ID##_store)
1028 DEVICE_BTNIMG_ATTR(0);
1029 DEVICE_BTNIMG_ATTR(1);
1030 DEVICE_BTNIMG_ATTR(2);
1031 DEVICE_BTNIMG_ATTR(3);
1032 DEVICE_BTNIMG_ATTR(4);
1033 DEVICE_BTNIMG_ATTR(5);
1034 DEVICE_BTNIMG_ATTR(6);
1035 DEVICE_BTNIMG_ATTR(7);
1037 static struct attribute *cintiq_led_attrs[] = {
1038 &dev_attr_status_led0_select.attr,
1039 &dev_attr_status_led1_select.attr,
1043 static struct attribute_group cintiq_led_attr_group = {
1044 .name = "wacom_led",
1045 .attrs = cintiq_led_attrs,
1048 static struct attribute *intuos4_led_attrs[] = {
1049 &dev_attr_status0_luminance.attr,
1050 &dev_attr_status1_luminance.attr,
1051 &dev_attr_status_led0_select.attr,
1052 &dev_attr_buttons_luminance.attr,
1053 &dev_attr_button0_rawimg.attr,
1054 &dev_attr_button1_rawimg.attr,
1055 &dev_attr_button2_rawimg.attr,
1056 &dev_attr_button3_rawimg.attr,
1057 &dev_attr_button4_rawimg.attr,
1058 &dev_attr_button5_rawimg.attr,
1059 &dev_attr_button6_rawimg.attr,
1060 &dev_attr_button7_rawimg.attr,
1064 static struct attribute_group intuos4_led_attr_group = {
1065 .name = "wacom_led",
1066 .attrs = intuos4_led_attrs,
1069 static struct attribute *intuos5_led_attrs[] = {
1070 &dev_attr_status0_luminance.attr,
1071 &dev_attr_status_led0_select.attr,
1075 static struct attribute_group intuos5_led_attr_group = {
1076 .name = "wacom_led",
1077 .attrs = intuos5_led_attrs,
1080 static struct attribute *generic_led_attrs[] = {
1081 &dev_attr_status0_luminance.attr,
1082 &dev_attr_status_led0_select.attr,
1086 static struct attribute_group generic_led_attr_group = {
1087 .name = "wacom_led",
1088 .attrs = generic_led_attrs,
1091 struct wacom_sysfs_group_devres {
1092 struct attribute_group *group;
1093 struct kobject *root;
1096 static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1098 struct wacom_sysfs_group_devres *devres = res;
1099 struct kobject *kobj = devres->root;
1101 dev_dbg(dev, "%s: dropping reference to %s\n",
1102 __func__, devres->group->name);
1103 sysfs_remove_group(kobj, devres->group);
1106 static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1107 struct kobject *root,
1108 struct attribute_group *group)
1110 struct wacom_sysfs_group_devres *devres;
1113 devres = devres_alloc(wacom_devm_sysfs_group_release,
1114 sizeof(struct wacom_sysfs_group_devres),
1119 devres->group = group;
1120 devres->root = root;
1122 error = sysfs_create_group(devres->root, group);
1124 devres_free(devres);
1128 devres_add(&wacom->hdev->dev, devres);
1133 static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1134 struct attribute_group *group)
1136 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1140 enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
1142 struct wacom *wacom = led->wacom;
1144 if (wacom->led.max_hlv)
1145 return led->hlv * LED_FULL / wacom->led.max_hlv;
1147 if (wacom->led.max_llv)
1148 return led->llv * LED_FULL / wacom->led.max_llv;
1150 /* device doesn't support brightness tuning */
1154 static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1156 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1157 struct wacom *wacom = led->wacom;
1159 if (wacom->led.groups[led->group].select != led->id)
1162 return wacom_leds_brightness_get(led);
1165 static int wacom_led_brightness_set(struct led_classdev *cdev,
1166 enum led_brightness brightness)
1168 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1169 struct wacom *wacom = led->wacom;
1172 mutex_lock(&wacom->lock);
1174 if (!wacom->led.groups || (brightness == LED_OFF &&
1175 wacom->led.groups[led->group].select != led->id)) {
1180 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1181 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1183 wacom->led.groups[led->group].select = led->id;
1185 error = wacom_led_control(wacom);
1188 mutex_unlock(&wacom->lock);
1193 static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1194 enum led_brightness brightness)
1198 static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1199 struct wacom_led *led, unsigned int group,
1200 unsigned int id, bool read_only)
1205 name = devm_kasprintf(dev, GFP_KERNEL,
1214 led->trigger.name = name;
1215 error = devm_led_trigger_register(dev, &led->trigger);
1217 hid_err(wacom->hdev,
1218 "failed to register LED trigger %s: %d\n",
1219 led->cdev.name, error);
1227 led->llv = wacom->led.llv;
1228 led->hlv = wacom->led.hlv;
1229 led->cdev.name = name;
1230 led->cdev.max_brightness = LED_FULL;
1231 led->cdev.flags = LED_HW_PLUGGABLE;
1232 led->cdev.brightness_get = __wacom_led_brightness_get;
1234 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
1235 led->cdev.default_trigger = led->cdev.name;
1237 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
1240 error = devm_led_classdev_register(dev, &led->cdev);
1242 hid_err(wacom->hdev,
1243 "failed to register LED %s: %d\n",
1244 led->cdev.name, error);
1245 led->cdev.name = NULL;
1252 static void wacom_led_groups_release_one(void *data)
1254 struct wacom_group_leds *group = data;
1256 devres_release_group(group->dev, group);
1259 static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1260 struct wacom *wacom,
1261 int group_id, int count,
1264 struct wacom_led *leds;
1267 if (group_id >= wacom->led.count || count <= 0)
1270 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1273 leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
1279 wacom->led.groups[group_id].leds = leds;
1280 wacom->led.groups[group_id].count = count;
1282 for (i = 0; i < count; i++) {
1283 error = wacom_led_register_one(dev, wacom, &leds[i],
1284 group_id, i, read_only);
1289 wacom->led.groups[group_id].dev = dev;
1291 devres_close_group(dev, &wacom->led.groups[group_id]);
1294 * There is a bug (?) in devm_led_classdev_register() in which its
1295 * increments the refcount of the parent. If the parent is an input
1296 * device, that means the ref count never reaches 0 when
1297 * devm_input_device_release() gets called.
1298 * This means that the LEDs are still there after disconnect.
1299 * Manually force the release of the group so that the leds are released
1300 * once we are done using them.
1302 error = devm_add_action_or_reset(&wacom->hdev->dev,
1303 wacom_led_groups_release_one,
1304 &wacom->led.groups[group_id]);
1311 devres_release_group(dev, &wacom->led.groups[group_id]);
1315 struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1318 struct wacom_group_leds *group;
1320 if (group_id >= wacom->led.count)
1323 group = &wacom->led.groups[group_id];
1330 return &group->leds[id];
1334 * wacom_led_next: gives the next available led with a wacom trigger.
1336 * returns the next available struct wacom_led which has its default trigger
1337 * or the current one if none is available.
1339 struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1341 struct wacom_led *next_led;
1351 next_led = wacom_led_find(wacom, group, ++next);
1352 if (!next_led || next_led == cur)
1354 } while (next_led->cdev.trigger != &next_led->trigger);
1359 static void wacom_led_groups_release(void *data)
1361 struct wacom *wacom = data;
1363 wacom->led.groups = NULL;
1364 wacom->led.count = 0;
1367 static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1369 struct device *dev = &wacom->hdev->dev;
1370 struct wacom_group_leds *groups;
1373 groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
1378 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1382 wacom->led.groups = groups;
1383 wacom->led.count = count;
1388 static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1389 int led_per_group, bool read_only)
1394 if (!wacom->wacom_wac.pad_input)
1397 dev = &wacom->wacom_wac.pad_input->dev;
1399 error = wacom_led_groups_allocate(wacom, group_count);
1403 for (i = 0; i < group_count; i++) {
1404 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1414 int wacom_initialize_leds(struct wacom *wacom)
1418 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1421 /* Initialize default values */
1422 switch (wacom->wacom_wac.features.type) {
1424 if (!wacom->generic_has_leds)
1426 wacom->led.llv = 100;
1427 wacom->led.max_llv = 100;
1429 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1431 hid_err(wacom->hdev,
1432 "cannot create leds err: %d\n", error);
1436 error = wacom_devm_sysfs_create_group(wacom,
1437 &generic_led_attr_group);
1444 wacom->led.llv = 10;
1445 wacom->led.hlv = 20;
1446 wacom->led.max_llv = 127;
1447 wacom->led.max_hlv = 127;
1448 wacom->led.img_lum = 10;
1450 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1452 hid_err(wacom->hdev,
1453 "cannot create leds err: %d\n", error);
1457 error = wacom_devm_sysfs_create_group(wacom,
1458 &intuos4_led_attr_group);
1465 wacom->led.img_lum = 0;
1467 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1469 hid_err(wacom->hdev,
1470 "cannot create leds err: %d\n", error);
1474 error = wacom_devm_sysfs_create_group(wacom,
1475 &cintiq_led_attr_group);
1484 wacom->led.llv = 32;
1485 wacom->led.max_llv = 96;
1487 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1489 hid_err(wacom->hdev,
1490 "cannot create leds err: %d\n", error);
1494 error = wacom_devm_sysfs_create_group(wacom,
1495 &intuos5_led_attr_group);
1499 wacom->led.llv = 50;
1500 wacom->led.max_llv = 100;
1501 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1503 hid_err(wacom->hdev,
1504 "cannot create leds err: %d\n", error);
1510 wacom->led.llv = 255;
1511 wacom->led.max_llv = 255;
1512 error = wacom_led_groups_allocate(wacom, 5);
1514 hid_err(wacom->hdev,
1515 "cannot create leds err: %d\n", error);
1525 hid_err(wacom->hdev,
1526 "cannot create sysfs group err: %d\n", error);
1533 static void wacom_init_work(struct work_struct *work)
1535 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1537 _wacom_query_tablet_data(wacom);
1538 wacom_led_control(wacom);
1541 static void wacom_query_tablet_data(struct wacom *wacom)
1543 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1546 static enum power_supply_property wacom_battery_props[] = {
1547 POWER_SUPPLY_PROP_MODEL_NAME,
1548 POWER_SUPPLY_PROP_PRESENT,
1549 POWER_SUPPLY_PROP_STATUS,
1550 POWER_SUPPLY_PROP_SCOPE,
1551 POWER_SUPPLY_PROP_CAPACITY
1554 static int wacom_battery_get_property(struct power_supply *psy,
1555 enum power_supply_property psp,
1556 union power_supply_propval *val)
1558 struct wacom_battery *battery = power_supply_get_drvdata(psy);
1562 case POWER_SUPPLY_PROP_MODEL_NAME:
1563 val->strval = battery->wacom->wacom_wac.name;
1565 case POWER_SUPPLY_PROP_PRESENT:
1566 val->intval = battery->bat_connected;
1568 case POWER_SUPPLY_PROP_SCOPE:
1569 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1571 case POWER_SUPPLY_PROP_CAPACITY:
1572 val->intval = battery->battery_capacity;
1574 case POWER_SUPPLY_PROP_STATUS:
1575 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1576 val->intval = battery->bat_status;
1577 else if (battery->bat_charging)
1578 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1579 else if (battery->battery_capacity == 100 &&
1580 battery->ps_connected)
1581 val->intval = POWER_SUPPLY_STATUS_FULL;
1582 else if (battery->ps_connected)
1583 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1585 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1595 static int __wacom_initialize_battery(struct wacom *wacom,
1596 struct wacom_battery *battery)
1598 static atomic_t battery_no = ATOMIC_INIT(0);
1599 struct device *dev = &wacom->hdev->dev;
1600 struct power_supply_config psy_cfg = { .drv_data = battery, };
1601 struct power_supply *ps_bat;
1602 struct power_supply_desc *bat_desc = &battery->bat_desc;
1606 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1609 battery->wacom = wacom;
1611 n = atomic_inc_return(&battery_no) - 1;
1613 bat_desc->properties = wacom_battery_props;
1614 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1615 bat_desc->get_property = wacom_battery_get_property;
1616 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1617 bat_desc->name = battery->bat_name;
1618 bat_desc->type = POWER_SUPPLY_TYPE_USB;
1619 bat_desc->use_for_apm = 0;
1621 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1622 if (IS_ERR(ps_bat)) {
1623 error = PTR_ERR(ps_bat);
1627 power_supply_powers(ps_bat, &wacom->hdev->dev);
1629 battery->battery = ps_bat;
1631 devres_close_group(dev, bat_desc);
1635 devres_release_group(dev, bat_desc);
1639 static int wacom_initialize_battery(struct wacom *wacom)
1641 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1642 return __wacom_initialize_battery(wacom, &wacom->battery);
1647 static void wacom_destroy_battery(struct wacom *wacom)
1649 if (wacom->battery.battery) {
1650 devres_release_group(&wacom->hdev->dev,
1651 &wacom->battery.bat_desc);
1652 wacom->battery.battery = NULL;
1656 static ssize_t wacom_show_speed(struct device *dev,
1657 struct device_attribute
1660 struct hid_device *hdev = to_hid_device(dev);
1661 struct wacom *wacom = hid_get_drvdata(hdev);
1663 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1666 static ssize_t wacom_store_speed(struct device *dev,
1667 struct device_attribute *attr,
1668 const char *buf, size_t count)
1670 struct hid_device *hdev = to_hid_device(dev);
1671 struct wacom *wacom = hid_get_drvdata(hdev);
1674 if (kstrtou8(buf, 0, &new_speed))
1677 if (new_speed != 0 && new_speed != 1)
1680 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1685 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1686 wacom_show_speed, wacom_store_speed);
1689 static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1690 struct kobj_attribute *kattr,
1691 char *buf, int index)
1693 struct device *dev = kobj_to_dev(kobj->parent);
1694 struct hid_device *hdev = to_hid_device(dev);
1695 struct wacom *wacom = hid_get_drvdata(hdev);
1698 mode = wacom->led.groups[index].select;
1699 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
1702 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1703 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1704 struct kobj_attribute *kattr, char *buf) \
1706 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1708 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1709 .attr = {.name = "remote_mode", \
1710 .mode = DEV_ATTR_RO_PERM}, \
1711 .show = wacom_show_remote##SET_ID##_mode, \
1713 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1714 &remote##SET_ID##_mode_attr.attr, \
1717 static struct attribute_group remote##SET_ID##_serial_group = { \
1719 .attrs = remote##SET_ID##_serial_attrs, \
1722 DEVICE_EKR_ATTR_GROUP(0);
1723 DEVICE_EKR_ATTR_GROUP(1);
1724 DEVICE_EKR_ATTR_GROUP(2);
1725 DEVICE_EKR_ATTR_GROUP(3);
1726 DEVICE_EKR_ATTR_GROUP(4);
1728 static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1732 struct wacom_remote *remote = wacom->remote;
1734 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1737 if (!remote->remotes[index].group.name)
1740 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1741 &remote->remotes[index].group);
1743 remote->remotes[index].group.name = NULL;
1744 hid_err(wacom->hdev,
1745 "cannot create sysfs group err: %d\n", error);
1752 static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1754 const size_t buf_size = 2;
1758 buf = kzalloc(buf_size, GFP_KERNEL);
1762 buf[0] = WAC_CMD_DELETE_PAIRING;
1765 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1766 buf_size, WAC_CMD_RETRIES);
1772 static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1773 struct kobj_attribute *attr,
1774 const char *buf, size_t count)
1776 unsigned char selector = 0;
1777 struct device *dev = kobj_to_dev(kobj->parent);
1778 struct hid_device *hdev = to_hid_device(dev);
1779 struct wacom *wacom = hid_get_drvdata(hdev);
1782 if (!strncmp(buf, "*\n", 2)) {
1783 selector = WAC_CMD_UNPAIR_ALL;
1785 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1790 mutex_lock(&wacom->lock);
1792 err = wacom_cmd_unpair_remote(wacom, selector);
1793 mutex_unlock(&wacom->lock);
1795 return err < 0 ? err : count;
1798 static struct kobj_attribute unpair_remote_attr = {
1799 .attr = {.name = "unpair_remote", .mode = 0200},
1800 .store = wacom_store_unpair_remote,
1803 static const struct attribute *remote_unpair_attrs[] = {
1804 &unpair_remote_attr.attr,
1808 static void wacom_remotes_destroy(void *data)
1810 struct wacom *wacom = data;
1811 struct wacom_remote *remote = wacom->remote;
1816 kobject_put(remote->remote_dir);
1817 kfifo_free(&remote->remote_fifo);
1818 wacom->remote = NULL;
1821 static int wacom_initialize_remotes(struct wacom *wacom)
1824 struct wacom_remote *remote;
1827 if (wacom->wacom_wac.features.type != REMOTE)
1830 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1835 wacom->remote = remote;
1837 spin_lock_init(&remote->remote_lock);
1839 error = kfifo_alloc(&remote->remote_fifo,
1840 5 * sizeof(struct wacom_remote_data),
1843 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1847 remote->remotes[0].group = remote0_serial_group;
1848 remote->remotes[1].group = remote1_serial_group;
1849 remote->remotes[2].group = remote2_serial_group;
1850 remote->remotes[3].group = remote3_serial_group;
1851 remote->remotes[4].group = remote4_serial_group;
1853 remote->remote_dir = kobject_create_and_add("wacom_remote",
1854 &wacom->hdev->dev.kobj);
1855 if (!remote->remote_dir)
1858 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
1861 hid_err(wacom->hdev,
1862 "cannot create sysfs group err: %d\n", error);
1866 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1867 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
1868 remote->remotes[i].serial = 0;
1871 error = devm_add_action_or_reset(&wacom->hdev->dev,
1872 wacom_remotes_destroy, wacom);
1879 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1881 struct input_dev *input_dev;
1882 struct hid_device *hdev = wacom->hdev;
1883 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1885 input_dev = devm_input_allocate_device(&hdev->dev);
1889 input_dev->name = wacom_wac->features.name;
1890 input_dev->phys = hdev->phys;
1891 input_dev->dev.parent = &hdev->dev;
1892 input_dev->open = wacom_open;
1893 input_dev->close = wacom_close;
1894 input_dev->uniq = hdev->uniq;
1895 input_dev->id.bustype = hdev->bus;
1896 input_dev->id.vendor = hdev->vendor;
1897 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1898 input_dev->id.version = hdev->version;
1899 input_set_drvdata(input_dev, wacom);
1904 static int wacom_allocate_inputs(struct wacom *wacom)
1906 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1908 wacom_wac->pen_input = wacom_allocate_input(wacom);
1909 wacom_wac->touch_input = wacom_allocate_input(wacom);
1910 wacom_wac->pad_input = wacom_allocate_input(wacom);
1911 if (!wacom_wac->pen_input ||
1912 !wacom_wac->touch_input ||
1913 !wacom_wac->pad_input)
1916 wacom_wac->pen_input->name = wacom_wac->pen_name;
1917 wacom_wac->touch_input->name = wacom_wac->touch_name;
1918 wacom_wac->pad_input->name = wacom_wac->pad_name;
1923 static int wacom_register_inputs(struct wacom *wacom)
1925 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
1926 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1929 pen_input_dev = wacom_wac->pen_input;
1930 touch_input_dev = wacom_wac->touch_input;
1931 pad_input_dev = wacom_wac->pad_input;
1933 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
1936 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1938 /* no pen in use on this interface */
1939 input_free_device(pen_input_dev);
1940 wacom_wac->pen_input = NULL;
1941 pen_input_dev = NULL;
1943 error = input_register_device(pen_input_dev);
1948 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1950 /* no touch in use on this interface */
1951 input_free_device(touch_input_dev);
1952 wacom_wac->touch_input = NULL;
1953 touch_input_dev = NULL;
1955 error = input_register_device(touch_input_dev);
1960 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1962 /* no pad events using this interface */
1963 input_free_device(pad_input_dev);
1964 wacom_wac->pad_input = NULL;
1965 pad_input_dev = NULL;
1967 error = input_register_device(pad_input_dev);
1975 wacom_wac->pad_input = NULL;
1976 wacom_wac->touch_input = NULL;
1977 wacom_wac->pen_input = NULL;
1982 * Not all devices report physical dimensions from HID.
1983 * Compute the default from hardcoded logical dimension
1984 * and resolution before driver overwrites them.
1986 static void wacom_set_default_phy(struct wacom_features *features)
1988 if (features->x_resolution) {
1989 features->x_phy = (features->x_max * 100) /
1990 features->x_resolution;
1991 features->y_phy = (features->y_max * 100) /
1992 features->y_resolution;
1996 static void wacom_calculate_res(struct wacom_features *features)
1998 /* set unit to "100th of a mm" for devices not reported by HID */
1999 if (!features->unit) {
2000 features->unit = 0x11;
2001 features->unitExpo = -3;
2004 features->x_resolution = wacom_calc_hid_res(features->x_max,
2007 features->unitExpo);
2008 features->y_resolution = wacom_calc_hid_res(features->y_max,
2011 features->unitExpo);
2014 void wacom_battery_work(struct work_struct *work)
2016 struct wacom *wacom = container_of(work, struct wacom, battery_work);
2018 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2019 !wacom->battery.battery) {
2020 wacom_initialize_battery(wacom);
2022 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2023 wacom->battery.battery) {
2024 wacom_destroy_battery(wacom);
2028 static size_t wacom_compute_pktlen(struct hid_device *hdev)
2030 struct hid_report_enum *report_enum;
2031 struct hid_report *report;
2034 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2036 list_for_each_entry(report, &report_enum->report_list, list) {
2037 size_t report_size = hid_report_len(report);
2038 if (report_size > size)
2045 static void wacom_update_name(struct wacom *wacom, const char *suffix)
2047 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2048 struct wacom_features *features = &wacom_wac->features;
2049 char name[WACOM_NAME_MAX];
2051 /* Generic devices name unspecified */
2052 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
2053 char *product_name = wacom->hdev->name;
2055 if (hid_is_usb(wacom->hdev)) {
2056 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2057 struct usb_device *dev = interface_to_usbdev(intf);
2058 product_name = dev->product;
2061 if (wacom->hdev->bus == BUS_I2C) {
2062 snprintf(name, sizeof(name), "%s %X",
2063 features->name, wacom->hdev->product);
2064 } else if (strstr(product_name, "Wacom") ||
2065 strstr(product_name, "wacom") ||
2066 strstr(product_name, "WACOM")) {
2067 strlcpy(name, product_name, sizeof(name));
2069 snprintf(name, sizeof(name), "Wacom %s", product_name);
2072 /* strip out excess whitespaces */
2074 char *gap = strstr(name, " ");
2077 /* shift everything including the terminator */
2078 memmove(gap, gap+1, strlen(gap));
2081 /* get rid of trailing whitespace */
2082 if (name[strlen(name)-1] == ' ')
2083 name[strlen(name)-1] = '\0';
2085 strlcpy(name, features->name, sizeof(name));
2088 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2091 /* Append the device type to the name */
2092 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
2093 "%s%s Pen", name, suffix);
2094 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
2095 "%s%s Finger", name, suffix);
2096 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
2097 "%s%s Pad", name, suffix);
2100 static void wacom_release_resources(struct wacom *wacom)
2102 struct hid_device *hdev = wacom->hdev;
2104 if (!wacom->resources)
2107 devres_release_group(&hdev->dev, wacom);
2109 wacom->resources = false;
2111 wacom->wacom_wac.pen_input = NULL;
2112 wacom->wacom_wac.touch_input = NULL;
2113 wacom->wacom_wac.pad_input = NULL;
2116 static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2118 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2119 wacom_wac->shared->type = wacom_wac->features.type;
2120 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2123 if (wacom_wac->has_mute_touch_switch) {
2124 wacom_wac->shared->has_mute_touch_switch = true;
2125 wacom_wac->shared->is_touch_on = true;
2128 if (wacom_wac->shared->has_mute_touch_switch &&
2129 wacom_wac->shared->touch_input) {
2130 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2131 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2136 static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
2138 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2139 struct wacom_features *features = &wacom_wac->features;
2140 struct hid_device *hdev = wacom->hdev;
2142 unsigned int connect_mask = HID_CONNECT_HIDRAW;
2144 features->pktlen = wacom_compute_pktlen(hdev);
2145 if (features->pktlen > WACOM_PKGLEN_MAX)
2148 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2151 wacom->resources = true;
2153 error = wacom_allocate_inputs(wacom);
2158 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2159 * into debug mode for the touch part.
2160 * We ignore the other interfaces.
2162 if (features->type == BAMBOO_PAD) {
2163 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2164 features->type = HID_GENERIC;
2165 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2166 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2172 /* set the default size in case we do not get them from hid */
2173 wacom_set_default_phy(features);
2175 /* Retrieve the physical and logical size for touch devices */
2176 wacom_retrieve_hid_descriptor(hdev, features);
2177 wacom_setup_device_quirks(wacom);
2179 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2180 features->type != WIRELESS) {
2181 error = features->type == HID_GENERIC ? -ENODEV : 0;
2183 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
2185 error ? "Ignoring" : "Assuming pen");
2190 features->device_type |= WACOM_DEVICETYPE_PEN;
2193 wacom_calculate_res(features);
2195 wacom_update_name(wacom, wireless ? " (WL)" : "");
2197 /* pen only Bamboo neither support touch nor pad */
2198 if ((features->type == BAMBOO_PEN) &&
2199 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2200 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2205 error = wacom_add_shared_data(hdev);
2209 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2210 (features->quirks & WACOM_QUIRK_BATTERY)) {
2211 error = wacom_initialize_battery(wacom);
2216 error = wacom_register_inputs(wacom);
2220 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
2221 error = wacom_initialize_leds(wacom);
2225 error = wacom_initialize_remotes(wacom);
2230 if (features->type == HID_GENERIC)
2231 connect_mask |= HID_CONNECT_DRIVER;
2233 /* Regular HID work starts now */
2234 error = hid_hw_start(hdev, connect_mask);
2236 hid_err(hdev, "hw start failed\n");
2241 /* Note that if query fails it is not a hard failure */
2242 wacom_query_tablet_data(wacom);
2245 /* touch only Bamboo doesn't support pen */
2246 if ((features->type == BAMBOO_TOUCH) &&
2247 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2248 cancel_delayed_work_sync(&wacom->init_work);
2249 _wacom_query_tablet_data(wacom);
2254 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR) {
2255 error = hid_hw_open(hdev);
2257 hid_err(hdev, "hw open failed\n");
2262 wacom_set_shared_values(wacom_wac);
2263 devres_close_group(&hdev->dev, wacom);
2270 wacom_release_resources(wacom);
2274 static void wacom_wireless_work(struct work_struct *work)
2276 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2277 struct usb_device *usbdev = wacom->usbdev;
2278 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2279 struct hid_device *hdev1, *hdev2;
2280 struct wacom *wacom1, *wacom2;
2281 struct wacom_wac *wacom_wac1, *wacom_wac2;
2285 * Regardless if this is a disconnect or a new tablet,
2286 * remove any existing input and battery devices.
2289 wacom_destroy_battery(wacom);
2294 /* Stylus interface */
2295 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2296 wacom1 = hid_get_drvdata(hdev1);
2297 wacom_wac1 = &(wacom1->wacom_wac);
2298 wacom_release_resources(wacom1);
2300 /* Touch interface */
2301 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2302 wacom2 = hid_get_drvdata(hdev2);
2303 wacom_wac2 = &(wacom2->wacom_wac);
2304 wacom_release_resources(wacom2);
2306 if (wacom_wac->pid == 0) {
2307 hid_info(wacom->hdev, "wireless tablet disconnected\n");
2309 const struct hid_device_id *id = wacom_ids;
2311 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2315 if (id->vendor == USB_VENDOR_ID_WACOM &&
2316 id->product == wacom_wac->pid)
2322 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2326 /* Stylus interface */
2327 wacom_wac1->features =
2328 *((struct wacom_features *)id->driver_data);
2330 wacom_wac1->pid = wacom_wac->pid;
2332 error = wacom_parse_and_register(wacom1, true);
2336 /* Touch interface */
2337 if (wacom_wac1->features.touch_max ||
2338 (wacom_wac1->features.type >= INTUOSHT &&
2339 wacom_wac1->features.type <= BAMBOO_PT)) {
2340 wacom_wac2->features =
2341 *((struct wacom_features *)id->driver_data);
2342 wacom_wac2->pid = wacom_wac->pid;
2344 error = wacom_parse_and_register(wacom2, true);
2349 strlcpy(wacom_wac->name, wacom_wac1->name,
2350 sizeof(wacom_wac->name));
2351 error = wacom_initialize_battery(wacom);
2359 wacom_release_resources(wacom1);
2360 wacom_release_resources(wacom2);
2364 static void wacom_remote_destroy_battery(struct wacom *wacom, int index)
2366 struct wacom_remote *remote = wacom->remote;
2368 if (remote->remotes[index].battery.battery) {
2369 devres_release_group(&wacom->hdev->dev,
2370 &remote->remotes[index].battery.bat_desc);
2371 remote->remotes[index].battery.battery = NULL;
2372 remote->remotes[index].active_time = 0;
2376 static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2378 struct wacom_remote *remote = wacom->remote;
2379 u32 serial = remote->remotes[index].serial;
2381 unsigned long flags;
2383 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2384 if (remote->remotes[i].serial == serial) {
2386 spin_lock_irqsave(&remote->remote_lock, flags);
2387 remote->remotes[i].registered = false;
2388 spin_unlock_irqrestore(&remote->remote_lock, flags);
2390 wacom_remote_destroy_battery(wacom, i);
2392 if (remote->remotes[i].group.name)
2393 devres_release_group(&wacom->hdev->dev,
2394 &remote->remotes[i]);
2396 remote->remotes[i].serial = 0;
2397 remote->remotes[i].group.name = NULL;
2398 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2403 static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2406 struct wacom_remote *remote = wacom->remote;
2407 struct device *dev = &wacom->hdev->dev;
2410 /* A remote can pair more than once with an EKR,
2411 * check to make sure this serial isn't already paired.
2413 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2414 if (remote->remotes[k].serial == serial)
2418 if (k < WACOM_MAX_REMOTES) {
2419 remote->remotes[index].serial = serial;
2423 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2426 error = wacom_remote_create_attr_group(wacom, serial, index);
2430 remote->remotes[index].input = wacom_allocate_input(wacom);
2431 if (!remote->remotes[index].input) {
2435 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2436 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2438 if (!remote->remotes[index].input->name) {
2443 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2448 remote->remotes[index].serial = serial;
2450 error = input_register_device(remote->remotes[index].input);
2454 error = wacom_led_groups_alloc_and_register_one(
2455 &remote->remotes[index].input->dev,
2456 wacom, index, 3, true);
2460 remote->remotes[index].registered = true;
2462 devres_close_group(dev, &remote->remotes[index]);
2466 devres_release_group(dev, &remote->remotes[index]);
2467 remote->remotes[index].serial = 0;
2471 static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2473 struct wacom_remote *remote = wacom->remote;
2476 if (!remote->remotes[index].registered)
2479 if (remote->remotes[index].battery.battery)
2482 if (!remote->remotes[index].active_time)
2485 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2488 error = __wacom_initialize_battery(wacom,
2489 &wacom->remote->remotes[index].battery);
2496 static void wacom_remote_work(struct work_struct *work)
2498 struct wacom *wacom = container_of(work, struct wacom, remote_work);
2499 struct wacom_remote *remote = wacom->remote;
2500 ktime_t kt = ktime_get();
2501 struct wacom_remote_data data;
2502 unsigned long flags;
2507 spin_lock_irqsave(&remote->remote_lock, flags);
2509 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2511 if (count != sizeof(data)) {
2512 hid_err(wacom->hdev,
2513 "workitem triggered without status available\n");
2514 spin_unlock_irqrestore(&remote->remote_lock, flags);
2518 if (!kfifo_is_empty(&remote->remote_fifo))
2519 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2521 spin_unlock_irqrestore(&remote->remote_lock, flags);
2523 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2524 serial = data.remote[i].serial;
2525 if (data.remote[i].connected) {
2527 if (kt - remote->remotes[i].active_time > WACOM_REMOTE_BATTERY_TIMEOUT
2528 && remote->remotes[i].active_time != 0)
2529 wacom_remote_destroy_battery(wacom, i);
2531 if (remote->remotes[i].serial == serial) {
2532 wacom_remote_attach_battery(wacom, i);
2536 if (remote->remotes[i].serial)
2537 wacom_remote_destroy_one(wacom, i);
2539 wacom_remote_create_one(wacom, serial, i);
2541 } else if (remote->remotes[i].serial) {
2542 wacom_remote_destroy_one(wacom, i);
2547 static void wacom_mode_change_work(struct work_struct *work)
2549 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2550 struct wacom_shared *shared = wacom->wacom_wac.shared;
2551 struct wacom *wacom1 = NULL;
2552 struct wacom *wacom2 = NULL;
2553 bool is_direct = wacom->wacom_wac.is_direct_mode;
2557 wacom1 = hid_get_drvdata(shared->pen);
2558 wacom_release_resources(wacom1);
2559 hid_hw_stop(wacom1->hdev);
2560 wacom1->wacom_wac.has_mode_change = true;
2561 wacom1->wacom_wac.is_direct_mode = is_direct;
2564 if (shared->touch) {
2565 wacom2 = hid_get_drvdata(shared->touch);
2566 wacom_release_resources(wacom2);
2567 hid_hw_stop(wacom2->hdev);
2568 wacom2->wacom_wac.has_mode_change = true;
2569 wacom2->wacom_wac.is_direct_mode = is_direct;
2573 error = wacom_parse_and_register(wacom1, false);
2579 error = wacom_parse_and_register(wacom2, false);
2587 static int wacom_probe(struct hid_device *hdev,
2588 const struct hid_device_id *id)
2590 struct wacom *wacom;
2591 struct wacom_wac *wacom_wac;
2592 struct wacom_features *features;
2595 if (!id->driver_data)
2598 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2600 /* hid-core sets this quirk for the boot interface */
2601 hdev->quirks &= ~HID_QUIRK_NOGET;
2603 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2607 hid_set_drvdata(hdev, wacom);
2610 wacom_wac = &wacom->wacom_wac;
2611 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2612 features = &wacom_wac->features;
2614 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2619 wacom_wac->hid_data.inputmode = -1;
2620 wacom_wac->mode_report = -1;
2622 if (hid_is_usb(hdev)) {
2623 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2624 struct usb_device *dev = interface_to_usbdev(intf);
2626 wacom->usbdev = dev;
2630 mutex_init(&wacom->lock);
2631 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
2632 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2633 INIT_WORK(&wacom->battery_work, wacom_battery_work);
2634 INIT_WORK(&wacom->remote_work, wacom_remote_work);
2635 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
2637 /* ask for the report descriptor to be loaded by HID */
2638 error = hid_parse(hdev);
2640 hid_err(hdev, "parse failed\n");
2644 if (features->type == BOOTLOADER) {
2645 hid_warn(hdev, "Using device in hidraw-only mode");
2646 return hid_hw_start(hdev, HID_CONNECT_HIDRAW);
2649 error = wacom_parse_and_register(wacom, false);
2653 if (hdev->bus == BUS_BLUETOOTH) {
2654 error = device_create_file(&hdev->dev, &dev_attr_speed);
2657 "can't create sysfs speed attribute err: %d\n",
2664 hid_set_drvdata(hdev, NULL);
2668 static void wacom_remove(struct hid_device *hdev)
2670 struct wacom *wacom = hid_get_drvdata(hdev);
2671 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2672 struct wacom_features *features = &wacom_wac->features;
2674 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2679 cancel_delayed_work_sync(&wacom->init_work);
2680 cancel_work_sync(&wacom->wireless_work);
2681 cancel_work_sync(&wacom->battery_work);
2682 cancel_work_sync(&wacom->remote_work);
2683 cancel_work_sync(&wacom->mode_change_work);
2684 if (hdev->bus == BUS_BLUETOOTH)
2685 device_remove_file(&hdev->dev, &dev_attr_speed);
2687 /* make sure we don't trigger the LEDs */
2688 wacom_led_groups_release(wacom);
2690 if (wacom->wacom_wac.features.type != REMOTE)
2691 wacom_release_resources(wacom);
2693 hid_set_drvdata(hdev, NULL);
2697 static int wacom_resume(struct hid_device *hdev)
2699 struct wacom *wacom = hid_get_drvdata(hdev);
2701 mutex_lock(&wacom->lock);
2703 /* switch to wacom mode first */
2704 _wacom_query_tablet_data(wacom);
2705 wacom_led_control(wacom);
2707 mutex_unlock(&wacom->lock);
2712 static int wacom_reset_resume(struct hid_device *hdev)
2714 return wacom_resume(hdev);
2716 #endif /* CONFIG_PM */
2718 static struct hid_driver wacom_driver = {
2720 .id_table = wacom_ids,
2721 .probe = wacom_probe,
2722 .remove = wacom_remove,
2723 .report = wacom_wac_report,
2725 .resume = wacom_resume,
2726 .reset_resume = wacom_reset_resume,
2728 .raw_event = wacom_raw_event,
2730 module_hid_driver(wacom_driver);
2732 MODULE_VERSION(DRIVER_VERSION);
2733 MODULE_AUTHOR(DRIVER_AUTHOR);
2734 MODULE_DESCRIPTION(DRIVER_DESC);
2735 MODULE_LICENSE("GPL");