Linux 4.19.133
[linux/fpc-iii.git] / drivers / hid / wacom_sys.c
blob8249ff3a5a8d2f2c1b3acd3e532d101f51ce84e4
1 /*
2 * drivers/input/tablet/wacom_sys.c
4 * USB Wacom tablet support - system specific code
5 */
7 /*
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"
15 #include "wacom.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)
28 int retval;
30 do {
31 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
32 HID_REQ_GET_REPORT);
33 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
35 if (retval < 0)
36 hid_err(hdev, "wacom_get_report: ran out of retries "
37 "(last error = %d)\n", retval);
39 return retval;
42 static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
43 size_t size, unsigned int retries)
45 int retval;
47 do {
48 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
49 HID_REQ_SET_REPORT);
50 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
52 if (retval < 0)
53 hid_err(hdev, "wacom_set_report: ran out of retries "
54 "(last error = %d)\n", retval);
56 return 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)
63 bool warned = false;
65 while (kfifo_avail(fifo) < size) {
66 if (!warned)
67 hid_warn(hdev, "%s: kfifo has filled, starting to drop events\n", __func__);
68 warned = true;
70 kfifo_skip(fifo);
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];
81 int size;
82 int err;
84 size = kfifo_out(fifo, buf, sizeof(buf));
85 err = hid_report_raw_event(hdev, HID_INPUT_REPORT, buf, size, false);
86 if (err) {
87 hid_warn(hdev, "%s: unable to flush event due to error %d\n",
88 __func__, err);
93 static int wacom_wac_pen_serial_enforce(struct hid_device *hdev,
94 struct hid_report *report, u8 *raw_data, int report_size)
96 struct wacom *wacom = hid_get_drvdata(hdev);
97 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
98 struct wacom_features *features = &wacom_wac->features;
99 bool flush = false;
100 bool insert = false;
101 int i, j;
103 if (wacom_wac->serial[0] || !(features->quirks & WACOM_QUIRK_TOOLSERIAL))
104 return 0;
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);
112 unsigned int offset;
113 unsigned int size;
114 unsigned int value;
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)
120 continue;
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)
128 value = !value;
130 if (value) {
131 flush = true;
132 switch (equivalent_usage) {
133 case HID_DG_TOOLSERIALNUMBER:
134 wacom_wac->serial[0] = value;
135 break;
137 case WACOM_HID_WD_SERIALHI:
138 wacom_wac->serial[0] |= ((__u64)value) << 32;
139 break;
141 case WACOM_HID_WD_TOOLTYPE:
142 wacom_wac->id[0] = value;
143 break;
146 else {
147 insert = true;
152 if (flush)
153 wacom_wac_queue_flush(hdev, &wacom_wac->pen_fifo);
154 else if (insert)
155 wacom_wac_queue_insert(hdev, &wacom_wac->pen_fifo,
156 raw_data, report_size);
158 return insert && !flush;
161 static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
162 u8 *raw_data, int size)
164 struct wacom *wacom = hid_get_drvdata(hdev);
166 if (size > WACOM_PKGLEN_MAX)
167 return 1;
169 if (wacom_wac_pen_serial_enforce(hdev, report, raw_data, size))
170 return -1;
172 memcpy(wacom->wacom_wac.data, raw_data, size);
174 wacom_wac_irq(&wacom->wacom_wac, size);
176 return 0;
179 static int wacom_open(struct input_dev *dev)
181 struct wacom *wacom = input_get_drvdata(dev);
183 return hid_hw_open(wacom->hdev);
186 static void wacom_close(struct input_dev *dev)
188 struct wacom *wacom = input_get_drvdata(dev);
191 * wacom->hdev should never be null, but surprisingly, I had the case
192 * once while unplugging the Wacom Wireless Receiver.
194 if (wacom->hdev)
195 hid_hw_close(wacom->hdev);
199 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
201 static int wacom_calc_hid_res(int logical_extents, int physical_extents,
202 unsigned unit, int exponent)
204 struct hid_field field = {
205 .logical_maximum = logical_extents,
206 .physical_maximum = physical_extents,
207 .unit = unit,
208 .unit_exponent = exponent,
211 return hidinput_calc_abs_res(&field, ABS_X);
214 static void wacom_hid_usage_quirk(struct hid_device *hdev,
215 struct hid_field *field, struct hid_usage *usage)
217 struct wacom *wacom = hid_get_drvdata(hdev);
218 struct wacom_features *features = &wacom->wacom_wac.features;
219 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
222 * The Dell Canvas 27 needs to be switched to its vendor-defined
223 * report to provide the best resolution.
225 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
226 hdev->product == 0x4200 &&
227 field->application == HID_UP_MSVENDOR) {
228 wacom->wacom_wac.mode_report = field->report->id;
229 wacom->wacom_wac.mode_value = 2;
233 * ISDv4 devices which predate HID's adoption of the
234 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
235 * position instead. We can accurately detect if a
236 * usage with that value should be HID_DG_BARRELSWITCH2
237 * based on the surrounding usages, which have remained
238 * constant across generations.
240 if (features->type == HID_GENERIC &&
241 usage->hid == 0x000D0000 &&
242 field->application == HID_DG_PEN &&
243 field->physical == HID_DG_STYLUS) {
244 int i = usage->usage_index;
246 if (i-4 >= 0 && i+1 < field->maxusage &&
247 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
248 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
249 field->usage[i-2].hid == HID_DG_ERASER &&
250 field->usage[i-1].hid == HID_DG_INVERT &&
251 field->usage[i+1].hid == HID_DG_INRANGE) {
252 usage->hid = HID_DG_BARRELSWITCH2;
256 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
257 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
258 hdev->product == 0x0358 &&
259 WACOM_PEN_FIELD(field) &&
260 equivalent_usage == HID_GD_Y) {
261 field->logical_maximum = 43200;
265 static void wacom_feature_mapping(struct hid_device *hdev,
266 struct hid_field *field, struct hid_usage *usage)
268 struct wacom *wacom = hid_get_drvdata(hdev);
269 struct wacom_features *features = &wacom->wacom_wac.features;
270 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
271 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
272 u8 *data;
273 int ret;
274 u32 n;
276 wacom_hid_usage_quirk(hdev, field, usage);
278 switch (equivalent_usage) {
279 case WACOM_HID_WD_TOUCH_RING_SETTING:
280 wacom->generic_has_leds = true;
281 break;
282 case HID_DG_CONTACTMAX:
283 /* leave touch_max as is if predefined */
284 if (!features->touch_max) {
285 /* read manually */
286 n = hid_report_len(field->report);
287 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
288 if (!data)
289 break;
290 data[0] = field->report->id;
291 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
292 data, n, WAC_CMD_RETRIES);
293 if (ret == n && features->type == HID_GENERIC) {
294 ret = hid_report_raw_event(hdev,
295 HID_FEATURE_REPORT, data, n, 0);
296 } else if (ret == 2 && features->type != HID_GENERIC) {
297 features->touch_max = data[1];
298 } else {
299 features->touch_max = 16;
300 hid_warn(hdev, "wacom_feature_mapping: "
301 "could not get HID_DG_CONTACTMAX, "
302 "defaulting to %d\n",
303 features->touch_max);
305 kfree(data);
307 break;
308 case HID_DG_INPUTMODE:
309 /* Ignore if value index is out of bounds. */
310 if (usage->usage_index >= field->report_count) {
311 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
312 break;
315 hid_data->inputmode = field->report->id;
316 hid_data->inputmode_index = usage->usage_index;
317 break;
319 case HID_UP_DIGITIZER:
320 if (field->report->id == 0x0B &&
321 (field->application == WACOM_HID_G9_PEN ||
322 field->application == WACOM_HID_G11_PEN)) {
323 wacom->wacom_wac.mode_report = field->report->id;
324 wacom->wacom_wac.mode_value = 0;
326 break;
328 case WACOM_HID_WD_DATAMODE:
329 wacom->wacom_wac.mode_report = field->report->id;
330 wacom->wacom_wac.mode_value = 2;
331 break;
333 case WACOM_HID_UP_G9:
334 case WACOM_HID_UP_G11:
335 if (field->report->id == 0x03 &&
336 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
337 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
338 wacom->wacom_wac.mode_report = field->report->id;
339 wacom->wacom_wac.mode_value = 0;
341 break;
342 case WACOM_HID_WD_OFFSETLEFT:
343 case WACOM_HID_WD_OFFSETTOP:
344 case WACOM_HID_WD_OFFSETRIGHT:
345 case WACOM_HID_WD_OFFSETBOTTOM:
346 /* read manually */
347 n = hid_report_len(field->report);
348 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
349 if (!data)
350 break;
351 data[0] = field->report->id;
352 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
353 data, n, WAC_CMD_RETRIES);
354 if (ret == n) {
355 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
356 data, n, 0);
357 } else {
358 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
359 __func__);
361 kfree(data);
362 break;
367 * Interface Descriptor of wacom devices can be incomplete and
368 * inconsistent so wacom_features table is used to store stylus
369 * device's packet lengths, various maximum values, and tablet
370 * resolution based on product ID's.
372 * For devices that contain 2 interfaces, wacom_features table is
373 * inaccurate for the touch interface. Since the Interface Descriptor
374 * for touch interfaces has pretty complete data, this function exists
375 * to query tablet for this missing information instead of hard coding in
376 * an additional table.
378 * A typical Interface Descriptor for a stylus will contain a
379 * boot mouse application collection that is not of interest and this
380 * function will ignore it.
382 * It also contains a digitizer application collection that also is not
383 * of interest since any information it contains would be duplicate
384 * of what is in wacom_features. Usually it defines a report of an array
385 * of bytes that could be used as max length of the stylus packet returned.
386 * If it happens to define a Digitizer-Stylus Physical Collection then
387 * the X and Y logical values contain valid data but it is ignored.
389 * A typical Interface Descriptor for a touch interface will contain a
390 * Digitizer-Finger Physical Collection which will define both logical
391 * X/Y maximum as well as the physical size of tablet. Since touch
392 * interfaces haven't supported pressure or distance, this is enough
393 * information to override invalid values in the wacom_features table.
395 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
396 * data. We deal with them after returning from this function.
398 static void wacom_usage_mapping(struct hid_device *hdev,
399 struct hid_field *field, struct hid_usage *usage)
401 struct wacom *wacom = hid_get_drvdata(hdev);
402 struct wacom_features *features = &wacom->wacom_wac.features;
403 bool finger = WACOM_FINGER_FIELD(field);
404 bool pen = WACOM_PEN_FIELD(field);
405 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
408 * Requiring Stylus Usage will ignore boot mouse
409 * X/Y values and some cases of invalid Digitizer X/Y
410 * values commonly reported.
412 if (pen)
413 features->device_type |= WACOM_DEVICETYPE_PEN;
414 else if (finger)
415 features->device_type |= WACOM_DEVICETYPE_TOUCH;
416 else
417 return;
419 wacom_hid_usage_quirk(hdev, field, usage);
421 switch (equivalent_usage) {
422 case HID_GD_X:
423 features->x_max = field->logical_maximum;
424 if (finger) {
425 features->x_phy = field->physical_maximum;
426 if ((features->type != BAMBOO_PT) &&
427 (features->type != BAMBOO_TOUCH)) {
428 features->unit = field->unit;
429 features->unitExpo = field->unit_exponent;
432 break;
433 case HID_GD_Y:
434 features->y_max = field->logical_maximum;
435 if (finger) {
436 features->y_phy = field->physical_maximum;
437 if ((features->type != BAMBOO_PT) &&
438 (features->type != BAMBOO_TOUCH)) {
439 features->unit = field->unit;
440 features->unitExpo = field->unit_exponent;
443 break;
444 case HID_DG_TIPPRESSURE:
445 if (pen)
446 features->pressure_max = field->logical_maximum;
447 break;
450 if (features->type == HID_GENERIC)
451 wacom_wac_usage_mapping(hdev, field, usage);
454 static void wacom_post_parse_hid(struct hid_device *hdev,
455 struct wacom_features *features)
457 struct wacom *wacom = hid_get_drvdata(hdev);
458 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
460 if (features->type == HID_GENERIC) {
461 /* Any last-minute generic device setup */
462 if (wacom_wac->has_mode_change) {
463 if (wacom_wac->is_direct_mode)
464 features->device_type |= WACOM_DEVICETYPE_DIRECT;
465 else
466 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
469 if (features->touch_max > 1) {
470 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
471 input_mt_init_slots(wacom_wac->touch_input,
472 wacom_wac->features.touch_max,
473 INPUT_MT_DIRECT);
474 else
475 input_mt_init_slots(wacom_wac->touch_input,
476 wacom_wac->features.touch_max,
477 INPUT_MT_POINTER);
482 static void wacom_parse_hid(struct hid_device *hdev,
483 struct wacom_features *features)
485 struct hid_report_enum *rep_enum;
486 struct hid_report *hreport;
487 int i, j;
489 /* check features first */
490 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
491 list_for_each_entry(hreport, &rep_enum->report_list, list) {
492 for (i = 0; i < hreport->maxfield; i++) {
493 /* Ignore if report count is out of bounds. */
494 if (hreport->field[i]->report_count < 1)
495 continue;
497 for (j = 0; j < hreport->field[i]->maxusage; j++) {
498 wacom_feature_mapping(hdev, hreport->field[i],
499 hreport->field[i]->usage + j);
504 /* now check the input usages */
505 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
506 list_for_each_entry(hreport, &rep_enum->report_list, list) {
508 if (!hreport->maxfield)
509 continue;
511 for (i = 0; i < hreport->maxfield; i++)
512 for (j = 0; j < hreport->field[i]->maxusage; j++)
513 wacom_usage_mapping(hdev, hreport->field[i],
514 hreport->field[i]->usage + j);
517 wacom_post_parse_hid(hdev, features);
520 static int wacom_hid_set_device_mode(struct hid_device *hdev)
522 struct wacom *wacom = hid_get_drvdata(hdev);
523 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
524 struct hid_report *r;
525 struct hid_report_enum *re;
527 if (hid_data->inputmode < 0)
528 return 0;
530 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
531 r = re->report_id_hash[hid_data->inputmode];
532 if (r) {
533 r->field[0]->value[hid_data->inputmode_index] = 2;
534 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
536 return 0;
539 static int wacom_set_device_mode(struct hid_device *hdev,
540 struct wacom_wac *wacom_wac)
542 u8 *rep_data;
543 struct hid_report *r;
544 struct hid_report_enum *re;
545 u32 length;
546 int error = -ENOMEM, limit = 0;
548 if (wacom_wac->mode_report < 0)
549 return 0;
551 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
552 r = re->report_id_hash[wacom_wac->mode_report];
553 if (!r)
554 return -EINVAL;
556 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
557 if (!rep_data)
558 return -ENOMEM;
560 length = hid_report_len(r);
562 do {
563 rep_data[0] = wacom_wac->mode_report;
564 rep_data[1] = wacom_wac->mode_value;
566 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
567 length, 1);
568 if (error >= 0)
569 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
570 rep_data, length, 1);
571 } while (error >= 0 &&
572 rep_data[1] != wacom_wac->mode_report &&
573 limit++ < WAC_MSG_RETRIES);
575 kfree(rep_data);
577 return error < 0 ? error : 0;
580 static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
581 struct wacom_features *features)
583 struct wacom *wacom = hid_get_drvdata(hdev);
584 int ret;
585 u8 rep_data[2];
587 switch (features->type) {
588 case GRAPHIRE_BT:
589 rep_data[0] = 0x03;
590 rep_data[1] = 0x00;
591 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
594 if (ret >= 0) {
595 rep_data[0] = speed == 0 ? 0x05 : 0x06;
596 rep_data[1] = 0x00;
598 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
599 rep_data, 2, 3);
601 if (ret >= 0) {
602 wacom->wacom_wac.bt_high_speed = speed;
603 return 0;
608 * Note that if the raw queries fail, it's not a hard failure
609 * and it is safe to continue
611 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
612 rep_data[0], ret);
613 break;
614 case INTUOS4WL:
615 if (speed == 1)
616 wacom->wacom_wac.bt_features &= ~0x20;
617 else
618 wacom->wacom_wac.bt_features |= 0x20;
620 rep_data[0] = 0x03;
621 rep_data[1] = wacom->wacom_wac.bt_features;
623 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
625 if (ret >= 0)
626 wacom->wacom_wac.bt_high_speed = speed;
627 break;
630 return 0;
634 * Switch the tablet into its most-capable mode. Wacom tablets are
635 * typically configured to power-up in a mode which sends mouse-like
636 * reports to the OS. To get absolute position, pressure data, etc.
637 * from the tablet, it is necessary to switch the tablet out of this
638 * mode and into one which sends the full range of tablet data.
640 static int _wacom_query_tablet_data(struct wacom *wacom)
642 struct hid_device *hdev = wacom->hdev;
643 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
644 struct wacom_features *features = &wacom_wac->features;
646 if (hdev->bus == BUS_BLUETOOTH)
647 return wacom_bt_query_tablet_data(hdev, 1, features);
649 if (features->type != HID_GENERIC) {
650 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
651 if (features->type > TABLETPC) {
652 /* MT Tablet PC touch */
653 wacom_wac->mode_report = 3;
654 wacom_wac->mode_value = 4;
655 } else if (features->type == WACOM_24HDT) {
656 wacom_wac->mode_report = 18;
657 wacom_wac->mode_value = 2;
658 } else if (features->type == WACOM_27QHDT) {
659 wacom_wac->mode_report = 131;
660 wacom_wac->mode_value = 2;
661 } else if (features->type == BAMBOO_PAD) {
662 wacom_wac->mode_report = 2;
663 wacom_wac->mode_value = 2;
665 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
666 if (features->type <= BAMBOO_PT) {
667 wacom_wac->mode_report = 2;
668 wacom_wac->mode_value = 2;
673 wacom_set_device_mode(hdev, wacom_wac);
675 if (features->type == HID_GENERIC)
676 return wacom_hid_set_device_mode(hdev);
678 return 0;
681 static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
682 struct wacom_features *features)
684 struct wacom *wacom = hid_get_drvdata(hdev);
685 struct usb_interface *intf = wacom->intf;
687 /* default features */
688 features->x_fuzz = 4;
689 features->y_fuzz = 4;
690 features->pressure_fuzz = 0;
691 features->distance_fuzz = 1;
692 features->tilt_fuzz = 1;
695 * The wireless device HID is basic and layout conflicts with
696 * other tablets (monitor and touch interface can look like pen).
697 * Skip the query for this type and modify defaults based on
698 * interface number.
700 if (features->type == WIRELESS) {
701 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
702 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
703 else
704 features->device_type = WACOM_DEVICETYPE_NONE;
705 return;
708 wacom_parse_hid(hdev, features);
711 struct wacom_hdev_data {
712 struct list_head list;
713 struct kref kref;
714 struct hid_device *dev;
715 struct wacom_shared shared;
718 static LIST_HEAD(wacom_udev_list);
719 static DEFINE_MUTEX(wacom_udev_list_lock);
721 static bool wacom_are_sibling(struct hid_device *hdev,
722 struct hid_device *sibling)
724 struct wacom *wacom = hid_get_drvdata(hdev);
725 struct wacom_features *features = &wacom->wacom_wac.features;
726 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
727 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
728 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
729 __u32 oPid = features->oPid ? features->oPid : hdev->product;
731 /* The defined oVid/oPid must match that of the sibling */
732 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
733 return false;
734 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
735 return false;
738 * Devices with the same VID/PID must share the same physical
739 * device path, while those with different VID/PID must share
740 * the same physical parent device path.
742 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
743 if (!hid_compare_device_paths(hdev, sibling, '/'))
744 return false;
745 } else {
746 if (!hid_compare_device_paths(hdev, sibling, '.'))
747 return false;
750 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
751 if (features->type != HID_GENERIC)
752 return true;
755 * Direct-input devices may not be siblings of indirect-input
756 * devices.
758 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
759 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
760 return false;
763 * Indirect-input devices may not be siblings of direct-input
764 * devices.
766 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
767 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
768 return false;
770 /* Pen devices may only be siblings of touch devices */
771 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
772 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
773 return false;
775 /* Touch devices may only be siblings of pen devices */
776 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
777 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
778 return false;
781 * No reason could be found for these two devices to NOT be
782 * siblings, so there's a good chance they ARE siblings
784 return true;
787 static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
789 struct wacom_hdev_data *data;
791 /* Try to find an already-probed interface from the same device */
792 list_for_each_entry(data, &wacom_udev_list, list) {
793 if (hid_compare_device_paths(hdev, data->dev, '/')) {
794 kref_get(&data->kref);
795 return data;
799 /* Fallback to finding devices that appear to be "siblings" */
800 list_for_each_entry(data, &wacom_udev_list, list) {
801 if (wacom_are_sibling(hdev, data->dev)) {
802 kref_get(&data->kref);
803 return data;
807 return NULL;
810 static void wacom_release_shared_data(struct kref *kref)
812 struct wacom_hdev_data *data =
813 container_of(kref, struct wacom_hdev_data, kref);
815 mutex_lock(&wacom_udev_list_lock);
816 list_del(&data->list);
817 mutex_unlock(&wacom_udev_list_lock);
819 kfree(data);
822 static void wacom_remove_shared_data(void *res)
824 struct wacom *wacom = res;
825 struct wacom_hdev_data *data;
826 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
828 if (wacom_wac->shared) {
829 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
830 shared);
832 if (wacom_wac->shared->touch == wacom->hdev)
833 wacom_wac->shared->touch = NULL;
834 else if (wacom_wac->shared->pen == wacom->hdev)
835 wacom_wac->shared->pen = NULL;
837 kref_put(&data->kref, wacom_release_shared_data);
838 wacom_wac->shared = NULL;
842 static int wacom_add_shared_data(struct hid_device *hdev)
844 struct wacom *wacom = hid_get_drvdata(hdev);
845 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
846 struct wacom_hdev_data *data;
847 int retval = 0;
849 mutex_lock(&wacom_udev_list_lock);
851 data = wacom_get_hdev_data(hdev);
852 if (!data) {
853 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
854 if (!data) {
855 retval = -ENOMEM;
856 goto out;
859 kref_init(&data->kref);
860 data->dev = hdev;
861 list_add_tail(&data->list, &wacom_udev_list);
864 wacom_wac->shared = &data->shared;
866 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
867 if (retval) {
868 mutex_unlock(&wacom_udev_list_lock);
869 wacom_remove_shared_data(wacom);
870 return retval;
873 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
874 wacom_wac->shared->touch = hdev;
875 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
876 wacom_wac->shared->pen = hdev;
878 out:
879 mutex_unlock(&wacom_udev_list_lock);
880 return retval;
883 static int wacom_led_control(struct wacom *wacom)
885 unsigned char *buf;
886 int retval;
887 unsigned char report_id = WAC_CMD_LED_CONTROL;
888 int buf_size = 9;
890 if (!wacom->led.groups)
891 return -ENOTSUPP;
893 if (wacom->wacom_wac.features.type == REMOTE)
894 return -ENOTSUPP;
896 if (wacom->wacom_wac.pid) { /* wireless connected */
897 report_id = WAC_CMD_WL_LED_CONTROL;
898 buf_size = 13;
900 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
901 report_id = WAC_CMD_WL_INTUOSP2;
902 buf_size = 51;
904 buf = kzalloc(buf_size, GFP_KERNEL);
905 if (!buf)
906 return -ENOMEM;
908 if (wacom->wacom_wac.features.type == HID_GENERIC) {
909 buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
910 buf[1] = wacom->led.llv;
911 buf[2] = wacom->led.groups[0].select & 0x03;
913 } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
914 wacom->wacom_wac.features.type <= INTUOSPL)) {
916 * Touch Ring and crop mark LED luminance may take on
917 * one of four values:
918 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
920 int ring_led = wacom->led.groups[0].select & 0x03;
921 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
922 int crop_lum = 0;
923 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
925 buf[0] = report_id;
926 if (wacom->wacom_wac.pid) {
927 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
928 buf, buf_size, WAC_CMD_RETRIES);
929 buf[0] = report_id;
930 buf[4] = led_bits;
931 } else
932 buf[1] = led_bits;
934 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
935 buf[0] = report_id;
936 buf[4] = 100; // Power Connection LED (ORANGE)
937 buf[5] = 100; // BT Connection LED (BLUE)
938 buf[6] = 100; // Paper Mode (RED?)
939 buf[7] = 100; // Paper Mode (GREEN?)
940 buf[8] = 100; // Paper Mode (BLUE?)
941 buf[9] = wacom->led.llv;
942 buf[10] = wacom->led.groups[0].select & 0x03;
944 else {
945 int led = wacom->led.groups[0].select | 0x4;
947 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
948 wacom->wacom_wac.features.type == WACOM_24HD)
949 led |= (wacom->led.groups[1].select << 4) | 0x40;
951 buf[0] = report_id;
952 buf[1] = led;
953 buf[2] = wacom->led.llv;
954 buf[3] = wacom->led.hlv;
955 buf[4] = wacom->led.img_lum;
958 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
959 WAC_CMD_RETRIES);
960 kfree(buf);
962 return retval;
965 static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
966 const unsigned len, const void *img)
968 unsigned char *buf;
969 int i, retval;
970 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
972 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
973 if (!buf)
974 return -ENOMEM;
976 /* Send 'start' command */
977 buf[0] = WAC_CMD_ICON_START;
978 buf[1] = 1;
979 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
980 WAC_CMD_RETRIES);
981 if (retval < 0)
982 goto out;
984 buf[0] = xfer_id;
985 buf[1] = button_id & 0x07;
986 for (i = 0; i < 4; i++) {
987 buf[2] = i;
988 memcpy(buf + 3, img + i * chunk_len, chunk_len);
990 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
991 buf, chunk_len + 3, WAC_CMD_RETRIES);
992 if (retval < 0)
993 break;
996 /* Send 'stop' */
997 buf[0] = WAC_CMD_ICON_START;
998 buf[1] = 0;
999 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1000 WAC_CMD_RETRIES);
1002 out:
1003 kfree(buf);
1004 return retval;
1007 static ssize_t wacom_led_select_store(struct device *dev, int set_id,
1008 const char *buf, size_t count)
1010 struct hid_device *hdev = to_hid_device(dev);
1011 struct wacom *wacom = hid_get_drvdata(hdev);
1012 unsigned int id;
1013 int err;
1015 err = kstrtouint(buf, 10, &id);
1016 if (err)
1017 return err;
1019 mutex_lock(&wacom->lock);
1021 wacom->led.groups[set_id].select = id & 0x3;
1022 err = wacom_led_control(wacom);
1024 mutex_unlock(&wacom->lock);
1026 return err < 0 ? err : count;
1029 #define DEVICE_LED_SELECT_ATTR(SET_ID) \
1030 static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
1031 struct device_attribute *attr, const char *buf, size_t count) \
1033 return wacom_led_select_store(dev, SET_ID, buf, count); \
1035 static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
1036 struct device_attribute *attr, char *buf) \
1038 struct hid_device *hdev = to_hid_device(dev);\
1039 struct wacom *wacom = hid_get_drvdata(hdev); \
1040 return scnprintf(buf, PAGE_SIZE, "%d\n", \
1041 wacom->led.groups[SET_ID].select); \
1043 static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
1044 wacom_led##SET_ID##_select_show, \
1045 wacom_led##SET_ID##_select_store)
1047 DEVICE_LED_SELECT_ATTR(0);
1048 DEVICE_LED_SELECT_ATTR(1);
1050 static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
1051 const char *buf, size_t count)
1053 unsigned int value;
1054 int err;
1056 err = kstrtouint(buf, 10, &value);
1057 if (err)
1058 return err;
1060 mutex_lock(&wacom->lock);
1062 *dest = value & 0x7f;
1063 err = wacom_led_control(wacom);
1065 mutex_unlock(&wacom->lock);
1067 return err < 0 ? err : count;
1070 #define DEVICE_LUMINANCE_ATTR(name, field) \
1071 static ssize_t wacom_##name##_luminance_store(struct device *dev, \
1072 struct device_attribute *attr, const char *buf, size_t count) \
1074 struct hid_device *hdev = to_hid_device(dev);\
1075 struct wacom *wacom = hid_get_drvdata(hdev); \
1077 return wacom_luminance_store(wacom, &wacom->led.field, \
1078 buf, count); \
1080 static ssize_t wacom_##name##_luminance_show(struct device *dev, \
1081 struct device_attribute *attr, char *buf) \
1083 struct wacom *wacom = dev_get_drvdata(dev); \
1084 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
1086 static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
1087 wacom_##name##_luminance_show, \
1088 wacom_##name##_luminance_store)
1090 DEVICE_LUMINANCE_ATTR(status0, llv);
1091 DEVICE_LUMINANCE_ATTR(status1, hlv);
1092 DEVICE_LUMINANCE_ATTR(buttons, img_lum);
1094 static ssize_t wacom_button_image_store(struct device *dev, int button_id,
1095 const char *buf, size_t count)
1097 struct hid_device *hdev = to_hid_device(dev);
1098 struct wacom *wacom = hid_get_drvdata(hdev);
1099 int err;
1100 unsigned len;
1101 u8 xfer_id;
1103 if (hdev->bus == BUS_BLUETOOTH) {
1104 len = 256;
1105 xfer_id = WAC_CMD_ICON_BT_XFER;
1106 } else {
1107 len = 1024;
1108 xfer_id = WAC_CMD_ICON_XFER;
1111 if (count != len)
1112 return -EINVAL;
1114 mutex_lock(&wacom->lock);
1116 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
1118 mutex_unlock(&wacom->lock);
1120 return err < 0 ? err : count;
1123 #define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1124 static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1125 struct device_attribute *attr, const char *buf, size_t count) \
1127 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1129 static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
1130 NULL, wacom_btnimg##BUTTON_ID##_store)
1132 DEVICE_BTNIMG_ATTR(0);
1133 DEVICE_BTNIMG_ATTR(1);
1134 DEVICE_BTNIMG_ATTR(2);
1135 DEVICE_BTNIMG_ATTR(3);
1136 DEVICE_BTNIMG_ATTR(4);
1137 DEVICE_BTNIMG_ATTR(5);
1138 DEVICE_BTNIMG_ATTR(6);
1139 DEVICE_BTNIMG_ATTR(7);
1141 static struct attribute *cintiq_led_attrs[] = {
1142 &dev_attr_status_led0_select.attr,
1143 &dev_attr_status_led1_select.attr,
1144 NULL
1147 static struct attribute_group cintiq_led_attr_group = {
1148 .name = "wacom_led",
1149 .attrs = cintiq_led_attrs,
1152 static struct attribute *intuos4_led_attrs[] = {
1153 &dev_attr_status0_luminance.attr,
1154 &dev_attr_status1_luminance.attr,
1155 &dev_attr_status_led0_select.attr,
1156 &dev_attr_buttons_luminance.attr,
1157 &dev_attr_button0_rawimg.attr,
1158 &dev_attr_button1_rawimg.attr,
1159 &dev_attr_button2_rawimg.attr,
1160 &dev_attr_button3_rawimg.attr,
1161 &dev_attr_button4_rawimg.attr,
1162 &dev_attr_button5_rawimg.attr,
1163 &dev_attr_button6_rawimg.attr,
1164 &dev_attr_button7_rawimg.attr,
1165 NULL
1168 static struct attribute_group intuos4_led_attr_group = {
1169 .name = "wacom_led",
1170 .attrs = intuos4_led_attrs,
1173 static struct attribute *intuos5_led_attrs[] = {
1174 &dev_attr_status0_luminance.attr,
1175 &dev_attr_status_led0_select.attr,
1176 NULL
1179 static struct attribute_group intuos5_led_attr_group = {
1180 .name = "wacom_led",
1181 .attrs = intuos5_led_attrs,
1184 static struct attribute *generic_led_attrs[] = {
1185 &dev_attr_status0_luminance.attr,
1186 &dev_attr_status_led0_select.attr,
1187 NULL
1190 static struct attribute_group generic_led_attr_group = {
1191 .name = "wacom_led",
1192 .attrs = generic_led_attrs,
1195 struct wacom_sysfs_group_devres {
1196 struct attribute_group *group;
1197 struct kobject *root;
1200 static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1202 struct wacom_sysfs_group_devres *devres = res;
1203 struct kobject *kobj = devres->root;
1205 dev_dbg(dev, "%s: dropping reference to %s\n",
1206 __func__, devres->group->name);
1207 sysfs_remove_group(kobj, devres->group);
1210 static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1211 struct kobject *root,
1212 struct attribute_group *group)
1214 struct wacom_sysfs_group_devres *devres;
1215 int error;
1217 devres = devres_alloc(wacom_devm_sysfs_group_release,
1218 sizeof(struct wacom_sysfs_group_devres),
1219 GFP_KERNEL);
1220 if (!devres)
1221 return -ENOMEM;
1223 devres->group = group;
1224 devres->root = root;
1226 error = sysfs_create_group(devres->root, group);
1227 if (error) {
1228 devres_free(devres);
1229 return error;
1232 devres_add(&wacom->hdev->dev, devres);
1234 return 0;
1237 static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1238 struct attribute_group *group)
1240 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1241 group);
1244 enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
1246 struct wacom *wacom = led->wacom;
1248 if (wacom->led.max_hlv)
1249 return led->hlv * LED_FULL / wacom->led.max_hlv;
1251 if (wacom->led.max_llv)
1252 return led->llv * LED_FULL / wacom->led.max_llv;
1254 /* device doesn't support brightness tuning */
1255 return LED_FULL;
1258 static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1260 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1261 struct wacom *wacom = led->wacom;
1263 if (wacom->led.groups[led->group].select != led->id)
1264 return LED_OFF;
1266 return wacom_leds_brightness_get(led);
1269 static int wacom_led_brightness_set(struct led_classdev *cdev,
1270 enum led_brightness brightness)
1272 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1273 struct wacom *wacom = led->wacom;
1274 int error;
1276 mutex_lock(&wacom->lock);
1278 if (!wacom->led.groups || (brightness == LED_OFF &&
1279 wacom->led.groups[led->group].select != led->id)) {
1280 error = 0;
1281 goto out;
1284 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1285 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1287 wacom->led.groups[led->group].select = led->id;
1289 error = wacom_led_control(wacom);
1291 out:
1292 mutex_unlock(&wacom->lock);
1294 return error;
1297 static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1298 enum led_brightness brightness)
1302 static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1303 struct wacom_led *led, unsigned int group,
1304 unsigned int id, bool read_only)
1306 int error;
1307 char *name;
1309 name = devm_kasprintf(dev, GFP_KERNEL,
1310 "%s::wacom-%d.%d",
1311 dev_name(dev),
1312 group,
1313 id);
1314 if (!name)
1315 return -ENOMEM;
1317 if (!read_only) {
1318 led->trigger.name = name;
1319 error = devm_led_trigger_register(dev, &led->trigger);
1320 if (error) {
1321 hid_err(wacom->hdev,
1322 "failed to register LED trigger %s: %d\n",
1323 led->cdev.name, error);
1324 return error;
1328 led->group = group;
1329 led->id = id;
1330 led->wacom = wacom;
1331 led->llv = wacom->led.llv;
1332 led->hlv = wacom->led.hlv;
1333 led->cdev.name = name;
1334 led->cdev.max_brightness = LED_FULL;
1335 led->cdev.flags = LED_HW_PLUGGABLE;
1336 led->cdev.brightness_get = __wacom_led_brightness_get;
1337 if (!read_only) {
1338 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
1339 led->cdev.default_trigger = led->cdev.name;
1340 } else {
1341 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
1344 error = devm_led_classdev_register(dev, &led->cdev);
1345 if (error) {
1346 hid_err(wacom->hdev,
1347 "failed to register LED %s: %d\n",
1348 led->cdev.name, error);
1349 led->cdev.name = NULL;
1350 return error;
1353 return 0;
1356 static void wacom_led_groups_release_one(void *data)
1358 struct wacom_group_leds *group = data;
1360 devres_release_group(group->dev, group);
1363 static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1364 struct wacom *wacom,
1365 int group_id, int count,
1366 bool read_only)
1368 struct wacom_led *leds;
1369 int i, error;
1371 if (group_id >= wacom->led.count || count <= 0)
1372 return -EINVAL;
1374 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1375 return -ENOMEM;
1377 leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL);
1378 if (!leds) {
1379 error = -ENOMEM;
1380 goto err;
1383 wacom->led.groups[group_id].leds = leds;
1384 wacom->led.groups[group_id].count = count;
1386 for (i = 0; i < count; i++) {
1387 error = wacom_led_register_one(dev, wacom, &leds[i],
1388 group_id, i, read_only);
1389 if (error)
1390 goto err;
1393 wacom->led.groups[group_id].dev = dev;
1395 devres_close_group(dev, &wacom->led.groups[group_id]);
1398 * There is a bug (?) in devm_led_classdev_register() in which its
1399 * increments the refcount of the parent. If the parent is an input
1400 * device, that means the ref count never reaches 0 when
1401 * devm_input_device_release() gets called.
1402 * This means that the LEDs are still there after disconnect.
1403 * Manually force the release of the group so that the leds are released
1404 * once we are done using them.
1406 error = devm_add_action_or_reset(&wacom->hdev->dev,
1407 wacom_led_groups_release_one,
1408 &wacom->led.groups[group_id]);
1409 if (error)
1410 return error;
1412 return 0;
1414 err:
1415 devres_release_group(dev, &wacom->led.groups[group_id]);
1416 return error;
1419 struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1420 unsigned int id)
1422 struct wacom_group_leds *group;
1424 if (group_id >= wacom->led.count)
1425 return NULL;
1427 group = &wacom->led.groups[group_id];
1429 if (!group->leds)
1430 return NULL;
1432 id %= group->count;
1434 return &group->leds[id];
1438 * wacom_led_next: gives the next available led with a wacom trigger.
1440 * returns the next available struct wacom_led which has its default trigger
1441 * or the current one if none is available.
1443 struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1445 struct wacom_led *next_led;
1446 int group, next;
1448 if (!wacom || !cur)
1449 return NULL;
1451 group = cur->group;
1452 next = cur->id;
1454 do {
1455 next_led = wacom_led_find(wacom, group, ++next);
1456 if (!next_led || next_led == cur)
1457 return next_led;
1458 } while (next_led->cdev.trigger != &next_led->trigger);
1460 return next_led;
1463 static void wacom_led_groups_release(void *data)
1465 struct wacom *wacom = data;
1467 wacom->led.groups = NULL;
1468 wacom->led.count = 0;
1471 static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1473 struct device *dev = &wacom->hdev->dev;
1474 struct wacom_group_leds *groups;
1475 int error;
1477 groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds),
1478 GFP_KERNEL);
1479 if (!groups)
1480 return -ENOMEM;
1482 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1483 if (error)
1484 return error;
1486 wacom->led.groups = groups;
1487 wacom->led.count = count;
1489 return 0;
1492 static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1493 int led_per_group, bool read_only)
1495 struct device *dev;
1496 int i, error;
1498 if (!wacom->wacom_wac.pad_input)
1499 return -EINVAL;
1501 dev = &wacom->wacom_wac.pad_input->dev;
1503 error = wacom_led_groups_allocate(wacom, group_count);
1504 if (error)
1505 return error;
1507 for (i = 0; i < group_count; i++) {
1508 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1509 led_per_group,
1510 read_only);
1511 if (error)
1512 return error;
1515 return 0;
1518 int wacom_initialize_leds(struct wacom *wacom)
1520 int error;
1522 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1523 return 0;
1525 /* Initialize default values */
1526 switch (wacom->wacom_wac.features.type) {
1527 case HID_GENERIC:
1528 if (!wacom->generic_has_leds)
1529 return 0;
1530 wacom->led.llv = 100;
1531 wacom->led.max_llv = 100;
1533 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1534 if (error) {
1535 hid_err(wacom->hdev,
1536 "cannot create leds err: %d\n", error);
1537 return error;
1540 error = wacom_devm_sysfs_create_group(wacom,
1541 &generic_led_attr_group);
1542 break;
1544 case INTUOS4S:
1545 case INTUOS4:
1546 case INTUOS4WL:
1547 case INTUOS4L:
1548 wacom->led.llv = 10;
1549 wacom->led.hlv = 20;
1550 wacom->led.max_llv = 127;
1551 wacom->led.max_hlv = 127;
1552 wacom->led.img_lum = 10;
1554 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1555 if (error) {
1556 hid_err(wacom->hdev,
1557 "cannot create leds err: %d\n", error);
1558 return error;
1561 error = wacom_devm_sysfs_create_group(wacom,
1562 &intuos4_led_attr_group);
1563 break;
1565 case WACOM_24HD:
1566 case WACOM_21UX2:
1567 wacom->led.llv = 0;
1568 wacom->led.hlv = 0;
1569 wacom->led.img_lum = 0;
1571 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1572 if (error) {
1573 hid_err(wacom->hdev,
1574 "cannot create leds err: %d\n", error);
1575 return error;
1578 error = wacom_devm_sysfs_create_group(wacom,
1579 &cintiq_led_attr_group);
1580 break;
1582 case INTUOS5S:
1583 case INTUOS5:
1584 case INTUOS5L:
1585 case INTUOSPS:
1586 case INTUOSPM:
1587 case INTUOSPL:
1588 wacom->led.llv = 32;
1589 wacom->led.max_llv = 96;
1591 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1592 if (error) {
1593 hid_err(wacom->hdev,
1594 "cannot create leds err: %d\n", error);
1595 return error;
1598 error = wacom_devm_sysfs_create_group(wacom,
1599 &intuos5_led_attr_group);
1600 break;
1602 case INTUOSP2_BT:
1603 wacom->led.llv = 50;
1604 wacom->led.max_llv = 100;
1605 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1606 if (error) {
1607 hid_err(wacom->hdev,
1608 "cannot create leds err: %d\n", error);
1609 return error;
1611 return 0;
1613 case REMOTE:
1614 wacom->led.llv = 255;
1615 wacom->led.max_llv = 255;
1616 error = wacom_led_groups_allocate(wacom, 5);
1617 if (error) {
1618 hid_err(wacom->hdev,
1619 "cannot create leds err: %d\n", error);
1620 return error;
1622 return 0;
1624 default:
1625 return 0;
1628 if (error) {
1629 hid_err(wacom->hdev,
1630 "cannot create sysfs group err: %d\n", error);
1631 return error;
1634 return 0;
1637 static void wacom_init_work(struct work_struct *work)
1639 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1641 _wacom_query_tablet_data(wacom);
1642 wacom_led_control(wacom);
1645 static void wacom_query_tablet_data(struct wacom *wacom)
1647 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1650 static enum power_supply_property wacom_battery_props[] = {
1651 POWER_SUPPLY_PROP_MODEL_NAME,
1652 POWER_SUPPLY_PROP_PRESENT,
1653 POWER_SUPPLY_PROP_STATUS,
1654 POWER_SUPPLY_PROP_SCOPE,
1655 POWER_SUPPLY_PROP_CAPACITY
1658 static int wacom_battery_get_property(struct power_supply *psy,
1659 enum power_supply_property psp,
1660 union power_supply_propval *val)
1662 struct wacom_battery *battery = power_supply_get_drvdata(psy);
1663 int ret = 0;
1665 switch (psp) {
1666 case POWER_SUPPLY_PROP_MODEL_NAME:
1667 val->strval = battery->wacom->wacom_wac.name;
1668 break;
1669 case POWER_SUPPLY_PROP_PRESENT:
1670 val->intval = battery->bat_connected;
1671 break;
1672 case POWER_SUPPLY_PROP_SCOPE:
1673 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1674 break;
1675 case POWER_SUPPLY_PROP_CAPACITY:
1676 val->intval = battery->battery_capacity;
1677 break;
1678 case POWER_SUPPLY_PROP_STATUS:
1679 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1680 val->intval = battery->bat_status;
1681 else if (battery->bat_charging)
1682 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1683 else if (battery->battery_capacity == 100 &&
1684 battery->ps_connected)
1685 val->intval = POWER_SUPPLY_STATUS_FULL;
1686 else if (battery->ps_connected)
1687 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1688 else
1689 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1690 break;
1691 default:
1692 ret = -EINVAL;
1693 break;
1696 return ret;
1699 static int __wacom_initialize_battery(struct wacom *wacom,
1700 struct wacom_battery *battery)
1702 static atomic_t battery_no = ATOMIC_INIT(0);
1703 struct device *dev = &wacom->hdev->dev;
1704 struct power_supply_config psy_cfg = { .drv_data = battery, };
1705 struct power_supply *ps_bat;
1706 struct power_supply_desc *bat_desc = &battery->bat_desc;
1707 unsigned long n;
1708 int error;
1710 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1711 return -ENOMEM;
1713 battery->wacom = wacom;
1715 n = atomic_inc_return(&battery_no) - 1;
1717 bat_desc->properties = wacom_battery_props;
1718 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1719 bat_desc->get_property = wacom_battery_get_property;
1720 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1721 bat_desc->name = battery->bat_name;
1722 bat_desc->type = POWER_SUPPLY_TYPE_USB;
1723 bat_desc->use_for_apm = 0;
1725 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1726 if (IS_ERR(ps_bat)) {
1727 error = PTR_ERR(ps_bat);
1728 goto err;
1731 power_supply_powers(ps_bat, &wacom->hdev->dev);
1733 battery->battery = ps_bat;
1735 devres_close_group(dev, bat_desc);
1736 return 0;
1738 err:
1739 devres_release_group(dev, bat_desc);
1740 return error;
1743 static int wacom_initialize_battery(struct wacom *wacom)
1745 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1746 return __wacom_initialize_battery(wacom, &wacom->battery);
1748 return 0;
1751 static void wacom_destroy_battery(struct wacom *wacom)
1753 if (wacom->battery.battery) {
1754 devres_release_group(&wacom->hdev->dev,
1755 &wacom->battery.bat_desc);
1756 wacom->battery.battery = NULL;
1760 static ssize_t wacom_show_speed(struct device *dev,
1761 struct device_attribute
1762 *attr, char *buf)
1764 struct hid_device *hdev = to_hid_device(dev);
1765 struct wacom *wacom = hid_get_drvdata(hdev);
1767 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1770 static ssize_t wacom_store_speed(struct device *dev,
1771 struct device_attribute *attr,
1772 const char *buf, size_t count)
1774 struct hid_device *hdev = to_hid_device(dev);
1775 struct wacom *wacom = hid_get_drvdata(hdev);
1776 u8 new_speed;
1778 if (kstrtou8(buf, 0, &new_speed))
1779 return -EINVAL;
1781 if (new_speed != 0 && new_speed != 1)
1782 return -EINVAL;
1784 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1786 return count;
1789 static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1790 wacom_show_speed, wacom_store_speed);
1793 static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1794 struct kobj_attribute *kattr,
1795 char *buf, int index)
1797 struct device *dev = kobj_to_dev(kobj->parent);
1798 struct hid_device *hdev = to_hid_device(dev);
1799 struct wacom *wacom = hid_get_drvdata(hdev);
1800 u8 mode;
1802 mode = wacom->led.groups[index].select;
1803 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
1806 #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1807 static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1808 struct kobj_attribute *kattr, char *buf) \
1810 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1812 static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1813 .attr = {.name = "remote_mode", \
1814 .mode = DEV_ATTR_RO_PERM}, \
1815 .show = wacom_show_remote##SET_ID##_mode, \
1816 }; \
1817 static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1818 &remote##SET_ID##_mode_attr.attr, \
1819 NULL \
1820 }; \
1821 static struct attribute_group remote##SET_ID##_serial_group = { \
1822 .name = NULL, \
1823 .attrs = remote##SET_ID##_serial_attrs, \
1826 DEVICE_EKR_ATTR_GROUP(0);
1827 DEVICE_EKR_ATTR_GROUP(1);
1828 DEVICE_EKR_ATTR_GROUP(2);
1829 DEVICE_EKR_ATTR_GROUP(3);
1830 DEVICE_EKR_ATTR_GROUP(4);
1832 static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1833 int index)
1835 int error = 0;
1836 struct wacom_remote *remote = wacom->remote;
1838 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1839 GFP_KERNEL,
1840 "%d", serial);
1841 if (!remote->remotes[index].group.name)
1842 return -ENOMEM;
1844 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1845 &remote->remotes[index].group);
1846 if (error) {
1847 remote->remotes[index].group.name = NULL;
1848 hid_err(wacom->hdev,
1849 "cannot create sysfs group err: %d\n", error);
1850 return error;
1853 return 0;
1856 static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1858 const size_t buf_size = 2;
1859 unsigned char *buf;
1860 int retval;
1862 buf = kzalloc(buf_size, GFP_KERNEL);
1863 if (!buf)
1864 return -ENOMEM;
1866 buf[0] = WAC_CMD_DELETE_PAIRING;
1867 buf[1] = selector;
1869 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1870 buf_size, WAC_CMD_RETRIES);
1871 kfree(buf);
1873 return retval;
1876 static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1877 struct kobj_attribute *attr,
1878 const char *buf, size_t count)
1880 unsigned char selector = 0;
1881 struct device *dev = kobj_to_dev(kobj->parent);
1882 struct hid_device *hdev = to_hid_device(dev);
1883 struct wacom *wacom = hid_get_drvdata(hdev);
1884 int err;
1886 if (!strncmp(buf, "*\n", 2)) {
1887 selector = WAC_CMD_UNPAIR_ALL;
1888 } else {
1889 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1890 buf);
1891 return -1;
1894 mutex_lock(&wacom->lock);
1896 err = wacom_cmd_unpair_remote(wacom, selector);
1897 mutex_unlock(&wacom->lock);
1899 return err < 0 ? err : count;
1902 static struct kobj_attribute unpair_remote_attr = {
1903 .attr = {.name = "unpair_remote", .mode = 0200},
1904 .store = wacom_store_unpair_remote,
1907 static const struct attribute *remote_unpair_attrs[] = {
1908 &unpair_remote_attr.attr,
1909 NULL
1912 static void wacom_remotes_destroy(void *data)
1914 struct wacom *wacom = data;
1915 struct wacom_remote *remote = wacom->remote;
1917 if (!remote)
1918 return;
1920 kobject_put(remote->remote_dir);
1921 kfifo_free(&remote->remote_fifo);
1922 wacom->remote = NULL;
1925 static int wacom_initialize_remotes(struct wacom *wacom)
1927 int error = 0;
1928 struct wacom_remote *remote;
1929 int i;
1931 if (wacom->wacom_wac.features.type != REMOTE)
1932 return 0;
1934 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1935 GFP_KERNEL);
1936 if (!remote)
1937 return -ENOMEM;
1939 wacom->remote = remote;
1941 spin_lock_init(&remote->remote_lock);
1943 error = kfifo_alloc(&remote->remote_fifo,
1944 5 * sizeof(struct wacom_remote_data),
1945 GFP_KERNEL);
1946 if (error) {
1947 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1948 return -ENOMEM;
1951 remote->remotes[0].group = remote0_serial_group;
1952 remote->remotes[1].group = remote1_serial_group;
1953 remote->remotes[2].group = remote2_serial_group;
1954 remote->remotes[3].group = remote3_serial_group;
1955 remote->remotes[4].group = remote4_serial_group;
1957 remote->remote_dir = kobject_create_and_add("wacom_remote",
1958 &wacom->hdev->dev.kobj);
1959 if (!remote->remote_dir)
1960 return -ENOMEM;
1962 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
1964 if (error) {
1965 hid_err(wacom->hdev,
1966 "cannot create sysfs group err: %d\n", error);
1967 return error;
1970 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1971 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
1972 remote->remotes[i].serial = 0;
1975 error = devm_add_action_or_reset(&wacom->hdev->dev,
1976 wacom_remotes_destroy, wacom);
1977 if (error)
1978 return error;
1980 return 0;
1983 static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1985 struct input_dev *input_dev;
1986 struct hid_device *hdev = wacom->hdev;
1987 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1989 input_dev = devm_input_allocate_device(&hdev->dev);
1990 if (!input_dev)
1991 return NULL;
1993 input_dev->name = wacom_wac->features.name;
1994 input_dev->phys = hdev->phys;
1995 input_dev->dev.parent = &hdev->dev;
1996 input_dev->open = wacom_open;
1997 input_dev->close = wacom_close;
1998 input_dev->uniq = hdev->uniq;
1999 input_dev->id.bustype = hdev->bus;
2000 input_dev->id.vendor = hdev->vendor;
2001 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
2002 input_dev->id.version = hdev->version;
2003 input_set_drvdata(input_dev, wacom);
2005 return input_dev;
2008 static int wacom_allocate_inputs(struct wacom *wacom)
2010 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2012 wacom_wac->pen_input = wacom_allocate_input(wacom);
2013 wacom_wac->touch_input = wacom_allocate_input(wacom);
2014 wacom_wac->pad_input = wacom_allocate_input(wacom);
2015 if (!wacom_wac->pen_input ||
2016 !wacom_wac->touch_input ||
2017 !wacom_wac->pad_input)
2018 return -ENOMEM;
2020 wacom_wac->pen_input->name = wacom_wac->pen_name;
2021 wacom_wac->touch_input->name = wacom_wac->touch_name;
2022 wacom_wac->pad_input->name = wacom_wac->pad_name;
2024 return 0;
2027 static int wacom_register_inputs(struct wacom *wacom)
2029 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2030 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2031 int error = 0;
2033 pen_input_dev = wacom_wac->pen_input;
2034 touch_input_dev = wacom_wac->touch_input;
2035 pad_input_dev = wacom_wac->pad_input;
2037 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
2038 return -EINVAL;
2040 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
2041 if (error) {
2042 /* no pen in use on this interface */
2043 input_free_device(pen_input_dev);
2044 wacom_wac->pen_input = NULL;
2045 pen_input_dev = NULL;
2046 } else {
2047 error = input_register_device(pen_input_dev);
2048 if (error)
2049 goto fail;
2052 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
2053 if (error) {
2054 /* no touch in use on this interface */
2055 input_free_device(touch_input_dev);
2056 wacom_wac->touch_input = NULL;
2057 touch_input_dev = NULL;
2058 } else {
2059 error = input_register_device(touch_input_dev);
2060 if (error)
2061 goto fail;
2064 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2065 if (error) {
2066 /* no pad in use on this interface */
2067 input_free_device(pad_input_dev);
2068 wacom_wac->pad_input = NULL;
2069 pad_input_dev = NULL;
2070 } else {
2071 error = input_register_device(pad_input_dev);
2072 if (error)
2073 goto fail;
2076 return 0;
2078 fail:
2079 wacom_wac->pad_input = NULL;
2080 wacom_wac->touch_input = NULL;
2081 wacom_wac->pen_input = NULL;
2082 return error;
2086 * Not all devices report physical dimensions from HID.
2087 * Compute the default from hardcoded logical dimension
2088 * and resolution before driver overwrites them.
2090 static void wacom_set_default_phy(struct wacom_features *features)
2092 if (features->x_resolution) {
2093 features->x_phy = (features->x_max * 100) /
2094 features->x_resolution;
2095 features->y_phy = (features->y_max * 100) /
2096 features->y_resolution;
2100 static void wacom_calculate_res(struct wacom_features *features)
2102 /* set unit to "100th of a mm" for devices not reported by HID */
2103 if (!features->unit) {
2104 features->unit = 0x11;
2105 features->unitExpo = -3;
2108 features->x_resolution = wacom_calc_hid_res(features->x_max,
2109 features->x_phy,
2110 features->unit,
2111 features->unitExpo);
2112 features->y_resolution = wacom_calc_hid_res(features->y_max,
2113 features->y_phy,
2114 features->unit,
2115 features->unitExpo);
2118 void wacom_battery_work(struct work_struct *work)
2120 struct wacom *wacom = container_of(work, struct wacom, battery_work);
2122 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2123 !wacom->battery.battery) {
2124 wacom_initialize_battery(wacom);
2126 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
2127 wacom->battery.battery) {
2128 wacom_destroy_battery(wacom);
2132 static size_t wacom_compute_pktlen(struct hid_device *hdev)
2134 struct hid_report_enum *report_enum;
2135 struct hid_report *report;
2136 size_t size = 0;
2138 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2140 list_for_each_entry(report, &report_enum->report_list, list) {
2141 size_t report_size = hid_report_len(report);
2142 if (report_size > size)
2143 size = report_size;
2146 return size;
2149 static void wacom_update_name(struct wacom *wacom, const char *suffix)
2151 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2152 struct wacom_features *features = &wacom_wac->features;
2153 char name[WACOM_NAME_MAX - 20]; /* Leave some room for suffixes */
2155 /* Generic devices name unspecified */
2156 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
2157 char *product_name = wacom->hdev->name;
2159 if (hid_is_using_ll_driver(wacom->hdev, &usb_hid_driver)) {
2160 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2161 struct usb_device *dev = interface_to_usbdev(intf);
2162 product_name = dev->product;
2165 if (wacom->hdev->bus == BUS_I2C) {
2166 snprintf(name, sizeof(name), "%s %X",
2167 features->name, wacom->hdev->product);
2168 } else if (strstr(product_name, "Wacom") ||
2169 strstr(product_name, "wacom") ||
2170 strstr(product_name, "WACOM")) {
2171 strlcpy(name, product_name, sizeof(name));
2172 } else {
2173 snprintf(name, sizeof(name), "Wacom %s", product_name);
2176 /* strip out excess whitespaces */
2177 while (1) {
2178 char *gap = strstr(name, " ");
2179 if (gap == NULL)
2180 break;
2181 /* shift everything including the terminator */
2182 memmove(gap, gap+1, strlen(gap));
2185 /* get rid of trailing whitespace */
2186 if (name[strlen(name)-1] == ' ')
2187 name[strlen(name)-1] = '\0';
2188 } else {
2189 strlcpy(name, features->name, sizeof(name));
2192 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2193 name, suffix);
2195 /* Append the device type to the name */
2196 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
2197 "%s%s Pen", name, suffix);
2198 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
2199 "%s%s Finger", name, suffix);
2200 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
2201 "%s%s Pad", name, suffix);
2204 static void wacom_release_resources(struct wacom *wacom)
2206 struct hid_device *hdev = wacom->hdev;
2208 if (!wacom->resources)
2209 return;
2211 devres_release_group(&hdev->dev, wacom);
2213 wacom->resources = false;
2215 wacom->wacom_wac.pen_input = NULL;
2216 wacom->wacom_wac.touch_input = NULL;
2217 wacom->wacom_wac.pad_input = NULL;
2220 static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2222 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2223 wacom_wac->shared->type = wacom_wac->features.type;
2224 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2227 if (wacom_wac->has_mute_touch_switch) {
2228 wacom_wac->shared->has_mute_touch_switch = true;
2229 wacom_wac->shared->is_touch_on = true;
2232 if (wacom_wac->shared->has_mute_touch_switch &&
2233 wacom_wac->shared->touch_input) {
2234 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2235 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2236 SW_MUTE_DEVICE);
2240 static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
2242 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2243 struct wacom_features *features = &wacom_wac->features;
2244 struct hid_device *hdev = wacom->hdev;
2245 int error;
2246 unsigned int connect_mask = HID_CONNECT_HIDRAW;
2248 features->pktlen = wacom_compute_pktlen(hdev);
2249 if (features->pktlen > WACOM_PKGLEN_MAX)
2250 return -EINVAL;
2252 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2253 return -ENOMEM;
2255 wacom->resources = true;
2257 error = wacom_allocate_inputs(wacom);
2258 if (error)
2259 goto fail;
2262 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2263 * into debug mode for the touch part.
2264 * We ignore the other interfaces.
2266 if (features->type == BAMBOO_PAD) {
2267 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2268 features->type = HID_GENERIC;
2269 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2270 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2271 error = -ENODEV;
2272 goto fail;
2276 /* set the default size in case we do not get them from hid */
2277 wacom_set_default_phy(features);
2279 /* Retrieve the physical and logical size for touch devices */
2280 wacom_retrieve_hid_descriptor(hdev, features);
2281 wacom_setup_device_quirks(wacom);
2283 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2284 features->type != WIRELESS) {
2285 error = features->type == HID_GENERIC ? -ENODEV : 0;
2287 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
2288 hdev->name,
2289 error ? "Ignoring" : "Assuming pen");
2291 if (error)
2292 goto fail;
2294 features->device_type |= WACOM_DEVICETYPE_PEN;
2297 wacom_calculate_res(features);
2299 wacom_update_name(wacom, wireless ? " (WL)" : "");
2301 /* pen only Bamboo neither support touch nor pad */
2302 if ((features->type == BAMBOO_PEN) &&
2303 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2304 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2305 error = -ENODEV;
2306 goto fail;
2309 error = wacom_add_shared_data(hdev);
2310 if (error)
2311 goto fail;
2313 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2314 (features->quirks & WACOM_QUIRK_BATTERY)) {
2315 error = wacom_initialize_battery(wacom);
2316 if (error)
2317 goto fail;
2320 error = wacom_register_inputs(wacom);
2321 if (error)
2322 goto fail;
2324 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
2325 error = wacom_initialize_leds(wacom);
2326 if (error)
2327 goto fail;
2329 error = wacom_initialize_remotes(wacom);
2330 if (error)
2331 goto fail;
2334 if (features->type == HID_GENERIC)
2335 connect_mask |= HID_CONNECT_DRIVER;
2337 /* Regular HID work starts now */
2338 error = hid_hw_start(hdev, connect_mask);
2339 if (error) {
2340 hid_err(hdev, "hw start failed\n");
2341 goto fail;
2344 if (!wireless) {
2345 /* Note that if query fails it is not a hard failure */
2346 wacom_query_tablet_data(wacom);
2349 /* touch only Bamboo doesn't support pen */
2350 if ((features->type == BAMBOO_TOUCH) &&
2351 (features->device_type & WACOM_DEVICETYPE_PEN)) {
2352 cancel_delayed_work_sync(&wacom->init_work);
2353 _wacom_query_tablet_data(wacom);
2354 error = -ENODEV;
2355 goto fail_quirks;
2358 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2359 error = hid_hw_open(hdev);
2361 wacom_set_shared_values(wacom_wac);
2362 devres_close_group(&hdev->dev, wacom);
2364 return 0;
2366 fail_quirks:
2367 hid_hw_stop(hdev);
2368 fail:
2369 wacom_release_resources(wacom);
2370 return error;
2373 static void wacom_wireless_work(struct work_struct *work)
2375 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2376 struct usb_device *usbdev = wacom->usbdev;
2377 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2378 struct hid_device *hdev1, *hdev2;
2379 struct wacom *wacom1, *wacom2;
2380 struct wacom_wac *wacom_wac1, *wacom_wac2;
2381 int error;
2384 * Regardless if this is a disconnect or a new tablet,
2385 * remove any existing input and battery devices.
2388 wacom_destroy_battery(wacom);
2390 /* Stylus interface */
2391 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2392 wacom1 = hid_get_drvdata(hdev1);
2393 wacom_wac1 = &(wacom1->wacom_wac);
2394 wacom_release_resources(wacom1);
2396 /* Touch interface */
2397 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2398 wacom2 = hid_get_drvdata(hdev2);
2399 wacom_wac2 = &(wacom2->wacom_wac);
2400 wacom_release_resources(wacom2);
2402 if (wacom_wac->pid == 0) {
2403 hid_info(wacom->hdev, "wireless tablet disconnected\n");
2404 } else {
2405 const struct hid_device_id *id = wacom_ids;
2407 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2408 wacom_wac->pid);
2410 while (id->bus) {
2411 if (id->vendor == USB_VENDOR_ID_WACOM &&
2412 id->product == wacom_wac->pid)
2413 break;
2414 id++;
2417 if (!id->bus) {
2418 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2419 return;
2422 /* Stylus interface */
2423 wacom_wac1->features =
2424 *((struct wacom_features *)id->driver_data);
2426 wacom_wac1->pid = wacom_wac->pid;
2427 hid_hw_stop(hdev1);
2428 error = wacom_parse_and_register(wacom1, true);
2429 if (error)
2430 goto fail;
2432 /* Touch interface */
2433 if (wacom_wac1->features.touch_max ||
2434 (wacom_wac1->features.type >= INTUOSHT &&
2435 wacom_wac1->features.type <= BAMBOO_PT)) {
2436 wacom_wac2->features =
2437 *((struct wacom_features *)id->driver_data);
2438 wacom_wac2->pid = wacom_wac->pid;
2439 hid_hw_stop(hdev2);
2440 error = wacom_parse_and_register(wacom2, true);
2441 if (error)
2442 goto fail;
2445 strlcpy(wacom_wac->name, wacom_wac1->name,
2446 sizeof(wacom_wac->name));
2447 error = wacom_initialize_battery(wacom);
2448 if (error)
2449 goto fail;
2452 return;
2454 fail:
2455 wacom_release_resources(wacom1);
2456 wacom_release_resources(wacom2);
2457 return;
2460 static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2462 struct wacom_remote *remote = wacom->remote;
2463 u32 serial = remote->remotes[index].serial;
2464 int i;
2465 unsigned long flags;
2467 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2468 if (remote->remotes[i].serial == serial) {
2470 spin_lock_irqsave(&remote->remote_lock, flags);
2471 remote->remotes[i].registered = false;
2472 spin_unlock_irqrestore(&remote->remote_lock, flags);
2474 if (remote->remotes[i].battery.battery)
2475 devres_release_group(&wacom->hdev->dev,
2476 &remote->remotes[i].battery.bat_desc);
2478 if (remote->remotes[i].group.name)
2479 devres_release_group(&wacom->hdev->dev,
2480 &remote->remotes[i]);
2482 remote->remotes[i].serial = 0;
2483 remote->remotes[i].group.name = NULL;
2484 remote->remotes[i].battery.battery = NULL;
2485 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2490 static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2491 unsigned int index)
2493 struct wacom_remote *remote = wacom->remote;
2494 struct device *dev = &wacom->hdev->dev;
2495 int error, k;
2497 /* A remote can pair more than once with an EKR,
2498 * check to make sure this serial isn't already paired.
2500 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2501 if (remote->remotes[k].serial == serial)
2502 break;
2505 if (k < WACOM_MAX_REMOTES) {
2506 remote->remotes[index].serial = serial;
2507 return 0;
2510 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2511 return -ENOMEM;
2513 error = wacom_remote_create_attr_group(wacom, serial, index);
2514 if (error)
2515 goto fail;
2517 remote->remotes[index].input = wacom_allocate_input(wacom);
2518 if (!remote->remotes[index].input) {
2519 error = -ENOMEM;
2520 goto fail;
2522 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2523 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2525 if (!remote->remotes[index].input->name) {
2526 error = -EINVAL;
2527 goto fail;
2530 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2531 &wacom->wacom_wac);
2532 if (error)
2533 goto fail;
2535 remote->remotes[index].serial = serial;
2537 error = input_register_device(remote->remotes[index].input);
2538 if (error)
2539 goto fail;
2541 error = wacom_led_groups_alloc_and_register_one(
2542 &remote->remotes[index].input->dev,
2543 wacom, index, 3, true);
2544 if (error)
2545 goto fail;
2547 remote->remotes[index].registered = true;
2549 devres_close_group(dev, &remote->remotes[index]);
2550 return 0;
2552 fail:
2553 devres_release_group(dev, &remote->remotes[index]);
2554 remote->remotes[index].serial = 0;
2555 return error;
2558 static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2560 struct wacom_remote *remote = wacom->remote;
2561 int error;
2563 if (!remote->remotes[index].registered)
2564 return 0;
2566 if (remote->remotes[index].battery.battery)
2567 return 0;
2569 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2570 return 0;
2572 error = __wacom_initialize_battery(wacom,
2573 &wacom->remote->remotes[index].battery);
2574 if (error)
2575 return error;
2577 return 0;
2580 static void wacom_remote_work(struct work_struct *work)
2582 struct wacom *wacom = container_of(work, struct wacom, remote_work);
2583 struct wacom_remote *remote = wacom->remote;
2584 struct wacom_remote_data data;
2585 unsigned long flags;
2586 unsigned int count;
2587 u32 serial;
2588 int i;
2590 spin_lock_irqsave(&remote->remote_lock, flags);
2592 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2594 if (count != sizeof(data)) {
2595 hid_err(wacom->hdev,
2596 "workitem triggered without status available\n");
2597 spin_unlock_irqrestore(&remote->remote_lock, flags);
2598 return;
2601 if (!kfifo_is_empty(&remote->remote_fifo))
2602 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2604 spin_unlock_irqrestore(&remote->remote_lock, flags);
2606 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2607 serial = data.remote[i].serial;
2608 if (data.remote[i].connected) {
2610 if (remote->remotes[i].serial == serial) {
2611 wacom_remote_attach_battery(wacom, i);
2612 continue;
2615 if (remote->remotes[i].serial)
2616 wacom_remote_destroy_one(wacom, i);
2618 wacom_remote_create_one(wacom, serial, i);
2620 } else if (remote->remotes[i].serial) {
2621 wacom_remote_destroy_one(wacom, i);
2626 static void wacom_mode_change_work(struct work_struct *work)
2628 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2629 struct wacom_shared *shared = wacom->wacom_wac.shared;
2630 struct wacom *wacom1 = NULL;
2631 struct wacom *wacom2 = NULL;
2632 bool is_direct = wacom->wacom_wac.is_direct_mode;
2633 int error = 0;
2635 if (shared->pen) {
2636 wacom1 = hid_get_drvdata(shared->pen);
2637 wacom_release_resources(wacom1);
2638 hid_hw_stop(wacom1->hdev);
2639 wacom1->wacom_wac.has_mode_change = true;
2640 wacom1->wacom_wac.is_direct_mode = is_direct;
2643 if (shared->touch) {
2644 wacom2 = hid_get_drvdata(shared->touch);
2645 wacom_release_resources(wacom2);
2646 hid_hw_stop(wacom2->hdev);
2647 wacom2->wacom_wac.has_mode_change = true;
2648 wacom2->wacom_wac.is_direct_mode = is_direct;
2651 if (wacom1) {
2652 error = wacom_parse_and_register(wacom1, false);
2653 if (error)
2654 return;
2657 if (wacom2) {
2658 error = wacom_parse_and_register(wacom2, false);
2659 if (error)
2660 return;
2663 return;
2666 static int wacom_probe(struct hid_device *hdev,
2667 const struct hid_device_id *id)
2669 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2670 struct usb_device *dev = interface_to_usbdev(intf);
2671 struct wacom *wacom;
2672 struct wacom_wac *wacom_wac;
2673 struct wacom_features *features;
2674 int error;
2676 if (!id->driver_data)
2677 return -EINVAL;
2679 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2681 /* hid-core sets this quirk for the boot interface */
2682 hdev->quirks &= ~HID_QUIRK_NOGET;
2684 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2685 if (!wacom)
2686 return -ENOMEM;
2688 hid_set_drvdata(hdev, wacom);
2689 wacom->hdev = hdev;
2691 wacom_wac = &wacom->wacom_wac;
2692 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2693 features = &wacom_wac->features;
2695 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2696 error = -ENODEV;
2697 goto fail;
2700 error = kfifo_alloc(&wacom_wac->pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
2701 if (error)
2702 goto fail;
2704 wacom_wac->hid_data.inputmode = -1;
2705 wacom_wac->mode_report = -1;
2707 wacom->usbdev = dev;
2708 wacom->intf = intf;
2709 mutex_init(&wacom->lock);
2710 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
2711 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2712 INIT_WORK(&wacom->battery_work, wacom_battery_work);
2713 INIT_WORK(&wacom->remote_work, wacom_remote_work);
2714 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
2716 /* ask for the report descriptor to be loaded by HID */
2717 error = hid_parse(hdev);
2718 if (error) {
2719 hid_err(hdev, "parse failed\n");
2720 goto fail;
2723 error = wacom_parse_and_register(wacom, false);
2724 if (error)
2725 goto fail;
2727 if (hdev->bus == BUS_BLUETOOTH) {
2728 error = device_create_file(&hdev->dev, &dev_attr_speed);
2729 if (error)
2730 hid_warn(hdev,
2731 "can't create sysfs speed attribute err: %d\n",
2732 error);
2735 return 0;
2737 fail:
2738 hid_set_drvdata(hdev, NULL);
2739 return error;
2742 static void wacom_remove(struct hid_device *hdev)
2744 struct wacom *wacom = hid_get_drvdata(hdev);
2745 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2746 struct wacom_features *features = &wacom_wac->features;
2748 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2749 hid_hw_close(hdev);
2751 hid_hw_stop(hdev);
2753 cancel_delayed_work_sync(&wacom->init_work);
2754 cancel_work_sync(&wacom->wireless_work);
2755 cancel_work_sync(&wacom->battery_work);
2756 cancel_work_sync(&wacom->remote_work);
2757 cancel_work_sync(&wacom->mode_change_work);
2758 if (hdev->bus == BUS_BLUETOOTH)
2759 device_remove_file(&hdev->dev, &dev_attr_speed);
2761 /* make sure we don't trigger the LEDs */
2762 wacom_led_groups_release(wacom);
2764 if (wacom->wacom_wac.features.type != REMOTE)
2765 wacom_release_resources(wacom);
2767 kfifo_free(&wacom_wac->pen_fifo);
2769 hid_set_drvdata(hdev, NULL);
2772 #ifdef CONFIG_PM
2773 static int wacom_resume(struct hid_device *hdev)
2775 struct wacom *wacom = hid_get_drvdata(hdev);
2777 mutex_lock(&wacom->lock);
2779 /* switch to wacom mode first */
2780 _wacom_query_tablet_data(wacom);
2781 wacom_led_control(wacom);
2783 mutex_unlock(&wacom->lock);
2785 return 0;
2788 static int wacom_reset_resume(struct hid_device *hdev)
2790 return wacom_resume(hdev);
2792 #endif /* CONFIG_PM */
2794 static struct hid_driver wacom_driver = {
2795 .name = "wacom",
2796 .id_table = wacom_ids,
2797 .probe = wacom_probe,
2798 .remove = wacom_remove,
2799 .report = wacom_wac_report,
2800 #ifdef CONFIG_PM
2801 .resume = wacom_resume,
2802 .reset_resume = wacom_reset_resume,
2803 #endif
2804 .raw_event = wacom_raw_event,
2806 module_hid_driver(wacom_driver);
2808 MODULE_VERSION(DRIVER_VERSION);
2809 MODULE_AUTHOR(DRIVER_AUTHOR);
2810 MODULE_DESCRIPTION(DRIVER_DESC);
2811 MODULE_LICENSE("GPL");