blob: 83dd3a2a73163982c74877fe2d75a3f91947fa80 [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,
91 struct hid_report *report, u8 *raw_data, int size)
92{
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)
150 wacom_wac_queue_flush(hdev, &wacom_wac->pen_fifo);
151 else if (insert)
152 wacom_wac_queue_insert(hdev, &wacom_wac->pen_fifo, raw_data, size);
153
154 return insert && !flush;
155}
156
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700157static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
158 u8 *raw_data, int size)
Ping Cheng3bea7332006-07-13 18:01:36 -0700159{
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700160 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700161
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700162 if (size > WACOM_PKGLEN_MAX)
163 return 1;
Ping Cheng3bea7332006-07-13 18:01:36 -0700164
Jason Gerecke83417202017-11-10 11:50:01 -0800165 if (wacom_wac_pen_serial_enforce(hdev, report, raw_data, size))
166 return -1;
167
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700168 memcpy(wacom->wacom_wac.data, raw_data, size);
Ping Cheng3bea7332006-07-13 18:01:36 -0700169
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700170 wacom_wac_irq(&wacom->wacom_wac, size);
171
172 return 0;
Ping Cheng3bea7332006-07-13 18:01:36 -0700173}
174
Ping Cheng3bea7332006-07-13 18:01:36 -0700175static int wacom_open(struct input_dev *dev)
176{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400177 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700178
Benjamin Tissoiresdff67412014-12-01 11:52:40 -0500179 return hid_hw_open(wacom->hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700180}
181
182static void wacom_close(struct input_dev *dev)
183{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400184 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700185
Benjamin Tissoires3dad1882016-07-13 18:05:54 +0200186 /*
187 * wacom->hdev should never be null, but surprisingly, I had the case
188 * once while unplugging the Wacom Wireless Receiver.
189 */
190 if (wacom->hdev)
191 hid_hw_close(wacom->hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700192}
193
Chris Bagwell16bf2882012-03-25 23:26:20 -0700194/*
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700195 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
Jason Gerecke115d5e12012-10-03 17:24:32 -0700196 */
197static int wacom_calc_hid_res(int logical_extents, int physical_extents,
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700198 unsigned unit, int exponent)
Jason Gerecke115d5e12012-10-03 17:24:32 -0700199{
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700200 struct hid_field field = {
201 .logical_maximum = logical_extents,
202 .physical_maximum = physical_extents,
203 .unit = unit,
204 .unit_exponent = exponent,
205 };
Jason Gerecke115d5e12012-10-03 17:24:32 -0700206
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700207 return hidinput_calc_abs_res(&field, ABS_X);
Jason Gerecke115d5e12012-10-03 17:24:32 -0700208}
209
Jason Gerecke57832512018-06-25 13:24:35 -0700210static void wacom_hid_usage_quirk(struct hid_device *hdev,
211 struct hid_field *field, struct hid_usage *usage)
212{
213 struct wacom *wacom = hid_get_drvdata(hdev);
214 struct wacom_features *features = &wacom->wacom_wac.features;
215 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
216
217 /*
218 * The Dell Canvas 27 needs to be switched to its vendor-defined
219 * report to provide the best resolution.
220 */
221 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
222 hdev->product == 0x4200 &&
223 field->application == HID_UP_MSVENDOR) {
224 wacom->wacom_wac.mode_report = field->report->id;
225 wacom->wacom_wac.mode_value = 2;
226 }
227
228 /*
229 * ISDv4 devices which predate HID's adoption of the
230 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
231 * position instead. We can accurately detect if a
232 * usage with that value should be HID_DG_BARRELSWITCH2
233 * based on the surrounding usages, which have remained
234 * constant across generations.
235 */
236 if (features->type == HID_GENERIC &&
237 usage->hid == 0x000D0000 &&
238 field->application == HID_DG_PEN &&
239 field->physical == HID_DG_STYLUS) {
240 int i = usage->usage_index;
241
242 if (i-4 >= 0 && i+1 < field->maxusage &&
243 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
244 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
245 field->usage[i-2].hid == HID_DG_ERASER &&
246 field->usage[i-1].hid == HID_DG_INVERT &&
247 field->usage[i+1].hid == HID_DG_INRANGE) {
248 usage->hid = HID_DG_BARRELSWITCH2;
249 }
250 }
251
Jason Gereckee9fe0d42019-01-24 11:09:44 -0800252 /*
253 * Wacom's AES devices use different vendor-defined usages to
254 * report serial number information compared to their branded
255 * hardware. The usages are also sometimes ill-defined and do
256 * not have the correct logical min/max values set. Lets patch
257 * the descriptor to use the branded usage convention and fix
258 * the errors.
259 */
260 if (usage->hid == WACOM_HID_WT_SERIALNUMBER &&
261 field->report_size == 16 &&
262 field->index + 2 < field->report->maxfield) {
263 struct hid_field *a = field->report->field[field->index + 1];
264 struct hid_field *b = field->report->field[field->index + 2];
265
266 if (a->maxusage > 0 &&
267 a->usage[0].hid == HID_DG_TOOLSERIALNUMBER &&
268 a->report_size == 32 &&
269 b->maxusage > 0 &&
270 b->usage[0].hid == 0xFF000000 &&
271 b->report_size == 8) {
272 features->quirks |= WACOM_QUIRK_AESPEN;
273 usage->hid = WACOM_HID_WD_TOOLTYPE;
274 field->logical_minimum = S16_MIN;
275 field->logical_maximum = S16_MAX;
276 a->logical_minimum = S32_MIN;
277 a->logical_maximum = S32_MAX;
278 b->usage[0].hid = WACOM_HID_WD_SERIALHI;
279 b->logical_minimum = 0;
280 b->logical_maximum = U8_MAX;
281 }
282 }
283
Jason Gerecke57832512018-06-25 13:24:35 -0700284 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
285 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
286 hdev->product == 0x0358 &&
287 WACOM_PEN_FIELD(field) &&
288 equivalent_usage == HID_GD_Y) {
289 field->logical_maximum = 43200;
290 }
291}
292
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700293static void wacom_feature_mapping(struct hid_device *hdev,
294 struct hid_field *field, struct hid_usage *usage)
Chris Bagwell41343612011-10-26 22:32:52 -0700295{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700296 struct wacom *wacom = hid_get_drvdata(hdev);
297 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400298 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
Aaron Armstrong Skomraac2423c2017-01-25 12:08:40 -0800299 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400300 u8 *data;
301 int ret;
Aaron Ma3064a032018-02-03 23:57:15 +0800302 u32 n;
Chris Bagwell41343612011-10-26 22:32:52 -0700303
Jason Gerecke57832512018-06-25 13:24:35 -0700304 wacom_hid_usage_quirk(hdev, field, usage);
305
Aaron Armstrong Skomraac2423c2017-01-25 12:08:40 -0800306 switch (equivalent_usage) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700307 case HID_DG_CONTACTMAX:
308 /* leave touch_max as is if predefined */
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400309 if (!features->touch_max) {
310 /* read manually */
311 data = kzalloc(2, GFP_KERNEL);
312 if (!data)
313 break;
314 data[0] = field->report->id;
315 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700316 data, 2, WAC_CMD_RETRIES);
317 if (ret == 2) {
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400318 features->touch_max = data[1];
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700319 } else {
320 features->touch_max = 16;
321 hid_warn(hdev, "wacom_feature_mapping: "
322 "could not get HID_DG_CONTACTMAX, "
323 "defaulting to %d\n",
324 features->touch_max);
325 }
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400326 kfree(data);
327 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700328 break;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400329 case HID_DG_INPUTMODE:
330 /* Ignore if value index is out of bounds. */
331 if (usage->usage_index >= field->report_count) {
332 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
333 break;
334 }
335
336 hid_data->inputmode = field->report->id;
337 hid_data->inputmode_index = usage->usage_index;
338 break;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700339
340 case HID_UP_DIGITIZER:
341 if (field->report->id == 0x0B &&
Jason Gerecke8de82282016-10-19 18:03:37 -0700342 (field->application == WACOM_HID_G9_PEN ||
343 field->application == WACOM_HID_G11_PEN)) {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700344 wacom->wacom_wac.mode_report = field->report->id;
345 wacom->wacom_wac.mode_value = 0;
346 }
347 break;
348
Jason Gereckec9c09582016-10-19 18:03:43 -0700349 case WACOM_HID_WD_DATAMODE:
350 wacom->wacom_wac.mode_report = field->report->id;
351 wacom->wacom_wac.mode_value = 2;
352 break;
353
Jason Gerecke8de82282016-10-19 18:03:37 -0700354 case WACOM_HID_UP_G9:
355 case WACOM_HID_UP_G11:
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700356 if (field->report->id == 0x03 &&
Jason Gerecke8de82282016-10-19 18:03:37 -0700357 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
358 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700359 wacom->wacom_wac.mode_report = field->report->id;
360 wacom->wacom_wac.mode_value = 0;
361 }
362 break;
Jason Gerecke345857b2016-10-19 18:03:50 -0700363 case WACOM_HID_WD_OFFSETLEFT:
364 case WACOM_HID_WD_OFFSETTOP:
365 case WACOM_HID_WD_OFFSETRIGHT:
366 case WACOM_HID_WD_OFFSETBOTTOM:
367 /* read manually */
368 n = hid_report_len(field->report);
369 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
370 if (!data)
371 break;
372 data[0] = field->report->id;
373 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
374 data, n, WAC_CMD_RETRIES);
375 if (ret == n) {
376 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
377 data, n, 0);
378 } else {
379 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
380 __func__);
381 }
382 kfree(data);
383 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700384 }
385}
386
Chris Bagwell428f8582011-10-26 22:26:59 -0700387/*
388 * Interface Descriptor of wacom devices can be incomplete and
389 * inconsistent so wacom_features table is used to store stylus
390 * device's packet lengths, various maximum values, and tablet
391 * resolution based on product ID's.
392 *
393 * For devices that contain 2 interfaces, wacom_features table is
394 * inaccurate for the touch interface. Since the Interface Descriptor
395 * for touch interfaces has pretty complete data, this function exists
396 * to query tablet for this missing information instead of hard coding in
397 * an additional table.
398 *
399 * A typical Interface Descriptor for a stylus will contain a
400 * boot mouse application collection that is not of interest and this
401 * function will ignore it.
402 *
403 * It also contains a digitizer application collection that also is not
404 * of interest since any information it contains would be duplicate
405 * of what is in wacom_features. Usually it defines a report of an array
406 * of bytes that could be used as max length of the stylus packet returned.
407 * If it happens to define a Digitizer-Stylus Physical Collection then
408 * the X and Y logical values contain valid data but it is ignored.
409 *
410 * A typical Interface Descriptor for a touch interface will contain a
411 * Digitizer-Finger Physical Collection which will define both logical
412 * X/Y maximum as well as the physical size of tablet. Since touch
413 * interfaces haven't supported pressure or distance, this is enough
414 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700415 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700416 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
417 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700418 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700419static void wacom_usage_mapping(struct hid_device *hdev,
420 struct hid_field *field, struct hid_usage *usage)
421{
422 struct wacom *wacom = hid_get_drvdata(hdev);
423 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoiresd97a5522015-01-05 16:32:12 -0500424 bool finger = WACOM_FINGER_FIELD(field);
425 bool pen = WACOM_PEN_FIELD(field);
Ping Cheng418b5732018-06-25 13:24:36 -0700426 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700427
428 /*
429 * Requiring Stylus Usage will ignore boot mouse
430 * X/Y values and some cases of invalid Digitizer X/Y
431 * values commonly reported.
432 */
Jason Gerecke042628a2015-04-30 17:51:54 -0700433 if (pen)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700434 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -0700435 else if (finger)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700436 features->device_type |= WACOM_DEVICETYPE_TOUCH;
Jason Gerecke042628a2015-04-30 17:51:54 -0700437 else
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700438 return;
439
Jason Gerecke57832512018-06-25 13:24:35 -0700440 wacom_hid_usage_quirk(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700441
Ping Cheng418b5732018-06-25 13:24:36 -0700442 switch (equivalent_usage) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700443 case HID_GD_X:
444 features->x_max = field->logical_maximum;
445 if (finger) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700446 features->x_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700447 if ((features->type != BAMBOO_PT) &&
448 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700449 features->unit = field->unit;
450 features->unitExpo = field->unit_exponent;
451 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700452 }
453 break;
454 case HID_GD_Y:
455 features->y_max = field->logical_maximum;
456 if (finger) {
457 features->y_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700458 if ((features->type != BAMBOO_PT) &&
459 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700460 features->unit = field->unit;
461 features->unitExpo = field->unit_exponent;
462 }
463 }
464 break;
465 case HID_DG_TIPPRESSURE:
466 if (pen)
467 features->pressure_max = field->logical_maximum;
468 break;
469 }
Benjamin Tissoires7704ac92014-09-23 12:08:08 -0400470
471 if (features->type == HID_GENERIC)
472 wacom_wac_usage_mapping(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700473}
474
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800475static void wacom_post_parse_hid(struct hid_device *hdev,
476 struct wacom_features *features)
477{
478 struct wacom *wacom = hid_get_drvdata(hdev);
479 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
480
481 if (features->type == HID_GENERIC) {
482 /* Any last-minute generic device setup */
Benjamin Tissoires4082da82017-02-14 21:27:18 -0800483 if (wacom_wac->has_mode_change) {
484 if (wacom_wac->is_direct_mode)
485 features->device_type |= WACOM_DEVICETYPE_DIRECT;
486 else
487 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
488 }
489
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800490 if (features->touch_max > 1) {
Aaron Armstrong Skomraac2423c2017-01-25 12:08:40 -0800491 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
492 input_mt_init_slots(wacom_wac->touch_input,
493 wacom_wac->features.touch_max,
494 INPUT_MT_DIRECT);
495 else
496 input_mt_init_slots(wacom_wac->touch_input,
497 wacom_wac->features.touch_max,
498 INPUT_MT_POINTER);
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800499 }
500 }
501}
502
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700503static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800504 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500505{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700506 struct hid_report_enum *rep_enum;
507 struct hid_report *hreport;
508 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500509
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700510 /* check features first */
511 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
512 list_for_each_entry(hreport, &rep_enum->report_list, list) {
513 for (i = 0; i < hreport->maxfield; i++) {
514 /* Ignore if report count is out of bounds. */
515 if (hreport->field[i]->report_count < 1)
516 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500517
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700518 for (j = 0; j < hreport->field[i]->maxusage; j++) {
519 wacom_feature_mapping(hdev, hreport->field[i],
520 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500521 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500522 }
523 }
524
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700525 /* now check the input usages */
526 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
527 list_for_each_entry(hreport, &rep_enum->report_list, list) {
528
529 if (!hreport->maxfield)
530 continue;
531
532 for (i = 0; i < hreport->maxfield; i++)
533 for (j = 0; j < hreport->field[i]->maxusage; j++)
534 wacom_usage_mapping(hdev, hreport->field[i],
535 hreport->field[i]->usage + j);
536 }
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800537
538 wacom_post_parse_hid(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -0500539}
540
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400541static int wacom_hid_set_device_mode(struct hid_device *hdev)
542{
543 struct wacom *wacom = hid_get_drvdata(hdev);
544 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
545 struct hid_report *r;
546 struct hid_report_enum *re;
547
548 if (hid_data->inputmode < 0)
549 return 0;
550
551 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
552 r = re->report_id_hash[hid_data->inputmode];
553 if (r) {
554 r->field[0]->value[hid_data->inputmode_index] = 2;
555 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
556 }
557 return 0;
558}
559
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700560static int wacom_set_device_mode(struct hid_device *hdev,
561 struct wacom_wac *wacom_wac)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700562{
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700563 u8 *rep_data;
564 struct hid_report *r;
565 struct hid_report_enum *re;
Aaron Ma3064a032018-02-03 23:57:15 +0800566 u32 length;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700567 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700568
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700569 if (wacom_wac->mode_report < 0)
570 return 0;
571
572 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
573 r = re->report_id_hash[wacom_wac->mode_report];
574 if (!r)
575 return -EINVAL;
576
577 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700578 if (!rep_data)
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700579 return -ENOMEM;
580
581 length = hid_report_len(r);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700582
Jason Gereckefe494bc2012-10-03 17:25:35 -0700583 do {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700584 rep_data[0] = wacom_wac->mode_report;
585 rep_data[1] = wacom_wac->mode_value;
Chris Bagwell9937c022013-01-23 19:37:34 -0800586
Przemo Firszt296b7372014-08-06 14:00:38 -0700587 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
588 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700589 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700590 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700591 rep_data, length, 1);
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700592 } while (error >= 0 &&
593 rep_data[1] != wacom_wac->mode_report &&
594 limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700595
596 kfree(rep_data);
597
598 return error < 0 ? error : 0;
599}
600
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700601static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
602 struct wacom_features *features)
603{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700604 struct wacom *wacom = hid_get_drvdata(hdev);
605 int ret;
606 u8 rep_data[2];
607
608 switch (features->type) {
609 case GRAPHIRE_BT:
610 rep_data[0] = 0x03;
611 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700612 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
613 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700614
615 if (ret >= 0) {
616 rep_data[0] = speed == 0 ? 0x05 : 0x06;
617 rep_data[1] = 0x00;
618
619 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700620 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700621
622 if (ret >= 0) {
623 wacom->wacom_wac.bt_high_speed = speed;
624 return 0;
625 }
626 }
627
628 /*
629 * Note that if the raw queries fail, it's not a hard failure
630 * and it is safe to continue
631 */
632 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
633 rep_data[0], ret);
634 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700635 case INTUOS4WL:
636 if (speed == 1)
637 wacom->wacom_wac.bt_features &= ~0x20;
638 else
639 wacom->wacom_wac.bt_features |= 0x20;
640
641 rep_data[0] = 0x03;
642 rep_data[1] = wacom->wacom_wac.bt_features;
643
Przemo Firszt296b7372014-08-06 14:00:38 -0700644 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
645 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700646 if (ret >= 0)
647 wacom->wacom_wac.bt_high_speed = speed;
648 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700649 }
650
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700651 return 0;
652}
653
Jason Gereckefe494bc2012-10-03 17:25:35 -0700654/*
655 * Switch the tablet into its most-capable mode. Wacom tablets are
656 * typically configured to power-up in a mode which sends mouse-like
657 * reports to the OS. To get absolute position, pressure data, etc.
658 * from the tablet, it is necessary to switch the tablet out of this
659 * mode and into one which sends the full range of tablet data.
660 */
Benjamin Tissoiresa544c612017-01-20 16:20:13 +0100661static int _wacom_query_tablet_data(struct wacom *wacom)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700662{
Benjamin Tissoiresa544c612017-01-20 16:20:13 +0100663 struct hid_device *hdev = wacom->hdev;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700664 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoiresa544c612017-01-20 16:20:13 +0100665 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700666
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700667 if (hdev->bus == BUS_BLUETOOTH)
668 return wacom_bt_query_tablet_data(hdev, 1, features);
669
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700670 if (features->type != HID_GENERIC) {
671 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
672 if (features->type > TABLETPC) {
673 /* MT Tablet PC touch */
674 wacom_wac->mode_report = 3;
675 wacom_wac->mode_value = 4;
676 } else if (features->type == WACOM_24HDT) {
677 wacom_wac->mode_report = 18;
678 wacom_wac->mode_value = 2;
679 } else if (features->type == WACOM_27QHDT) {
680 wacom_wac->mode_report = 131;
681 wacom_wac->mode_value = 2;
682 } else if (features->type == BAMBOO_PAD) {
683 wacom_wac->mode_report = 2;
684 wacom_wac->mode_value = 2;
685 }
686 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
687 if (features->type <= BAMBOO_PT) {
688 wacom_wac->mode_report = 2;
689 wacom_wac->mode_value = 2;
690 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700691 }
692 }
693
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700694 wacom_set_device_mode(hdev, wacom_wac);
695
696 if (features->type == HID_GENERIC)
697 return wacom_hid_set_device_mode(hdev);
698
Jason Gereckefe494bc2012-10-03 17:25:35 -0700699 return 0;
700}
701
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700702static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700703 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800704{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700705 struct wacom *wacom = hid_get_drvdata(hdev);
706 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800707
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700708 /* default features */
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700709 features->x_fuzz = 4;
710 features->y_fuzz = 4;
711 features->pressure_fuzz = 0;
Jason Gereckebef7e202016-04-22 14:30:53 -0700712 features->distance_fuzz = 1;
713 features->tilt_fuzz = 1;
Ping Chengec67bbe2009-12-15 00:35:24 -0800714
Chris Bagwelld3825d52012-03-25 23:26:11 -0700715 /*
716 * The wireless device HID is basic and layout conflicts with
717 * other tablets (monitor and touch interface can look like pen).
718 * Skip the query for this type and modify defaults based on
719 * interface number.
720 */
721 if (features->type == WIRELESS) {
Jason Gerecke3f14a632015-08-03 10:17:05 -0700722 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
Jason Gereckeccad85c2015-08-03 10:17:04 -0700723 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700724 else
Jason Gereckeaa86b182015-06-15 18:01:42 -0700725 features->device_type = WACOM_DEVICETYPE_NONE;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700726 return;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700727 }
728
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700729 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800730}
731
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700732struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700733 struct list_head list;
734 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700735 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700736 struct wacom_shared shared;
737};
738
739static LIST_HEAD(wacom_udev_list);
740static DEFINE_MUTEX(wacom_udev_list_lock);
741
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700742static bool wacom_are_sibling(struct hid_device *hdev,
743 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700744{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700745 struct wacom *wacom = hid_get_drvdata(hdev);
746 struct wacom_features *features = &wacom->wacom_wac.features;
Jason Gerecke41372d52016-08-08 12:06:30 -0700747 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
748 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
749 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
750 __u32 oPid = features->oPid ? features->oPid : hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700751
Jason Gerecke41372d52016-08-08 12:06:30 -0700752 /* The defined oVid/oPid must match that of the sibling */
753 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
754 return false;
755 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
756 return false;
757
758 /*
759 * Devices with the same VID/PID must share the same physical
760 * device path, while those with different VID/PID must share
761 * the same physical parent device path.
762 */
763 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
Daniel M. Lambea1a8861f2018-07-17 22:35:36 +0100764 if (!hid_compare_device_paths(hdev, sibling, '/'))
Jason Gerecke41372d52016-08-08 12:06:30 -0700765 return false;
766 } else {
Daniel M. Lambea1a8861f2018-07-17 22:35:36 +0100767 if (!hid_compare_device_paths(hdev, sibling, '.'))
Jason Gerecke41372d52016-08-08 12:06:30 -0700768 return false;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700769 }
770
Jason Gerecke41372d52016-08-08 12:06:30 -0700771 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
772 if (features->type != HID_GENERIC)
773 return true;
774
775 /*
776 * Direct-input devices may not be siblings of indirect-input
777 * devices.
778 */
779 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
780 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700781 return false;
782
Jason Gerecke41372d52016-08-08 12:06:30 -0700783 /*
784 * Indirect-input devices may not be siblings of direct-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 /* Pen devices may only be siblings of touch devices */
792 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
793 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
794 return false;
795
796 /* Touch devices may only be siblings of pen devices */
797 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
798 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
799 return false;
800
801 /*
802 * No reason could be found for these two devices to NOT be
803 * siblings, so there's a good chance they ARE siblings
804 */
805 return true;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700806}
807
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700808static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700809{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700810 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700811
Jason Gerecke41372d52016-08-08 12:06:30 -0700812 /* Try to find an already-probed interface from the same device */
813 list_for_each_entry(data, &wacom_udev_list, list) {
Daniel M. Lambea1a8861f2018-07-17 22:35:36 +0100814 if (hid_compare_device_paths(hdev, data->dev, '/')) {
Jason Gerecke2a5e5972017-09-18 09:27:42 -0700815 kref_get(&data->kref);
Jason Gerecke41372d52016-08-08 12:06:30 -0700816 return data;
Jason Gerecke2a5e5972017-09-18 09:27:42 -0700817 }
Jason Gerecke41372d52016-08-08 12:06:30 -0700818 }
819
820 /* Fallback to finding devices that appear to be "siblings" */
Ping Cheng4492eff2010-03-19 22:18:15 -0700821 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700822 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700823 kref_get(&data->kref);
824 return data;
825 }
826 }
827
828 return NULL;
829}
830
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200831static void wacom_release_shared_data(struct kref *kref)
832{
833 struct wacom_hdev_data *data =
834 container_of(kref, struct wacom_hdev_data, kref);
835
836 mutex_lock(&wacom_udev_list_lock);
837 list_del(&data->list);
838 mutex_unlock(&wacom_udev_list_lock);
839
840 kfree(data);
841}
842
843static void wacom_remove_shared_data(void *res)
844{
845 struct wacom *wacom = res;
846 struct wacom_hdev_data *data;
847 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
848
849 if (wacom_wac->shared) {
850 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
851 shared);
852
853 if (wacom_wac->shared->touch == wacom->hdev)
854 wacom_wac->shared->touch = NULL;
855 else if (wacom_wac->shared->pen == wacom->hdev)
856 wacom_wac->shared->pen = NULL;
857
858 kref_put(&data->kref, wacom_release_shared_data);
859 wacom_wac->shared = NULL;
860 }
861}
862
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700863static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700864{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700865 struct wacom *wacom = hid_get_drvdata(hdev);
866 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
867 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700868 int retval = 0;
869
870 mutex_lock(&wacom_udev_list_lock);
871
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700872 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700873 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700874 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700875 if (!data) {
876 retval = -ENOMEM;
877 goto out;
878 }
879
880 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700881 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700882 list_add_tail(&data->list, &wacom_udev_list);
883 }
884
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700885 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700886
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200887 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
888 if (retval) {
889 mutex_unlock(&wacom_udev_list_lock);
890 wacom_remove_shared_data(wacom);
891 return retval;
892 }
893
Jason Gereckea9ce7852017-01-17 15:38:58 -0800894 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
895 wacom_wac->shared->touch = hdev;
896 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
897 wacom_wac->shared->pen = hdev;
898
Ping Cheng4492eff2010-03-19 22:18:15 -0700899out:
900 mutex_unlock(&wacom_udev_list_lock);
901 return retval;
902}
903
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700904static int wacom_led_control(struct wacom *wacom)
905{
906 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700907 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700908 unsigned char report_id = WAC_CMD_LED_CONTROL;
909 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700910
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200911 if (!wacom->led.groups)
912 return -ENOTSUPP;
913
Aaron Armstrong Skomra74aebed62017-08-28 14:15:39 -0700914 if (wacom->wacom_wac.features.type == REMOTE)
915 return -ENOTSUPP;
916
Ping Cheng912ca212014-09-10 12:41:31 -0700917 if (wacom->wacom_wac.pid) { /* wireless connected */
918 report_id = WAC_CMD_WL_LED_CONTROL;
919 buf_size = 13;
920 }
Jason Gerecke4922cd22017-01-25 12:08:37 -0800921 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
922 report_id = WAC_CMD_WL_INTUOSP2;
923 buf_size = 51;
924 }
Ping Cheng912ca212014-09-10 12:41:31 -0700925 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700926 if (!buf)
927 return -ENOMEM;
928
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -0800929 if (wacom->wacom_wac.features.type == HID_GENERIC) {
930 buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
931 buf[1] = wacom->led.llv;
932 buf[2] = wacom->led.groups[0].select & 0x03;
933
934 } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
935 wacom->wacom_wac.features.type <= INTUOSPL)) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700936 /*
937 * Touch Ring and crop mark LED luminance may take on
938 * one of four values:
939 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
940 */
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200941 int ring_led = wacom->led.groups[0].select & 0x03;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700942 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
943 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700944 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700945
Ping Cheng912ca212014-09-10 12:41:31 -0700946 buf[0] = report_id;
947 if (wacom->wacom_wac.pid) {
948 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
949 buf, buf_size, WAC_CMD_RETRIES);
950 buf[0] = report_id;
951 buf[4] = led_bits;
952 } else
953 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700954 }
Jason Gerecke4922cd22017-01-25 12:08:37 -0800955 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
956 buf[0] = report_id;
957 buf[4] = 100; // Power Connection LED (ORANGE)
958 buf[5] = 100; // BT Connection LED (BLUE)
959 buf[6] = 100; // Paper Mode (RED?)
960 buf[7] = 100; // Paper Mode (GREEN?)
961 buf[8] = 100; // Paper Mode (BLUE?)
962 buf[9] = wacom->led.llv;
963 buf[10] = wacom->led.groups[0].select & 0x03;
964 }
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700965 else {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200966 int led = wacom->led.groups[0].select | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700967
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700968 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
969 wacom->wacom_wac.features.type == WACOM_24HD)
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200970 led |= (wacom->led.groups[1].select << 4) | 0x40;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700971
Ping Cheng912ca212014-09-10 12:41:31 -0700972 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700973 buf[1] = led;
974 buf[2] = wacom->led.llv;
975 buf[3] = wacom->led.hlv;
976 buf[4] = wacom->led.img_lum;
977 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700978
Ping Cheng912ca212014-09-10 12:41:31 -0700979 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700980 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700981 kfree(buf);
982
983 return retval;
984}
985
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700986static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
987 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700988{
989 unsigned char *buf;
990 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700991 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700992
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700993 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700994 if (!buf)
995 return -ENOMEM;
996
997 /* Send 'start' command */
998 buf[0] = WAC_CMD_ICON_START;
999 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -07001000 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1001 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001002 if (retval < 0)
1003 goto out;
1004
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001005 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001006 buf[1] = button_id & 0x07;
1007 for (i = 0; i < 4; i++) {
1008 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001009 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001010
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001011 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -07001012 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001013 if (retval < 0)
1014 break;
1015 }
1016
1017 /* Send 'stop' */
1018 buf[0] = WAC_CMD_ICON_START;
1019 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -07001020 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1021 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001022
1023out:
1024 kfree(buf);
1025 return retval;
1026}
1027
Ping Cheng09e7d942011-10-04 23:51:14 -07001028static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001029 const char *buf, size_t count)
1030{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001031 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001032 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001033 unsigned int id;
1034 int err;
1035
1036 err = kstrtouint(buf, 10, &id);
1037 if (err)
1038 return err;
1039
1040 mutex_lock(&wacom->lock);
1041
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001042 wacom->led.groups[set_id].select = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001043 err = wacom_led_control(wacom);
1044
1045 mutex_unlock(&wacom->lock);
1046
1047 return err < 0 ? err : count;
1048}
1049
Ping Cheng09e7d942011-10-04 23:51:14 -07001050#define DEVICE_LED_SELECT_ATTR(SET_ID) \
1051static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
1052 struct device_attribute *attr, const char *buf, size_t count) \
1053{ \
1054 return wacom_led_select_store(dev, SET_ID, buf, count); \
1055} \
Ping Cheng04c59ab2011-10-04 23:51:49 -07001056static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
1057 struct device_attribute *attr, char *buf) \
1058{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +08001059 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001060 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -07001061 return scnprintf(buf, PAGE_SIZE, "%d\n", \
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001062 wacom->led.groups[SET_ID].select); \
Ping Cheng04c59ab2011-10-04 23:51:49 -07001063} \
Ping Chenge0984bc2014-09-10 12:40:05 -07001064static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -07001065 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -07001066 wacom_led##SET_ID##_select_store)
1067
1068DEVICE_LED_SELECT_ATTR(0);
1069DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001070
1071static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
1072 const char *buf, size_t count)
1073{
1074 unsigned int value;
1075 int err;
1076
1077 err = kstrtouint(buf, 10, &value);
1078 if (err)
1079 return err;
1080
1081 mutex_lock(&wacom->lock);
1082
1083 *dest = value & 0x7f;
1084 err = wacom_led_control(wacom);
1085
1086 mutex_unlock(&wacom->lock);
1087
1088 return err < 0 ? err : count;
1089}
1090
1091#define DEVICE_LUMINANCE_ATTR(name, field) \
1092static ssize_t wacom_##name##_luminance_store(struct device *dev, \
1093 struct device_attribute *attr, const char *buf, size_t count) \
1094{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +08001095 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001096 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001097 \
1098 return wacom_luminance_store(wacom, &wacom->led.field, \
1099 buf, count); \
1100} \
Ping Cheng37449ad2014-09-10 12:40:30 -07001101static ssize_t wacom_##name##_luminance_show(struct device *dev, \
1102 struct device_attribute *attr, char *buf) \
1103{ \
1104 struct wacom *wacom = dev_get_drvdata(dev); \
1105 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
1106} \
Ping Chenge0984bc2014-09-10 12:40:05 -07001107static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -07001108 wacom_##name##_luminance_show, \
1109 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001110
1111DEVICE_LUMINANCE_ATTR(status0, llv);
1112DEVICE_LUMINANCE_ATTR(status1, hlv);
1113DEVICE_LUMINANCE_ATTR(buttons, img_lum);
1114
1115static ssize_t wacom_button_image_store(struct device *dev, int button_id,
1116 const char *buf, size_t count)
1117{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001118 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001119 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001120 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001121 unsigned len;
1122 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001123
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001124 if (hdev->bus == BUS_BLUETOOTH) {
1125 len = 256;
1126 xfer_id = WAC_CMD_ICON_BT_XFER;
1127 } else {
1128 len = 1024;
1129 xfer_id = WAC_CMD_ICON_XFER;
1130 }
1131
1132 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001133 return -EINVAL;
1134
1135 mutex_lock(&wacom->lock);
1136
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001137 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001138
1139 mutex_unlock(&wacom->lock);
1140
1141 return err < 0 ? err : count;
1142}
1143
1144#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1145static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1146 struct device_attribute *attr, const char *buf, size_t count) \
1147{ \
1148 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1149} \
Ping Chenge0984bc2014-09-10 12:40:05 -07001150static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001151 NULL, wacom_btnimg##BUTTON_ID##_store)
1152
1153DEVICE_BTNIMG_ATTR(0);
1154DEVICE_BTNIMG_ATTR(1);
1155DEVICE_BTNIMG_ATTR(2);
1156DEVICE_BTNIMG_ATTR(3);
1157DEVICE_BTNIMG_ATTR(4);
1158DEVICE_BTNIMG_ATTR(5);
1159DEVICE_BTNIMG_ATTR(6);
1160DEVICE_BTNIMG_ATTR(7);
1161
Ping Cheng09e7d942011-10-04 23:51:14 -07001162static struct attribute *cintiq_led_attrs[] = {
1163 &dev_attr_status_led0_select.attr,
1164 &dev_attr_status_led1_select.attr,
1165 NULL
1166};
1167
1168static struct attribute_group cintiq_led_attr_group = {
1169 .name = "wacom_led",
1170 .attrs = cintiq_led_attrs,
1171};
1172
1173static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001174 &dev_attr_status0_luminance.attr,
1175 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -07001176 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001177 &dev_attr_buttons_luminance.attr,
1178 &dev_attr_button0_rawimg.attr,
1179 &dev_attr_button1_rawimg.attr,
1180 &dev_attr_button2_rawimg.attr,
1181 &dev_attr_button3_rawimg.attr,
1182 &dev_attr_button4_rawimg.attr,
1183 &dev_attr_button5_rawimg.attr,
1184 &dev_attr_button6_rawimg.attr,
1185 &dev_attr_button7_rawimg.attr,
1186 NULL
1187};
1188
Ping Cheng09e7d942011-10-04 23:51:14 -07001189static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001190 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -07001191 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001192};
1193
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001194static struct attribute *intuos5_led_attrs[] = {
1195 &dev_attr_status0_luminance.attr,
1196 &dev_attr_status_led0_select.attr,
1197 NULL
1198};
1199
1200static struct attribute_group intuos5_led_attr_group = {
1201 .name = "wacom_led",
1202 .attrs = intuos5_led_attrs,
1203};
1204
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001205static struct attribute *generic_led_attrs[] = {
1206 &dev_attr_status0_luminance.attr,
1207 &dev_attr_status_led0_select.attr,
1208 NULL
1209};
1210
1211static struct attribute_group generic_led_attr_group = {
1212 .name = "wacom_led",
1213 .attrs = generic_led_attrs,
1214};
1215
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001216struct wacom_sysfs_group_devres {
1217 struct attribute_group *group;
1218 struct kobject *root;
1219};
1220
1221static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1222{
1223 struct wacom_sysfs_group_devres *devres = res;
1224 struct kobject *kobj = devres->root;
1225
1226 dev_dbg(dev, "%s: dropping reference to %s\n",
1227 __func__, devres->group->name);
1228 sysfs_remove_group(kobj, devres->group);
1229}
1230
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001231static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1232 struct kobject *root,
1233 struct attribute_group *group)
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001234{
1235 struct wacom_sysfs_group_devres *devres;
1236 int error;
1237
1238 devres = devres_alloc(wacom_devm_sysfs_group_release,
1239 sizeof(struct wacom_sysfs_group_devres),
1240 GFP_KERNEL);
1241 if (!devres)
1242 return -ENOMEM;
1243
1244 devres->group = group;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001245 devres->root = root;
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001246
1247 error = sysfs_create_group(devres->root, group);
Arvind Yadav097b8f62018-04-24 13:33:03 +05301248 if (error) {
1249 devres_free(devres);
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001250 return error;
Arvind Yadav097b8f62018-04-24 13:33:03 +05301251 }
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001252
1253 devres_add(&wacom->hdev->dev, devres);
1254
1255 return 0;
1256}
1257
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001258static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1259 struct attribute_group *group)
1260{
1261 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1262 group);
1263}
1264
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001265enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001266{
1267 struct wacom *wacom = led->wacom;
1268
1269 if (wacom->led.max_hlv)
1270 return led->hlv * LED_FULL / wacom->led.max_hlv;
1271
1272 if (wacom->led.max_llv)
1273 return led->llv * LED_FULL / wacom->led.max_llv;
1274
1275 /* device doesn't support brightness tuning */
1276 return LED_FULL;
1277}
1278
1279static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1280{
1281 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1282 struct wacom *wacom = led->wacom;
1283
1284 if (wacom->led.groups[led->group].select != led->id)
1285 return LED_OFF;
1286
1287 return wacom_leds_brightness_get(led);
1288}
1289
1290static int wacom_led_brightness_set(struct led_classdev *cdev,
1291 enum led_brightness brightness)
1292{
1293 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1294 struct wacom *wacom = led->wacom;
1295 int error;
1296
1297 mutex_lock(&wacom->lock);
1298
1299 if (!wacom->led.groups || (brightness == LED_OFF &&
1300 wacom->led.groups[led->group].select != led->id)) {
1301 error = 0;
1302 goto out;
1303 }
1304
1305 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1306 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1307
1308 wacom->led.groups[led->group].select = led->id;
1309
1310 error = wacom_led_control(wacom);
1311
1312out:
1313 mutex_unlock(&wacom->lock);
1314
1315 return error;
1316}
1317
1318static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1319 enum led_brightness brightness)
1320{
1321}
1322
1323static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1324 struct wacom_led *led, unsigned int group,
1325 unsigned int id, bool read_only)
1326{
1327 int error;
1328 char *name;
1329
1330 name = devm_kasprintf(dev, GFP_KERNEL,
1331 "%s::wacom-%d.%d",
1332 dev_name(dev),
1333 group,
1334 id);
1335 if (!name)
1336 return -ENOMEM;
1337
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001338 if (!read_only) {
1339 led->trigger.name = name;
1340 error = devm_led_trigger_register(dev, &led->trigger);
1341 if (error) {
1342 hid_err(wacom->hdev,
1343 "failed to register LED trigger %s: %d\n",
1344 led->cdev.name, error);
1345 return error;
1346 }
1347 }
1348
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001349 led->group = group;
1350 led->id = id;
1351 led->wacom = wacom;
1352 led->llv = wacom->led.llv;
1353 led->hlv = wacom->led.hlv;
1354 led->cdev.name = name;
1355 led->cdev.max_brightness = LED_FULL;
1356 led->cdev.flags = LED_HW_PLUGGABLE;
1357 led->cdev.brightness_get = __wacom_led_brightness_get;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001358 if (!read_only) {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001359 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001360 led->cdev.default_trigger = led->cdev.name;
1361 } else {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001362 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001363 }
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001364
1365 error = devm_led_classdev_register(dev, &led->cdev);
1366 if (error) {
1367 hid_err(wacom->hdev,
1368 "failed to register LED %s: %d\n",
1369 led->cdev.name, error);
1370 led->cdev.name = NULL;
1371 return error;
1372 }
1373
1374 return 0;
1375}
1376
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001377static void wacom_led_groups_release_one(void *data)
1378{
1379 struct wacom_group_leds *group = data;
1380
1381 devres_release_group(group->dev, group);
1382}
1383
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001384static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1385 struct wacom *wacom,
1386 int group_id, int count,
1387 bool read_only)
1388{
1389 struct wacom_led *leds;
1390 int i, error;
1391
1392 if (group_id >= wacom->led.count || count <= 0)
1393 return -EINVAL;
1394
1395 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1396 return -ENOMEM;
1397
Kees Cooka86854d2018-06-12 14:07:58 -07001398 leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL);
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001399 if (!leds) {
1400 error = -ENOMEM;
1401 goto err;
1402 }
1403
1404 wacom->led.groups[group_id].leds = leds;
1405 wacom->led.groups[group_id].count = count;
1406
1407 for (i = 0; i < count; i++) {
1408 error = wacom_led_register_one(dev, wacom, &leds[i],
1409 group_id, i, read_only);
1410 if (error)
1411 goto err;
1412 }
1413
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001414 wacom->led.groups[group_id].dev = dev;
1415
1416 devres_close_group(dev, &wacom->led.groups[group_id]);
1417
1418 /*
1419 * There is a bug (?) in devm_led_classdev_register() in which its
1420 * increments the refcount of the parent. If the parent is an input
1421 * device, that means the ref count never reaches 0 when
1422 * devm_input_device_release() gets called.
1423 * This means that the LEDs are still there after disconnect.
1424 * Manually force the release of the group so that the leds are released
1425 * once we are done using them.
1426 */
1427 error = devm_add_action_or_reset(&wacom->hdev->dev,
1428 wacom_led_groups_release_one,
1429 &wacom->led.groups[group_id]);
1430 if (error)
1431 return error;
1432
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001433 return 0;
1434
1435err:
1436 devres_release_group(dev, &wacom->led.groups[group_id]);
1437 return error;
1438}
1439
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001440struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1441 unsigned int id)
1442{
1443 struct wacom_group_leds *group;
1444
1445 if (group_id >= wacom->led.count)
1446 return NULL;
1447
1448 group = &wacom->led.groups[group_id];
1449
1450 if (!group->leds)
1451 return NULL;
1452
1453 id %= group->count;
1454
1455 return &group->leds[id];
1456}
1457
1458/**
1459 * wacom_led_next: gives the next available led with a wacom trigger.
1460 *
1461 * returns the next available struct wacom_led which has its default trigger
1462 * or the current one if none is available.
1463 */
1464struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1465{
1466 struct wacom_led *next_led;
1467 int group, next;
1468
1469 if (!wacom || !cur)
1470 return NULL;
1471
1472 group = cur->group;
1473 next = cur->id;
1474
1475 do {
1476 next_led = wacom_led_find(wacom, group, ++next);
1477 if (!next_led || next_led == cur)
1478 return next_led;
1479 } while (next_led->cdev.trigger != &next_led->trigger);
1480
1481 return next_led;
1482}
1483
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001484static void wacom_led_groups_release(void *data)
1485{
1486 struct wacom *wacom = data;
1487
1488 wacom->led.groups = NULL;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001489 wacom->led.count = 0;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001490}
1491
1492static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1493{
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001494 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001495 struct wacom_group_leds *groups;
1496 int error;
1497
Kees Cooka86854d2018-06-12 14:07:58 -07001498 groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds),
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001499 GFP_KERNEL);
1500 if (!groups)
1501 return -ENOMEM;
1502
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001503 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001504 if (error)
1505 return error;
1506
1507 wacom->led.groups = groups;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001508 wacom->led.count = count;
1509
1510 return 0;
1511}
1512
1513static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1514 int led_per_group, bool read_only)
1515{
1516 struct device *dev;
1517 int i, error;
1518
1519 if (!wacom->wacom_wac.pad_input)
1520 return -EINVAL;
1521
1522 dev = &wacom->wacom_wac.pad_input->dev;
1523
1524 error = wacom_led_groups_allocate(wacom, group_count);
1525 if (error)
1526 return error;
1527
1528 for (i = 0; i < group_count; i++) {
1529 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1530 led_per_group,
1531 read_only);
1532 if (error)
1533 return error;
1534 }
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001535
1536 return 0;
1537}
1538
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001539int wacom_initialize_leds(struct wacom *wacom)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001540{
1541 int error;
1542
Jason Gerecke862cf552015-06-15 18:01:43 -07001543 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1544 return 0;
1545
Ping Cheng09e7d942011-10-04 23:51:14 -07001546 /* Initialize default values */
1547 switch (wacom->wacom_wac.features.type) {
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001548 case HID_GENERIC:
1549 if (!wacom->generic_has_leds)
1550 return 0;
1551 wacom->led.llv = 100;
1552 wacom->led.max_llv = 100;
1553
1554 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1555 if (error) {
1556 hid_err(wacom->hdev,
1557 "cannot create leds err: %d\n", error);
1558 return error;
1559 }
1560
1561 error = wacom_devm_sysfs_create_group(wacom,
1562 &generic_led_attr_group);
1563 break;
1564
Jason Gereckea19fc982012-06-12 00:27:53 -07001565 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -07001566 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -07001567 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -07001568 case INTUOS4L:
Ping Chengf4fa9a62011-10-04 23:49:42 -07001569 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001570 wacom->led.hlv = 20;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001571 wacom->led.max_llv = 127;
1572 wacom->led.max_hlv = 127;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001573 wacom->led.img_lum = 10;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001574
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001575 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001576 if (error) {
1577 hid_err(wacom->hdev,
1578 "cannot create leds err: %d\n", error);
1579 return error;
1580 }
1581
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001582 error = wacom_devm_sysfs_create_group(wacom,
1583 &intuos4_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001584 break;
1585
Jason Gerecke246835f2011-12-12 00:12:04 -08001586 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -07001587 case WACOM_21UX2:
Ping Cheng09e7d942011-10-04 23:51:14 -07001588 wacom->led.llv = 0;
1589 wacom->led.hlv = 0;
1590 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001591
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001592 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001593 if (error) {
1594 hid_err(wacom->hdev,
1595 "cannot create leds err: %d\n", error);
1596 return error;
1597 }
1598
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001599 error = wacom_devm_sysfs_create_group(wacom,
1600 &cintiq_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001601 break;
1602
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001603 case INTUOS5S:
1604 case INTUOS5:
1605 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -07001606 case INTUOSPS:
1607 case INTUOSPM:
1608 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -07001609 wacom->led.llv = 32;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001610 wacom->led.max_llv = 96;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001611
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 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001621 break;
1622
Jason Gerecke4922cd22017-01-25 12:08:37 -08001623 case INTUOSP2_BT:
1624 wacom->led.llv = 50;
1625 wacom->led.max_llv = 100;
1626 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1627 if (error) {
1628 hid_err(wacom->hdev,
1629 "cannot create leds err: %d\n", error);
1630 return error;
1631 }
1632 return 0;
1633
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001634 case REMOTE:
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001635 wacom->led.llv = 255;
1636 wacom->led.max_llv = 255;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001637 error = wacom_led_groups_allocate(wacom, 5);
1638 if (error) {
1639 hid_err(wacom->hdev,
1640 "cannot create leds err: %d\n", error);
1641 return error;
1642 }
1643 return 0;
1644
Ping Cheng09e7d942011-10-04 23:51:14 -07001645 default:
1646 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001647 }
1648
Ping Cheng09e7d942011-10-04 23:51:14 -07001649 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -07001650 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -07001651 "cannot create sysfs group err: %d\n", error);
1652 return error;
1653 }
Ping Cheng09e7d942011-10-04 23:51:14 -07001654
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001655 return 0;
1656}
1657
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01001658static void wacom_init_work(struct work_struct *work)
1659{
1660 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1661
1662 _wacom_query_tablet_data(wacom);
1663 wacom_led_control(wacom);
1664}
1665
1666static void wacom_query_tablet_data(struct wacom *wacom)
1667{
1668 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1669}
1670
Chris Bagwella1d552c2012-03-25 23:26:30 -07001671static enum power_supply_property wacom_battery_props[] = {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001672 POWER_SUPPLY_PROP_MODEL_NAME,
Jason Gerecke71fa6412015-03-11 10:25:41 -07001673 POWER_SUPPLY_PROP_PRESENT,
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001674 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001675 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001676 POWER_SUPPLY_PROP_CAPACITY
1677};
1678
1679static int wacom_battery_get_property(struct power_supply *psy,
1680 enum power_supply_property psp,
1681 union power_supply_propval *val)
1682{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001683 struct wacom_battery *battery = power_supply_get_drvdata(psy);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001684 int ret = 0;
1685
1686 switch (psp) {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001687 case POWER_SUPPLY_PROP_MODEL_NAME:
1688 val->strval = battery->wacom->wacom_wac.name;
1689 break;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001690 case POWER_SUPPLY_PROP_PRESENT:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001691 val->intval = battery->bat_connected;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001692 break;
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001693 case POWER_SUPPLY_PROP_SCOPE:
1694 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1695 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001696 case POWER_SUPPLY_PROP_CAPACITY:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001697 val->intval = battery->battery_capacity;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001698 break;
1699 case POWER_SUPPLY_PROP_STATUS:
Jason Gerecke16e45982017-04-28 09:25:33 -07001700 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1701 val->intval = battery->bat_status;
1702 else if (battery->bat_charging)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001703 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001704 else if (battery->battery_capacity == 100 &&
1705 battery->ps_connected)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001706 val->intval = POWER_SUPPLY_STATUS_FULL;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001707 else if (battery->ps_connected)
Jason Gereckeb0882cb2015-03-06 11:47:43 -08001708 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001709 else
1710 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001711 break;
1712 default:
1713 ret = -EINVAL;
1714 break;
1715 }
1716
1717 return ret;
1718}
1719
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001720static int __wacom_initialize_battery(struct wacom *wacom,
1721 struct wacom_battery *battery)
Chris Bagwella1d552c2012-03-25 23:26:30 -07001722{
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001723 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001724 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001725 struct power_supply_config psy_cfg = { .drv_data = battery, };
Benjamin Tissoires136ae5e2016-07-13 18:06:16 +02001726 struct power_supply *ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001727 struct power_supply_desc *bat_desc = &battery->bat_desc;
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001728 unsigned long n;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001729 int error;
1730
1731 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1732 return -ENOMEM;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001733
Benjamin Tissoires99569532016-07-13 18:06:17 +02001734 battery->wacom = wacom;
1735
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001736 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001737
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001738 bat_desc->properties = wacom_battery_props;
1739 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1740 bat_desc->get_property = wacom_battery_get_property;
1741 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1742 bat_desc->name = battery->bat_name;
Benjamin Tissoires96983292016-07-13 18:06:15 +02001743 bat_desc->type = POWER_SUPPLY_TYPE_USB;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001744 bat_desc->use_for_apm = 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001745
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001746 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1747 if (IS_ERR(ps_bat)) {
1748 error = PTR_ERR(ps_bat);
1749 goto err;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001750 }
1751
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001752 power_supply_powers(ps_bat, &wacom->hdev->dev);
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001753
1754 battery->battery = ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001755
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001756 devres_close_group(dev, bat_desc);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001757 return 0;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001758
1759err:
1760 devres_release_group(dev, bat_desc);
1761 return error;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001762}
1763
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001764static int wacom_initialize_battery(struct wacom *wacom)
1765{
1766 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1767 return __wacom_initialize_battery(wacom, &wacom->battery);
1768
1769 return 0;
1770}
1771
Chris Bagwella1d552c2012-03-25 23:26:30 -07001772static void wacom_destroy_battery(struct wacom *wacom)
1773{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001774 if (wacom->battery.battery) {
1775 devres_release_group(&wacom->hdev->dev,
1776 &wacom->battery.bat_desc);
1777 wacom->battery.battery = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001778 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001779}
1780
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001781static ssize_t wacom_show_speed(struct device *dev,
1782 struct device_attribute
1783 *attr, char *buf)
1784{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001785 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001786 struct wacom *wacom = hid_get_drvdata(hdev);
1787
1788 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1789}
1790
1791static ssize_t wacom_store_speed(struct device *dev,
1792 struct device_attribute *attr,
1793 const char *buf, size_t count)
1794{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001795 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001796 struct wacom *wacom = hid_get_drvdata(hdev);
1797 u8 new_speed;
1798
1799 if (kstrtou8(buf, 0, &new_speed))
1800 return -EINVAL;
1801
1802 if (new_speed != 0 && new_speed != 1)
1803 return -EINVAL;
1804
1805 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1806
1807 return count;
1808}
1809
Ping Chenge0984bc2014-09-10 12:40:05 -07001810static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001811 wacom_show_speed, wacom_store_speed);
1812
Aaron Skomra72b236d2015-08-20 16:05:17 -07001813
1814static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1815 struct kobj_attribute *kattr,
1816 char *buf, int index)
1817{
Geliang Tang2cf83832015-12-27 17:25:24 +08001818 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001819 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001820 struct wacom *wacom = hid_get_drvdata(hdev);
1821 u8 mode;
1822
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001823 mode = wacom->led.groups[index].select;
Christos Gkekasbc35f732017-07-08 20:25:48 +01001824 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001825}
1826
1827#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1828static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1829 struct kobj_attribute *kattr, char *buf) \
1830{ \
1831 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1832} \
1833static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1834 .attr = {.name = "remote_mode", \
1835 .mode = DEV_ATTR_RO_PERM}, \
1836 .show = wacom_show_remote##SET_ID##_mode, \
1837}; \
1838static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1839 &remote##SET_ID##_mode_attr.attr, \
1840 NULL \
1841}; \
1842static struct attribute_group remote##SET_ID##_serial_group = { \
1843 .name = NULL, \
1844 .attrs = remote##SET_ID##_serial_attrs, \
1845}
1846
1847DEVICE_EKR_ATTR_GROUP(0);
1848DEVICE_EKR_ATTR_GROUP(1);
1849DEVICE_EKR_ATTR_GROUP(2);
1850DEVICE_EKR_ATTR_GROUP(3);
1851DEVICE_EKR_ATTR_GROUP(4);
1852
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02001853static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1854 int index)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001855{
1856 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001857 struct wacom_remote *remote = wacom->remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001858
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001859 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001860 GFP_KERNEL,
1861 "%d", serial);
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001862 if (!remote->remotes[index].group.name)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001863 return -ENOMEM;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001864
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001865 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001866 &remote->remotes[index].group);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001867 if (error) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001868 remote->remotes[index].group.name = NULL;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001869 hid_err(wacom->hdev,
1870 "cannot create sysfs group err: %d\n", error);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001871 return error;
1872 }
1873
1874 return 0;
1875}
1876
Aaron Skomra72b236d2015-08-20 16:05:17 -07001877static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1878{
1879 const size_t buf_size = 2;
1880 unsigned char *buf;
1881 int retval;
1882
1883 buf = kzalloc(buf_size, GFP_KERNEL);
1884 if (!buf)
1885 return -ENOMEM;
1886
1887 buf[0] = WAC_CMD_DELETE_PAIRING;
1888 buf[1] = selector;
1889
1890 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1891 buf_size, WAC_CMD_RETRIES);
1892 kfree(buf);
1893
1894 return retval;
1895}
1896
1897static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1898 struct kobj_attribute *attr,
1899 const char *buf, size_t count)
1900{
1901 unsigned char selector = 0;
Geliang Tang2cf83832015-12-27 17:25:24 +08001902 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001903 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001904 struct wacom *wacom = hid_get_drvdata(hdev);
1905 int err;
1906
1907 if (!strncmp(buf, "*\n", 2)) {
1908 selector = WAC_CMD_UNPAIR_ALL;
1909 } else {
1910 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1911 buf);
1912 return -1;
1913 }
1914
1915 mutex_lock(&wacom->lock);
1916
1917 err = wacom_cmd_unpair_remote(wacom, selector);
1918 mutex_unlock(&wacom->lock);
1919
1920 return err < 0 ? err : count;
1921}
1922
1923static struct kobj_attribute unpair_remote_attr = {
1924 .attr = {.name = "unpair_remote", .mode = 0200},
1925 .store = wacom_store_unpair_remote,
1926};
1927
1928static const struct attribute *remote_unpair_attrs[] = {
1929 &unpair_remote_attr.attr,
1930 NULL
1931};
1932
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001933static void wacom_remotes_destroy(void *data)
1934{
1935 struct wacom *wacom = data;
1936 struct wacom_remote *remote = wacom->remote;
1937
1938 if (!remote)
1939 return;
1940
1941 kobject_put(remote->remote_dir);
1942 kfifo_free(&remote->remote_fifo);
1943 wacom->remote = NULL;
1944}
1945
1946static int wacom_initialize_remotes(struct wacom *wacom)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001947{
1948 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001949 struct wacom_remote *remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001950 int i;
1951
1952 if (wacom->wacom_wac.features.type != REMOTE)
1953 return 0;
1954
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001955 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1956 GFP_KERNEL);
1957 if (!remote)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001958 return -ENOMEM;
1959
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001960 wacom->remote = remote;
1961
1962 spin_lock_init(&remote->remote_lock);
1963
1964 error = kfifo_alloc(&remote->remote_fifo,
1965 5 * sizeof(struct wacom_remote_data),
1966 GFP_KERNEL);
1967 if (error) {
1968 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1969 return -ENOMEM;
1970 }
1971
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001972 remote->remotes[0].group = remote0_serial_group;
1973 remote->remotes[1].group = remote1_serial_group;
1974 remote->remotes[2].group = remote2_serial_group;
1975 remote->remotes[3].group = remote3_serial_group;
1976 remote->remotes[4].group = remote4_serial_group;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001977
1978 remote->remote_dir = kobject_create_and_add("wacom_remote",
1979 &wacom->hdev->dev.kobj);
1980 if (!remote->remote_dir)
1981 return -ENOMEM;
1982
1983 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001984
1985 if (error) {
1986 hid_err(wacom->hdev,
1987 "cannot create sysfs group err: %d\n", error);
1988 return error;
1989 }
1990
1991 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001992 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001993 remote->remotes[i].serial = 0;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001994 }
1995
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001996 error = devm_add_action_or_reset(&wacom->hdev->dev,
1997 wacom_remotes_destroy, wacom);
1998 if (error)
1999 return error;
2000
Aaron Skomra72b236d2015-08-20 16:05:17 -07002001 return 0;
2002}
2003
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002004static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002005{
2006 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07002007 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002008 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002009
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002010 input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002011 if (!input_dev)
2012 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002013
Jason Gerecke2bdd1632015-07-13 18:03:45 -07002014 input_dev->name = wacom_wac->features.name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07002015 input_dev->phys = hdev->phys;
2016 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002017 input_dev->open = wacom_open;
2018 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07002019 input_dev->uniq = hdev->uniq;
2020 input_dev->id.bustype = hdev->bus;
2021 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04002022 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07002023 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002024 input_set_drvdata(input_dev, wacom);
2025
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002026 return input_dev;
2027}
2028
Jason Gerecked9f2d202015-07-13 18:03:44 -07002029static int wacom_allocate_inputs(struct wacom *wacom)
2030{
2031 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2032
2033 wacom_wac->pen_input = wacom_allocate_input(wacom);
2034 wacom_wac->touch_input = wacom_allocate_input(wacom);
2035 wacom_wac->pad_input = wacom_allocate_input(wacom);
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002036 if (!wacom_wac->pen_input ||
2037 !wacom_wac->touch_input ||
2038 !wacom_wac->pad_input)
Jason Gerecked9f2d202015-07-13 18:03:44 -07002039 return -ENOMEM;
Jason Gerecked9f2d202015-07-13 18:03:44 -07002040
Jason Gerecke2bdd1632015-07-13 18:03:45 -07002041 wacom_wac->pen_input->name = wacom_wac->pen_name;
Jason Gerecked9f2d202015-07-13 18:03:44 -07002042 wacom_wac->touch_input->name = wacom_wac->touch_name;
2043 wacom_wac->pad_input->name = wacom_wac->pad_name;
2044
2045 return 0;
2046}
2047
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07002048static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002049{
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002050 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002051 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Jason Gerecke2636a3f2015-06-15 18:01:44 -07002052 int error = 0;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002053
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002054 pen_input_dev = wacom_wac->pen_input;
2055 touch_input_dev = wacom_wac->touch_input;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04002056 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002057
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002058 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04002059 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002060
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002061 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
2062 if (error) {
2063 /* no pen in use on this interface */
2064 input_free_device(pen_input_dev);
2065 wacom_wac->pen_input = NULL;
2066 pen_input_dev = NULL;
2067 } else {
2068 error = input_register_device(pen_input_dev);
Ping Cheng30ebc1a2014-11-18 13:29:16 -08002069 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002070 goto fail;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002071 }
2072
2073 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
2074 if (error) {
2075 /* no touch in use on this interface */
2076 input_free_device(touch_input_dev);
2077 wacom_wac->touch_input = NULL;
2078 touch_input_dev = NULL;
2079 } else {
2080 error = input_register_device(touch_input_dev);
2081 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002082 goto fail;
Ping Cheng30ebc1a2014-11-18 13:29:16 -08002083 }
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002084
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002085 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2086 if (error) {
2087 /* no pad in use on this interface */
2088 input_free_device(pad_input_dev);
2089 wacom_wac->pad_input = NULL;
2090 pad_input_dev = NULL;
2091 } else {
2092 error = input_register_device(pad_input_dev);
2093 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002094 goto fail;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002095 }
2096
Ping Cheng19635182012-04-29 21:09:18 -07002097 return 0;
2098
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002099fail:
2100 wacom_wac->pad_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002101 wacom_wac->touch_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002102 wacom_wac->pen_input = NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002103 return error;
2104}
2105
Jason Gerecke0be01712015-08-05 15:44:53 -07002106/*
2107 * Not all devices report physical dimensions from HID.
2108 * Compute the default from hardcoded logical dimension
2109 * and resolution before driver overwrites them.
2110 */
2111static void wacom_set_default_phy(struct wacom_features *features)
2112{
2113 if (features->x_resolution) {
2114 features->x_phy = (features->x_max * 100) /
2115 features->x_resolution;
2116 features->y_phy = (features->y_max * 100) /
2117 features->y_resolution;
2118 }
2119}
2120
2121static void wacom_calculate_res(struct wacom_features *features)
2122{
2123 /* set unit to "100th of a mm" for devices not reported by HID */
2124 if (!features->unit) {
2125 features->unit = 0x11;
2126 features->unitExpo = -3;
2127 }
2128
2129 features->x_resolution = wacom_calc_hid_res(features->x_max,
2130 features->x_phy,
2131 features->unit,
2132 features->unitExpo);
2133 features->y_resolution = wacom_calc_hid_res(features->y_max,
2134 features->y_phy,
2135 features->unit,
2136 features->unitExpo);
2137}
2138
Jason Gereckefce99572015-03-06 11:47:40 -08002139void wacom_battery_work(struct work_struct *work)
2140{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002141 struct wacom *wacom = container_of(work, struct wacom, battery_work);
Jason Gereckefce99572015-03-06 11:47:40 -08002142
2143 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02002144 !wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08002145 wacom_initialize_battery(wacom);
2146 }
2147 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02002148 wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08002149 wacom_destroy_battery(wacom);
2150 }
2151}
2152
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002153static size_t wacom_compute_pktlen(struct hid_device *hdev)
2154{
2155 struct hid_report_enum *report_enum;
2156 struct hid_report *report;
2157 size_t size = 0;
2158
2159 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2160
2161 list_for_each_entry(report, &report_enum->report_list, list) {
Mathieu Magnaudetdabb05c62014-11-27 16:02:36 +01002162 size_t report_size = hid_report_len(report);
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002163 if (report_size > size)
2164 size = report_size;
2165 }
2166
2167 return size;
2168}
2169
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002170static void wacom_update_name(struct wacom *wacom, const char *suffix)
Ping Chengc24eab42015-04-24 15:32:51 -07002171{
2172 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2173 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke44b52502015-06-15 18:01:41 -07002174 char name[WACOM_NAME_MAX];
Ping Chengc24eab42015-04-24 15:32:51 -07002175
2176 /* Generic devices name unspecified */
2177 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
Jason Gerecke09dc28a2017-07-24 09:46:19 -07002178 char *product_name = wacom->hdev->name;
Ping Chengc24eab42015-04-24 15:32:51 -07002179
Jason Gerecke09dc28a2017-07-24 09:46:19 -07002180 if (hid_is_using_ll_driver(wacom->hdev, &usb_hid_driver)) {
2181 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2182 struct usb_device *dev = interface_to_usbdev(intf);
2183 product_name = dev->product;
Ping Chengc24eab42015-04-24 15:32:51 -07002184 }
Jason Gerecke09dc28a2017-07-24 09:46:19 -07002185
2186 if (wacom->hdev->bus == BUS_I2C) {
2187 snprintf(name, sizeof(name), "%s %X",
2188 features->name, wacom->hdev->product);
2189 } else if (strstr(product_name, "Wacom") ||
2190 strstr(product_name, "wacom") ||
2191 strstr(product_name, "WACOM")) {
2192 strlcpy(name, product_name, sizeof(name));
2193 } else {
2194 snprintf(name, sizeof(name), "Wacom %s", product_name);
2195 }
2196
2197 /* strip out excess whitespaces */
2198 while (1) {
2199 char *gap = strstr(name, " ");
2200 if (gap == NULL)
2201 break;
2202 /* shift everything including the terminator */
2203 memmove(gap, gap+1, strlen(gap));
2204 }
2205
2206 /* get rid of trailing whitespace */
2207 if (name[strlen(name)-1] == ' ')
2208 name[strlen(name)-1] = '\0';
Ping Chengc24eab42015-04-24 15:32:51 -07002209 } else {
Jason Gerecke44b52502015-06-15 18:01:41 -07002210 strlcpy(name, features->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07002211 }
2212
Benjamin Tissoires99569532016-07-13 18:06:17 +02002213 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2214 name, suffix);
2215
Ping Chengc24eab42015-04-24 15:32:51 -07002216 /* Append the device type to the name */
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002217 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002218 "%s%s Pen", name, suffix);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002219 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002220 "%s%s Finger", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07002221 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002222 "%s%s Pad", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07002223}
2224
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002225static void wacom_release_resources(struct wacom *wacom)
2226{
2227 struct hid_device *hdev = wacom->hdev;
2228
2229 if (!wacom->resources)
2230 return;
2231
2232 devres_release_group(&hdev->dev, wacom);
2233
2234 wacom->resources = false;
2235
2236 wacom->wacom_wac.pen_input = NULL;
2237 wacom->wacom_wac.touch_input = NULL;
2238 wacom->wacom_wac.pad_input = NULL;
2239}
2240
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002241static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2242{
2243 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2244 wacom_wac->shared->type = wacom_wac->features.type;
2245 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2246 }
2247
Ping Chengd793ff82017-02-14 21:27:45 -08002248 if (wacom_wac->has_mute_touch_switch) {
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002249 wacom_wac->shared->has_mute_touch_switch = true;
Ping Chengd793ff82017-02-14 21:27:45 -08002250 wacom_wac->shared->is_touch_on = true;
2251 }
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002252
2253 if (wacom_wac->shared->has_mute_touch_switch &&
2254 wacom_wac->shared->touch_input) {
2255 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2256 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2257 SW_MUTE_DEVICE);
2258 }
2259}
2260
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002261static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
Ping Cheng3bea7332006-07-13 18:01:36 -07002262{
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002263 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2264 struct wacom_features *features = &wacom_wac->features;
2265 struct hid_device *hdev = wacom->hdev;
Jason Childse33da8a2010-02-17 22:38:31 -08002266 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002267 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07002268
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002269 features->pktlen = wacom_compute_pktlen(hdev);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002270 if (features->pktlen > WACOM_PKGLEN_MAX)
2271 return -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07002272
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002273 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2274 return -ENOMEM;
2275
2276 wacom->resources = true;
2277
Jason Gerecke3f14a632015-08-03 10:17:05 -07002278 error = wacom_allocate_inputs(wacom);
2279 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002280 goto fail;
Benjamin Tissoires494078b2014-09-23 12:08:07 -04002281
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05002282 /*
2283 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2284 * into debug mode for the touch part.
2285 * We ignore the other interfaces.
2286 */
2287 if (features->type == BAMBOO_PAD) {
2288 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2289 features->type = HID_GENERIC;
2290 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2291 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2292 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002293 goto fail;
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05002294 }
2295 }
2296
Ping Cheng401d7d12013-07-28 00:38:54 -07002297 /* set the default size in case we do not get them from hid */
2298 wacom_set_default_phy(features);
2299
Ping Chengf393ee22012-04-29 21:09:17 -07002300 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07002301 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng42f4f272015-04-15 16:53:54 -07002302 wacom_setup_device_quirks(wacom);
Jason Gerecke042628a2015-04-30 17:51:54 -07002303
Jason Gereckeaa86b182015-06-15 18:01:42 -07002304 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2305 features->type != WIRELESS) {
Jason Gerecke8e116d32015-04-30 17:51:55 -07002306 error = features->type == HID_GENERIC ? -ENODEV : 0;
2307
Jason Gerecke042628a2015-04-30 17:51:54 -07002308 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
Jason Gerecke8e116d32015-04-30 17:51:55 -07002309 hdev->name,
2310 error ? "Ignoring" : "Assuming pen");
2311
2312 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002313 goto fail;
Jason Gerecke042628a2015-04-30 17:51:54 -07002314
Jason Gereckeaa86b182015-06-15 18:01:42 -07002315 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -07002316 }
2317
Ping Cheng401d7d12013-07-28 00:38:54 -07002318 wacom_calculate_res(features);
2319
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002320 wacom_update_name(wacom, wireless ? " (WL)" : "");
Ping Cheng4492eff2010-03-19 22:18:15 -07002321
Aaron Armstrong Skomra8b407352017-03-29 10:35:39 -07002322 /* pen only Bamboo neither support touch nor pad */
2323 if ((features->type == BAMBOO_PEN) &&
2324 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2325 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2326 error = -ENODEV;
2327 goto fail;
2328 }
2329
Jason Gereckea9ce7852017-01-17 15:38:58 -08002330 error = wacom_add_shared_data(hdev);
2331 if (error)
2332 goto fail;
Ping Cheng49b764a2010-02-20 00:53:49 -08002333
Jason Gereckeccad85c2015-08-03 10:17:04 -07002334 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002335 (features->quirks & WACOM_QUIRK_BATTERY)) {
2336 error = wacom_initialize_battery(wacom);
2337 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002338 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002339 }
2340
Jason Gerecke3f14a632015-08-03 10:17:05 -07002341 error = wacom_register_inputs(wacom);
2342 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002343 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002344
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002345 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002346 error = wacom_initialize_leds(wacom);
2347 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002348 goto fail;
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002349
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002350 error = wacom_initialize_remotes(wacom);
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002351 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002352 goto fail;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002353 }
2354
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002355 if (features->type == HID_GENERIC)
2356 connect_mask |= HID_CONNECT_DRIVER;
2357
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002358 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002359 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002360 if (error) {
2361 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002362 goto fail;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002363 }
2364
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002365 if (!wireless) {
2366 /* Note that if query fails it is not a hard failure */
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002367 wacom_query_tablet_data(wacom);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002368 }
Jason Gerecke86e88f02015-11-03 16:57:58 -08002369
2370 /* touch only Bamboo doesn't support pen */
2371 if ((features->type == BAMBOO_TOUCH) &&
2372 (features->device_type & WACOM_DEVICETYPE_PEN)) {
Aaron Armstrong Skomra4d20c332017-03-29 11:41:28 -07002373 cancel_delayed_work_sync(&wacom->init_work);
2374 _wacom_query_tablet_data(wacom);
Jason Gerecke86e88f02015-11-03 16:57:58 -08002375 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002376 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08002377 }
2378
Jason Gereckeccad85c2015-08-03 10:17:04 -07002379 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002380 error = hid_hw_open(hdev);
2381
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002382 wacom_set_shared_values(wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002383 devres_close_group(&hdev->dev, wacom);
2384
Ping Cheng3bea7332006-07-13 18:01:36 -07002385 return 0;
2386
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002387fail_quirks:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002388 hid_hw_stop(hdev);
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002389fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002390 wacom_release_resources(wacom);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002391 return error;
2392}
2393
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002394static void wacom_wireless_work(struct work_struct *work)
2395{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002396 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002397 struct usb_device *usbdev = wacom->usbdev;
2398 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2399 struct hid_device *hdev1, *hdev2;
2400 struct wacom *wacom1, *wacom2;
2401 struct wacom_wac *wacom_wac1, *wacom_wac2;
2402 int error;
2403
2404 /*
2405 * Regardless if this is a disconnect or a new tablet,
2406 * remove any existing input and battery devices.
2407 */
2408
2409 wacom_destroy_battery(wacom);
2410
2411 /* Stylus interface */
2412 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2413 wacom1 = hid_get_drvdata(hdev1);
2414 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002415 wacom_release_resources(wacom1);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002416
2417 /* Touch interface */
2418 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2419 wacom2 = hid_get_drvdata(hdev2);
2420 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002421 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002422
2423 if (wacom_wac->pid == 0) {
2424 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002425 } else {
2426 const struct hid_device_id *id = wacom_ids;
2427
2428 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2429 wacom_wac->pid);
2430
2431 while (id->bus) {
2432 if (id->vendor == USB_VENDOR_ID_WACOM &&
2433 id->product == wacom_wac->pid)
2434 break;
2435 id++;
2436 }
2437
2438 if (!id->bus) {
2439 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2440 return;
2441 }
2442
2443 /* Stylus interface */
2444 wacom_wac1->features =
2445 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002446
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002447 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002448 hid_hw_stop(hdev1);
2449 error = wacom_parse_and_register(wacom1, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002450 if (error)
2451 goto fail;
2452
2453 /* Touch interface */
2454 if (wacom_wac1->features.touch_max ||
2455 (wacom_wac1->features.type >= INTUOSHT &&
2456 wacom_wac1->features.type <= BAMBOO_PT)) {
2457 wacom_wac2->features =
2458 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002459 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002460 hid_hw_stop(hdev2);
2461 error = wacom_parse_and_register(wacom2, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002462 if (error)
2463 goto fail;
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002464 }
2465
Benjamin Tissoires99569532016-07-13 18:06:17 +02002466 strlcpy(wacom_wac->name, wacom_wac1->name,
2467 sizeof(wacom_wac->name));
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002468 error = wacom_initialize_battery(wacom);
2469 if (error)
2470 goto fail;
2471 }
2472
2473 return;
2474
2475fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002476 wacom_release_resources(wacom1);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002477 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002478 return;
2479}
2480
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002481static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2482{
2483 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002484 u32 serial = remote->remotes[index].serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002485 int i;
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002486 unsigned long flags;
2487
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002488 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002489 if (remote->remotes[i].serial == serial) {
Aaron Armstrong Skomra791ae272017-12-07 12:31:56 -08002490
2491 spin_lock_irqsave(&remote->remote_lock, flags);
2492 remote->remotes[i].registered = false;
2493 spin_unlock_irqrestore(&remote->remote_lock, flags);
2494
2495 if (remote->remotes[i].battery.battery)
2496 devres_release_group(&wacom->hdev->dev,
2497 &remote->remotes[i].battery.bat_desc);
2498
2499 if (remote->remotes[i].group.name)
2500 devres_release_group(&wacom->hdev->dev,
2501 &remote->remotes[i]);
2502
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002503 remote->remotes[i].serial = 0;
2504 remote->remotes[i].group.name = NULL;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002505 remote->remotes[i].battery.battery = NULL;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002506 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2507 }
2508 }
2509}
2510
2511static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2512 unsigned int index)
2513{
2514 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002515 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002516 int error, k;
2517
2518 /* A remote can pair more than once with an EKR,
2519 * check to make sure this serial isn't already paired.
2520 */
2521 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002522 if (remote->remotes[k].serial == serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002523 break;
2524 }
2525
2526 if (k < WACOM_MAX_REMOTES) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002527 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002528 return 0;
2529 }
2530
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002531 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002532 return -ENOMEM;
2533
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002534 error = wacom_remote_create_attr_group(wacom, serial, index);
2535 if (error)
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002536 goto fail;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002537
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002538 remote->remotes[index].input = wacom_allocate_input(wacom);
2539 if (!remote->remotes[index].input) {
2540 error = -ENOMEM;
2541 goto fail;
2542 }
2543 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2544 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2545
2546 if (!remote->remotes[index].input->name) {
2547 error = -EINVAL;
2548 goto fail;
2549 }
2550
2551 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2552 &wacom->wacom_wac);
2553 if (error)
2554 goto fail;
2555
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002556 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002557
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002558 error = input_register_device(remote->remotes[index].input);
2559 if (error)
2560 goto fail;
2561
Benjamin Tissoires97f55412016-07-13 18:06:10 +02002562 error = wacom_led_groups_alloc_and_register_one(
2563 &remote->remotes[index].input->dev,
2564 wacom, index, 3, true);
2565 if (error)
2566 goto fail;
2567
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002568 remote->remotes[index].registered = true;
2569
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002570 devres_close_group(dev, &remote->remotes[index]);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002571 return 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002572
2573fail:
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002574 devres_release_group(dev, &remote->remotes[index]);
2575 remote->remotes[index].serial = 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002576 return error;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002577}
2578
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002579static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2580{
2581 struct wacom_remote *remote = wacom->remote;
2582 int error;
2583
2584 if (!remote->remotes[index].registered)
2585 return 0;
2586
2587 if (remote->remotes[index].battery.battery)
2588 return 0;
2589
2590 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2591 return 0;
2592
2593 error = __wacom_initialize_battery(wacom,
2594 &wacom->remote->remotes[index].battery);
2595 if (error)
2596 return error;
2597
2598 return 0;
2599}
2600
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002601static void wacom_remote_work(struct work_struct *work)
2602{
2603 struct wacom *wacom = container_of(work, struct wacom, remote_work);
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002604 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002605 struct wacom_remote_data data;
2606 unsigned long flags;
2607 unsigned int count;
2608 u32 serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002609 int i;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002610
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002611 spin_lock_irqsave(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002612
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002613 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002614
2615 if (count != sizeof(data)) {
2616 hid_err(wacom->hdev,
2617 "workitem triggered without status available\n");
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002618 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002619 return;
2620 }
2621
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002622 if (!kfifo_is_empty(&remote->remote_fifo))
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002623 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2624
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002625 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002626
2627 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2628 serial = data.remote[i].serial;
2629 if (data.remote[i].connected) {
2630
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002631 if (remote->remotes[i].serial == serial) {
2632 wacom_remote_attach_battery(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002633 continue;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002634 }
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002635
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002636 if (remote->remotes[i].serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002637 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002638
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002639 wacom_remote_create_one(wacom, serial, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002640
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002641 } else if (remote->remotes[i].serial) {
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002642 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002643 }
2644 }
2645}
2646
Benjamin Tissoires4082da82017-02-14 21:27:18 -08002647static void wacom_mode_change_work(struct work_struct *work)
2648{
2649 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2650 struct wacom_shared *shared = wacom->wacom_wac.shared;
2651 struct wacom *wacom1 = NULL;
2652 struct wacom *wacom2 = NULL;
2653 bool is_direct = wacom->wacom_wac.is_direct_mode;
2654 int error = 0;
2655
2656 if (shared->pen) {
2657 wacom1 = hid_get_drvdata(shared->pen);
2658 wacom_release_resources(wacom1);
2659 hid_hw_stop(wacom1->hdev);
2660 wacom1->wacom_wac.has_mode_change = true;
2661 wacom1->wacom_wac.is_direct_mode = is_direct;
2662 }
2663
2664 if (shared->touch) {
2665 wacom2 = hid_get_drvdata(shared->touch);
2666 wacom_release_resources(wacom2);
2667 hid_hw_stop(wacom2->hdev);
2668 wacom2->wacom_wac.has_mode_change = true;
2669 wacom2->wacom_wac.is_direct_mode = is_direct;
2670 }
2671
2672 if (wacom1) {
2673 error = wacom_parse_and_register(wacom1, false);
2674 if (error)
2675 return;
2676 }
2677
2678 if (wacom2) {
2679 error = wacom_parse_and_register(wacom2, false);
2680 if (error)
2681 return;
2682 }
2683
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002684 return;
2685}
2686
2687static int wacom_probe(struct hid_device *hdev,
2688 const struct hid_device_id *id)
2689{
2690 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2691 struct usb_device *dev = interface_to_usbdev(intf);
2692 struct wacom *wacom;
2693 struct wacom_wac *wacom_wac;
2694 struct wacom_features *features;
2695 int error;
2696
2697 if (!id->driver_data)
2698 return -EINVAL;
2699
2700 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2701
2702 /* hid-core sets this quirk for the boot interface */
2703 hdev->quirks &= ~HID_QUIRK_NOGET;
2704
Benjamin Tissoires19b64332016-07-13 18:05:58 +02002705 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002706 if (!wacom)
2707 return -ENOMEM;
2708
2709 hid_set_drvdata(hdev, wacom);
2710 wacom->hdev = hdev;
2711
2712 wacom_wac = &wacom->wacom_wac;
2713 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2714 features = &wacom_wac->features;
2715
2716 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2717 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002718 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002719 }
2720
Jason Gerecke83417202017-11-10 11:50:01 -08002721 error = kfifo_alloc(&wacom_wac->pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
2722 if (error)
2723 goto fail;
2724
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002725 wacom_wac->hid_data.inputmode = -1;
Jason Gerecke326ea2a2016-04-04 11:26:52 -07002726 wacom_wac->mode_report = -1;
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002727
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002728 wacom->usbdev = dev;
2729 wacom->intf = intf;
2730 mutex_init(&wacom->lock);
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002731 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002732 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2733 INIT_WORK(&wacom->battery_work, wacom_battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002734 INIT_WORK(&wacom->remote_work, wacom_remote_work);
Benjamin Tissoires4082da82017-02-14 21:27:18 -08002735 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002736
2737 /* ask for the report descriptor to be loaded by HID */
2738 error = hid_parse(hdev);
2739 if (error) {
2740 hid_err(hdev, "parse failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002741 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002742 }
2743
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002744 error = wacom_parse_and_register(wacom, false);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002745 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002746 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002747
2748 if (hdev->bus == BUS_BLUETOOTH) {
2749 error = device_create_file(&hdev->dev, &dev_attr_speed);
2750 if (error)
2751 hid_warn(hdev,
2752 "can't create sysfs speed attribute err: %d\n",
2753 error);
2754 }
2755
2756 return 0;
2757
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002758fail:
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002759 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04002760 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07002761}
2762
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002763static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07002764{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002765 struct wacom *wacom = hid_get_drvdata(hdev);
Benjamin Tissoiresf6205162016-02-12 17:27:45 +01002766 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2767 struct wacom_features *features = &wacom_wac->features;
2768
2769 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2770 hid_hw_close(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07002771
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002772 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002773
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002774 cancel_delayed_work_sync(&wacom->init_work);
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002775 cancel_work_sync(&wacom->wireless_work);
2776 cancel_work_sync(&wacom->battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002777 cancel_work_sync(&wacom->remote_work);
Benjamin Tissoires4082da82017-02-14 21:27:18 -08002778 cancel_work_sync(&wacom->mode_change_work);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002779 if (hdev->bus == BUS_BLUETOOTH)
2780 device_remove_file(&hdev->dev, &dev_attr_speed);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002781
Benjamin Tissoiresc0265a942017-01-20 16:20:12 +01002782 /* make sure we don't trigger the LEDs */
2783 wacom_led_groups_release(wacom);
Aaron Armstrong Skomrab6b1f192017-03-06 10:54:58 -08002784
2785 if (wacom->wacom_wac.features.type != REMOTE)
2786 wacom_release_resources(wacom);
Benjamin Tissoires5b779fc2017-01-20 16:20:11 +01002787
Jason Gerecke83417202017-11-10 11:50:01 -08002788 kfifo_free(&wacom_wac->pen_fifo);
2789
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002790 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04002791}
2792
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002793#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002794static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002795{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002796 struct wacom *wacom = hid_get_drvdata(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002797
2798 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002799
2800 /* switch to wacom mode first */
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002801 _wacom_query_tablet_data(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002802 wacom_led_control(wacom);
2803
Oliver Neukume7224092008-04-15 01:31:57 -04002804 mutex_unlock(&wacom->lock);
2805
2806 return 0;
2807}
2808
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002809static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002810{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002811 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002812}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002813#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04002814
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002815static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07002816 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08002817 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07002818 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002819 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002820 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002821#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04002822 .resume = wacom_resume,
2823 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002824#endif
2825 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07002826};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002827module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07002828
2829MODULE_VERSION(DRIVER_VERSION);
2830MODULE_AUTHOR(DRIVER_AUTHOR);
2831MODULE_DESCRIPTION(DRIVER_DESC);
Grant Grundler7021b602017-01-05 11:07:04 -08002832MODULE_LICENSE("GPL");