blob: 066c567dbaa229f3fa5948cbc3cc1484e8bf5e2b [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Ping Cheng3bea7332006-07-13 18:01:36 -07002/*
Dmitry Torokhov4104d132007-05-07 16:16:29 -04003 * drivers/input/tablet/wacom_sys.c
Ping Cheng3bea7332006-07-13 18:01:36 -07004 *
Ping Cheng232f5692009-12-15 00:35:24 -08005 * USB Wacom tablet support - system specific code
Ping Cheng3bea7332006-07-13 18:01:36 -07006 */
7
8/*
Ping Cheng3bea7332006-07-13 18:01:36 -07009 */
10
Ping Cheng3bea7332006-07-13 18:01:36 -070011#include "wacom_wac.h"
Dmitry Torokhov51269fe2010-03-19 22:18:15 -070012#include "wacom.h"
Jason Gereckeb58ba1b2014-12-05 13:37:32 -080013#include <linux/input/mt.h>
Ping Cheng3bea7332006-07-13 18:01:36 -070014
Ping Chenga417ea42011-08-16 00:17:56 -070015#define WAC_MSG_RETRIES 5
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070016#define WAC_CMD_RETRIES 10
17
Ping Chenge0984bc2014-09-10 12:40:05 -070018#define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
19#define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
Aaron Skomra72b236d2015-08-20 16:05:17 -070020#define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
Ping Chenge0984bc2014-09-10 12:40:05 -070021
Ping Chengc64d8832014-09-10 12:41:04 -070022static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
23 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070024{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070025 int retval;
26
27 do {
Ping Chengc64d8832014-09-10 12:41:04 -070028 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070029 HID_REQ_GET_REPORT);
Jason Gereckeaef31562015-05-21 10:44:31 -070030 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
31
32 if (retval < 0)
33 hid_err(hdev, "wacom_get_report: ran out of retries "
34 "(last error = %d)\n", retval);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070035
36 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070037}
38
Przemo Firszt296b7372014-08-06 14:00:38 -070039static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
40 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070041{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070042 int retval;
43
44 do {
Przemo Firszt296b7372014-08-06 14:00:38 -070045 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070046 HID_REQ_SET_REPORT);
Jason Gereckeaef31562015-05-21 10:44:31 -070047 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
48
49 if (retval < 0)
50 hid_err(hdev, "wacom_set_report: ran out of retries "
51 "(last error = %d)\n", retval);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070052
53 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070054}
55
Jason Gerecke83417202017-11-10 11:50:01 -080056static void wacom_wac_queue_insert(struct hid_device *hdev,
57 struct kfifo_rec_ptr_2 *fifo,
58 u8 *raw_data, int size)
59{
60 bool warned = false;
61
62 while (kfifo_avail(fifo) < size) {
63 if (!warned)
64 hid_warn(hdev, "%s: kfifo has filled, starting to drop events\n", __func__);
65 warned = true;
66
67 kfifo_skip(fifo);
68 }
69
70 kfifo_in(fifo, raw_data, size);
71}
72
73static void wacom_wac_queue_flush(struct hid_device *hdev,
74 struct kfifo_rec_ptr_2 *fifo)
75{
76 while (!kfifo_is_empty(fifo)) {
77 u8 buf[WACOM_PKGLEN_MAX];
78 int size;
79 int err;
80
81 size = kfifo_out(fifo, buf, sizeof(buf));
82 err = hid_report_raw_event(hdev, HID_INPUT_REPORT, buf, size, false);
83 if (err) {
84 hid_warn(hdev, "%s: unable to flush event due to error %d\n",
85 __func__, err);
86 }
87 }
88}
89
90static int wacom_wac_pen_serial_enforce(struct hid_device *hdev,
Jason Gerecke073b50b2019-08-16 11:54:26 -070091 struct hid_report *report, u8 *raw_data, int report_size)
Jason Gerecke83417202017-11-10 11:50:01 -080092{
93 struct wacom *wacom = hid_get_drvdata(hdev);
94 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
95 struct wacom_features *features = &wacom_wac->features;
96 bool flush = false;
97 bool insert = false;
98 int i, j;
99
100 if (wacom_wac->serial[0] || !(features->quirks & WACOM_QUIRK_TOOLSERIAL))
101 return 0;
102
103 /* Queue events which have invalid tool type or serial number */
104 for (i = 0; i < report->maxfield; i++) {
105 for (j = 0; j < report->field[i]->maxusage; j++) {
106 struct hid_field *field = report->field[i];
107 struct hid_usage *usage = &field->usage[j];
108 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
109 unsigned int offset;
110 unsigned int size;
111 unsigned int value;
112
113 if (equivalent_usage != HID_DG_INRANGE &&
114 equivalent_usage != HID_DG_TOOLSERIALNUMBER &&
115 equivalent_usage != WACOM_HID_WD_SERIALHI &&
116 equivalent_usage != WACOM_HID_WD_TOOLTYPE)
117 continue;
118
119 offset = field->report_offset;
120 size = field->report_size;
121 value = hid_field_extract(hdev, raw_data+1, offset + j * size, size);
122
123 /* If we go out of range, we need to flush the queue ASAP */
124 if (equivalent_usage == HID_DG_INRANGE)
125 value = !value;
126
127 if (value) {
128 flush = true;
129 switch (equivalent_usage) {
130 case HID_DG_TOOLSERIALNUMBER:
131 wacom_wac->serial[0] = value;
132 break;
133
134 case WACOM_HID_WD_SERIALHI:
135 wacom_wac->serial[0] |= ((__u64)value) << 32;
136 break;
137
138 case WACOM_HID_WD_TOOLTYPE:
139 wacom_wac->id[0] = value;
140 break;
141 }
142 }
143 else {
144 insert = true;
145 }
146 }
147 }
148
149 if (flush)
Jason Gerecke179e8e42021-01-21 10:46:49 -0800150 wacom_wac_queue_flush(hdev, wacom_wac->pen_fifo);
Jason Gerecke83417202017-11-10 11:50:01 -0800151 else if (insert)
Jason Gerecke179e8e42021-01-21 10:46:49 -0800152 wacom_wac_queue_insert(hdev, wacom_wac->pen_fifo,
Jason Gerecke073b50b2019-08-16 11:54:26 -0700153 raw_data, report_size);
Jason Gerecke83417202017-11-10 11:50:01 -0800154
155 return insert && !flush;
156}
157
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700158static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
159 u8 *raw_data, int size)
Ping Cheng3bea7332006-07-13 18:01:36 -0700160{
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700161 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700162
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700163 if (size > WACOM_PKGLEN_MAX)
164 return 1;
Ping Cheng3bea7332006-07-13 18:01:36 -0700165
Jason Gerecke83417202017-11-10 11:50:01 -0800166 if (wacom_wac_pen_serial_enforce(hdev, report, raw_data, size))
167 return -1;
168
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700169 memcpy(wacom->wacom_wac.data, raw_data, size);
Ping Cheng3bea7332006-07-13 18:01:36 -0700170
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700171 wacom_wac_irq(&wacom->wacom_wac, size);
172
173 return 0;
Ping Cheng3bea7332006-07-13 18:01:36 -0700174}
175
Ping Cheng3bea7332006-07-13 18:01:36 -0700176static int wacom_open(struct input_dev *dev)
177{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400178 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700179
Benjamin Tissoiresdff67412014-12-01 11:52:40 -0500180 return hid_hw_open(wacom->hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700181}
182
183static void wacom_close(struct input_dev *dev)
184{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400185 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700186
Benjamin Tissoires3dad1882016-07-13 18:05:54 +0200187 /*
188 * wacom->hdev should never be null, but surprisingly, I had the case
189 * once while unplugging the Wacom Wireless Receiver.
190 */
191 if (wacom->hdev)
192 hid_hw_close(wacom->hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700193}
194
Chris Bagwell16bf2882012-03-25 23:26:20 -0700195/*
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700196 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
Jason Gerecke115d5e12012-10-03 17:24:32 -0700197 */
198static int wacom_calc_hid_res(int logical_extents, int physical_extents,
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700199 unsigned unit, int exponent)
Jason Gerecke115d5e12012-10-03 17:24:32 -0700200{
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700201 struct hid_field field = {
202 .logical_maximum = logical_extents,
203 .physical_maximum = physical_extents,
204 .unit = unit,
205 .unit_exponent = exponent,
206 };
Jason Gerecke115d5e12012-10-03 17:24:32 -0700207
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700208 return hidinput_calc_abs_res(&field, ABS_X);
Jason Gerecke115d5e12012-10-03 17:24:32 -0700209}
210
Jason Gerecke57832512018-06-25 13:24:35 -0700211static void wacom_hid_usage_quirk(struct hid_device *hdev,
212 struct hid_field *field, struct hid_usage *usage)
213{
214 struct wacom *wacom = hid_get_drvdata(hdev);
215 struct wacom_features *features = &wacom->wacom_wac.features;
216 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
217
218 /*
219 * The Dell Canvas 27 needs to be switched to its vendor-defined
220 * report to provide the best resolution.
221 */
222 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
223 hdev->product == 0x4200 &&
224 field->application == HID_UP_MSVENDOR) {
225 wacom->wacom_wac.mode_report = field->report->id;
226 wacom->wacom_wac.mode_value = 2;
227 }
228
229 /*
230 * ISDv4 devices which predate HID's adoption of the
231 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
232 * position instead. We can accurately detect if a
233 * usage with that value should be HID_DG_BARRELSWITCH2
234 * based on the surrounding usages, which have remained
235 * constant across generations.
236 */
237 if (features->type == HID_GENERIC &&
238 usage->hid == 0x000D0000 &&
239 field->application == HID_DG_PEN &&
240 field->physical == HID_DG_STYLUS) {
241 int i = usage->usage_index;
242
243 if (i-4 >= 0 && i+1 < field->maxusage &&
244 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
245 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
246 field->usage[i-2].hid == HID_DG_ERASER &&
247 field->usage[i-1].hid == HID_DG_INVERT &&
248 field->usage[i+1].hid == HID_DG_INRANGE) {
249 usage->hid = HID_DG_BARRELSWITCH2;
250 }
251 }
252
Jason Gereckee9fe0d42019-01-24 11:09:44 -0800253 /*
254 * Wacom's AES devices use different vendor-defined usages to
255 * report serial number information compared to their branded
256 * hardware. The usages are also sometimes ill-defined and do
257 * not have the correct logical min/max values set. Lets patch
258 * the descriptor to use the branded usage convention and fix
259 * the errors.
260 */
261 if (usage->hid == WACOM_HID_WT_SERIALNUMBER &&
262 field->report_size == 16 &&
263 field->index + 2 < field->report->maxfield) {
264 struct hid_field *a = field->report->field[field->index + 1];
265 struct hid_field *b = field->report->field[field->index + 2];
266
267 if (a->maxusage > 0 &&
268 a->usage[0].hid == HID_DG_TOOLSERIALNUMBER &&
269 a->report_size == 32 &&
270 b->maxusage > 0 &&
271 b->usage[0].hid == 0xFF000000 &&
272 b->report_size == 8) {
273 features->quirks |= WACOM_QUIRK_AESPEN;
274 usage->hid = WACOM_HID_WD_TOOLTYPE;
275 field->logical_minimum = S16_MIN;
276 field->logical_maximum = S16_MAX;
277 a->logical_minimum = S32_MIN;
278 a->logical_maximum = S32_MAX;
279 b->usage[0].hid = WACOM_HID_WD_SERIALHI;
280 b->logical_minimum = 0;
281 b->logical_maximum = U8_MAX;
282 }
283 }
284
Jason Gerecke57832512018-06-25 13:24:35 -0700285 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
286 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
287 hdev->product == 0x0358 &&
288 WACOM_PEN_FIELD(field) &&
289 equivalent_usage == HID_GD_Y) {
290 field->logical_maximum = 43200;
291 }
292}
293
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700294static void wacom_feature_mapping(struct hid_device *hdev,
295 struct hid_field *field, struct hid_usage *usage)
Chris Bagwell41343612011-10-26 22:32:52 -0700296{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700297 struct wacom *wacom = hid_get_drvdata(hdev);
298 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400299 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
Aaron Armstrong Skomraac2423c2017-01-25 12:08:40 -0800300 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400301 u8 *data;
302 int ret;
Aaron Ma3064a032018-02-03 23:57:15 +0800303 u32 n;
Chris Bagwell41343612011-10-26 22:32:52 -0700304
Jason Gerecke57832512018-06-25 13:24:35 -0700305 wacom_hid_usage_quirk(hdev, field, usage);
306
Aaron Armstrong Skomraac2423c2017-01-25 12:08:40 -0800307 switch (equivalent_usage) {
Aaron Armstrong Skomrad8e98062019-05-10 15:31:16 -0700308 case WACOM_HID_WD_TOUCH_RING_SETTING:
309 wacom->generic_has_leds = true;
310 break;
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700311 case HID_DG_CONTACTMAX:
312 /* leave touch_max as is if predefined */
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400313 if (!features->touch_max) {
314 /* read manually */
Aaron Armstrong Skomra184eccd2019-06-12 14:19:29 -0700315 n = hid_report_len(field->report);
316 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400317 if (!data)
318 break;
319 data[0] = field->report->id;
320 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
Aaron Armstrong Skomra184eccd2019-06-12 14:19:29 -0700321 data, n, WAC_CMD_RETRIES);
Jason Gerecke778fbf42020-04-01 14:23:29 -0700322 if (ret == n && features->type == HID_GENERIC) {
Aaron Armstrong Skomra184eccd2019-06-12 14:19:29 -0700323 ret = hid_report_raw_event(hdev,
324 HID_FEATURE_REPORT, data, n, 0);
Jason Gerecke778fbf42020-04-01 14:23:29 -0700325 } else if (ret == 2 && features->type != HID_GENERIC) {
326 features->touch_max = data[1];
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700327 } else {
328 features->touch_max = 16;
329 hid_warn(hdev, "wacom_feature_mapping: "
330 "could not get HID_DG_CONTACTMAX, "
331 "defaulting to %d\n",
332 features->touch_max);
333 }
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400334 kfree(data);
335 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700336 break;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400337 case HID_DG_INPUTMODE:
338 /* Ignore if value index is out of bounds. */
339 if (usage->usage_index >= field->report_count) {
340 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
341 break;
342 }
343
344 hid_data->inputmode = field->report->id;
345 hid_data->inputmode_index = usage->usage_index;
346 break;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700347
348 case HID_UP_DIGITIZER:
349 if (field->report->id == 0x0B &&
Jason Gerecke8de82282016-10-19 18:03:37 -0700350 (field->application == WACOM_HID_G9_PEN ||
351 field->application == WACOM_HID_G11_PEN)) {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700352 wacom->wacom_wac.mode_report = field->report->id;
353 wacom->wacom_wac.mode_value = 0;
354 }
355 break;
356
Jason Gereckec9c09582016-10-19 18:03:43 -0700357 case WACOM_HID_WD_DATAMODE:
358 wacom->wacom_wac.mode_report = field->report->id;
359 wacom->wacom_wac.mode_value = 2;
360 break;
361
Jason Gerecke8de82282016-10-19 18:03:37 -0700362 case WACOM_HID_UP_G9:
363 case WACOM_HID_UP_G11:
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700364 if (field->report->id == 0x03 &&
Jason Gerecke8de82282016-10-19 18:03:37 -0700365 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
366 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700367 wacom->wacom_wac.mode_report = field->report->id;
368 wacom->wacom_wac.mode_value = 0;
369 }
370 break;
Jason Gerecke345857b2016-10-19 18:03:50 -0700371 case WACOM_HID_WD_OFFSETLEFT:
372 case WACOM_HID_WD_OFFSETTOP:
373 case WACOM_HID_WD_OFFSETRIGHT:
374 case WACOM_HID_WD_OFFSETBOTTOM:
375 /* read manually */
376 n = hid_report_len(field->report);
377 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
378 if (!data)
379 break;
380 data[0] = field->report->id;
381 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
382 data, n, WAC_CMD_RETRIES);
383 if (ret == n) {
384 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
385 data, n, 0);
386 } else {
387 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
388 __func__);
389 }
390 kfree(data);
391 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700392 }
393}
394
Chris Bagwell428f8582011-10-26 22:26:59 -0700395/*
396 * Interface Descriptor of wacom devices can be incomplete and
397 * inconsistent so wacom_features table is used to store stylus
398 * device's packet lengths, various maximum values, and tablet
399 * resolution based on product ID's.
400 *
401 * For devices that contain 2 interfaces, wacom_features table is
402 * inaccurate for the touch interface. Since the Interface Descriptor
403 * for touch interfaces has pretty complete data, this function exists
404 * to query tablet for this missing information instead of hard coding in
405 * an additional table.
406 *
407 * A typical Interface Descriptor for a stylus will contain a
408 * boot mouse application collection that is not of interest and this
409 * function will ignore it.
410 *
411 * It also contains a digitizer application collection that also is not
412 * of interest since any information it contains would be duplicate
413 * of what is in wacom_features. Usually it defines a report of an array
414 * of bytes that could be used as max length of the stylus packet returned.
415 * If it happens to define a Digitizer-Stylus Physical Collection then
416 * the X and Y logical values contain valid data but it is ignored.
417 *
418 * A typical Interface Descriptor for a touch interface will contain a
419 * Digitizer-Finger Physical Collection which will define both logical
420 * X/Y maximum as well as the physical size of tablet. Since touch
421 * interfaces haven't supported pressure or distance, this is enough
422 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700423 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700424 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
425 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700426 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700427static void wacom_usage_mapping(struct hid_device *hdev,
428 struct hid_field *field, struct hid_usage *usage)
429{
430 struct wacom *wacom = hid_get_drvdata(hdev);
431 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoiresd97a5522015-01-05 16:32:12 -0500432 bool finger = WACOM_FINGER_FIELD(field);
433 bool pen = WACOM_PEN_FIELD(field);
Ping Cheng418b5732018-06-25 13:24:36 -0700434 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700435
436 /*
437 * Requiring Stylus Usage will ignore boot mouse
438 * X/Y values and some cases of invalid Digitizer X/Y
439 * values commonly reported.
440 */
Jason Gerecke042628a2015-04-30 17:51:54 -0700441 if (pen)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700442 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -0700443 else if (finger)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700444 features->device_type |= WACOM_DEVICETYPE_TOUCH;
Jason Gerecke042628a2015-04-30 17:51:54 -0700445 else
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700446 return;
447
Jason Gerecke57832512018-06-25 13:24:35 -0700448 wacom_hid_usage_quirk(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700449
Ping Cheng418b5732018-06-25 13:24:36 -0700450 switch (equivalent_usage) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700451 case HID_GD_X:
452 features->x_max = field->logical_maximum;
453 if (finger) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700454 features->x_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700455 if ((features->type != BAMBOO_PT) &&
456 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700457 features->unit = field->unit;
458 features->unitExpo = field->unit_exponent;
459 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700460 }
461 break;
462 case HID_GD_Y:
463 features->y_max = field->logical_maximum;
464 if (finger) {
465 features->y_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700466 if ((features->type != BAMBOO_PT) &&
467 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700468 features->unit = field->unit;
469 features->unitExpo = field->unit_exponent;
470 }
471 }
472 break;
473 case HID_DG_TIPPRESSURE:
474 if (pen)
475 features->pressure_max = field->logical_maximum;
476 break;
477 }
Benjamin Tissoires7704ac92014-09-23 12:08:08 -0400478
479 if (features->type == HID_GENERIC)
480 wacom_wac_usage_mapping(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700481}
482
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800483static void wacom_post_parse_hid(struct hid_device *hdev,
484 struct wacom_features *features)
485{
486 struct wacom *wacom = hid_get_drvdata(hdev);
487 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
488
489 if (features->type == HID_GENERIC) {
490 /* Any last-minute generic device setup */
Benjamin Tissoires4082da82017-02-14 21:27:18 -0800491 if (wacom_wac->has_mode_change) {
492 if (wacom_wac->is_direct_mode)
493 features->device_type |= WACOM_DEVICETYPE_DIRECT;
494 else
495 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
496 }
497
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800498 if (features->touch_max > 1) {
Aaron Armstrong Skomraac2423c2017-01-25 12:08:40 -0800499 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
500 input_mt_init_slots(wacom_wac->touch_input,
501 wacom_wac->features.touch_max,
502 INPUT_MT_DIRECT);
503 else
504 input_mt_init_slots(wacom_wac->touch_input,
505 wacom_wac->features.touch_max,
506 INPUT_MT_POINTER);
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800507 }
508 }
509}
510
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700511static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800512 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500513{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700514 struct hid_report_enum *rep_enum;
515 struct hid_report *hreport;
516 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500517
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700518 /* check features first */
519 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
520 list_for_each_entry(hreport, &rep_enum->report_list, list) {
521 for (i = 0; i < hreport->maxfield; i++) {
522 /* Ignore if report count is out of bounds. */
523 if (hreport->field[i]->report_count < 1)
524 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500525
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700526 for (j = 0; j < hreport->field[i]->maxusage; j++) {
527 wacom_feature_mapping(hdev, hreport->field[i],
528 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500529 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500530 }
531 }
532
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700533 /* now check the input usages */
534 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
535 list_for_each_entry(hreport, &rep_enum->report_list, list) {
536
537 if (!hreport->maxfield)
538 continue;
539
540 for (i = 0; i < hreport->maxfield; i++)
541 for (j = 0; j < hreport->field[i]->maxusage; j++)
542 wacom_usage_mapping(hdev, hreport->field[i],
543 hreport->field[i]->usage + j);
544 }
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800545
546 wacom_post_parse_hid(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -0500547}
548
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400549static int wacom_hid_set_device_mode(struct hid_device *hdev)
550{
551 struct wacom *wacom = hid_get_drvdata(hdev);
552 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
553 struct hid_report *r;
554 struct hid_report_enum *re;
555
556 if (hid_data->inputmode < 0)
557 return 0;
558
559 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
560 r = re->report_id_hash[hid_data->inputmode];
561 if (r) {
562 r->field[0]->value[hid_data->inputmode_index] = 2;
563 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
564 }
565 return 0;
566}
567
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700568static int wacom_set_device_mode(struct hid_device *hdev,
569 struct wacom_wac *wacom_wac)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700570{
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700571 u8 *rep_data;
572 struct hid_report *r;
573 struct hid_report_enum *re;
Aaron Ma3064a032018-02-03 23:57:15 +0800574 u32 length;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700575 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700576
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700577 if (wacom_wac->mode_report < 0)
578 return 0;
579
580 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
581 r = re->report_id_hash[wacom_wac->mode_report];
582 if (!r)
583 return -EINVAL;
584
585 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700586 if (!rep_data)
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700587 return -ENOMEM;
588
589 length = hid_report_len(r);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700590
Jason Gereckefe494bc2012-10-03 17:25:35 -0700591 do {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700592 rep_data[0] = wacom_wac->mode_report;
593 rep_data[1] = wacom_wac->mode_value;
Chris Bagwell9937c022013-01-23 19:37:34 -0800594
Przemo Firszt296b7372014-08-06 14:00:38 -0700595 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
596 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700597 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700598 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700599 rep_data, length, 1);
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700600 } while (error >= 0 &&
601 rep_data[1] != wacom_wac->mode_report &&
602 limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700603
604 kfree(rep_data);
605
606 return error < 0 ? error : 0;
607}
608
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700609static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
610 struct wacom_features *features)
611{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700612 struct wacom *wacom = hid_get_drvdata(hdev);
613 int ret;
614 u8 rep_data[2];
615
616 switch (features->type) {
617 case GRAPHIRE_BT:
618 rep_data[0] = 0x03;
619 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700620 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
621 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700622
623 if (ret >= 0) {
624 rep_data[0] = speed == 0 ? 0x05 : 0x06;
625 rep_data[1] = 0x00;
626
627 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700628 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700629
630 if (ret >= 0) {
631 wacom->wacom_wac.bt_high_speed = speed;
632 return 0;
633 }
634 }
635
636 /*
637 * Note that if the raw queries fail, it's not a hard failure
638 * and it is safe to continue
639 */
640 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
641 rep_data[0], ret);
642 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700643 case INTUOS4WL:
644 if (speed == 1)
645 wacom->wacom_wac.bt_features &= ~0x20;
646 else
647 wacom->wacom_wac.bt_features |= 0x20;
648
649 rep_data[0] = 0x03;
650 rep_data[1] = wacom->wacom_wac.bt_features;
651
Przemo Firszt296b7372014-08-06 14:00:38 -0700652 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
653 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700654 if (ret >= 0)
655 wacom->wacom_wac.bt_high_speed = speed;
656 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700657 }
658
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700659 return 0;
660}
661
Jason Gereckefe494bc2012-10-03 17:25:35 -0700662/*
663 * Switch the tablet into its most-capable mode. Wacom tablets are
664 * typically configured to power-up in a mode which sends mouse-like
665 * reports to the OS. To get absolute position, pressure data, etc.
666 * from the tablet, it is necessary to switch the tablet out of this
667 * mode and into one which sends the full range of tablet data.
668 */
Benjamin Tissoiresa544c612017-01-20 16:20:13 +0100669static int _wacom_query_tablet_data(struct wacom *wacom)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700670{
Benjamin Tissoiresa544c612017-01-20 16:20:13 +0100671 struct hid_device *hdev = wacom->hdev;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700672 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoiresa544c612017-01-20 16:20:13 +0100673 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700674
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700675 if (hdev->bus == BUS_BLUETOOTH)
676 return wacom_bt_query_tablet_data(hdev, 1, features);
677
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700678 if (features->type != HID_GENERIC) {
679 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
680 if (features->type > TABLETPC) {
681 /* MT Tablet PC touch */
682 wacom_wac->mode_report = 3;
683 wacom_wac->mode_value = 4;
684 } else if (features->type == WACOM_24HDT) {
685 wacom_wac->mode_report = 18;
686 wacom_wac->mode_value = 2;
687 } else if (features->type == WACOM_27QHDT) {
688 wacom_wac->mode_report = 131;
689 wacom_wac->mode_value = 2;
690 } else if (features->type == BAMBOO_PAD) {
691 wacom_wac->mode_report = 2;
692 wacom_wac->mode_value = 2;
693 }
694 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
695 if (features->type <= BAMBOO_PT) {
696 wacom_wac->mode_report = 2;
697 wacom_wac->mode_value = 2;
698 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700699 }
700 }
701
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700702 wacom_set_device_mode(hdev, wacom_wac);
703
704 if (features->type == HID_GENERIC)
705 return wacom_hid_set_device_mode(hdev);
706
Jason Gereckefe494bc2012-10-03 17:25:35 -0700707 return 0;
708}
709
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700710static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700711 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800712{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700713 struct wacom *wacom = hid_get_drvdata(hdev);
714 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800715
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700716 /* default features */
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700717 features->x_fuzz = 4;
718 features->y_fuzz = 4;
719 features->pressure_fuzz = 0;
Jason Gereckebef7e202016-04-22 14:30:53 -0700720 features->distance_fuzz = 1;
721 features->tilt_fuzz = 1;
Ping Chengec67bbe2009-12-15 00:35:24 -0800722
Chris Bagwelld3825d52012-03-25 23:26:11 -0700723 /*
724 * The wireless device HID is basic and layout conflicts with
725 * other tablets (monitor and touch interface can look like pen).
726 * Skip the query for this type and modify defaults based on
727 * interface number.
728 */
Greg Kroah-Hartman720ac462021-12-01 19:35:02 +0100729 if (features->type == WIRELESS && intf) {
Jason Gerecke3f14a632015-08-03 10:17:05 -0700730 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
Jason Gereckeccad85c2015-08-03 10:17:04 -0700731 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700732 else
Jason Gereckeaa86b182015-06-15 18:01:42 -0700733 features->device_type = WACOM_DEVICETYPE_NONE;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700734 return;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700735 }
736
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700737 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800738}
739
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700740struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700741 struct list_head list;
742 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700743 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700744 struct wacom_shared shared;
745};
746
747static LIST_HEAD(wacom_udev_list);
748static DEFINE_MUTEX(wacom_udev_list_lock);
749
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700750static bool wacom_are_sibling(struct hid_device *hdev,
751 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700752{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700753 struct wacom *wacom = hid_get_drvdata(hdev);
754 struct wacom_features *features = &wacom->wacom_wac.features;
Jason Gerecke41372d52016-08-08 12:06:30 -0700755 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
756 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
757 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
758 __u32 oPid = features->oPid ? features->oPid : hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700759
Jason Gerecke41372d52016-08-08 12:06:30 -0700760 /* The defined oVid/oPid must match that of the sibling */
761 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
762 return false;
763 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
764 return false;
765
766 /*
767 * Devices with the same VID/PID must share the same physical
768 * device path, while those with different VID/PID must share
769 * the same physical parent device path.
770 */
771 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
Daniel M. Lambea1a8861f2018-07-17 22:35:36 +0100772 if (!hid_compare_device_paths(hdev, sibling, '/'))
Jason Gerecke41372d52016-08-08 12:06:30 -0700773 return false;
774 } else {
Daniel M. Lambea1a8861f2018-07-17 22:35:36 +0100775 if (!hid_compare_device_paths(hdev, sibling, '.'))
Jason Gerecke41372d52016-08-08 12:06:30 -0700776 return false;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700777 }
778
Jason Gerecke41372d52016-08-08 12:06:30 -0700779 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
780 if (features->type != HID_GENERIC)
781 return true;
782
783 /*
784 * Direct-input devices may not be siblings of indirect-input
785 * devices.
786 */
787 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
788 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700789 return false;
790
Jason Gerecke41372d52016-08-08 12:06:30 -0700791 /*
792 * Indirect-input devices may not be siblings of direct-input
793 * devices.
794 */
795 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
796 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700797 return false;
798
Jason Gerecke41372d52016-08-08 12:06:30 -0700799 /* Pen devices may only be siblings of touch devices */
800 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
801 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
802 return false;
803
804 /* Touch devices may only be siblings of pen devices */
805 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
806 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
807 return false;
808
809 /*
810 * No reason could be found for these two devices to NOT be
811 * siblings, so there's a good chance they ARE siblings
812 */
813 return true;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700814}
815
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700816static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700817{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700818 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700819
Jason Gerecke41372d52016-08-08 12:06:30 -0700820 /* Try to find an already-probed interface from the same device */
821 list_for_each_entry(data, &wacom_udev_list, list) {
Daniel M. Lambea1a8861f2018-07-17 22:35:36 +0100822 if (hid_compare_device_paths(hdev, data->dev, '/')) {
Jason Gerecke2a5e5972017-09-18 09:27:42 -0700823 kref_get(&data->kref);
Jason Gerecke41372d52016-08-08 12:06:30 -0700824 return data;
Jason Gerecke2a5e5972017-09-18 09:27:42 -0700825 }
Jason Gerecke41372d52016-08-08 12:06:30 -0700826 }
827
828 /* Fallback to finding devices that appear to be "siblings" */
Ping Cheng4492eff2010-03-19 22:18:15 -0700829 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700830 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700831 kref_get(&data->kref);
832 return data;
833 }
834 }
835
836 return NULL;
837}
838
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200839static void wacom_release_shared_data(struct kref *kref)
840{
841 struct wacom_hdev_data *data =
842 container_of(kref, struct wacom_hdev_data, kref);
843
844 mutex_lock(&wacom_udev_list_lock);
845 list_del(&data->list);
846 mutex_unlock(&wacom_udev_list_lock);
847
848 kfree(data);
849}
850
851static void wacom_remove_shared_data(void *res)
852{
853 struct wacom *wacom = res;
854 struct wacom_hdev_data *data;
855 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
856
857 if (wacom_wac->shared) {
858 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
859 shared);
860
861 if (wacom_wac->shared->touch == wacom->hdev)
862 wacom_wac->shared->touch = NULL;
863 else if (wacom_wac->shared->pen == wacom->hdev)
864 wacom_wac->shared->pen = NULL;
865
866 kref_put(&data->kref, wacom_release_shared_data);
867 wacom_wac->shared = NULL;
868 }
869}
870
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700871static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700872{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700873 struct wacom *wacom = hid_get_drvdata(hdev);
874 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
875 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700876 int retval = 0;
877
878 mutex_lock(&wacom_udev_list_lock);
879
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700880 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700881 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700882 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700883 if (!data) {
Jason Gereckeb7644592021-10-15 10:28:02 +0800884 mutex_unlock(&wacom_udev_list_lock);
885 return -ENOMEM;
Ping Cheng4492eff2010-03-19 22:18:15 -0700886 }
887
888 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700889 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700890 list_add_tail(&data->list, &wacom_udev_list);
891 }
892
Jason Gereckeb7644592021-10-15 10:28:02 +0800893 mutex_unlock(&wacom_udev_list_lock);
894
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700895 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700896
Cai Huoqing3d422a42021-10-15 10:28:03 +0800897 retval = devm_add_action_or_reset(&hdev->dev, wacom_remove_shared_data, wacom);
898 if (retval)
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200899 return retval;
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200900
Jason Gereckea9ce7852017-01-17 15:38:58 -0800901 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
902 wacom_wac->shared->touch = hdev;
903 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
904 wacom_wac->shared->pen = hdev;
905
Ping Cheng4492eff2010-03-19 22:18:15 -0700906 return retval;
907}
908
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700909static int wacom_led_control(struct wacom *wacom)
910{
911 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700912 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700913 unsigned char report_id = WAC_CMD_LED_CONTROL;
914 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700915
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200916 if (!wacom->led.groups)
917 return -ENOTSUPP;
918
Aaron Armstrong Skomra74aebed62017-08-28 14:15:39 -0700919 if (wacom->wacom_wac.features.type == REMOTE)
920 return -ENOTSUPP;
921
Ping Cheng912ca212014-09-10 12:41:31 -0700922 if (wacom->wacom_wac.pid) { /* wireless connected */
923 report_id = WAC_CMD_WL_LED_CONTROL;
924 buf_size = 13;
925 }
Jason Gerecke4922cd22017-01-25 12:08:37 -0800926 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
927 report_id = WAC_CMD_WL_INTUOSP2;
928 buf_size = 51;
929 }
Ping Cheng912ca212014-09-10 12:41:31 -0700930 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700931 if (!buf)
932 return -ENOMEM;
933
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -0800934 if (wacom->wacom_wac.features.type == HID_GENERIC) {
935 buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
936 buf[1] = wacom->led.llv;
937 buf[2] = wacom->led.groups[0].select & 0x03;
938
939 } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
940 wacom->wacom_wac.features.type <= INTUOSPL)) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700941 /*
942 * Touch Ring and crop mark LED luminance may take on
943 * one of four values:
944 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
945 */
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200946 int ring_led = wacom->led.groups[0].select & 0x03;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700947 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
948 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700949 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700950
Ping Cheng912ca212014-09-10 12:41:31 -0700951 buf[0] = report_id;
952 if (wacom->wacom_wac.pid) {
953 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
954 buf, buf_size, WAC_CMD_RETRIES);
955 buf[0] = report_id;
956 buf[4] = led_bits;
957 } else
958 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700959 }
Jason Gerecke4922cd22017-01-25 12:08:37 -0800960 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
961 buf[0] = report_id;
962 buf[4] = 100; // Power Connection LED (ORANGE)
963 buf[5] = 100; // BT Connection LED (BLUE)
964 buf[6] = 100; // Paper Mode (RED?)
965 buf[7] = 100; // Paper Mode (GREEN?)
966 buf[8] = 100; // Paper Mode (BLUE?)
967 buf[9] = wacom->led.llv;
968 buf[10] = wacom->led.groups[0].select & 0x03;
969 }
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700970 else {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200971 int led = wacom->led.groups[0].select | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700972
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700973 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
974 wacom->wacom_wac.features.type == WACOM_24HD)
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200975 led |= (wacom->led.groups[1].select << 4) | 0x40;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700976
Ping Cheng912ca212014-09-10 12:41:31 -0700977 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700978 buf[1] = led;
979 buf[2] = wacom->led.llv;
980 buf[3] = wacom->led.hlv;
981 buf[4] = wacom->led.img_lum;
982 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700983
Ping Cheng912ca212014-09-10 12:41:31 -0700984 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700985 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700986 kfree(buf);
987
988 return retval;
989}
990
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700991static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
992 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700993{
994 unsigned char *buf;
995 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700996 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700997
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700998 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700999 if (!buf)
1000 return -ENOMEM;
1001
1002 /* Send 'start' command */
1003 buf[0] = WAC_CMD_ICON_START;
1004 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -07001005 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1006 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001007 if (retval < 0)
1008 goto out;
1009
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001010 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001011 buf[1] = button_id & 0x07;
1012 for (i = 0; i < 4; i++) {
1013 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001014 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001015
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001016 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -07001017 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001018 if (retval < 0)
1019 break;
1020 }
1021
1022 /* Send 'stop' */
1023 buf[0] = WAC_CMD_ICON_START;
1024 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -07001025 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1026 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001027
1028out:
1029 kfree(buf);
1030 return retval;
1031}
1032
Ping Cheng09e7d942011-10-04 23:51:14 -07001033static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001034 const char *buf, size_t count)
1035{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001036 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001037 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001038 unsigned int id;
1039 int err;
1040
1041 err = kstrtouint(buf, 10, &id);
1042 if (err)
1043 return err;
1044
1045 mutex_lock(&wacom->lock);
1046
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001047 wacom->led.groups[set_id].select = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001048 err = wacom_led_control(wacom);
1049
1050 mutex_unlock(&wacom->lock);
1051
1052 return err < 0 ? err : count;
1053}
1054
Ping Cheng09e7d942011-10-04 23:51:14 -07001055#define DEVICE_LED_SELECT_ATTR(SET_ID) \
1056static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
1057 struct device_attribute *attr, const char *buf, size_t count) \
1058{ \
1059 return wacom_led_select_store(dev, SET_ID, buf, count); \
1060} \
Ping Cheng04c59ab2011-10-04 23:51:49 -07001061static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
1062 struct device_attribute *attr, char *buf) \
1063{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +08001064 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001065 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -07001066 return scnprintf(buf, PAGE_SIZE, "%d\n", \
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001067 wacom->led.groups[SET_ID].select); \
Ping Cheng04c59ab2011-10-04 23:51:49 -07001068} \
Ping Chenge0984bc2014-09-10 12:40:05 -07001069static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -07001070 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -07001071 wacom_led##SET_ID##_select_store)
1072
1073DEVICE_LED_SELECT_ATTR(0);
1074DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001075
1076static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
1077 const char *buf, size_t count)
1078{
1079 unsigned int value;
1080 int err;
1081
1082 err = kstrtouint(buf, 10, &value);
1083 if (err)
1084 return err;
1085
1086 mutex_lock(&wacom->lock);
1087
1088 *dest = value & 0x7f;
1089 err = wacom_led_control(wacom);
1090
1091 mutex_unlock(&wacom->lock);
1092
1093 return err < 0 ? err : count;
1094}
1095
1096#define DEVICE_LUMINANCE_ATTR(name, field) \
1097static ssize_t wacom_##name##_luminance_store(struct device *dev, \
1098 struct device_attribute *attr, const char *buf, size_t count) \
1099{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +08001100 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001101 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001102 \
1103 return wacom_luminance_store(wacom, &wacom->led.field, \
1104 buf, count); \
1105} \
Ping Cheng37449ad2014-09-10 12:40:30 -07001106static ssize_t wacom_##name##_luminance_show(struct device *dev, \
1107 struct device_attribute *attr, char *buf) \
1108{ \
1109 struct wacom *wacom = dev_get_drvdata(dev); \
1110 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
1111} \
Ping Chenge0984bc2014-09-10 12:40:05 -07001112static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -07001113 wacom_##name##_luminance_show, \
1114 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001115
1116DEVICE_LUMINANCE_ATTR(status0, llv);
1117DEVICE_LUMINANCE_ATTR(status1, hlv);
1118DEVICE_LUMINANCE_ATTR(buttons, img_lum);
1119
1120static ssize_t wacom_button_image_store(struct device *dev, int button_id,
1121 const char *buf, size_t count)
1122{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001123 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001124 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001125 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001126 unsigned len;
1127 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001128
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001129 if (hdev->bus == BUS_BLUETOOTH) {
1130 len = 256;
1131 xfer_id = WAC_CMD_ICON_BT_XFER;
1132 } else {
1133 len = 1024;
1134 xfer_id = WAC_CMD_ICON_XFER;
1135 }
1136
1137 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001138 return -EINVAL;
1139
1140 mutex_lock(&wacom->lock);
1141
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001142 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001143
1144 mutex_unlock(&wacom->lock);
1145
1146 return err < 0 ? err : count;
1147}
1148
1149#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1150static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1151 struct device_attribute *attr, const char *buf, size_t count) \
1152{ \
1153 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1154} \
Ping Chenge0984bc2014-09-10 12:40:05 -07001155static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001156 NULL, wacom_btnimg##BUTTON_ID##_store)
1157
1158DEVICE_BTNIMG_ATTR(0);
1159DEVICE_BTNIMG_ATTR(1);
1160DEVICE_BTNIMG_ATTR(2);
1161DEVICE_BTNIMG_ATTR(3);
1162DEVICE_BTNIMG_ATTR(4);
1163DEVICE_BTNIMG_ATTR(5);
1164DEVICE_BTNIMG_ATTR(6);
1165DEVICE_BTNIMG_ATTR(7);
1166
Ping Cheng09e7d942011-10-04 23:51:14 -07001167static struct attribute *cintiq_led_attrs[] = {
1168 &dev_attr_status_led0_select.attr,
1169 &dev_attr_status_led1_select.attr,
1170 NULL
1171};
1172
Rikard Falkeborn2a6a35a2020-11-25 00:01:09 +01001173static const struct attribute_group cintiq_led_attr_group = {
Ping Cheng09e7d942011-10-04 23:51:14 -07001174 .name = "wacom_led",
1175 .attrs = cintiq_led_attrs,
1176};
1177
1178static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001179 &dev_attr_status0_luminance.attr,
1180 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -07001181 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001182 &dev_attr_buttons_luminance.attr,
1183 &dev_attr_button0_rawimg.attr,
1184 &dev_attr_button1_rawimg.attr,
1185 &dev_attr_button2_rawimg.attr,
1186 &dev_attr_button3_rawimg.attr,
1187 &dev_attr_button4_rawimg.attr,
1188 &dev_attr_button5_rawimg.attr,
1189 &dev_attr_button6_rawimg.attr,
1190 &dev_attr_button7_rawimg.attr,
1191 NULL
1192};
1193
Rikard Falkeborn2a6a35a2020-11-25 00:01:09 +01001194static const struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001195 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -07001196 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001197};
1198
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001199static struct attribute *intuos5_led_attrs[] = {
1200 &dev_attr_status0_luminance.attr,
1201 &dev_attr_status_led0_select.attr,
1202 NULL
1203};
1204
Rikard Falkeborn2a6a35a2020-11-25 00:01:09 +01001205static const struct attribute_group intuos5_led_attr_group = {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001206 .name = "wacom_led",
1207 .attrs = intuos5_led_attrs,
1208};
1209
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001210static struct attribute *generic_led_attrs[] = {
1211 &dev_attr_status0_luminance.attr,
1212 &dev_attr_status_led0_select.attr,
1213 NULL
1214};
1215
Rikard Falkeborn2a6a35a2020-11-25 00:01:09 +01001216static const struct attribute_group generic_led_attr_group = {
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001217 .name = "wacom_led",
1218 .attrs = generic_led_attrs,
1219};
1220
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001221struct wacom_sysfs_group_devres {
Rikard Falkeborn2a6a35a2020-11-25 00:01:09 +01001222 const struct attribute_group *group;
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001223 struct kobject *root;
1224};
1225
1226static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1227{
1228 struct wacom_sysfs_group_devres *devres = res;
1229 struct kobject *kobj = devres->root;
1230
1231 dev_dbg(dev, "%s: dropping reference to %s\n",
1232 __func__, devres->group->name);
1233 sysfs_remove_group(kobj, devres->group);
1234}
1235
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001236static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1237 struct kobject *root,
Rikard Falkeborn2a6a35a2020-11-25 00:01:09 +01001238 const struct attribute_group *group)
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001239{
1240 struct wacom_sysfs_group_devres *devres;
1241 int error;
1242
1243 devres = devres_alloc(wacom_devm_sysfs_group_release,
1244 sizeof(struct wacom_sysfs_group_devres),
1245 GFP_KERNEL);
1246 if (!devres)
1247 return -ENOMEM;
1248
1249 devres->group = group;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001250 devres->root = root;
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001251
1252 error = sysfs_create_group(devres->root, group);
Arvind Yadav097b8f62018-04-24 13:33:03 +05301253 if (error) {
1254 devres_free(devres);
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001255 return error;
Arvind Yadav097b8f62018-04-24 13:33:03 +05301256 }
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001257
1258 devres_add(&wacom->hdev->dev, devres);
1259
1260 return 0;
1261}
1262
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001263static int wacom_devm_sysfs_create_group(struct wacom *wacom,
Rikard Falkeborn2a6a35a2020-11-25 00:01:09 +01001264 const struct attribute_group *group)
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001265{
1266 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1267 group);
1268}
1269
Ping Cheng37309f42020-12-09 20:52:30 -08001270static void wacom_devm_kfifo_release(struct device *dev, void *res)
1271{
1272 struct kfifo_rec_ptr_2 *devres = res;
1273
1274 kfifo_free(devres);
1275}
1276
1277static int wacom_devm_kfifo_alloc(struct wacom *wacom)
1278{
1279 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Jason Gerecke179e8e42021-01-21 10:46:49 -08001280 struct kfifo_rec_ptr_2 *pen_fifo;
Ping Cheng37309f42020-12-09 20:52:30 -08001281 int error;
1282
1283 pen_fifo = devres_alloc(wacom_devm_kfifo_release,
1284 sizeof(struct kfifo_rec_ptr_2),
1285 GFP_KERNEL);
1286
1287 if (!pen_fifo)
1288 return -ENOMEM;
1289
1290 error = kfifo_alloc(pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
1291 if (error) {
1292 devres_free(pen_fifo);
1293 return error;
1294 }
1295
1296 devres_add(&wacom->hdev->dev, pen_fifo);
Jason Gerecke179e8e42021-01-21 10:46:49 -08001297 wacom_wac->pen_fifo = pen_fifo;
Ping Cheng37309f42020-12-09 20:52:30 -08001298
1299 return 0;
1300}
1301
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001302enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001303{
1304 struct wacom *wacom = led->wacom;
1305
1306 if (wacom->led.max_hlv)
1307 return led->hlv * LED_FULL / wacom->led.max_hlv;
1308
1309 if (wacom->led.max_llv)
1310 return led->llv * LED_FULL / wacom->led.max_llv;
1311
1312 /* device doesn't support brightness tuning */
1313 return LED_FULL;
1314}
1315
1316static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1317{
1318 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1319 struct wacom *wacom = led->wacom;
1320
1321 if (wacom->led.groups[led->group].select != led->id)
1322 return LED_OFF;
1323
1324 return wacom_leds_brightness_get(led);
1325}
1326
1327static int wacom_led_brightness_set(struct led_classdev *cdev,
1328 enum led_brightness brightness)
1329{
1330 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1331 struct wacom *wacom = led->wacom;
1332 int error;
1333
1334 mutex_lock(&wacom->lock);
1335
1336 if (!wacom->led.groups || (brightness == LED_OFF &&
1337 wacom->led.groups[led->group].select != led->id)) {
1338 error = 0;
1339 goto out;
1340 }
1341
1342 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1343 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1344
1345 wacom->led.groups[led->group].select = led->id;
1346
1347 error = wacom_led_control(wacom);
1348
1349out:
1350 mutex_unlock(&wacom->lock);
1351
1352 return error;
1353}
1354
1355static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1356 enum led_brightness brightness)
1357{
1358}
1359
1360static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1361 struct wacom_led *led, unsigned int group,
1362 unsigned int id, bool read_only)
1363{
1364 int error;
1365 char *name;
1366
1367 name = devm_kasprintf(dev, GFP_KERNEL,
1368 "%s::wacom-%d.%d",
1369 dev_name(dev),
1370 group,
1371 id);
1372 if (!name)
1373 return -ENOMEM;
1374
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001375 if (!read_only) {
1376 led->trigger.name = name;
1377 error = devm_led_trigger_register(dev, &led->trigger);
1378 if (error) {
1379 hid_err(wacom->hdev,
1380 "failed to register LED trigger %s: %d\n",
1381 led->cdev.name, error);
1382 return error;
1383 }
1384 }
1385
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001386 led->group = group;
1387 led->id = id;
1388 led->wacom = wacom;
1389 led->llv = wacom->led.llv;
1390 led->hlv = wacom->led.hlv;
1391 led->cdev.name = name;
1392 led->cdev.max_brightness = LED_FULL;
1393 led->cdev.flags = LED_HW_PLUGGABLE;
1394 led->cdev.brightness_get = __wacom_led_brightness_get;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001395 if (!read_only) {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001396 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001397 led->cdev.default_trigger = led->cdev.name;
1398 } else {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001399 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001400 }
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001401
1402 error = devm_led_classdev_register(dev, &led->cdev);
1403 if (error) {
1404 hid_err(wacom->hdev,
1405 "failed to register LED %s: %d\n",
1406 led->cdev.name, error);
1407 led->cdev.name = NULL;
1408 return error;
1409 }
1410
1411 return 0;
1412}
1413
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001414static void wacom_led_groups_release_one(void *data)
1415{
1416 struct wacom_group_leds *group = data;
1417
1418 devres_release_group(group->dev, group);
1419}
1420
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001421static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1422 struct wacom *wacom,
1423 int group_id, int count,
1424 bool read_only)
1425{
1426 struct wacom_led *leds;
1427 int i, error;
1428
1429 if (group_id >= wacom->led.count || count <= 0)
1430 return -EINVAL;
1431
1432 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1433 return -ENOMEM;
1434
Kees Cooka86854d2018-06-12 14:07:58 -07001435 leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL);
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001436 if (!leds) {
1437 error = -ENOMEM;
1438 goto err;
1439 }
1440
1441 wacom->led.groups[group_id].leds = leds;
1442 wacom->led.groups[group_id].count = count;
1443
1444 for (i = 0; i < count; i++) {
1445 error = wacom_led_register_one(dev, wacom, &leds[i],
1446 group_id, i, read_only);
1447 if (error)
1448 goto err;
1449 }
1450
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001451 wacom->led.groups[group_id].dev = dev;
1452
1453 devres_close_group(dev, &wacom->led.groups[group_id]);
1454
1455 /*
1456 * There is a bug (?) in devm_led_classdev_register() in which its
1457 * increments the refcount of the parent. If the parent is an input
1458 * device, that means the ref count never reaches 0 when
1459 * devm_input_device_release() gets called.
1460 * This means that the LEDs are still there after disconnect.
1461 * Manually force the release of the group so that the leds are released
1462 * once we are done using them.
1463 */
1464 error = devm_add_action_or_reset(&wacom->hdev->dev,
1465 wacom_led_groups_release_one,
1466 &wacom->led.groups[group_id]);
1467 if (error)
1468 return error;
1469
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001470 return 0;
1471
1472err:
1473 devres_release_group(dev, &wacom->led.groups[group_id]);
1474 return error;
1475}
1476
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001477struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1478 unsigned int id)
1479{
1480 struct wacom_group_leds *group;
1481
1482 if (group_id >= wacom->led.count)
1483 return NULL;
1484
1485 group = &wacom->led.groups[group_id];
1486
1487 if (!group->leds)
1488 return NULL;
1489
1490 id %= group->count;
1491
1492 return &group->leds[id];
1493}
1494
Lee Jones1784cd52021-03-26 14:34:54 +00001495/*
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001496 * wacom_led_next: gives the next available led with a wacom trigger.
1497 *
1498 * returns the next available struct wacom_led which has its default trigger
1499 * or the current one if none is available.
1500 */
1501struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1502{
1503 struct wacom_led *next_led;
1504 int group, next;
1505
1506 if (!wacom || !cur)
1507 return NULL;
1508
1509 group = cur->group;
1510 next = cur->id;
1511
1512 do {
1513 next_led = wacom_led_find(wacom, group, ++next);
1514 if (!next_led || next_led == cur)
1515 return next_led;
1516 } while (next_led->cdev.trigger != &next_led->trigger);
1517
1518 return next_led;
1519}
1520
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001521static void wacom_led_groups_release(void *data)
1522{
1523 struct wacom *wacom = data;
1524
1525 wacom->led.groups = NULL;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001526 wacom->led.count = 0;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001527}
1528
1529static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1530{
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001531 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001532 struct wacom_group_leds *groups;
1533 int error;
1534
Kees Cooka86854d2018-06-12 14:07:58 -07001535 groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds),
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001536 GFP_KERNEL);
1537 if (!groups)
1538 return -ENOMEM;
1539
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001540 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001541 if (error)
1542 return error;
1543
1544 wacom->led.groups = groups;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001545 wacom->led.count = count;
1546
1547 return 0;
1548}
1549
1550static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1551 int led_per_group, bool read_only)
1552{
1553 struct device *dev;
1554 int i, error;
1555
1556 if (!wacom->wacom_wac.pad_input)
1557 return -EINVAL;
1558
1559 dev = &wacom->wacom_wac.pad_input->dev;
1560
1561 error = wacom_led_groups_allocate(wacom, group_count);
1562 if (error)
1563 return error;
1564
1565 for (i = 0; i < group_count; i++) {
1566 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1567 led_per_group,
1568 read_only);
1569 if (error)
1570 return error;
1571 }
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001572
1573 return 0;
1574}
1575
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001576int wacom_initialize_leds(struct wacom *wacom)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001577{
1578 int error;
1579
Jason Gerecke862cf552015-06-15 18:01:43 -07001580 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1581 return 0;
1582
Ping Cheng09e7d942011-10-04 23:51:14 -07001583 /* Initialize default values */
1584 switch (wacom->wacom_wac.features.type) {
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001585 case HID_GENERIC:
1586 if (!wacom->generic_has_leds)
1587 return 0;
1588 wacom->led.llv = 100;
1589 wacom->led.max_llv = 100;
1590
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;
1596 }
1597
1598 error = wacom_devm_sysfs_create_group(wacom,
1599 &generic_led_attr_group);
1600 break;
1601
Jason Gereckea19fc982012-06-12 00:27:53 -07001602 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -07001603 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -07001604 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -07001605 case INTUOS4L:
Ping Chengf4fa9a62011-10-04 23:49:42 -07001606 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001607 wacom->led.hlv = 20;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001608 wacom->led.max_llv = 127;
1609 wacom->led.max_hlv = 127;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001610 wacom->led.img_lum = 10;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001611
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001612 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001613 if (error) {
1614 hid_err(wacom->hdev,
1615 "cannot create leds err: %d\n", error);
1616 return error;
1617 }
1618
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001619 error = wacom_devm_sysfs_create_group(wacom,
1620 &intuos4_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001621 break;
1622
Jason Gerecke246835f2011-12-12 00:12:04 -08001623 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -07001624 case WACOM_21UX2:
Ping Cheng09e7d942011-10-04 23:51:14 -07001625 wacom->led.llv = 0;
1626 wacom->led.hlv = 0;
1627 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001628
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001629 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001630 if (error) {
1631 hid_err(wacom->hdev,
1632 "cannot create leds err: %d\n", error);
1633 return error;
1634 }
1635
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001636 error = wacom_devm_sysfs_create_group(wacom,
1637 &cintiq_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001638 break;
1639
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001640 case INTUOS5S:
1641 case INTUOS5:
1642 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -07001643 case INTUOSPS:
1644 case INTUOSPM:
1645 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -07001646 wacom->led.llv = 32;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001647 wacom->led.max_llv = 96;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001648
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001649 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001650 if (error) {
1651 hid_err(wacom->hdev,
1652 "cannot create leds err: %d\n", error);
1653 return error;
1654 }
1655
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001656 error = wacom_devm_sysfs_create_group(wacom,
1657 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001658 break;
1659
Jason Gerecke4922cd22017-01-25 12:08:37 -08001660 case INTUOSP2_BT:
1661 wacom->led.llv = 50;
1662 wacom->led.max_llv = 100;
1663 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1664 if (error) {
1665 hid_err(wacom->hdev,
1666 "cannot create leds err: %d\n", error);
1667 return error;
1668 }
1669 return 0;
1670
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001671 case REMOTE:
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001672 wacom->led.llv = 255;
1673 wacom->led.max_llv = 255;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001674 error = wacom_led_groups_allocate(wacom, 5);
1675 if (error) {
1676 hid_err(wacom->hdev,
1677 "cannot create leds err: %d\n", error);
1678 return error;
1679 }
1680 return 0;
1681
Ping Cheng09e7d942011-10-04 23:51:14 -07001682 default:
1683 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001684 }
1685
Ping Cheng09e7d942011-10-04 23:51:14 -07001686 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -07001687 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -07001688 "cannot create sysfs group err: %d\n", error);
1689 return error;
1690 }
Ping Cheng09e7d942011-10-04 23:51:14 -07001691
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001692 return 0;
1693}
1694
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01001695static void wacom_init_work(struct work_struct *work)
1696{
1697 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1698
1699 _wacom_query_tablet_data(wacom);
1700 wacom_led_control(wacom);
1701}
1702
1703static void wacom_query_tablet_data(struct wacom *wacom)
1704{
1705 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1706}
1707
Chris Bagwella1d552c2012-03-25 23:26:30 -07001708static enum power_supply_property wacom_battery_props[] = {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001709 POWER_SUPPLY_PROP_MODEL_NAME,
Jason Gerecke71fa6412015-03-11 10:25:41 -07001710 POWER_SUPPLY_PROP_PRESENT,
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001711 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001712 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001713 POWER_SUPPLY_PROP_CAPACITY
1714};
1715
1716static int wacom_battery_get_property(struct power_supply *psy,
1717 enum power_supply_property psp,
1718 union power_supply_propval *val)
1719{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001720 struct wacom_battery *battery = power_supply_get_drvdata(psy);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001721 int ret = 0;
1722
1723 switch (psp) {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001724 case POWER_SUPPLY_PROP_MODEL_NAME:
1725 val->strval = battery->wacom->wacom_wac.name;
1726 break;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001727 case POWER_SUPPLY_PROP_PRESENT:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001728 val->intval = battery->bat_connected;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001729 break;
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001730 case POWER_SUPPLY_PROP_SCOPE:
1731 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1732 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001733 case POWER_SUPPLY_PROP_CAPACITY:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001734 val->intval = battery->battery_capacity;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001735 break;
1736 case POWER_SUPPLY_PROP_STATUS:
Jason Gerecke16e45982017-04-28 09:25:33 -07001737 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1738 val->intval = battery->bat_status;
1739 else if (battery->bat_charging)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001740 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001741 else if (battery->battery_capacity == 100 &&
1742 battery->ps_connected)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001743 val->intval = POWER_SUPPLY_STATUS_FULL;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001744 else if (battery->ps_connected)
Jason Gereckeb0882cb2015-03-06 11:47:43 -08001745 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001746 else
1747 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001748 break;
1749 default:
1750 ret = -EINVAL;
1751 break;
1752 }
1753
1754 return ret;
1755}
1756
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001757static int __wacom_initialize_battery(struct wacom *wacom,
1758 struct wacom_battery *battery)
Chris Bagwella1d552c2012-03-25 23:26:30 -07001759{
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001760 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001761 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001762 struct power_supply_config psy_cfg = { .drv_data = battery, };
Benjamin Tissoires136ae5e2016-07-13 18:06:16 +02001763 struct power_supply *ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001764 struct power_supply_desc *bat_desc = &battery->bat_desc;
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001765 unsigned long n;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001766 int error;
1767
1768 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1769 return -ENOMEM;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001770
Benjamin Tissoires99569532016-07-13 18:06:17 +02001771 battery->wacom = wacom;
1772
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001773 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001774
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001775 bat_desc->properties = wacom_battery_props;
1776 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1777 bat_desc->get_property = wacom_battery_get_property;
1778 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1779 bat_desc->name = battery->bat_name;
Benjamin Tissoires96983292016-07-13 18:06:15 +02001780 bat_desc->type = POWER_SUPPLY_TYPE_USB;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001781 bat_desc->use_for_apm = 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001782
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001783 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1784 if (IS_ERR(ps_bat)) {
1785 error = PTR_ERR(ps_bat);
1786 goto err;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001787 }
1788
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001789 power_supply_powers(ps_bat, &wacom->hdev->dev);
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001790
1791 battery->battery = ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001792
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001793 devres_close_group(dev, bat_desc);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001794 return 0;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001795
1796err:
1797 devres_release_group(dev, bat_desc);
1798 return error;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001799}
1800
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001801static int wacom_initialize_battery(struct wacom *wacom)
1802{
1803 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1804 return __wacom_initialize_battery(wacom, &wacom->battery);
1805
1806 return 0;
1807}
1808
Chris Bagwella1d552c2012-03-25 23:26:30 -07001809static void wacom_destroy_battery(struct wacom *wacom)
1810{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001811 if (wacom->battery.battery) {
1812 devres_release_group(&wacom->hdev->dev,
1813 &wacom->battery.bat_desc);
1814 wacom->battery.battery = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001815 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001816}
1817
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001818static ssize_t wacom_show_speed(struct device *dev,
1819 struct device_attribute
1820 *attr, char *buf)
1821{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001822 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001823 struct wacom *wacom = hid_get_drvdata(hdev);
1824
Jiapeng Chongd8833122021-02-02 16:19:00 +08001825 return sysfs_emit(buf, "%i\n", wacom->wacom_wac.bt_high_speed);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001826}
1827
1828static ssize_t wacom_store_speed(struct device *dev,
1829 struct device_attribute *attr,
1830 const char *buf, size_t count)
1831{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001832 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001833 struct wacom *wacom = hid_get_drvdata(hdev);
1834 u8 new_speed;
1835
1836 if (kstrtou8(buf, 0, &new_speed))
1837 return -EINVAL;
1838
1839 if (new_speed != 0 && new_speed != 1)
1840 return -EINVAL;
1841
1842 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1843
1844 return count;
1845}
1846
Ping Chenge0984bc2014-09-10 12:40:05 -07001847static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001848 wacom_show_speed, wacom_store_speed);
1849
Aaron Skomra72b236d2015-08-20 16:05:17 -07001850
1851static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1852 struct kobj_attribute *kattr,
1853 char *buf, int index)
1854{
Geliang Tang2cf83832015-12-27 17:25:24 +08001855 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001856 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001857 struct wacom *wacom = hid_get_drvdata(hdev);
1858 u8 mode;
1859
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001860 mode = wacom->led.groups[index].select;
Christos Gkekasbc35f732017-07-08 20:25:48 +01001861 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001862}
1863
1864#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1865static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1866 struct kobj_attribute *kattr, char *buf) \
1867{ \
1868 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1869} \
1870static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1871 .attr = {.name = "remote_mode", \
1872 .mode = DEV_ATTR_RO_PERM}, \
1873 .show = wacom_show_remote##SET_ID##_mode, \
1874}; \
1875static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1876 &remote##SET_ID##_mode_attr.attr, \
1877 NULL \
1878}; \
Rikard Falkeborn2a6a35a2020-11-25 00:01:09 +01001879static const struct attribute_group remote##SET_ID##_serial_group = { \
Aaron Skomra72b236d2015-08-20 16:05:17 -07001880 .name = NULL, \
1881 .attrs = remote##SET_ID##_serial_attrs, \
1882}
1883
1884DEVICE_EKR_ATTR_GROUP(0);
1885DEVICE_EKR_ATTR_GROUP(1);
1886DEVICE_EKR_ATTR_GROUP(2);
1887DEVICE_EKR_ATTR_GROUP(3);
1888DEVICE_EKR_ATTR_GROUP(4);
1889
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02001890static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1891 int index)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001892{
1893 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001894 struct wacom_remote *remote = wacom->remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001895
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001896 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001897 GFP_KERNEL,
1898 "%d", serial);
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001899 if (!remote->remotes[index].group.name)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001900 return -ENOMEM;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001901
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001902 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001903 &remote->remotes[index].group);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001904 if (error) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001905 remote->remotes[index].group.name = NULL;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001906 hid_err(wacom->hdev,
1907 "cannot create sysfs group err: %d\n", error);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001908 return error;
1909 }
1910
1911 return 0;
1912}
1913
Aaron Skomra72b236d2015-08-20 16:05:17 -07001914static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1915{
1916 const size_t buf_size = 2;
1917 unsigned char *buf;
1918 int retval;
1919
1920 buf = kzalloc(buf_size, GFP_KERNEL);
1921 if (!buf)
1922 return -ENOMEM;
1923
1924 buf[0] = WAC_CMD_DELETE_PAIRING;
1925 buf[1] = selector;
1926
1927 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1928 buf_size, WAC_CMD_RETRIES);
1929 kfree(buf);
1930
1931 return retval;
1932}
1933
1934static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1935 struct kobj_attribute *attr,
1936 const char *buf, size_t count)
1937{
1938 unsigned char selector = 0;
Geliang Tang2cf83832015-12-27 17:25:24 +08001939 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001940 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001941 struct wacom *wacom = hid_get_drvdata(hdev);
1942 int err;
1943
1944 if (!strncmp(buf, "*\n", 2)) {
1945 selector = WAC_CMD_UNPAIR_ALL;
1946 } else {
1947 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1948 buf);
1949 return -1;
1950 }
1951
1952 mutex_lock(&wacom->lock);
1953
1954 err = wacom_cmd_unpair_remote(wacom, selector);
1955 mutex_unlock(&wacom->lock);
1956
1957 return err < 0 ? err : count;
1958}
1959
1960static struct kobj_attribute unpair_remote_attr = {
1961 .attr = {.name = "unpair_remote", .mode = 0200},
1962 .store = wacom_store_unpair_remote,
1963};
1964
1965static const struct attribute *remote_unpair_attrs[] = {
1966 &unpair_remote_attr.attr,
1967 NULL
1968};
1969
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001970static void wacom_remotes_destroy(void *data)
1971{
1972 struct wacom *wacom = data;
1973 struct wacom_remote *remote = wacom->remote;
1974
1975 if (!remote)
1976 return;
1977
1978 kobject_put(remote->remote_dir);
1979 kfifo_free(&remote->remote_fifo);
1980 wacom->remote = NULL;
1981}
1982
1983static int wacom_initialize_remotes(struct wacom *wacom)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001984{
1985 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001986 struct wacom_remote *remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001987 int i;
1988
1989 if (wacom->wacom_wac.features.type != REMOTE)
1990 return 0;
1991
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001992 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1993 GFP_KERNEL);
1994 if (!remote)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001995 return -ENOMEM;
1996
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001997 wacom->remote = remote;
1998
1999 spin_lock_init(&remote->remote_lock);
2000
2001 error = kfifo_alloc(&remote->remote_fifo,
2002 5 * sizeof(struct wacom_remote_data),
2003 GFP_KERNEL);
2004 if (error) {
2005 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
2006 return -ENOMEM;
2007 }
2008
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002009 remote->remotes[0].group = remote0_serial_group;
2010 remote->remotes[1].group = remote1_serial_group;
2011 remote->remotes[2].group = remote2_serial_group;
2012 remote->remotes[3].group = remote3_serial_group;
2013 remote->remotes[4].group = remote4_serial_group;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002014
2015 remote->remote_dir = kobject_create_and_add("wacom_remote",
2016 &wacom->hdev->dev.kobj);
2017 if (!remote->remote_dir)
2018 return -ENOMEM;
2019
2020 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
Aaron Skomra72b236d2015-08-20 16:05:17 -07002021
2022 if (error) {
2023 hid_err(wacom->hdev,
2024 "cannot create sysfs group err: %d\n", error);
2025 return error;
2026 }
2027
2028 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02002029 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002030 remote->remotes[i].serial = 0;
Aaron Skomra72b236d2015-08-20 16:05:17 -07002031 }
2032
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002033 error = devm_add_action_or_reset(&wacom->hdev->dev,
2034 wacom_remotes_destroy, wacom);
2035 if (error)
2036 return error;
2037
Aaron Skomra72b236d2015-08-20 16:05:17 -07002038 return 0;
2039}
2040
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002041static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002042{
2043 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07002044 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002045 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002046
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002047 input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002048 if (!input_dev)
2049 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002050
Jason Gerecke2bdd1632015-07-13 18:03:45 -07002051 input_dev->name = wacom_wac->features.name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07002052 input_dev->phys = hdev->phys;
2053 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002054 input_dev->open = wacom_open;
2055 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07002056 input_dev->uniq = hdev->uniq;
2057 input_dev->id.bustype = hdev->bus;
2058 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04002059 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07002060 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002061 input_set_drvdata(input_dev, wacom);
2062
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002063 return input_dev;
2064}
2065
Jason Gerecked9f2d202015-07-13 18:03:44 -07002066static int wacom_allocate_inputs(struct wacom *wacom)
2067{
2068 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2069
2070 wacom_wac->pen_input = wacom_allocate_input(wacom);
2071 wacom_wac->touch_input = wacom_allocate_input(wacom);
2072 wacom_wac->pad_input = wacom_allocate_input(wacom);
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002073 if (!wacom_wac->pen_input ||
2074 !wacom_wac->touch_input ||
2075 !wacom_wac->pad_input)
Jason Gerecked9f2d202015-07-13 18:03:44 -07002076 return -ENOMEM;
Jason Gerecked9f2d202015-07-13 18:03:44 -07002077
Jason Gerecke2bdd1632015-07-13 18:03:45 -07002078 wacom_wac->pen_input->name = wacom_wac->pen_name;
Jason Gerecked9f2d202015-07-13 18:03:44 -07002079 wacom_wac->touch_input->name = wacom_wac->touch_name;
2080 wacom_wac->pad_input->name = wacom_wac->pad_name;
2081
2082 return 0;
2083}
2084
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07002085static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002086{
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002087 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002088 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Jason Gerecke2636a3f2015-06-15 18:01:44 -07002089 int error = 0;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002090
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002091 pen_input_dev = wacom_wac->pen_input;
2092 touch_input_dev = wacom_wac->touch_input;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04002093 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002094
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002095 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04002096 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002097
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002098 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
2099 if (error) {
2100 /* no pen in use on this interface */
2101 input_free_device(pen_input_dev);
2102 wacom_wac->pen_input = NULL;
2103 pen_input_dev = NULL;
2104 } else {
2105 error = input_register_device(pen_input_dev);
Ping Cheng30ebc1a2014-11-18 13:29:16 -08002106 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002107 goto fail;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002108 }
2109
2110 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
2111 if (error) {
2112 /* no touch in use on this interface */
2113 input_free_device(touch_input_dev);
2114 wacom_wac->touch_input = NULL;
2115 touch_input_dev = NULL;
2116 } else {
2117 error = input_register_device(touch_input_dev);
2118 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002119 goto fail;
Ping Cheng30ebc1a2014-11-18 13:29:16 -08002120 }
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002121
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002122 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2123 if (error) {
2124 /* no pad in use on this interface */
2125 input_free_device(pad_input_dev);
2126 wacom_wac->pad_input = NULL;
2127 pad_input_dev = NULL;
2128 } else {
2129 error = input_register_device(pad_input_dev);
2130 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002131 goto fail;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002132 }
2133
Ping Cheng19635182012-04-29 21:09:18 -07002134 return 0;
2135
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002136fail:
2137 wacom_wac->pad_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002138 wacom_wac->touch_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002139 wacom_wac->pen_input = NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002140 return error;
2141}
2142
Jason Gerecke0be01712015-08-05 15:44:53 -07002143/*
2144 * Not all devices report physical dimensions from HID.
2145 * Compute the default from hardcoded logical dimension
2146 * and resolution before driver overwrites them.
2147 */
2148static void wacom_set_default_phy(struct wacom_features *features)
2149{
2150 if (features->x_resolution) {
2151 features->x_phy = (features->x_max * 100) /
2152 features->x_resolution;
2153 features->y_phy = (features->y_max * 100) /
2154 features->y_resolution;
2155 }
2156}
2157
2158static void wacom_calculate_res(struct wacom_features *features)
2159{
2160 /* set unit to "100th of a mm" for devices not reported by HID */
2161 if (!features->unit) {
2162 features->unit = 0x11;
2163 features->unitExpo = -3;
2164 }
2165
2166 features->x_resolution = wacom_calc_hid_res(features->x_max,
2167 features->x_phy,
2168 features->unit,
2169 features->unitExpo);
2170 features->y_resolution = wacom_calc_hid_res(features->y_max,
2171 features->y_phy,
2172 features->unit,
2173 features->unitExpo);
2174}
2175
Jason Gereckefce99572015-03-06 11:47:40 -08002176void wacom_battery_work(struct work_struct *work)
2177{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002178 struct wacom *wacom = container_of(work, struct wacom, battery_work);
Jason Gereckefce99572015-03-06 11:47:40 -08002179
2180 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02002181 !wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08002182 wacom_initialize_battery(wacom);
2183 }
2184 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02002185 wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08002186 wacom_destroy_battery(wacom);
2187 }
2188}
2189
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002190static size_t wacom_compute_pktlen(struct hid_device *hdev)
2191{
2192 struct hid_report_enum *report_enum;
2193 struct hid_report *report;
2194 size_t size = 0;
2195
2196 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2197
2198 list_for_each_entry(report, &report_enum->report_list, list) {
Mathieu Magnaudetdabb05c62014-11-27 16:02:36 +01002199 size_t report_size = hid_report_len(report);
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002200 if (report_size > size)
2201 size = report_size;
2202 }
2203
2204 return size;
2205}
2206
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002207static void wacom_update_name(struct wacom *wacom, const char *suffix)
Ping Chengc24eab42015-04-24 15:32:51 -07002208{
2209 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2210 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke073b50b2019-08-16 11:54:26 -07002211 char name[WACOM_NAME_MAX - 20]; /* Leave some room for suffixes */
Ping Chengc24eab42015-04-24 15:32:51 -07002212
2213 /* Generic devices name unspecified */
2214 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
Jason Gerecke09dc28a2017-07-24 09:46:19 -07002215 char *product_name = wacom->hdev->name;
Ping Chengc24eab42015-04-24 15:32:51 -07002216
Greg Kroah-Hartmanf83baa02021-12-01 19:35:01 +01002217 if (hid_is_usb(wacom->hdev)) {
Jason Gerecke09dc28a2017-07-24 09:46:19 -07002218 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2219 struct usb_device *dev = interface_to_usbdev(intf);
2220 product_name = dev->product;
Ping Chengc24eab42015-04-24 15:32:51 -07002221 }
Jason Gerecke09dc28a2017-07-24 09:46:19 -07002222
2223 if (wacom->hdev->bus == BUS_I2C) {
2224 snprintf(name, sizeof(name), "%s %X",
2225 features->name, wacom->hdev->product);
2226 } else if (strstr(product_name, "Wacom") ||
2227 strstr(product_name, "wacom") ||
2228 strstr(product_name, "WACOM")) {
2229 strlcpy(name, product_name, sizeof(name));
2230 } else {
2231 snprintf(name, sizeof(name), "Wacom %s", product_name);
2232 }
2233
2234 /* strip out excess whitespaces */
2235 while (1) {
2236 char *gap = strstr(name, " ");
2237 if (gap == NULL)
2238 break;
2239 /* shift everything including the terminator */
2240 memmove(gap, gap+1, strlen(gap));
2241 }
2242
2243 /* get rid of trailing whitespace */
2244 if (name[strlen(name)-1] == ' ')
2245 name[strlen(name)-1] = '\0';
Ping Chengc24eab42015-04-24 15:32:51 -07002246 } else {
Jason Gerecke44b52502015-06-15 18:01:41 -07002247 strlcpy(name, features->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07002248 }
2249
Benjamin Tissoires99569532016-07-13 18:06:17 +02002250 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2251 name, suffix);
2252
Ping Chengc24eab42015-04-24 15:32:51 -07002253 /* Append the device type to the name */
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002254 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002255 "%s%s Pen", name, suffix);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002256 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002257 "%s%s Finger", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07002258 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002259 "%s%s Pad", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07002260}
2261
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002262static void wacom_release_resources(struct wacom *wacom)
2263{
2264 struct hid_device *hdev = wacom->hdev;
2265
2266 if (!wacom->resources)
2267 return;
2268
2269 devres_release_group(&hdev->dev, wacom);
2270
2271 wacom->resources = false;
2272
2273 wacom->wacom_wac.pen_input = NULL;
2274 wacom->wacom_wac.touch_input = NULL;
2275 wacom->wacom_wac.pad_input = NULL;
2276}
2277
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002278static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2279{
2280 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2281 wacom_wac->shared->type = wacom_wac->features.type;
2282 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2283 }
2284
Ping Chengd793ff82017-02-14 21:27:45 -08002285 if (wacom_wac->has_mute_touch_switch) {
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002286 wacom_wac->shared->has_mute_touch_switch = true;
Ping Chengdc9dc862021-07-19 13:55:29 -07002287 /* Hardware touch switch may be off. Wait until
2288 * we know the switch state to decide is_touch_on.
2289 * Softkey state should be initialized to "on" to
2290 * match historic default.
2291 */
2292 if (wacom_wac->is_soft_touch_switch)
2293 wacom_wac->shared->is_touch_on = true;
Ping Chengd793ff82017-02-14 21:27:45 -08002294 }
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002295
2296 if (wacom_wac->shared->has_mute_touch_switch &&
2297 wacom_wac->shared->touch_input) {
2298 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2299 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2300 SW_MUTE_DEVICE);
2301 }
2302}
2303
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002304static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
Ping Cheng3bea7332006-07-13 18:01:36 -07002305{
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002306 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2307 struct wacom_features *features = &wacom_wac->features;
2308 struct hid_device *hdev = wacom->hdev;
Jason Childse33da8a2010-02-17 22:38:31 -08002309 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002310 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07002311
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002312 features->pktlen = wacom_compute_pktlen(hdev);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002313 if (features->pktlen > WACOM_PKGLEN_MAX)
2314 return -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07002315
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002316 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2317 return -ENOMEM;
2318
2319 wacom->resources = true;
2320
Jason Gerecke3f14a632015-08-03 10:17:05 -07002321 error = wacom_allocate_inputs(wacom);
2322 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002323 goto fail;
Benjamin Tissoires494078b2014-09-23 12:08:07 -04002324
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05002325 /*
2326 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2327 * into debug mode for the touch part.
2328 * We ignore the other interfaces.
2329 */
2330 if (features->type == BAMBOO_PAD) {
2331 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2332 features->type = HID_GENERIC;
2333 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2334 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2335 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002336 goto fail;
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05002337 }
2338 }
2339
Ping Cheng401d7d12013-07-28 00:38:54 -07002340 /* set the default size in case we do not get them from hid */
2341 wacom_set_default_phy(features);
2342
Ping Chengf393ee22012-04-29 21:09:17 -07002343 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07002344 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng42f4f272015-04-15 16:53:54 -07002345 wacom_setup_device_quirks(wacom);
Jason Gerecke042628a2015-04-30 17:51:54 -07002346
Jason Gereckeaa86b182015-06-15 18:01:42 -07002347 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2348 features->type != WIRELESS) {
Jason Gerecke8e116d32015-04-30 17:51:55 -07002349 error = features->type == HID_GENERIC ? -ENODEV : 0;
2350
Jason Gerecke042628a2015-04-30 17:51:54 -07002351 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
Jason Gerecke8e116d32015-04-30 17:51:55 -07002352 hdev->name,
2353 error ? "Ignoring" : "Assuming pen");
2354
2355 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002356 goto fail;
Jason Gerecke042628a2015-04-30 17:51:54 -07002357
Jason Gereckeaa86b182015-06-15 18:01:42 -07002358 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -07002359 }
2360
Ping Cheng401d7d12013-07-28 00:38:54 -07002361 wacom_calculate_res(features);
2362
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002363 wacom_update_name(wacom, wireless ? " (WL)" : "");
Ping Cheng4492eff2010-03-19 22:18:15 -07002364
Aaron Armstrong Skomra8b407352017-03-29 10:35:39 -07002365 /* pen only Bamboo neither support touch nor pad */
2366 if ((features->type == BAMBOO_PEN) &&
2367 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2368 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2369 error = -ENODEV;
2370 goto fail;
2371 }
2372
Jason Gereckea9ce7852017-01-17 15:38:58 -08002373 error = wacom_add_shared_data(hdev);
2374 if (error)
2375 goto fail;
Ping Cheng49b764a2010-02-20 00:53:49 -08002376
Jason Gereckeccad85c2015-08-03 10:17:04 -07002377 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002378 (features->quirks & WACOM_QUIRK_BATTERY)) {
2379 error = wacom_initialize_battery(wacom);
2380 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002381 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002382 }
2383
Jason Gerecke3f14a632015-08-03 10:17:05 -07002384 error = wacom_register_inputs(wacom);
2385 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002386 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002387
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002388 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002389 error = wacom_initialize_leds(wacom);
2390 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002391 goto fail;
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002392
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002393 error = wacom_initialize_remotes(wacom);
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002394 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002395 goto fail;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002396 }
2397
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002398 if (features->type == HID_GENERIC)
2399 connect_mask |= HID_CONNECT_DRIVER;
2400
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002401 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002402 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002403 if (error) {
2404 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002405 goto fail;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002406 }
2407
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002408 if (!wireless) {
2409 /* Note that if query fails it is not a hard failure */
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002410 wacom_query_tablet_data(wacom);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002411 }
Jason Gerecke86e88f02015-11-03 16:57:58 -08002412
2413 /* touch only Bamboo doesn't support pen */
2414 if ((features->type == BAMBOO_TOUCH) &&
2415 (features->device_type & WACOM_DEVICETYPE_PEN)) {
Aaron Armstrong Skomra4d20c332017-03-29 11:41:28 -07002416 cancel_delayed_work_sync(&wacom->init_work);
2417 _wacom_query_tablet_data(wacom);
Jason Gerecke86e88f02015-11-03 16:57:58 -08002418 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002419 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08002420 }
2421
Jason Gereckeccad85c2015-08-03 10:17:04 -07002422 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002423 error = hid_hw_open(hdev);
2424
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002425 wacom_set_shared_values(wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002426 devres_close_group(&hdev->dev, wacom);
2427
Ping Cheng3bea7332006-07-13 18:01:36 -07002428 return 0;
2429
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002430fail_quirks:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002431 hid_hw_stop(hdev);
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002432fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002433 wacom_release_resources(wacom);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002434 return error;
2435}
2436
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002437static void wacom_wireless_work(struct work_struct *work)
2438{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002439 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002440 struct usb_device *usbdev = wacom->usbdev;
2441 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2442 struct hid_device *hdev1, *hdev2;
2443 struct wacom *wacom1, *wacom2;
2444 struct wacom_wac *wacom_wac1, *wacom_wac2;
2445 int error;
2446
2447 /*
2448 * Regardless if this is a disconnect or a new tablet,
2449 * remove any existing input and battery devices.
2450 */
2451
2452 wacom_destroy_battery(wacom);
2453
Greg Kroah-Hartman720ac462021-12-01 19:35:02 +01002454 if (!usbdev)
2455 return;
2456
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002457 /* Stylus interface */
2458 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2459 wacom1 = hid_get_drvdata(hdev1);
2460 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002461 wacom_release_resources(wacom1);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002462
2463 /* Touch interface */
2464 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2465 wacom2 = hid_get_drvdata(hdev2);
2466 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002467 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002468
2469 if (wacom_wac->pid == 0) {
2470 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002471 } else {
2472 const struct hid_device_id *id = wacom_ids;
2473
2474 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2475 wacom_wac->pid);
2476
2477 while (id->bus) {
2478 if (id->vendor == USB_VENDOR_ID_WACOM &&
2479 id->product == wacom_wac->pid)
2480 break;
2481 id++;
2482 }
2483
2484 if (!id->bus) {
2485 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2486 return;
2487 }
2488
2489 /* Stylus interface */
2490 wacom_wac1->features =
2491 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002492
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002493 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002494 hid_hw_stop(hdev1);
2495 error = wacom_parse_and_register(wacom1, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002496 if (error)
2497 goto fail;
2498
2499 /* Touch interface */
2500 if (wacom_wac1->features.touch_max ||
2501 (wacom_wac1->features.type >= INTUOSHT &&
2502 wacom_wac1->features.type <= BAMBOO_PT)) {
2503 wacom_wac2->features =
2504 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002505 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002506 hid_hw_stop(hdev2);
2507 error = wacom_parse_and_register(wacom2, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002508 if (error)
2509 goto fail;
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002510 }
2511
Benjamin Tissoires99569532016-07-13 18:06:17 +02002512 strlcpy(wacom_wac->name, wacom_wac1->name,
2513 sizeof(wacom_wac->name));
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002514 error = wacom_initialize_battery(wacom);
2515 if (error)
2516 goto fail;
2517 }
2518
2519 return;
2520
2521fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002522 wacom_release_resources(wacom1);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002523 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002524 return;
2525}
2526
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002527static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2528{
2529 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002530 u32 serial = remote->remotes[index].serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002531 int i;
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002532 unsigned long flags;
2533
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002534 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002535 if (remote->remotes[i].serial == serial) {
Aaron Armstrong Skomra791ae272017-12-07 12:31:56 -08002536
2537 spin_lock_irqsave(&remote->remote_lock, flags);
2538 remote->remotes[i].registered = false;
2539 spin_unlock_irqrestore(&remote->remote_lock, flags);
2540
2541 if (remote->remotes[i].battery.battery)
2542 devres_release_group(&wacom->hdev->dev,
2543 &remote->remotes[i].battery.bat_desc);
2544
2545 if (remote->remotes[i].group.name)
2546 devres_release_group(&wacom->hdev->dev,
2547 &remote->remotes[i]);
2548
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002549 remote->remotes[i].serial = 0;
2550 remote->remotes[i].group.name = NULL;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002551 remote->remotes[i].battery.battery = NULL;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002552 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2553 }
2554 }
2555}
2556
2557static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2558 unsigned int index)
2559{
2560 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002561 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002562 int error, k;
2563
2564 /* A remote can pair more than once with an EKR,
2565 * check to make sure this serial isn't already paired.
2566 */
2567 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002568 if (remote->remotes[k].serial == serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002569 break;
2570 }
2571
2572 if (k < WACOM_MAX_REMOTES) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002573 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002574 return 0;
2575 }
2576
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002577 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002578 return -ENOMEM;
2579
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002580 error = wacom_remote_create_attr_group(wacom, serial, index);
2581 if (error)
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002582 goto fail;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002583
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002584 remote->remotes[index].input = wacom_allocate_input(wacom);
2585 if (!remote->remotes[index].input) {
2586 error = -ENOMEM;
2587 goto fail;
2588 }
2589 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2590 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2591
2592 if (!remote->remotes[index].input->name) {
2593 error = -EINVAL;
2594 goto fail;
2595 }
2596
2597 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2598 &wacom->wacom_wac);
2599 if (error)
2600 goto fail;
2601
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002602 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002603
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002604 error = input_register_device(remote->remotes[index].input);
2605 if (error)
2606 goto fail;
2607
Benjamin Tissoires97f55412016-07-13 18:06:10 +02002608 error = wacom_led_groups_alloc_and_register_one(
2609 &remote->remotes[index].input->dev,
2610 wacom, index, 3, true);
2611 if (error)
2612 goto fail;
2613
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002614 remote->remotes[index].registered = true;
2615
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002616 devres_close_group(dev, &remote->remotes[index]);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002617 return 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002618
2619fail:
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002620 devres_release_group(dev, &remote->remotes[index]);
2621 remote->remotes[index].serial = 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002622 return error;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002623}
2624
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002625static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2626{
2627 struct wacom_remote *remote = wacom->remote;
2628 int error;
2629
2630 if (!remote->remotes[index].registered)
2631 return 0;
2632
2633 if (remote->remotes[index].battery.battery)
2634 return 0;
2635
2636 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2637 return 0;
2638
2639 error = __wacom_initialize_battery(wacom,
2640 &wacom->remote->remotes[index].battery);
2641 if (error)
2642 return error;
2643
2644 return 0;
2645}
2646
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002647static void wacom_remote_work(struct work_struct *work)
2648{
2649 struct wacom *wacom = container_of(work, struct wacom, remote_work);
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002650 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002651 struct wacom_remote_data data;
2652 unsigned long flags;
2653 unsigned int count;
2654 u32 serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002655 int i;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002656
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002657 spin_lock_irqsave(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002658
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002659 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002660
2661 if (count != sizeof(data)) {
2662 hid_err(wacom->hdev,
2663 "workitem triggered without status available\n");
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002664 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002665 return;
2666 }
2667
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002668 if (!kfifo_is_empty(&remote->remote_fifo))
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002669 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2670
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002671 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002672
2673 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2674 serial = data.remote[i].serial;
2675 if (data.remote[i].connected) {
2676
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002677 if (remote->remotes[i].serial == serial) {
2678 wacom_remote_attach_battery(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002679 continue;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002680 }
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002681
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002682 if (remote->remotes[i].serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002683 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002684
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002685 wacom_remote_create_one(wacom, serial, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002686
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002687 } else if (remote->remotes[i].serial) {
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002688 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002689 }
2690 }
2691}
2692
Benjamin Tissoires4082da82017-02-14 21:27:18 -08002693static void wacom_mode_change_work(struct work_struct *work)
2694{
2695 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2696 struct wacom_shared *shared = wacom->wacom_wac.shared;
2697 struct wacom *wacom1 = NULL;
2698 struct wacom *wacom2 = NULL;
2699 bool is_direct = wacom->wacom_wac.is_direct_mode;
2700 int error = 0;
2701
2702 if (shared->pen) {
2703 wacom1 = hid_get_drvdata(shared->pen);
2704 wacom_release_resources(wacom1);
2705 hid_hw_stop(wacom1->hdev);
2706 wacom1->wacom_wac.has_mode_change = true;
2707 wacom1->wacom_wac.is_direct_mode = is_direct;
2708 }
2709
2710 if (shared->touch) {
2711 wacom2 = hid_get_drvdata(shared->touch);
2712 wacom_release_resources(wacom2);
2713 hid_hw_stop(wacom2->hdev);
2714 wacom2->wacom_wac.has_mode_change = true;
2715 wacom2->wacom_wac.is_direct_mode = is_direct;
2716 }
2717
2718 if (wacom1) {
2719 error = wacom_parse_and_register(wacom1, false);
2720 if (error)
2721 return;
2722 }
2723
2724 if (wacom2) {
2725 error = wacom_parse_and_register(wacom2, false);
2726 if (error)
2727 return;
2728 }
2729
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002730 return;
2731}
2732
2733static int wacom_probe(struct hid_device *hdev,
2734 const struct hid_device_id *id)
2735{
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002736 struct wacom *wacom;
2737 struct wacom_wac *wacom_wac;
2738 struct wacom_features *features;
2739 int error;
2740
2741 if (!id->driver_data)
2742 return -EINVAL;
2743
2744 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2745
2746 /* hid-core sets this quirk for the boot interface */
2747 hdev->quirks &= ~HID_QUIRK_NOGET;
2748
Benjamin Tissoires19b64332016-07-13 18:05:58 +02002749 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002750 if (!wacom)
2751 return -ENOMEM;
2752
2753 hid_set_drvdata(hdev, wacom);
2754 wacom->hdev = hdev;
2755
2756 wacom_wac = &wacom->wacom_wac;
2757 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2758 features = &wacom_wac->features;
2759
Benjamin Tissoires362c5712019-08-12 18:27:40 +02002760 if (features->check_for_hid_type && features->hid_type != hdev->type)
2761 return -ENODEV;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002762
Ping Cheng37309f42020-12-09 20:52:30 -08002763 error = wacom_devm_kfifo_alloc(wacom);
Jason Gerecke83417202017-11-10 11:50:01 -08002764 if (error)
Benjamin Tissoires362c5712019-08-12 18:27:40 +02002765 return error;
Jason Gerecke83417202017-11-10 11:50:01 -08002766
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002767 wacom_wac->hid_data.inputmode = -1;
Jason Gerecke326ea2a2016-04-04 11:26:52 -07002768 wacom_wac->mode_report = -1;
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002769
Greg Kroah-Hartman720ac462021-12-01 19:35:02 +01002770 if (hid_is_usb(hdev)) {
2771 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2772 struct usb_device *dev = interface_to_usbdev(intf);
2773
2774 wacom->usbdev = dev;
2775 wacom->intf = intf;
2776 }
2777
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002778 mutex_init(&wacom->lock);
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002779 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002780 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2781 INIT_WORK(&wacom->battery_work, wacom_battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002782 INIT_WORK(&wacom->remote_work, wacom_remote_work);
Benjamin Tissoires4082da82017-02-14 21:27:18 -08002783 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002784
2785 /* ask for the report descriptor to be loaded by HID */
2786 error = hid_parse(hdev);
2787 if (error) {
2788 hid_err(hdev, "parse failed\n");
Benjamin Tissoires362c5712019-08-12 18:27:40 +02002789 return error;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002790 }
2791
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002792 error = wacom_parse_and_register(wacom, false);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002793 if (error)
Benjamin Tissoires362c5712019-08-12 18:27:40 +02002794 return error;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002795
2796 if (hdev->bus == BUS_BLUETOOTH) {
2797 error = device_create_file(&hdev->dev, &dev_attr_speed);
2798 if (error)
2799 hid_warn(hdev,
2800 "can't create sysfs speed attribute err: %d\n",
2801 error);
2802 }
2803
Jason Gerecke5bed0122021-07-19 13:55:30 -07002804 wacom_wac->probe_complete = true;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002805 return 0;
Ping Cheng3bea7332006-07-13 18:01:36 -07002806}
2807
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002808static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07002809{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002810 struct wacom *wacom = hid_get_drvdata(hdev);
Benjamin Tissoiresf6205162016-02-12 17:27:45 +01002811 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2812 struct wacom_features *features = &wacom_wac->features;
2813
2814 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2815 hid_hw_close(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07002816
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002817 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002818
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002819 cancel_delayed_work_sync(&wacom->init_work);
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002820 cancel_work_sync(&wacom->wireless_work);
2821 cancel_work_sync(&wacom->battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002822 cancel_work_sync(&wacom->remote_work);
Benjamin Tissoires4082da82017-02-14 21:27:18 -08002823 cancel_work_sync(&wacom->mode_change_work);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002824 if (hdev->bus == BUS_BLUETOOTH)
2825 device_remove_file(&hdev->dev, &dev_attr_speed);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002826
Benjamin Tissoiresc0265a942017-01-20 16:20:12 +01002827 /* make sure we don't trigger the LEDs */
2828 wacom_led_groups_release(wacom);
Aaron Armstrong Skomrab6b1f192017-03-06 10:54:58 -08002829
2830 if (wacom->wacom_wac.features.type != REMOTE)
2831 wacom_release_resources(wacom);
Oliver Neukume7224092008-04-15 01:31:57 -04002832}
2833
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002834#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002835static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002836{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002837 struct wacom *wacom = hid_get_drvdata(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002838
2839 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002840
2841 /* switch to wacom mode first */
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002842 _wacom_query_tablet_data(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002843 wacom_led_control(wacom);
2844
Oliver Neukume7224092008-04-15 01:31:57 -04002845 mutex_unlock(&wacom->lock);
2846
2847 return 0;
2848}
2849
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002850static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002851{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002852 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002853}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002854#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04002855
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002856static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07002857 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08002858 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07002859 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002860 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002861 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002862#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04002863 .resume = wacom_resume,
2864 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002865#endif
2866 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07002867};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002868module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07002869
2870MODULE_VERSION(DRIVER_VERSION);
2871MODULE_AUTHOR(DRIVER_AUTHOR);
2872MODULE_DESCRIPTION(DRIVER_DESC);
Grant Grundler7021b602017-01-05 11:07:04 -08002873MODULE_LICENSE("GPL");