blob: a8633b1437b2bb87b7f51f77ce8fb2e31d3fb464 [file] [log] [blame]
Ping Cheng3bea7332006-07-13 18:01:36 -07001/*
Dmitry Torokhov4104d132007-05-07 16:16:29 -04002 * drivers/input/tablet/wacom_sys.c
Ping Cheng3bea7332006-07-13 18:01:36 -07003 *
Ping Cheng232f5692009-12-15 00:35:24 -08004 * USB Wacom tablet support - system specific code
Ping Cheng3bea7332006-07-13 18:01:36 -07005 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
Ping Cheng3bea7332006-07-13 18:01:36 -070014#include "wacom_wac.h"
Dmitry Torokhov51269fe2010-03-19 22:18:15 -070015#include "wacom.h"
Jason Gereckeb58ba1b2014-12-05 13:37:32 -080016#include <linux/input/mt.h>
Ping Cheng3bea7332006-07-13 18:01:36 -070017
Ping Chenga417ea42011-08-16 00:17:56 -070018#define WAC_MSG_RETRIES 5
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070019#define WAC_CMD_RETRIES 10
20
Ping Chenge0984bc2014-09-10 12:40:05 -070021#define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
22#define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
Aaron Skomra72b236d2015-08-20 16:05:17 -070023#define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
Ping Chenge0984bc2014-09-10 12:40:05 -070024
Ping Chengc64d8832014-09-10 12:41:04 -070025static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
26 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070027{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070028 int retval;
29
30 do {
Ping Chengc64d8832014-09-10 12:41:04 -070031 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070032 HID_REQ_GET_REPORT);
Jason Gereckeaef31562015-05-21 10:44:31 -070033 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
34
35 if (retval < 0)
36 hid_err(hdev, "wacom_get_report: ran out of retries "
37 "(last error = %d)\n", retval);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070038
39 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070040}
41
Przemo Firszt296b7372014-08-06 14:00:38 -070042static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
43 size_t size, unsigned int retries)
Ping Cheng3bea7332006-07-13 18:01:36 -070044{
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070045 int retval;
46
47 do {
Przemo Firszt296b7372014-08-06 14:00:38 -070048 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
Benjamin Tissoires27b20a92014-07-24 12:56:22 -070049 HID_REQ_SET_REPORT);
Jason Gereckeaef31562015-05-21 10:44:31 -070050 } while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
51
52 if (retval < 0)
53 hid_err(hdev, "wacom_set_report: ran out of retries "
54 "(last error = %d)\n", retval);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -070055
56 return retval;
Ping Cheng3bea7332006-07-13 18:01:36 -070057}
58
Jason Gerecke83417202017-11-10 11:50:01 -080059static void wacom_wac_queue_insert(struct hid_device *hdev,
60 struct kfifo_rec_ptr_2 *fifo,
61 u8 *raw_data, int size)
62{
63 bool warned = false;
64
65 while (kfifo_avail(fifo) < size) {
66 if (!warned)
67 hid_warn(hdev, "%s: kfifo has filled, starting to drop events\n", __func__);
68 warned = true;
69
70 kfifo_skip(fifo);
71 }
72
73 kfifo_in(fifo, raw_data, size);
74}
75
76static void wacom_wac_queue_flush(struct hid_device *hdev,
77 struct kfifo_rec_ptr_2 *fifo)
78{
79 while (!kfifo_is_empty(fifo)) {
80 u8 buf[WACOM_PKGLEN_MAX];
81 int size;
82 int err;
83
84 size = kfifo_out(fifo, buf, sizeof(buf));
85 err = hid_report_raw_event(hdev, HID_INPUT_REPORT, buf, size, false);
86 if (err) {
87 hid_warn(hdev, "%s: unable to flush event due to error %d\n",
88 __func__, err);
89 }
90 }
91}
92
93static int wacom_wac_pen_serial_enforce(struct hid_device *hdev,
94 struct hid_report *report, u8 *raw_data, int size)
95{
96 struct wacom *wacom = hid_get_drvdata(hdev);
97 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
98 struct wacom_features *features = &wacom_wac->features;
99 bool flush = false;
100 bool insert = false;
101 int i, j;
102
103 if (wacom_wac->serial[0] || !(features->quirks & WACOM_QUIRK_TOOLSERIAL))
104 return 0;
105
106 /* Queue events which have invalid tool type or serial number */
107 for (i = 0; i < report->maxfield; i++) {
108 for (j = 0; j < report->field[i]->maxusage; j++) {
109 struct hid_field *field = report->field[i];
110 struct hid_usage *usage = &field->usage[j];
111 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
112 unsigned int offset;
113 unsigned int size;
114 unsigned int value;
115
116 if (equivalent_usage != HID_DG_INRANGE &&
117 equivalent_usage != HID_DG_TOOLSERIALNUMBER &&
118 equivalent_usage != WACOM_HID_WD_SERIALHI &&
119 equivalent_usage != WACOM_HID_WD_TOOLTYPE)
120 continue;
121
122 offset = field->report_offset;
123 size = field->report_size;
124 value = hid_field_extract(hdev, raw_data+1, offset + j * size, size);
125
126 /* If we go out of range, we need to flush the queue ASAP */
127 if (equivalent_usage == HID_DG_INRANGE)
128 value = !value;
129
130 if (value) {
131 flush = true;
132 switch (equivalent_usage) {
133 case HID_DG_TOOLSERIALNUMBER:
134 wacom_wac->serial[0] = value;
135 break;
136
137 case WACOM_HID_WD_SERIALHI:
138 wacom_wac->serial[0] |= ((__u64)value) << 32;
139 break;
140
141 case WACOM_HID_WD_TOOLTYPE:
142 wacom_wac->id[0] = value;
143 break;
144 }
145 }
146 else {
147 insert = true;
148 }
149 }
150 }
151
152 if (flush)
153 wacom_wac_queue_flush(hdev, &wacom_wac->pen_fifo);
154 else if (insert)
155 wacom_wac_queue_insert(hdev, &wacom_wac->pen_fifo, raw_data, size);
156
157 return insert && !flush;
158}
159
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700160static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
161 u8 *raw_data, int size)
Ping Cheng3bea7332006-07-13 18:01:36 -0700162{
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700163 struct wacom *wacom = hid_get_drvdata(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700164
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700165 if (size > WACOM_PKGLEN_MAX)
166 return 1;
Ping Cheng3bea7332006-07-13 18:01:36 -0700167
Jason Gerecke83417202017-11-10 11:50:01 -0800168 if (wacom_wac_pen_serial_enforce(hdev, report, raw_data, size))
169 return -1;
170
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700171 memcpy(wacom->wacom_wac.data, raw_data, size);
Ping Cheng3bea7332006-07-13 18:01:36 -0700172
Benjamin Tissoires29b47392014-07-24 12:52:23 -0700173 wacom_wac_irq(&wacom->wacom_wac, size);
174
175 return 0;
Ping Cheng3bea7332006-07-13 18:01:36 -0700176}
177
Ping Cheng3bea7332006-07-13 18:01:36 -0700178static int wacom_open(struct input_dev *dev)
179{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400180 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700181
Benjamin Tissoiresdff67412014-12-01 11:52:40 -0500182 return hid_hw_open(wacom->hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700183}
184
185static void wacom_close(struct input_dev *dev)
186{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400187 struct wacom *wacom = input_get_drvdata(dev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700188
Benjamin Tissoires3dad1882016-07-13 18:05:54 +0200189 /*
190 * wacom->hdev should never be null, but surprisingly, I had the case
191 * once while unplugging the Wacom Wireless Receiver.
192 */
193 if (wacom->hdev)
194 hid_hw_close(wacom->hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -0700195}
196
Chris Bagwell16bf2882012-03-25 23:26:20 -0700197/*
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700198 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
Jason Gerecke115d5e12012-10-03 17:24:32 -0700199 */
200static int wacom_calc_hid_res(int logical_extents, int physical_extents,
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700201 unsigned unit, int exponent)
Jason Gerecke115d5e12012-10-03 17:24:32 -0700202{
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700203 struct hid_field field = {
204 .logical_maximum = logical_extents,
205 .physical_maximum = physical_extents,
206 .unit = unit,
207 .unit_exponent = exponent,
208 };
Jason Gerecke115d5e12012-10-03 17:24:32 -0700209
Benjamin Tissoires198fdee2014-07-24 13:03:05 -0700210 return hidinput_calc_abs_res(&field, ABS_X);
Jason Gerecke115d5e12012-10-03 17:24:32 -0700211}
212
Jason Gerecke57832512018-06-25 13:24:35 -0700213static void wacom_hid_usage_quirk(struct hid_device *hdev,
214 struct hid_field *field, struct hid_usage *usage)
215{
216 struct wacom *wacom = hid_get_drvdata(hdev);
217 struct wacom_features *features = &wacom->wacom_wac.features;
218 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
219
220 /*
221 * The Dell Canvas 27 needs to be switched to its vendor-defined
222 * report to provide the best resolution.
223 */
224 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
225 hdev->product == 0x4200 &&
226 field->application == HID_UP_MSVENDOR) {
227 wacom->wacom_wac.mode_report = field->report->id;
228 wacom->wacom_wac.mode_value = 2;
229 }
230
231 /*
232 * ISDv4 devices which predate HID's adoption of the
233 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
234 * position instead. We can accurately detect if a
235 * usage with that value should be HID_DG_BARRELSWITCH2
236 * based on the surrounding usages, which have remained
237 * constant across generations.
238 */
239 if (features->type == HID_GENERIC &&
240 usage->hid == 0x000D0000 &&
241 field->application == HID_DG_PEN &&
242 field->physical == HID_DG_STYLUS) {
243 int i = usage->usage_index;
244
245 if (i-4 >= 0 && i+1 < field->maxusage &&
246 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
247 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
248 field->usage[i-2].hid == HID_DG_ERASER &&
249 field->usage[i-1].hid == HID_DG_INVERT &&
250 field->usage[i+1].hid == HID_DG_INRANGE) {
251 usage->hid = HID_DG_BARRELSWITCH2;
252 }
253 }
254
Jason Gereckee9fe0d42019-01-24 11:09:44 -0800255 /*
256 * Wacom's AES devices use different vendor-defined usages to
257 * report serial number information compared to their branded
258 * hardware. The usages are also sometimes ill-defined and do
259 * not have the correct logical min/max values set. Lets patch
260 * the descriptor to use the branded usage convention and fix
261 * the errors.
262 */
263 if (usage->hid == WACOM_HID_WT_SERIALNUMBER &&
264 field->report_size == 16 &&
265 field->index + 2 < field->report->maxfield) {
266 struct hid_field *a = field->report->field[field->index + 1];
267 struct hid_field *b = field->report->field[field->index + 2];
268
269 if (a->maxusage > 0 &&
270 a->usage[0].hid == HID_DG_TOOLSERIALNUMBER &&
271 a->report_size == 32 &&
272 b->maxusage > 0 &&
273 b->usage[0].hid == 0xFF000000 &&
274 b->report_size == 8) {
275 features->quirks |= WACOM_QUIRK_AESPEN;
276 usage->hid = WACOM_HID_WD_TOOLTYPE;
277 field->logical_minimum = S16_MIN;
278 field->logical_maximum = S16_MAX;
279 a->logical_minimum = S32_MIN;
280 a->logical_maximum = S32_MAX;
281 b->usage[0].hid = WACOM_HID_WD_SERIALHI;
282 b->logical_minimum = 0;
283 b->logical_maximum = U8_MAX;
284 }
285 }
286
Jason Gerecke57832512018-06-25 13:24:35 -0700287 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
288 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
289 hdev->product == 0x0358 &&
290 WACOM_PEN_FIELD(field) &&
291 equivalent_usage == HID_GD_Y) {
292 field->logical_maximum = 43200;
293 }
294}
295
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700296static void wacom_feature_mapping(struct hid_device *hdev,
297 struct hid_field *field, struct hid_usage *usage)
Chris Bagwell41343612011-10-26 22:32:52 -0700298{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700299 struct wacom *wacom = hid_get_drvdata(hdev);
300 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400301 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
Aaron Armstrong Skomraac2423c2017-01-25 12:08:40 -0800302 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400303 u8 *data;
304 int ret;
Aaron Ma3064a032018-02-03 23:57:15 +0800305 u32 n;
Chris Bagwell41343612011-10-26 22:32:52 -0700306
Jason Gerecke57832512018-06-25 13:24:35 -0700307 wacom_hid_usage_quirk(hdev, field, usage);
308
Aaron Armstrong Skomraac2423c2017-01-25 12:08:40 -0800309 switch (equivalent_usage) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700310 case HID_DG_CONTACTMAX:
311 /* leave touch_max as is if predefined */
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400312 if (!features->touch_max) {
313 /* read manually */
314 data = kzalloc(2, GFP_KERNEL);
315 if (!data)
316 break;
317 data[0] = field->report->id;
318 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700319 data, 2, WAC_CMD_RETRIES);
320 if (ret == 2) {
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400321 features->touch_max = data[1];
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700322 } else {
323 features->touch_max = 16;
324 hid_warn(hdev, "wacom_feature_mapping: "
325 "could not get HID_DG_CONTACTMAX, "
326 "defaulting to %d\n",
327 features->touch_max);
328 }
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400329 kfree(data);
330 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700331 break;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400332 case HID_DG_INPUTMODE:
333 /* Ignore if value index is out of bounds. */
334 if (usage->usage_index >= field->report_count) {
335 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
336 break;
337 }
338
339 hid_data->inputmode = field->report->id;
340 hid_data->inputmode_index = usage->usage_index;
341 break;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700342
343 case HID_UP_DIGITIZER:
344 if (field->report->id == 0x0B &&
Jason Gerecke8de82282016-10-19 18:03:37 -0700345 (field->application == WACOM_HID_G9_PEN ||
346 field->application == WACOM_HID_G11_PEN)) {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700347 wacom->wacom_wac.mode_report = field->report->id;
348 wacom->wacom_wac.mode_value = 0;
349 }
350 break;
351
Jason Gereckec9c09582016-10-19 18:03:43 -0700352 case WACOM_HID_WD_DATAMODE:
353 wacom->wacom_wac.mode_report = field->report->id;
354 wacom->wacom_wac.mode_value = 2;
355 break;
356
Jason Gerecke8de82282016-10-19 18:03:37 -0700357 case WACOM_HID_UP_G9:
358 case WACOM_HID_UP_G11:
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700359 if (field->report->id == 0x03 &&
Jason Gerecke8de82282016-10-19 18:03:37 -0700360 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
361 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700362 wacom->wacom_wac.mode_report = field->report->id;
363 wacom->wacom_wac.mode_value = 0;
364 }
365 break;
Jason Gerecke345857b2016-10-19 18:03:50 -0700366 case WACOM_HID_WD_OFFSETLEFT:
367 case WACOM_HID_WD_OFFSETTOP:
368 case WACOM_HID_WD_OFFSETRIGHT:
369 case WACOM_HID_WD_OFFSETBOTTOM:
370 /* read manually */
371 n = hid_report_len(field->report);
372 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
373 if (!data)
374 break;
375 data[0] = field->report->id;
376 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
377 data, n, WAC_CMD_RETRIES);
378 if (ret == n) {
379 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
380 data, n, 0);
381 } else {
382 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
383 __func__);
384 }
385 kfree(data);
386 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700387 }
388}
389
Chris Bagwell428f8582011-10-26 22:26:59 -0700390/*
391 * Interface Descriptor of wacom devices can be incomplete and
392 * inconsistent so wacom_features table is used to store stylus
393 * device's packet lengths, various maximum values, and tablet
394 * resolution based on product ID's.
395 *
396 * For devices that contain 2 interfaces, wacom_features table is
397 * inaccurate for the touch interface. Since the Interface Descriptor
398 * for touch interfaces has pretty complete data, this function exists
399 * to query tablet for this missing information instead of hard coding in
400 * an additional table.
401 *
402 * A typical Interface Descriptor for a stylus will contain a
403 * boot mouse application collection that is not of interest and this
404 * function will ignore it.
405 *
406 * It also contains a digitizer application collection that also is not
407 * of interest since any information it contains would be duplicate
408 * of what is in wacom_features. Usually it defines a report of an array
409 * of bytes that could be used as max length of the stylus packet returned.
410 * If it happens to define a Digitizer-Stylus Physical Collection then
411 * the X and Y logical values contain valid data but it is ignored.
412 *
413 * A typical Interface Descriptor for a touch interface will contain a
414 * Digitizer-Finger Physical Collection which will define both logical
415 * X/Y maximum as well as the physical size of tablet. Since touch
416 * interfaces haven't supported pressure or distance, this is enough
417 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700418 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700419 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
420 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700421 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700422static void wacom_usage_mapping(struct hid_device *hdev,
423 struct hid_field *field, struct hid_usage *usage)
424{
425 struct wacom *wacom = hid_get_drvdata(hdev);
426 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoiresd97a5522015-01-05 16:32:12 -0500427 bool finger = WACOM_FINGER_FIELD(field);
428 bool pen = WACOM_PEN_FIELD(field);
Ping Cheng418b5732018-06-25 13:24:36 -0700429 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700430
431 /*
432 * Requiring Stylus Usage will ignore boot mouse
433 * X/Y values and some cases of invalid Digitizer X/Y
434 * values commonly reported.
435 */
Jason Gerecke042628a2015-04-30 17:51:54 -0700436 if (pen)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700437 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -0700438 else if (finger)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700439 features->device_type |= WACOM_DEVICETYPE_TOUCH;
Jason Gerecke042628a2015-04-30 17:51:54 -0700440 else
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700441 return;
442
Jason Gerecke57832512018-06-25 13:24:35 -0700443 wacom_hid_usage_quirk(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700444
Ping Cheng418b5732018-06-25 13:24:36 -0700445 switch (equivalent_usage) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700446 case HID_GD_X:
447 features->x_max = field->logical_maximum;
448 if (finger) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700449 features->x_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700450 if ((features->type != BAMBOO_PT) &&
451 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700452 features->unit = field->unit;
453 features->unitExpo = field->unit_exponent;
454 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700455 }
456 break;
457 case HID_GD_Y:
458 features->y_max = field->logical_maximum;
459 if (finger) {
460 features->y_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700461 if ((features->type != BAMBOO_PT) &&
462 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700463 features->unit = field->unit;
464 features->unitExpo = field->unit_exponent;
465 }
466 }
467 break;
468 case HID_DG_TIPPRESSURE:
469 if (pen)
470 features->pressure_max = field->logical_maximum;
471 break;
472 }
Benjamin Tissoires7704ac92014-09-23 12:08:08 -0400473
474 if (features->type == HID_GENERIC)
475 wacom_wac_usage_mapping(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700476}
477
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800478static void wacom_post_parse_hid(struct hid_device *hdev,
479 struct wacom_features *features)
480{
481 struct wacom *wacom = hid_get_drvdata(hdev);
482 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
483
484 if (features->type == HID_GENERIC) {
485 /* Any last-minute generic device setup */
Benjamin Tissoires4082da82017-02-14 21:27:18 -0800486 if (wacom_wac->has_mode_change) {
487 if (wacom_wac->is_direct_mode)
488 features->device_type |= WACOM_DEVICETYPE_DIRECT;
489 else
490 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
491 }
492
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800493 if (features->touch_max > 1) {
Aaron Armstrong Skomraac2423c2017-01-25 12:08:40 -0800494 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
495 input_mt_init_slots(wacom_wac->touch_input,
496 wacom_wac->features.touch_max,
497 INPUT_MT_DIRECT);
498 else
499 input_mt_init_slots(wacom_wac->touch_input,
500 wacom_wac->features.touch_max,
501 INPUT_MT_POINTER);
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800502 }
503 }
504}
505
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700506static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800507 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500508{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700509 struct hid_report_enum *rep_enum;
510 struct hid_report *hreport;
511 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500512
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700513 /* check features first */
514 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
515 list_for_each_entry(hreport, &rep_enum->report_list, list) {
516 for (i = 0; i < hreport->maxfield; i++) {
517 /* Ignore if report count is out of bounds. */
518 if (hreport->field[i]->report_count < 1)
519 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500520
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700521 for (j = 0; j < hreport->field[i]->maxusage; j++) {
522 wacom_feature_mapping(hdev, hreport->field[i],
523 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500524 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500525 }
526 }
527
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700528 /* now check the input usages */
529 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
530 list_for_each_entry(hreport, &rep_enum->report_list, list) {
531
532 if (!hreport->maxfield)
533 continue;
534
535 for (i = 0; i < hreport->maxfield; i++)
536 for (j = 0; j < hreport->field[i]->maxusage; j++)
537 wacom_usage_mapping(hdev, hreport->field[i],
538 hreport->field[i]->usage + j);
539 }
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800540
541 wacom_post_parse_hid(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -0500542}
543
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400544static int wacom_hid_set_device_mode(struct hid_device *hdev)
545{
546 struct wacom *wacom = hid_get_drvdata(hdev);
547 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
548 struct hid_report *r;
549 struct hid_report_enum *re;
550
551 if (hid_data->inputmode < 0)
552 return 0;
553
554 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
555 r = re->report_id_hash[hid_data->inputmode];
556 if (r) {
557 r->field[0]->value[hid_data->inputmode_index] = 2;
558 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
559 }
560 return 0;
561}
562
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700563static int wacom_set_device_mode(struct hid_device *hdev,
564 struct wacom_wac *wacom_wac)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700565{
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700566 u8 *rep_data;
567 struct hid_report *r;
568 struct hid_report_enum *re;
Aaron Ma3064a032018-02-03 23:57:15 +0800569 u32 length;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700570 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700571
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700572 if (wacom_wac->mode_report < 0)
573 return 0;
574
575 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
576 r = re->report_id_hash[wacom_wac->mode_report];
577 if (!r)
578 return -EINVAL;
579
580 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700581 if (!rep_data)
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700582 return -ENOMEM;
583
584 length = hid_report_len(r);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700585
Jason Gereckefe494bc2012-10-03 17:25:35 -0700586 do {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700587 rep_data[0] = wacom_wac->mode_report;
588 rep_data[1] = wacom_wac->mode_value;
Chris Bagwell9937c022013-01-23 19:37:34 -0800589
Przemo Firszt296b7372014-08-06 14:00:38 -0700590 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
591 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700592 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700593 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700594 rep_data, length, 1);
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700595 } while (error >= 0 &&
596 rep_data[1] != wacom_wac->mode_report &&
597 limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700598
599 kfree(rep_data);
600
601 return error < 0 ? error : 0;
602}
603
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700604static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
605 struct wacom_features *features)
606{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700607 struct wacom *wacom = hid_get_drvdata(hdev);
608 int ret;
609 u8 rep_data[2];
610
611 switch (features->type) {
612 case GRAPHIRE_BT:
613 rep_data[0] = 0x03;
614 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700615 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
616 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700617
618 if (ret >= 0) {
619 rep_data[0] = speed == 0 ? 0x05 : 0x06;
620 rep_data[1] = 0x00;
621
622 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700623 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700624
625 if (ret >= 0) {
626 wacom->wacom_wac.bt_high_speed = speed;
627 return 0;
628 }
629 }
630
631 /*
632 * Note that if the raw queries fail, it's not a hard failure
633 * and it is safe to continue
634 */
635 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
636 rep_data[0], ret);
637 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700638 case INTUOS4WL:
639 if (speed == 1)
640 wacom->wacom_wac.bt_features &= ~0x20;
641 else
642 wacom->wacom_wac.bt_features |= 0x20;
643
644 rep_data[0] = 0x03;
645 rep_data[1] = wacom->wacom_wac.bt_features;
646
Przemo Firszt296b7372014-08-06 14:00:38 -0700647 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
648 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700649 if (ret >= 0)
650 wacom->wacom_wac.bt_high_speed = speed;
651 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700652 }
653
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700654 return 0;
655}
656
Jason Gereckefe494bc2012-10-03 17:25:35 -0700657/*
658 * Switch the tablet into its most-capable mode. Wacom tablets are
659 * typically configured to power-up in a mode which sends mouse-like
660 * reports to the OS. To get absolute position, pressure data, etc.
661 * from the tablet, it is necessary to switch the tablet out of this
662 * mode and into one which sends the full range of tablet data.
663 */
Benjamin Tissoiresa544c612017-01-20 16:20:13 +0100664static int _wacom_query_tablet_data(struct wacom *wacom)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700665{
Benjamin Tissoiresa544c612017-01-20 16:20:13 +0100666 struct hid_device *hdev = wacom->hdev;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700667 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoiresa544c612017-01-20 16:20:13 +0100668 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700669
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700670 if (hdev->bus == BUS_BLUETOOTH)
671 return wacom_bt_query_tablet_data(hdev, 1, features);
672
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700673 if (features->type != HID_GENERIC) {
674 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
675 if (features->type > TABLETPC) {
676 /* MT Tablet PC touch */
677 wacom_wac->mode_report = 3;
678 wacom_wac->mode_value = 4;
679 } else if (features->type == WACOM_24HDT) {
680 wacom_wac->mode_report = 18;
681 wacom_wac->mode_value = 2;
682 } else if (features->type == WACOM_27QHDT) {
683 wacom_wac->mode_report = 131;
684 wacom_wac->mode_value = 2;
685 } else if (features->type == BAMBOO_PAD) {
686 wacom_wac->mode_report = 2;
687 wacom_wac->mode_value = 2;
688 }
689 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
690 if (features->type <= BAMBOO_PT) {
691 wacom_wac->mode_report = 2;
692 wacom_wac->mode_value = 2;
693 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700694 }
695 }
696
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700697 wacom_set_device_mode(hdev, wacom_wac);
698
699 if (features->type == HID_GENERIC)
700 return wacom_hid_set_device_mode(hdev);
701
Jason Gereckefe494bc2012-10-03 17:25:35 -0700702 return 0;
703}
704
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700705static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700706 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800707{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700708 struct wacom *wacom = hid_get_drvdata(hdev);
709 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800710
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700711 /* default features */
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700712 features->x_fuzz = 4;
713 features->y_fuzz = 4;
714 features->pressure_fuzz = 0;
Jason Gereckebef7e202016-04-22 14:30:53 -0700715 features->distance_fuzz = 1;
716 features->tilt_fuzz = 1;
Ping Chengec67bbe2009-12-15 00:35:24 -0800717
Chris Bagwelld3825d52012-03-25 23:26:11 -0700718 /*
719 * The wireless device HID is basic and layout conflicts with
720 * other tablets (monitor and touch interface can look like pen).
721 * Skip the query for this type and modify defaults based on
722 * interface number.
723 */
724 if (features->type == WIRELESS) {
Jason Gerecke3f14a632015-08-03 10:17:05 -0700725 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
Jason Gereckeccad85c2015-08-03 10:17:04 -0700726 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700727 else
Jason Gereckeaa86b182015-06-15 18:01:42 -0700728 features->device_type = WACOM_DEVICETYPE_NONE;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700729 return;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700730 }
731
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700732 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800733}
734
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700735struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700736 struct list_head list;
737 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700738 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700739 struct wacom_shared shared;
740};
741
742static LIST_HEAD(wacom_udev_list);
743static DEFINE_MUTEX(wacom_udev_list_lock);
744
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700745static bool wacom_are_sibling(struct hid_device *hdev,
746 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700747{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700748 struct wacom *wacom = hid_get_drvdata(hdev);
749 struct wacom_features *features = &wacom->wacom_wac.features;
Jason Gerecke41372d52016-08-08 12:06:30 -0700750 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
751 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
752 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
753 __u32 oPid = features->oPid ? features->oPid : hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700754
Jason Gerecke41372d52016-08-08 12:06:30 -0700755 /* The defined oVid/oPid must match that of the sibling */
756 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
757 return false;
758 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
759 return false;
760
761 /*
762 * Devices with the same VID/PID must share the same physical
763 * device path, while those with different VID/PID must share
764 * the same physical parent device path.
765 */
766 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
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;
769 } else {
Daniel M. Lambea1a8861f2018-07-17 22:35:36 +0100770 if (!hid_compare_device_paths(hdev, sibling, '.'))
Jason Gerecke41372d52016-08-08 12:06:30 -0700771 return false;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700772 }
773
Jason Gerecke41372d52016-08-08 12:06:30 -0700774 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
775 if (features->type != HID_GENERIC)
776 return true;
777
778 /*
779 * Direct-input devices may not be siblings of indirect-input
780 * devices.
781 */
782 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
783 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700784 return false;
785
Jason Gerecke41372d52016-08-08 12:06:30 -0700786 /*
787 * Indirect-input devices may not be siblings of direct-input
788 * devices.
789 */
790 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
791 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700792 return false;
793
Jason Gerecke41372d52016-08-08 12:06:30 -0700794 /* Pen devices may only be siblings of touch devices */
795 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
796 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
797 return false;
798
799 /* Touch devices may only be siblings of pen devices */
800 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
801 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
802 return false;
803
804 /*
805 * No reason could be found for these two devices to NOT be
806 * siblings, so there's a good chance they ARE siblings
807 */
808 return true;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700809}
810
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700811static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700812{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700813 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700814
Jason Gerecke41372d52016-08-08 12:06:30 -0700815 /* Try to find an already-probed interface from the same device */
816 list_for_each_entry(data, &wacom_udev_list, list) {
Daniel M. Lambea1a8861f2018-07-17 22:35:36 +0100817 if (hid_compare_device_paths(hdev, data->dev, '/')) {
Jason Gerecke2a5e5972017-09-18 09:27:42 -0700818 kref_get(&data->kref);
Jason Gerecke41372d52016-08-08 12:06:30 -0700819 return data;
Jason Gerecke2a5e5972017-09-18 09:27:42 -0700820 }
Jason Gerecke41372d52016-08-08 12:06:30 -0700821 }
822
823 /* Fallback to finding devices that appear to be "siblings" */
Ping Cheng4492eff2010-03-19 22:18:15 -0700824 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700825 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700826 kref_get(&data->kref);
827 return data;
828 }
829 }
830
831 return NULL;
832}
833
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200834static void wacom_release_shared_data(struct kref *kref)
835{
836 struct wacom_hdev_data *data =
837 container_of(kref, struct wacom_hdev_data, kref);
838
839 mutex_lock(&wacom_udev_list_lock);
840 list_del(&data->list);
841 mutex_unlock(&wacom_udev_list_lock);
842
843 kfree(data);
844}
845
846static void wacom_remove_shared_data(void *res)
847{
848 struct wacom *wacom = res;
849 struct wacom_hdev_data *data;
850 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
851
852 if (wacom_wac->shared) {
853 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
854 shared);
855
856 if (wacom_wac->shared->touch == wacom->hdev)
857 wacom_wac->shared->touch = NULL;
858 else if (wacom_wac->shared->pen == wacom->hdev)
859 wacom_wac->shared->pen = NULL;
860
861 kref_put(&data->kref, wacom_release_shared_data);
862 wacom_wac->shared = NULL;
863 }
864}
865
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700866static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700867{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700868 struct wacom *wacom = hid_get_drvdata(hdev);
869 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
870 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700871 int retval = 0;
872
873 mutex_lock(&wacom_udev_list_lock);
874
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700875 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700876 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700877 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700878 if (!data) {
879 retval = -ENOMEM;
880 goto out;
881 }
882
883 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700884 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700885 list_add_tail(&data->list, &wacom_udev_list);
886 }
887
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700888 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700889
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200890 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
891 if (retval) {
892 mutex_unlock(&wacom_udev_list_lock);
893 wacom_remove_shared_data(wacom);
894 return retval;
895 }
896
Jason Gereckea9ce7852017-01-17 15:38:58 -0800897 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
898 wacom_wac->shared->touch = hdev;
899 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
900 wacom_wac->shared->pen = hdev;
901
Ping Cheng4492eff2010-03-19 22:18:15 -0700902out:
903 mutex_unlock(&wacom_udev_list_lock);
904 return retval;
905}
906
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700907static int wacom_led_control(struct wacom *wacom)
908{
909 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700910 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700911 unsigned char report_id = WAC_CMD_LED_CONTROL;
912 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700913
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200914 if (!wacom->led.groups)
915 return -ENOTSUPP;
916
Aaron Armstrong Skomra74aebed62017-08-28 14:15:39 -0700917 if (wacom->wacom_wac.features.type == REMOTE)
918 return -ENOTSUPP;
919
Ping Cheng912ca212014-09-10 12:41:31 -0700920 if (wacom->wacom_wac.pid) { /* wireless connected */
921 report_id = WAC_CMD_WL_LED_CONTROL;
922 buf_size = 13;
923 }
Jason Gerecke4922cd22017-01-25 12:08:37 -0800924 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
925 report_id = WAC_CMD_WL_INTUOSP2;
926 buf_size = 51;
927 }
Ping Cheng912ca212014-09-10 12:41:31 -0700928 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700929 if (!buf)
930 return -ENOMEM;
931
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -0800932 if (wacom->wacom_wac.features.type == HID_GENERIC) {
933 buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
934 buf[1] = wacom->led.llv;
935 buf[2] = wacom->led.groups[0].select & 0x03;
936
937 } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
938 wacom->wacom_wac.features.type <= INTUOSPL)) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700939 /*
940 * Touch Ring and crop mark LED luminance may take on
941 * one of four values:
942 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
943 */
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200944 int ring_led = wacom->led.groups[0].select & 0x03;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700945 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
946 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700947 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700948
Ping Cheng912ca212014-09-10 12:41:31 -0700949 buf[0] = report_id;
950 if (wacom->wacom_wac.pid) {
951 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
952 buf, buf_size, WAC_CMD_RETRIES);
953 buf[0] = report_id;
954 buf[4] = led_bits;
955 } else
956 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700957 }
Jason Gerecke4922cd22017-01-25 12:08:37 -0800958 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
959 buf[0] = report_id;
960 buf[4] = 100; // Power Connection LED (ORANGE)
961 buf[5] = 100; // BT Connection LED (BLUE)
962 buf[6] = 100; // Paper Mode (RED?)
963 buf[7] = 100; // Paper Mode (GREEN?)
964 buf[8] = 100; // Paper Mode (BLUE?)
965 buf[9] = wacom->led.llv;
966 buf[10] = wacom->led.groups[0].select & 0x03;
967 }
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700968 else {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200969 int led = wacom->led.groups[0].select | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700970
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700971 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
972 wacom->wacom_wac.features.type == WACOM_24HD)
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200973 led |= (wacom->led.groups[1].select << 4) | 0x40;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700974
Ping Cheng912ca212014-09-10 12:41:31 -0700975 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700976 buf[1] = led;
977 buf[2] = wacom->led.llv;
978 buf[3] = wacom->led.hlv;
979 buf[4] = wacom->led.img_lum;
980 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700981
Ping Cheng912ca212014-09-10 12:41:31 -0700982 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700983 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700984 kfree(buf);
985
986 return retval;
987}
988
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700989static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
990 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700991{
992 unsigned char *buf;
993 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700994 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700995
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700996 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700997 if (!buf)
998 return -ENOMEM;
999
1000 /* Send 'start' command */
1001 buf[0] = WAC_CMD_ICON_START;
1002 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -07001003 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1004 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001005 if (retval < 0)
1006 goto out;
1007
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001008 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001009 buf[1] = button_id & 0x07;
1010 for (i = 0; i < 4; i++) {
1011 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001012 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001013
Benjamin Tissoires27b20a92014-07-24 12:56:22 -07001014 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -07001015 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001016 if (retval < 0)
1017 break;
1018 }
1019
1020 /* Send 'stop' */
1021 buf[0] = WAC_CMD_ICON_START;
1022 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -07001023 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1024 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001025
1026out:
1027 kfree(buf);
1028 return retval;
1029}
1030
Ping Cheng09e7d942011-10-04 23:51:14 -07001031static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001032 const char *buf, size_t count)
1033{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001034 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001035 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001036 unsigned int id;
1037 int err;
1038
1039 err = kstrtouint(buf, 10, &id);
1040 if (err)
1041 return err;
1042
1043 mutex_lock(&wacom->lock);
1044
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001045 wacom->led.groups[set_id].select = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001046 err = wacom_led_control(wacom);
1047
1048 mutex_unlock(&wacom->lock);
1049
1050 return err < 0 ? err : count;
1051}
1052
Ping Cheng09e7d942011-10-04 23:51:14 -07001053#define DEVICE_LED_SELECT_ATTR(SET_ID) \
1054static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
1055 struct device_attribute *attr, const char *buf, size_t count) \
1056{ \
1057 return wacom_led_select_store(dev, SET_ID, buf, count); \
1058} \
Ping Cheng04c59ab2011-10-04 23:51:49 -07001059static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
1060 struct device_attribute *attr, char *buf) \
1061{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +08001062 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001063 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -07001064 return scnprintf(buf, PAGE_SIZE, "%d\n", \
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001065 wacom->led.groups[SET_ID].select); \
Ping Cheng04c59ab2011-10-04 23:51:49 -07001066} \
Ping Chenge0984bc2014-09-10 12:40:05 -07001067static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -07001068 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -07001069 wacom_led##SET_ID##_select_store)
1070
1071DEVICE_LED_SELECT_ATTR(0);
1072DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001073
1074static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
1075 const char *buf, size_t count)
1076{
1077 unsigned int value;
1078 int err;
1079
1080 err = kstrtouint(buf, 10, &value);
1081 if (err)
1082 return err;
1083
1084 mutex_lock(&wacom->lock);
1085
1086 *dest = value & 0x7f;
1087 err = wacom_led_control(wacom);
1088
1089 mutex_unlock(&wacom->lock);
1090
1091 return err < 0 ? err : count;
1092}
1093
1094#define DEVICE_LUMINANCE_ATTR(name, field) \
1095static ssize_t wacom_##name##_luminance_store(struct device *dev, \
1096 struct device_attribute *attr, const char *buf, size_t count) \
1097{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +08001098 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001099 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001100 \
1101 return wacom_luminance_store(wacom, &wacom->led.field, \
1102 buf, count); \
1103} \
Ping Cheng37449ad2014-09-10 12:40:30 -07001104static ssize_t wacom_##name##_luminance_show(struct device *dev, \
1105 struct device_attribute *attr, char *buf) \
1106{ \
1107 struct wacom *wacom = dev_get_drvdata(dev); \
1108 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
1109} \
Ping Chenge0984bc2014-09-10 12:40:05 -07001110static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -07001111 wacom_##name##_luminance_show, \
1112 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001113
1114DEVICE_LUMINANCE_ATTR(status0, llv);
1115DEVICE_LUMINANCE_ATTR(status1, hlv);
1116DEVICE_LUMINANCE_ATTR(buttons, img_lum);
1117
1118static ssize_t wacom_button_image_store(struct device *dev, int button_id,
1119 const char *buf, size_t count)
1120{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001121 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001122 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001123 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001124 unsigned len;
1125 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001126
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001127 if (hdev->bus == BUS_BLUETOOTH) {
1128 len = 256;
1129 xfer_id = WAC_CMD_ICON_BT_XFER;
1130 } else {
1131 len = 1024;
1132 xfer_id = WAC_CMD_ICON_XFER;
1133 }
1134
1135 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001136 return -EINVAL;
1137
1138 mutex_lock(&wacom->lock);
1139
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001140 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001141
1142 mutex_unlock(&wacom->lock);
1143
1144 return err < 0 ? err : count;
1145}
1146
1147#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1148static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1149 struct device_attribute *attr, const char *buf, size_t count) \
1150{ \
1151 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1152} \
Ping Chenge0984bc2014-09-10 12:40:05 -07001153static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001154 NULL, wacom_btnimg##BUTTON_ID##_store)
1155
1156DEVICE_BTNIMG_ATTR(0);
1157DEVICE_BTNIMG_ATTR(1);
1158DEVICE_BTNIMG_ATTR(2);
1159DEVICE_BTNIMG_ATTR(3);
1160DEVICE_BTNIMG_ATTR(4);
1161DEVICE_BTNIMG_ATTR(5);
1162DEVICE_BTNIMG_ATTR(6);
1163DEVICE_BTNIMG_ATTR(7);
1164
Ping Cheng09e7d942011-10-04 23:51:14 -07001165static struct attribute *cintiq_led_attrs[] = {
1166 &dev_attr_status_led0_select.attr,
1167 &dev_attr_status_led1_select.attr,
1168 NULL
1169};
1170
1171static struct attribute_group cintiq_led_attr_group = {
1172 .name = "wacom_led",
1173 .attrs = cintiq_led_attrs,
1174};
1175
1176static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001177 &dev_attr_status0_luminance.attr,
1178 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -07001179 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001180 &dev_attr_buttons_luminance.attr,
1181 &dev_attr_button0_rawimg.attr,
1182 &dev_attr_button1_rawimg.attr,
1183 &dev_attr_button2_rawimg.attr,
1184 &dev_attr_button3_rawimg.attr,
1185 &dev_attr_button4_rawimg.attr,
1186 &dev_attr_button5_rawimg.attr,
1187 &dev_attr_button6_rawimg.attr,
1188 &dev_attr_button7_rawimg.attr,
1189 NULL
1190};
1191
Ping Cheng09e7d942011-10-04 23:51:14 -07001192static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001193 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -07001194 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001195};
1196
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001197static struct attribute *intuos5_led_attrs[] = {
1198 &dev_attr_status0_luminance.attr,
1199 &dev_attr_status_led0_select.attr,
1200 NULL
1201};
1202
1203static struct attribute_group intuos5_led_attr_group = {
1204 .name = "wacom_led",
1205 .attrs = intuos5_led_attrs,
1206};
1207
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001208static struct attribute *generic_led_attrs[] = {
1209 &dev_attr_status0_luminance.attr,
1210 &dev_attr_status_led0_select.attr,
1211 NULL
1212};
1213
1214static struct attribute_group generic_led_attr_group = {
1215 .name = "wacom_led",
1216 .attrs = generic_led_attrs,
1217};
1218
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001219struct wacom_sysfs_group_devres {
1220 struct attribute_group *group;
1221 struct kobject *root;
1222};
1223
1224static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1225{
1226 struct wacom_sysfs_group_devres *devres = res;
1227 struct kobject *kobj = devres->root;
1228
1229 dev_dbg(dev, "%s: dropping reference to %s\n",
1230 __func__, devres->group->name);
1231 sysfs_remove_group(kobj, devres->group);
1232}
1233
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001234static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1235 struct kobject *root,
1236 struct attribute_group *group)
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001237{
1238 struct wacom_sysfs_group_devres *devres;
1239 int error;
1240
1241 devres = devres_alloc(wacom_devm_sysfs_group_release,
1242 sizeof(struct wacom_sysfs_group_devres),
1243 GFP_KERNEL);
1244 if (!devres)
1245 return -ENOMEM;
1246
1247 devres->group = group;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001248 devres->root = root;
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001249
1250 error = sysfs_create_group(devres->root, group);
Arvind Yadav097b8f62018-04-24 13:33:03 +05301251 if (error) {
1252 devres_free(devres);
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001253 return error;
Arvind Yadav097b8f62018-04-24 13:33:03 +05301254 }
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001255
1256 devres_add(&wacom->hdev->dev, devres);
1257
1258 return 0;
1259}
1260
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001261static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1262 struct attribute_group *group)
1263{
1264 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1265 group);
1266}
1267
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001268enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001269{
1270 struct wacom *wacom = led->wacom;
1271
1272 if (wacom->led.max_hlv)
1273 return led->hlv * LED_FULL / wacom->led.max_hlv;
1274
1275 if (wacom->led.max_llv)
1276 return led->llv * LED_FULL / wacom->led.max_llv;
1277
1278 /* device doesn't support brightness tuning */
1279 return LED_FULL;
1280}
1281
1282static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1283{
1284 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1285 struct wacom *wacom = led->wacom;
1286
1287 if (wacom->led.groups[led->group].select != led->id)
1288 return LED_OFF;
1289
1290 return wacom_leds_brightness_get(led);
1291}
1292
1293static int wacom_led_brightness_set(struct led_classdev *cdev,
1294 enum led_brightness brightness)
1295{
1296 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1297 struct wacom *wacom = led->wacom;
1298 int error;
1299
1300 mutex_lock(&wacom->lock);
1301
1302 if (!wacom->led.groups || (brightness == LED_OFF &&
1303 wacom->led.groups[led->group].select != led->id)) {
1304 error = 0;
1305 goto out;
1306 }
1307
1308 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1309 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1310
1311 wacom->led.groups[led->group].select = led->id;
1312
1313 error = wacom_led_control(wacom);
1314
1315out:
1316 mutex_unlock(&wacom->lock);
1317
1318 return error;
1319}
1320
1321static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1322 enum led_brightness brightness)
1323{
1324}
1325
1326static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1327 struct wacom_led *led, unsigned int group,
1328 unsigned int id, bool read_only)
1329{
1330 int error;
1331 char *name;
1332
1333 name = devm_kasprintf(dev, GFP_KERNEL,
1334 "%s::wacom-%d.%d",
1335 dev_name(dev),
1336 group,
1337 id);
1338 if (!name)
1339 return -ENOMEM;
1340
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001341 if (!read_only) {
1342 led->trigger.name = name;
1343 error = devm_led_trigger_register(dev, &led->trigger);
1344 if (error) {
1345 hid_err(wacom->hdev,
1346 "failed to register LED trigger %s: %d\n",
1347 led->cdev.name, error);
1348 return error;
1349 }
1350 }
1351
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001352 led->group = group;
1353 led->id = id;
1354 led->wacom = wacom;
1355 led->llv = wacom->led.llv;
1356 led->hlv = wacom->led.hlv;
1357 led->cdev.name = name;
1358 led->cdev.max_brightness = LED_FULL;
1359 led->cdev.flags = LED_HW_PLUGGABLE;
1360 led->cdev.brightness_get = __wacom_led_brightness_get;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001361 if (!read_only) {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001362 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001363 led->cdev.default_trigger = led->cdev.name;
1364 } else {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001365 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001366 }
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001367
1368 error = devm_led_classdev_register(dev, &led->cdev);
1369 if (error) {
1370 hid_err(wacom->hdev,
1371 "failed to register LED %s: %d\n",
1372 led->cdev.name, error);
1373 led->cdev.name = NULL;
1374 return error;
1375 }
1376
1377 return 0;
1378}
1379
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001380static void wacom_led_groups_release_one(void *data)
1381{
1382 struct wacom_group_leds *group = data;
1383
1384 devres_release_group(group->dev, group);
1385}
1386
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001387static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1388 struct wacom *wacom,
1389 int group_id, int count,
1390 bool read_only)
1391{
1392 struct wacom_led *leds;
1393 int i, error;
1394
1395 if (group_id >= wacom->led.count || count <= 0)
1396 return -EINVAL;
1397
1398 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1399 return -ENOMEM;
1400
Kees Cooka86854d2018-06-12 14:07:58 -07001401 leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL);
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001402 if (!leds) {
1403 error = -ENOMEM;
1404 goto err;
1405 }
1406
1407 wacom->led.groups[group_id].leds = leds;
1408 wacom->led.groups[group_id].count = count;
1409
1410 for (i = 0; i < count; i++) {
1411 error = wacom_led_register_one(dev, wacom, &leds[i],
1412 group_id, i, read_only);
1413 if (error)
1414 goto err;
1415 }
1416
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001417 wacom->led.groups[group_id].dev = dev;
1418
1419 devres_close_group(dev, &wacom->led.groups[group_id]);
1420
1421 /*
1422 * There is a bug (?) in devm_led_classdev_register() in which its
1423 * increments the refcount of the parent. If the parent is an input
1424 * device, that means the ref count never reaches 0 when
1425 * devm_input_device_release() gets called.
1426 * This means that the LEDs are still there after disconnect.
1427 * Manually force the release of the group so that the leds are released
1428 * once we are done using them.
1429 */
1430 error = devm_add_action_or_reset(&wacom->hdev->dev,
1431 wacom_led_groups_release_one,
1432 &wacom->led.groups[group_id]);
1433 if (error)
1434 return error;
1435
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001436 return 0;
1437
1438err:
1439 devres_release_group(dev, &wacom->led.groups[group_id]);
1440 return error;
1441}
1442
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001443struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1444 unsigned int id)
1445{
1446 struct wacom_group_leds *group;
1447
1448 if (group_id >= wacom->led.count)
1449 return NULL;
1450
1451 group = &wacom->led.groups[group_id];
1452
1453 if (!group->leds)
1454 return NULL;
1455
1456 id %= group->count;
1457
1458 return &group->leds[id];
1459}
1460
1461/**
1462 * wacom_led_next: gives the next available led with a wacom trigger.
1463 *
1464 * returns the next available struct wacom_led which has its default trigger
1465 * or the current one if none is available.
1466 */
1467struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1468{
1469 struct wacom_led *next_led;
1470 int group, next;
1471
1472 if (!wacom || !cur)
1473 return NULL;
1474
1475 group = cur->group;
1476 next = cur->id;
1477
1478 do {
1479 next_led = wacom_led_find(wacom, group, ++next);
1480 if (!next_led || next_led == cur)
1481 return next_led;
1482 } while (next_led->cdev.trigger != &next_led->trigger);
1483
1484 return next_led;
1485}
1486
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001487static void wacom_led_groups_release(void *data)
1488{
1489 struct wacom *wacom = data;
1490
1491 wacom->led.groups = NULL;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001492 wacom->led.count = 0;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001493}
1494
1495static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1496{
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001497 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001498 struct wacom_group_leds *groups;
1499 int error;
1500
Kees Cooka86854d2018-06-12 14:07:58 -07001501 groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds),
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001502 GFP_KERNEL);
1503 if (!groups)
1504 return -ENOMEM;
1505
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001506 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001507 if (error)
1508 return error;
1509
1510 wacom->led.groups = groups;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001511 wacom->led.count = count;
1512
1513 return 0;
1514}
1515
1516static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1517 int led_per_group, bool read_only)
1518{
1519 struct device *dev;
1520 int i, error;
1521
1522 if (!wacom->wacom_wac.pad_input)
1523 return -EINVAL;
1524
1525 dev = &wacom->wacom_wac.pad_input->dev;
1526
1527 error = wacom_led_groups_allocate(wacom, group_count);
1528 if (error)
1529 return error;
1530
1531 for (i = 0; i < group_count; i++) {
1532 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1533 led_per_group,
1534 read_only);
1535 if (error)
1536 return error;
1537 }
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001538
1539 return 0;
1540}
1541
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001542int wacom_initialize_leds(struct wacom *wacom)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001543{
1544 int error;
1545
Jason Gerecke862cf552015-06-15 18:01:43 -07001546 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1547 return 0;
1548
Ping Cheng09e7d942011-10-04 23:51:14 -07001549 /* Initialize default values */
1550 switch (wacom->wacom_wac.features.type) {
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001551 case HID_GENERIC:
1552 if (!wacom->generic_has_leds)
1553 return 0;
1554 wacom->led.llv = 100;
1555 wacom->led.max_llv = 100;
1556
1557 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1558 if (error) {
1559 hid_err(wacom->hdev,
1560 "cannot create leds err: %d\n", error);
1561 return error;
1562 }
1563
1564 error = wacom_devm_sysfs_create_group(wacom,
1565 &generic_led_attr_group);
1566 break;
1567
Jason Gereckea19fc982012-06-12 00:27:53 -07001568 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -07001569 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -07001570 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -07001571 case INTUOS4L:
Ping Chengf4fa9a62011-10-04 23:49:42 -07001572 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001573 wacom->led.hlv = 20;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001574 wacom->led.max_llv = 127;
1575 wacom->led.max_hlv = 127;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001576 wacom->led.img_lum = 10;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001577
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001578 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001579 if (error) {
1580 hid_err(wacom->hdev,
1581 "cannot create leds err: %d\n", error);
1582 return error;
1583 }
1584
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001585 error = wacom_devm_sysfs_create_group(wacom,
1586 &intuos4_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001587 break;
1588
Jason Gerecke246835f2011-12-12 00:12:04 -08001589 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -07001590 case WACOM_21UX2:
Ping Cheng09e7d942011-10-04 23:51:14 -07001591 wacom->led.llv = 0;
1592 wacom->led.hlv = 0;
1593 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001594
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001595 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001596 if (error) {
1597 hid_err(wacom->hdev,
1598 "cannot create leds err: %d\n", error);
1599 return error;
1600 }
1601
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001602 error = wacom_devm_sysfs_create_group(wacom,
1603 &cintiq_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001604 break;
1605
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001606 case INTUOS5S:
1607 case INTUOS5:
1608 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -07001609 case INTUOSPS:
1610 case INTUOSPM:
1611 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -07001612 wacom->led.llv = 32;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001613 wacom->led.max_llv = 96;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001614
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001615 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001616 if (error) {
1617 hid_err(wacom->hdev,
1618 "cannot create leds err: %d\n", error);
1619 return error;
1620 }
1621
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001622 error = wacom_devm_sysfs_create_group(wacom,
1623 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001624 break;
1625
Jason Gerecke4922cd22017-01-25 12:08:37 -08001626 case INTUOSP2_BT:
1627 wacom->led.llv = 50;
1628 wacom->led.max_llv = 100;
1629 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1630 if (error) {
1631 hid_err(wacom->hdev,
1632 "cannot create leds err: %d\n", error);
1633 return error;
1634 }
1635 return 0;
1636
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001637 case REMOTE:
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001638 wacom->led.llv = 255;
1639 wacom->led.max_llv = 255;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001640 error = wacom_led_groups_allocate(wacom, 5);
1641 if (error) {
1642 hid_err(wacom->hdev,
1643 "cannot create leds err: %d\n", error);
1644 return error;
1645 }
1646 return 0;
1647
Ping Cheng09e7d942011-10-04 23:51:14 -07001648 default:
1649 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001650 }
1651
Ping Cheng09e7d942011-10-04 23:51:14 -07001652 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -07001653 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -07001654 "cannot create sysfs group err: %d\n", error);
1655 return error;
1656 }
Ping Cheng09e7d942011-10-04 23:51:14 -07001657
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001658 return 0;
1659}
1660
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01001661static void wacom_init_work(struct work_struct *work)
1662{
1663 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1664
1665 _wacom_query_tablet_data(wacom);
1666 wacom_led_control(wacom);
1667}
1668
1669static void wacom_query_tablet_data(struct wacom *wacom)
1670{
1671 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1672}
1673
Chris Bagwella1d552c2012-03-25 23:26:30 -07001674static enum power_supply_property wacom_battery_props[] = {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001675 POWER_SUPPLY_PROP_MODEL_NAME,
Jason Gerecke71fa6412015-03-11 10:25:41 -07001676 POWER_SUPPLY_PROP_PRESENT,
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001677 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001678 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001679 POWER_SUPPLY_PROP_CAPACITY
1680};
1681
1682static int wacom_battery_get_property(struct power_supply *psy,
1683 enum power_supply_property psp,
1684 union power_supply_propval *val)
1685{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001686 struct wacom_battery *battery = power_supply_get_drvdata(psy);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001687 int ret = 0;
1688
1689 switch (psp) {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001690 case POWER_SUPPLY_PROP_MODEL_NAME:
1691 val->strval = battery->wacom->wacom_wac.name;
1692 break;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001693 case POWER_SUPPLY_PROP_PRESENT:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001694 val->intval = battery->bat_connected;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001695 break;
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001696 case POWER_SUPPLY_PROP_SCOPE:
1697 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1698 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001699 case POWER_SUPPLY_PROP_CAPACITY:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001700 val->intval = battery->battery_capacity;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001701 break;
1702 case POWER_SUPPLY_PROP_STATUS:
Jason Gerecke16e45982017-04-28 09:25:33 -07001703 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1704 val->intval = battery->bat_status;
1705 else if (battery->bat_charging)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001706 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001707 else if (battery->battery_capacity == 100 &&
1708 battery->ps_connected)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001709 val->intval = POWER_SUPPLY_STATUS_FULL;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001710 else if (battery->ps_connected)
Jason Gereckeb0882cb2015-03-06 11:47:43 -08001711 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001712 else
1713 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001714 break;
1715 default:
1716 ret = -EINVAL;
1717 break;
1718 }
1719
1720 return ret;
1721}
1722
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001723static int __wacom_initialize_battery(struct wacom *wacom,
1724 struct wacom_battery *battery)
Chris Bagwella1d552c2012-03-25 23:26:30 -07001725{
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001726 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001727 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001728 struct power_supply_config psy_cfg = { .drv_data = battery, };
Benjamin Tissoires136ae5e2016-07-13 18:06:16 +02001729 struct power_supply *ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001730 struct power_supply_desc *bat_desc = &battery->bat_desc;
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001731 unsigned long n;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001732 int error;
1733
1734 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1735 return -ENOMEM;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001736
Benjamin Tissoires99569532016-07-13 18:06:17 +02001737 battery->wacom = wacom;
1738
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001739 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001740
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001741 bat_desc->properties = wacom_battery_props;
1742 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1743 bat_desc->get_property = wacom_battery_get_property;
1744 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1745 bat_desc->name = battery->bat_name;
Benjamin Tissoires96983292016-07-13 18:06:15 +02001746 bat_desc->type = POWER_SUPPLY_TYPE_USB;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001747 bat_desc->use_for_apm = 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001748
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001749 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1750 if (IS_ERR(ps_bat)) {
1751 error = PTR_ERR(ps_bat);
1752 goto err;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001753 }
1754
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001755 power_supply_powers(ps_bat, &wacom->hdev->dev);
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001756
1757 battery->battery = ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001758
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001759 devres_close_group(dev, bat_desc);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001760 return 0;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001761
1762err:
1763 devres_release_group(dev, bat_desc);
1764 return error;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001765}
1766
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001767static int wacom_initialize_battery(struct wacom *wacom)
1768{
1769 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1770 return __wacom_initialize_battery(wacom, &wacom->battery);
1771
1772 return 0;
1773}
1774
Chris Bagwella1d552c2012-03-25 23:26:30 -07001775static void wacom_destroy_battery(struct wacom *wacom)
1776{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001777 if (wacom->battery.battery) {
1778 devres_release_group(&wacom->hdev->dev,
1779 &wacom->battery.bat_desc);
1780 wacom->battery.battery = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001781 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001782}
1783
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001784static ssize_t wacom_show_speed(struct device *dev,
1785 struct device_attribute
1786 *attr, char *buf)
1787{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001788 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001789 struct wacom *wacom = hid_get_drvdata(hdev);
1790
1791 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1792}
1793
1794static ssize_t wacom_store_speed(struct device *dev,
1795 struct device_attribute *attr,
1796 const char *buf, size_t count)
1797{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001798 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001799 struct wacom *wacom = hid_get_drvdata(hdev);
1800 u8 new_speed;
1801
1802 if (kstrtou8(buf, 0, &new_speed))
1803 return -EINVAL;
1804
1805 if (new_speed != 0 && new_speed != 1)
1806 return -EINVAL;
1807
1808 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1809
1810 return count;
1811}
1812
Ping Chenge0984bc2014-09-10 12:40:05 -07001813static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001814 wacom_show_speed, wacom_store_speed);
1815
Aaron Skomra72b236d2015-08-20 16:05:17 -07001816
1817static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1818 struct kobj_attribute *kattr,
1819 char *buf, int index)
1820{
Geliang Tang2cf83832015-12-27 17:25:24 +08001821 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001822 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001823 struct wacom *wacom = hid_get_drvdata(hdev);
1824 u8 mode;
1825
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001826 mode = wacom->led.groups[index].select;
Christos Gkekasbc35f732017-07-08 20:25:48 +01001827 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001828}
1829
1830#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1831static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1832 struct kobj_attribute *kattr, char *buf) \
1833{ \
1834 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1835} \
1836static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1837 .attr = {.name = "remote_mode", \
1838 .mode = DEV_ATTR_RO_PERM}, \
1839 .show = wacom_show_remote##SET_ID##_mode, \
1840}; \
1841static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1842 &remote##SET_ID##_mode_attr.attr, \
1843 NULL \
1844}; \
1845static struct attribute_group remote##SET_ID##_serial_group = { \
1846 .name = NULL, \
1847 .attrs = remote##SET_ID##_serial_attrs, \
1848}
1849
1850DEVICE_EKR_ATTR_GROUP(0);
1851DEVICE_EKR_ATTR_GROUP(1);
1852DEVICE_EKR_ATTR_GROUP(2);
1853DEVICE_EKR_ATTR_GROUP(3);
1854DEVICE_EKR_ATTR_GROUP(4);
1855
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02001856static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1857 int index)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001858{
1859 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001860 struct wacom_remote *remote = wacom->remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001861
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001862 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001863 GFP_KERNEL,
1864 "%d", serial);
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001865 if (!remote->remotes[index].group.name)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001866 return -ENOMEM;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001867
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001868 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001869 &remote->remotes[index].group);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001870 if (error) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001871 remote->remotes[index].group.name = NULL;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001872 hid_err(wacom->hdev,
1873 "cannot create sysfs group err: %d\n", error);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001874 return error;
1875 }
1876
1877 return 0;
1878}
1879
Aaron Skomra72b236d2015-08-20 16:05:17 -07001880static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1881{
1882 const size_t buf_size = 2;
1883 unsigned char *buf;
1884 int retval;
1885
1886 buf = kzalloc(buf_size, GFP_KERNEL);
1887 if (!buf)
1888 return -ENOMEM;
1889
1890 buf[0] = WAC_CMD_DELETE_PAIRING;
1891 buf[1] = selector;
1892
1893 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1894 buf_size, WAC_CMD_RETRIES);
1895 kfree(buf);
1896
1897 return retval;
1898}
1899
1900static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1901 struct kobj_attribute *attr,
1902 const char *buf, size_t count)
1903{
1904 unsigned char selector = 0;
Geliang Tang2cf83832015-12-27 17:25:24 +08001905 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001906 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001907 struct wacom *wacom = hid_get_drvdata(hdev);
1908 int err;
1909
1910 if (!strncmp(buf, "*\n", 2)) {
1911 selector = WAC_CMD_UNPAIR_ALL;
1912 } else {
1913 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1914 buf);
1915 return -1;
1916 }
1917
1918 mutex_lock(&wacom->lock);
1919
1920 err = wacom_cmd_unpair_remote(wacom, selector);
1921 mutex_unlock(&wacom->lock);
1922
1923 return err < 0 ? err : count;
1924}
1925
1926static struct kobj_attribute unpair_remote_attr = {
1927 .attr = {.name = "unpair_remote", .mode = 0200},
1928 .store = wacom_store_unpair_remote,
1929};
1930
1931static const struct attribute *remote_unpair_attrs[] = {
1932 &unpair_remote_attr.attr,
1933 NULL
1934};
1935
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001936static void wacom_remotes_destroy(void *data)
1937{
1938 struct wacom *wacom = data;
1939 struct wacom_remote *remote = wacom->remote;
1940
1941 if (!remote)
1942 return;
1943
1944 kobject_put(remote->remote_dir);
1945 kfifo_free(&remote->remote_fifo);
1946 wacom->remote = NULL;
1947}
1948
1949static int wacom_initialize_remotes(struct wacom *wacom)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001950{
1951 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001952 struct wacom_remote *remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001953 int i;
1954
1955 if (wacom->wacom_wac.features.type != REMOTE)
1956 return 0;
1957
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001958 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1959 GFP_KERNEL);
1960 if (!remote)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001961 return -ENOMEM;
1962
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001963 wacom->remote = remote;
1964
1965 spin_lock_init(&remote->remote_lock);
1966
1967 error = kfifo_alloc(&remote->remote_fifo,
1968 5 * sizeof(struct wacom_remote_data),
1969 GFP_KERNEL);
1970 if (error) {
1971 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1972 return -ENOMEM;
1973 }
1974
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001975 remote->remotes[0].group = remote0_serial_group;
1976 remote->remotes[1].group = remote1_serial_group;
1977 remote->remotes[2].group = remote2_serial_group;
1978 remote->remotes[3].group = remote3_serial_group;
1979 remote->remotes[4].group = remote4_serial_group;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001980
1981 remote->remote_dir = kobject_create_and_add("wacom_remote",
1982 &wacom->hdev->dev.kobj);
1983 if (!remote->remote_dir)
1984 return -ENOMEM;
1985
1986 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001987
1988 if (error) {
1989 hid_err(wacom->hdev,
1990 "cannot create sysfs group err: %d\n", error);
1991 return error;
1992 }
1993
1994 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001995 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001996 remote->remotes[i].serial = 0;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001997 }
1998
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001999 error = devm_add_action_or_reset(&wacom->hdev->dev,
2000 wacom_remotes_destroy, wacom);
2001 if (error)
2002 return error;
2003
Aaron Skomra72b236d2015-08-20 16:05:17 -07002004 return 0;
2005}
2006
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002007static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002008{
2009 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07002010 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002011 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002012
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002013 input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002014 if (!input_dev)
2015 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002016
Jason Gerecke2bdd1632015-07-13 18:03:45 -07002017 input_dev->name = wacom_wac->features.name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07002018 input_dev->phys = hdev->phys;
2019 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002020 input_dev->open = wacom_open;
2021 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07002022 input_dev->uniq = hdev->uniq;
2023 input_dev->id.bustype = hdev->bus;
2024 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04002025 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07002026 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002027 input_set_drvdata(input_dev, wacom);
2028
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002029 return input_dev;
2030}
2031
Jason Gerecked9f2d202015-07-13 18:03:44 -07002032static int wacom_allocate_inputs(struct wacom *wacom)
2033{
2034 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2035
2036 wacom_wac->pen_input = wacom_allocate_input(wacom);
2037 wacom_wac->touch_input = wacom_allocate_input(wacom);
2038 wacom_wac->pad_input = wacom_allocate_input(wacom);
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002039 if (!wacom_wac->pen_input ||
2040 !wacom_wac->touch_input ||
2041 !wacom_wac->pad_input)
Jason Gerecked9f2d202015-07-13 18:03:44 -07002042 return -ENOMEM;
Jason Gerecked9f2d202015-07-13 18:03:44 -07002043
Jason Gerecke2bdd1632015-07-13 18:03:45 -07002044 wacom_wac->pen_input->name = wacom_wac->pen_name;
Jason Gerecked9f2d202015-07-13 18:03:44 -07002045 wacom_wac->touch_input->name = wacom_wac->touch_name;
2046 wacom_wac->pad_input->name = wacom_wac->pad_name;
2047
2048 return 0;
2049}
2050
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07002051static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002052{
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002053 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002054 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Jason Gerecke2636a3f2015-06-15 18:01:44 -07002055 int error = 0;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002056
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002057 pen_input_dev = wacom_wac->pen_input;
2058 touch_input_dev = wacom_wac->touch_input;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04002059 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002060
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002061 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04002062 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002063
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002064 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
2065 if (error) {
2066 /* no pen in use on this interface */
2067 input_free_device(pen_input_dev);
2068 wacom_wac->pen_input = NULL;
2069 pen_input_dev = NULL;
2070 } else {
2071 error = input_register_device(pen_input_dev);
Ping Cheng30ebc1a2014-11-18 13:29:16 -08002072 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002073 goto fail;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002074 }
2075
2076 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
2077 if (error) {
2078 /* no touch in use on this interface */
2079 input_free_device(touch_input_dev);
2080 wacom_wac->touch_input = NULL;
2081 touch_input_dev = NULL;
2082 } else {
2083 error = input_register_device(touch_input_dev);
2084 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002085 goto fail;
Ping Cheng30ebc1a2014-11-18 13:29:16 -08002086 }
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002087
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002088 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2089 if (error) {
2090 /* no pad in use on this interface */
2091 input_free_device(pad_input_dev);
2092 wacom_wac->pad_input = NULL;
2093 pad_input_dev = NULL;
2094 } else {
2095 error = input_register_device(pad_input_dev);
2096 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002097 goto fail;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002098 }
2099
Ping Cheng19635182012-04-29 21:09:18 -07002100 return 0;
2101
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002102fail:
2103 wacom_wac->pad_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002104 wacom_wac->touch_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002105 wacom_wac->pen_input = NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002106 return error;
2107}
2108
Jason Gerecke0be01712015-08-05 15:44:53 -07002109/*
2110 * Not all devices report physical dimensions from HID.
2111 * Compute the default from hardcoded logical dimension
2112 * and resolution before driver overwrites them.
2113 */
2114static void wacom_set_default_phy(struct wacom_features *features)
2115{
2116 if (features->x_resolution) {
2117 features->x_phy = (features->x_max * 100) /
2118 features->x_resolution;
2119 features->y_phy = (features->y_max * 100) /
2120 features->y_resolution;
2121 }
2122}
2123
2124static void wacom_calculate_res(struct wacom_features *features)
2125{
2126 /* set unit to "100th of a mm" for devices not reported by HID */
2127 if (!features->unit) {
2128 features->unit = 0x11;
2129 features->unitExpo = -3;
2130 }
2131
2132 features->x_resolution = wacom_calc_hid_res(features->x_max,
2133 features->x_phy,
2134 features->unit,
2135 features->unitExpo);
2136 features->y_resolution = wacom_calc_hid_res(features->y_max,
2137 features->y_phy,
2138 features->unit,
2139 features->unitExpo);
2140}
2141
Jason Gereckefce99572015-03-06 11:47:40 -08002142void wacom_battery_work(struct work_struct *work)
2143{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002144 struct wacom *wacom = container_of(work, struct wacom, battery_work);
Jason Gereckefce99572015-03-06 11:47:40 -08002145
2146 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02002147 !wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08002148 wacom_initialize_battery(wacom);
2149 }
2150 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02002151 wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08002152 wacom_destroy_battery(wacom);
2153 }
2154}
2155
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002156static size_t wacom_compute_pktlen(struct hid_device *hdev)
2157{
2158 struct hid_report_enum *report_enum;
2159 struct hid_report *report;
2160 size_t size = 0;
2161
2162 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2163
2164 list_for_each_entry(report, &report_enum->report_list, list) {
Mathieu Magnaudetdabb05c62014-11-27 16:02:36 +01002165 size_t report_size = hid_report_len(report);
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002166 if (report_size > size)
2167 size = report_size;
2168 }
2169
2170 return size;
2171}
2172
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002173static void wacom_update_name(struct wacom *wacom, const char *suffix)
Ping Chengc24eab42015-04-24 15:32:51 -07002174{
2175 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2176 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke44b52502015-06-15 18:01:41 -07002177 char name[WACOM_NAME_MAX];
Ping Chengc24eab42015-04-24 15:32:51 -07002178
2179 /* Generic devices name unspecified */
2180 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
Jason Gerecke09dc28a2017-07-24 09:46:19 -07002181 char *product_name = wacom->hdev->name;
Ping Chengc24eab42015-04-24 15:32:51 -07002182
Jason Gerecke09dc28a2017-07-24 09:46:19 -07002183 if (hid_is_using_ll_driver(wacom->hdev, &usb_hid_driver)) {
2184 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2185 struct usb_device *dev = interface_to_usbdev(intf);
2186 product_name = dev->product;
Ping Chengc24eab42015-04-24 15:32:51 -07002187 }
Jason Gerecke09dc28a2017-07-24 09:46:19 -07002188
2189 if (wacom->hdev->bus == BUS_I2C) {
2190 snprintf(name, sizeof(name), "%s %X",
2191 features->name, wacom->hdev->product);
2192 } else if (strstr(product_name, "Wacom") ||
2193 strstr(product_name, "wacom") ||
2194 strstr(product_name, "WACOM")) {
2195 strlcpy(name, product_name, sizeof(name));
2196 } else {
2197 snprintf(name, sizeof(name), "Wacom %s", product_name);
2198 }
2199
2200 /* strip out excess whitespaces */
2201 while (1) {
2202 char *gap = strstr(name, " ");
2203 if (gap == NULL)
2204 break;
2205 /* shift everything including the terminator */
2206 memmove(gap, gap+1, strlen(gap));
2207 }
2208
2209 /* get rid of trailing whitespace */
2210 if (name[strlen(name)-1] == ' ')
2211 name[strlen(name)-1] = '\0';
Ping Chengc24eab42015-04-24 15:32:51 -07002212 } else {
Jason Gerecke44b52502015-06-15 18:01:41 -07002213 strlcpy(name, features->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07002214 }
2215
Benjamin Tissoires99569532016-07-13 18:06:17 +02002216 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2217 name, suffix);
2218
Ping Chengc24eab42015-04-24 15:32:51 -07002219 /* Append the device type to the name */
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002220 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002221 "%s%s Pen", name, suffix);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002222 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002223 "%s%s Finger", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07002224 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002225 "%s%s Pad", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07002226}
2227
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002228static void wacom_release_resources(struct wacom *wacom)
2229{
2230 struct hid_device *hdev = wacom->hdev;
2231
2232 if (!wacom->resources)
2233 return;
2234
2235 devres_release_group(&hdev->dev, wacom);
2236
2237 wacom->resources = false;
2238
2239 wacom->wacom_wac.pen_input = NULL;
2240 wacom->wacom_wac.touch_input = NULL;
2241 wacom->wacom_wac.pad_input = NULL;
2242}
2243
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002244static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2245{
2246 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2247 wacom_wac->shared->type = wacom_wac->features.type;
2248 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2249 }
2250
Ping Chengd793ff82017-02-14 21:27:45 -08002251 if (wacom_wac->has_mute_touch_switch) {
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002252 wacom_wac->shared->has_mute_touch_switch = true;
Ping Chengd793ff82017-02-14 21:27:45 -08002253 wacom_wac->shared->is_touch_on = true;
2254 }
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002255
2256 if (wacom_wac->shared->has_mute_touch_switch &&
2257 wacom_wac->shared->touch_input) {
2258 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2259 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2260 SW_MUTE_DEVICE);
2261 }
2262}
2263
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002264static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
Ping Cheng3bea7332006-07-13 18:01:36 -07002265{
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002266 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2267 struct wacom_features *features = &wacom_wac->features;
2268 struct hid_device *hdev = wacom->hdev;
Jason Childse33da8a2010-02-17 22:38:31 -08002269 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002270 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07002271
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002272 features->pktlen = wacom_compute_pktlen(hdev);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002273 if (features->pktlen > WACOM_PKGLEN_MAX)
2274 return -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07002275
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002276 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2277 return -ENOMEM;
2278
2279 wacom->resources = true;
2280
Jason Gerecke3f14a632015-08-03 10:17:05 -07002281 error = wacom_allocate_inputs(wacom);
2282 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002283 goto fail;
Benjamin Tissoires494078b2014-09-23 12:08:07 -04002284
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05002285 /*
2286 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2287 * into debug mode for the touch part.
2288 * We ignore the other interfaces.
2289 */
2290 if (features->type == BAMBOO_PAD) {
2291 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2292 features->type = HID_GENERIC;
2293 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2294 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2295 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002296 goto fail;
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05002297 }
2298 }
2299
Ping Cheng401d7d12013-07-28 00:38:54 -07002300 /* set the default size in case we do not get them from hid */
2301 wacom_set_default_phy(features);
2302
Ping Chengf393ee22012-04-29 21:09:17 -07002303 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07002304 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng42f4f272015-04-15 16:53:54 -07002305 wacom_setup_device_quirks(wacom);
Jason Gerecke042628a2015-04-30 17:51:54 -07002306
Jason Gereckeaa86b182015-06-15 18:01:42 -07002307 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2308 features->type != WIRELESS) {
Jason Gerecke8e116d32015-04-30 17:51:55 -07002309 error = features->type == HID_GENERIC ? -ENODEV : 0;
2310
Jason Gerecke042628a2015-04-30 17:51:54 -07002311 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
Jason Gerecke8e116d32015-04-30 17:51:55 -07002312 hdev->name,
2313 error ? "Ignoring" : "Assuming pen");
2314
2315 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002316 goto fail;
Jason Gerecke042628a2015-04-30 17:51:54 -07002317
Jason Gereckeaa86b182015-06-15 18:01:42 -07002318 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -07002319 }
2320
Ping Cheng401d7d12013-07-28 00:38:54 -07002321 wacom_calculate_res(features);
2322
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002323 wacom_update_name(wacom, wireless ? " (WL)" : "");
Ping Cheng4492eff2010-03-19 22:18:15 -07002324
Aaron Armstrong Skomra8b407352017-03-29 10:35:39 -07002325 /* pen only Bamboo neither support touch nor pad */
2326 if ((features->type == BAMBOO_PEN) &&
2327 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2328 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2329 error = -ENODEV;
2330 goto fail;
2331 }
2332
Jason Gereckea9ce7852017-01-17 15:38:58 -08002333 error = wacom_add_shared_data(hdev);
2334 if (error)
2335 goto fail;
Ping Cheng49b764a2010-02-20 00:53:49 -08002336
Jason Gereckeccad85c2015-08-03 10:17:04 -07002337 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002338 (features->quirks & WACOM_QUIRK_BATTERY)) {
2339 error = wacom_initialize_battery(wacom);
2340 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002341 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002342 }
2343
Jason Gerecke3f14a632015-08-03 10:17:05 -07002344 error = wacom_register_inputs(wacom);
2345 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002346 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002347
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002348 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002349 error = wacom_initialize_leds(wacom);
2350 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002351 goto fail;
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002352
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002353 error = wacom_initialize_remotes(wacom);
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002354 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002355 goto fail;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002356 }
2357
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002358 if (features->type == HID_GENERIC)
2359 connect_mask |= HID_CONNECT_DRIVER;
2360
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002361 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002362 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002363 if (error) {
2364 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002365 goto fail;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002366 }
2367
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002368 if (!wireless) {
2369 /* Note that if query fails it is not a hard failure */
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002370 wacom_query_tablet_data(wacom);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002371 }
Jason Gerecke86e88f02015-11-03 16:57:58 -08002372
2373 /* touch only Bamboo doesn't support pen */
2374 if ((features->type == BAMBOO_TOUCH) &&
2375 (features->device_type & WACOM_DEVICETYPE_PEN)) {
Aaron Armstrong Skomra4d20c332017-03-29 11:41:28 -07002376 cancel_delayed_work_sync(&wacom->init_work);
2377 _wacom_query_tablet_data(wacom);
Jason Gerecke86e88f02015-11-03 16:57:58 -08002378 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002379 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08002380 }
2381
Jason Gereckeccad85c2015-08-03 10:17:04 -07002382 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002383 error = hid_hw_open(hdev);
2384
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002385 wacom_set_shared_values(wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002386 devres_close_group(&hdev->dev, wacom);
2387
Ping Cheng3bea7332006-07-13 18:01:36 -07002388 return 0;
2389
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002390fail_quirks:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002391 hid_hw_stop(hdev);
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002392fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002393 wacom_release_resources(wacom);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002394 return error;
2395}
2396
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002397static void wacom_wireless_work(struct work_struct *work)
2398{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002399 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002400 struct usb_device *usbdev = wacom->usbdev;
2401 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2402 struct hid_device *hdev1, *hdev2;
2403 struct wacom *wacom1, *wacom2;
2404 struct wacom_wac *wacom_wac1, *wacom_wac2;
2405 int error;
2406
2407 /*
2408 * Regardless if this is a disconnect or a new tablet,
2409 * remove any existing input and battery devices.
2410 */
2411
2412 wacom_destroy_battery(wacom);
2413
2414 /* Stylus interface */
2415 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2416 wacom1 = hid_get_drvdata(hdev1);
2417 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002418 wacom_release_resources(wacom1);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002419
2420 /* Touch interface */
2421 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2422 wacom2 = hid_get_drvdata(hdev2);
2423 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002424 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002425
2426 if (wacom_wac->pid == 0) {
2427 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002428 } else {
2429 const struct hid_device_id *id = wacom_ids;
2430
2431 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2432 wacom_wac->pid);
2433
2434 while (id->bus) {
2435 if (id->vendor == USB_VENDOR_ID_WACOM &&
2436 id->product == wacom_wac->pid)
2437 break;
2438 id++;
2439 }
2440
2441 if (!id->bus) {
2442 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2443 return;
2444 }
2445
2446 /* Stylus interface */
2447 wacom_wac1->features =
2448 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002449
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002450 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002451 hid_hw_stop(hdev1);
2452 error = wacom_parse_and_register(wacom1, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002453 if (error)
2454 goto fail;
2455
2456 /* Touch interface */
2457 if (wacom_wac1->features.touch_max ||
2458 (wacom_wac1->features.type >= INTUOSHT &&
2459 wacom_wac1->features.type <= BAMBOO_PT)) {
2460 wacom_wac2->features =
2461 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002462 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002463 hid_hw_stop(hdev2);
2464 error = wacom_parse_and_register(wacom2, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002465 if (error)
2466 goto fail;
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002467 }
2468
Benjamin Tissoires99569532016-07-13 18:06:17 +02002469 strlcpy(wacom_wac->name, wacom_wac1->name,
2470 sizeof(wacom_wac->name));
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002471 error = wacom_initialize_battery(wacom);
2472 if (error)
2473 goto fail;
2474 }
2475
2476 return;
2477
2478fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002479 wacom_release_resources(wacom1);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002480 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002481 return;
2482}
2483
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002484static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2485{
2486 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002487 u32 serial = remote->remotes[index].serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002488 int i;
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002489 unsigned long flags;
2490
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002491 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002492 if (remote->remotes[i].serial == serial) {
Aaron Armstrong Skomra791ae272017-12-07 12:31:56 -08002493
2494 spin_lock_irqsave(&remote->remote_lock, flags);
2495 remote->remotes[i].registered = false;
2496 spin_unlock_irqrestore(&remote->remote_lock, flags);
2497
2498 if (remote->remotes[i].battery.battery)
2499 devres_release_group(&wacom->hdev->dev,
2500 &remote->remotes[i].battery.bat_desc);
2501
2502 if (remote->remotes[i].group.name)
2503 devres_release_group(&wacom->hdev->dev,
2504 &remote->remotes[i]);
2505
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002506 remote->remotes[i].serial = 0;
2507 remote->remotes[i].group.name = NULL;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002508 remote->remotes[i].battery.battery = NULL;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002509 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2510 }
2511 }
2512}
2513
2514static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2515 unsigned int index)
2516{
2517 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002518 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002519 int error, k;
2520
2521 /* A remote can pair more than once with an EKR,
2522 * check to make sure this serial isn't already paired.
2523 */
2524 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002525 if (remote->remotes[k].serial == serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002526 break;
2527 }
2528
2529 if (k < WACOM_MAX_REMOTES) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002530 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002531 return 0;
2532 }
2533
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002534 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002535 return -ENOMEM;
2536
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002537 error = wacom_remote_create_attr_group(wacom, serial, index);
2538 if (error)
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002539 goto fail;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002540
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002541 remote->remotes[index].input = wacom_allocate_input(wacom);
2542 if (!remote->remotes[index].input) {
2543 error = -ENOMEM;
2544 goto fail;
2545 }
2546 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2547 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2548
2549 if (!remote->remotes[index].input->name) {
2550 error = -EINVAL;
2551 goto fail;
2552 }
2553
2554 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2555 &wacom->wacom_wac);
2556 if (error)
2557 goto fail;
2558
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002559 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002560
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002561 error = input_register_device(remote->remotes[index].input);
2562 if (error)
2563 goto fail;
2564
Benjamin Tissoires97f55412016-07-13 18:06:10 +02002565 error = wacom_led_groups_alloc_and_register_one(
2566 &remote->remotes[index].input->dev,
2567 wacom, index, 3, true);
2568 if (error)
2569 goto fail;
2570
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002571 remote->remotes[index].registered = true;
2572
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002573 devres_close_group(dev, &remote->remotes[index]);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002574 return 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002575
2576fail:
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002577 devres_release_group(dev, &remote->remotes[index]);
2578 remote->remotes[index].serial = 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002579 return error;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002580}
2581
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002582static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2583{
2584 struct wacom_remote *remote = wacom->remote;
2585 int error;
2586
2587 if (!remote->remotes[index].registered)
2588 return 0;
2589
2590 if (remote->remotes[index].battery.battery)
2591 return 0;
2592
2593 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2594 return 0;
2595
2596 error = __wacom_initialize_battery(wacom,
2597 &wacom->remote->remotes[index].battery);
2598 if (error)
2599 return error;
2600
2601 return 0;
2602}
2603
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002604static void wacom_remote_work(struct work_struct *work)
2605{
2606 struct wacom *wacom = container_of(work, struct wacom, remote_work);
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002607 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002608 struct wacom_remote_data data;
2609 unsigned long flags;
2610 unsigned int count;
2611 u32 serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002612 int i;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002613
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002614 spin_lock_irqsave(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002615
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002616 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002617
2618 if (count != sizeof(data)) {
2619 hid_err(wacom->hdev,
2620 "workitem triggered without status available\n");
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002621 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002622 return;
2623 }
2624
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002625 if (!kfifo_is_empty(&remote->remote_fifo))
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002626 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2627
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002628 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002629
2630 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2631 serial = data.remote[i].serial;
2632 if (data.remote[i].connected) {
2633
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002634 if (remote->remotes[i].serial == serial) {
2635 wacom_remote_attach_battery(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002636 continue;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002637 }
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002638
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002639 if (remote->remotes[i].serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002640 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002641
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002642 wacom_remote_create_one(wacom, serial, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002643
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002644 } else if (remote->remotes[i].serial) {
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002645 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002646 }
2647 }
2648}
2649
Benjamin Tissoires4082da82017-02-14 21:27:18 -08002650static void wacom_mode_change_work(struct work_struct *work)
2651{
2652 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2653 struct wacom_shared *shared = wacom->wacom_wac.shared;
2654 struct wacom *wacom1 = NULL;
2655 struct wacom *wacom2 = NULL;
2656 bool is_direct = wacom->wacom_wac.is_direct_mode;
2657 int error = 0;
2658
2659 if (shared->pen) {
2660 wacom1 = hid_get_drvdata(shared->pen);
2661 wacom_release_resources(wacom1);
2662 hid_hw_stop(wacom1->hdev);
2663 wacom1->wacom_wac.has_mode_change = true;
2664 wacom1->wacom_wac.is_direct_mode = is_direct;
2665 }
2666
2667 if (shared->touch) {
2668 wacom2 = hid_get_drvdata(shared->touch);
2669 wacom_release_resources(wacom2);
2670 hid_hw_stop(wacom2->hdev);
2671 wacom2->wacom_wac.has_mode_change = true;
2672 wacom2->wacom_wac.is_direct_mode = is_direct;
2673 }
2674
2675 if (wacom1) {
2676 error = wacom_parse_and_register(wacom1, false);
2677 if (error)
2678 return;
2679 }
2680
2681 if (wacom2) {
2682 error = wacom_parse_and_register(wacom2, false);
2683 if (error)
2684 return;
2685 }
2686
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002687 return;
2688}
2689
2690static int wacom_probe(struct hid_device *hdev,
2691 const struct hid_device_id *id)
2692{
2693 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2694 struct usb_device *dev = interface_to_usbdev(intf);
2695 struct wacom *wacom;
2696 struct wacom_wac *wacom_wac;
2697 struct wacom_features *features;
2698 int error;
2699
2700 if (!id->driver_data)
2701 return -EINVAL;
2702
2703 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2704
2705 /* hid-core sets this quirk for the boot interface */
2706 hdev->quirks &= ~HID_QUIRK_NOGET;
2707
Benjamin Tissoires19b64332016-07-13 18:05:58 +02002708 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002709 if (!wacom)
2710 return -ENOMEM;
2711
2712 hid_set_drvdata(hdev, wacom);
2713 wacom->hdev = hdev;
2714
2715 wacom_wac = &wacom->wacom_wac;
2716 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2717 features = &wacom_wac->features;
2718
2719 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2720 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002721 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002722 }
2723
Jason Gerecke83417202017-11-10 11:50:01 -08002724 error = kfifo_alloc(&wacom_wac->pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
2725 if (error)
2726 goto fail;
2727
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002728 wacom_wac->hid_data.inputmode = -1;
Jason Gerecke326ea2a2016-04-04 11:26:52 -07002729 wacom_wac->mode_report = -1;
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002730
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002731 wacom->usbdev = dev;
2732 wacom->intf = intf;
2733 mutex_init(&wacom->lock);
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002734 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002735 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2736 INIT_WORK(&wacom->battery_work, wacom_battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002737 INIT_WORK(&wacom->remote_work, wacom_remote_work);
Benjamin Tissoires4082da82017-02-14 21:27:18 -08002738 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002739
2740 /* ask for the report descriptor to be loaded by HID */
2741 error = hid_parse(hdev);
2742 if (error) {
2743 hid_err(hdev, "parse failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002744 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002745 }
2746
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002747 error = wacom_parse_and_register(wacom, false);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002748 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002749 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002750
2751 if (hdev->bus == BUS_BLUETOOTH) {
2752 error = device_create_file(&hdev->dev, &dev_attr_speed);
2753 if (error)
2754 hid_warn(hdev,
2755 "can't create sysfs speed attribute err: %d\n",
2756 error);
2757 }
2758
2759 return 0;
2760
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002761fail:
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002762 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04002763 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07002764}
2765
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002766static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07002767{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002768 struct wacom *wacom = hid_get_drvdata(hdev);
Benjamin Tissoiresf6205162016-02-12 17:27:45 +01002769 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2770 struct wacom_features *features = &wacom_wac->features;
2771
2772 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2773 hid_hw_close(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07002774
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002775 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002776
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002777 cancel_delayed_work_sync(&wacom->init_work);
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002778 cancel_work_sync(&wacom->wireless_work);
2779 cancel_work_sync(&wacom->battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002780 cancel_work_sync(&wacom->remote_work);
Benjamin Tissoires4082da82017-02-14 21:27:18 -08002781 cancel_work_sync(&wacom->mode_change_work);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002782 if (hdev->bus == BUS_BLUETOOTH)
2783 device_remove_file(&hdev->dev, &dev_attr_speed);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002784
Benjamin Tissoiresc0265a942017-01-20 16:20:12 +01002785 /* make sure we don't trigger the LEDs */
2786 wacom_led_groups_release(wacom);
Aaron Armstrong Skomrab6b1f192017-03-06 10:54:58 -08002787
2788 if (wacom->wacom_wac.features.type != REMOTE)
2789 wacom_release_resources(wacom);
Benjamin Tissoires5b779fc2017-01-20 16:20:11 +01002790
Jason Gerecke83417202017-11-10 11:50:01 -08002791 kfifo_free(&wacom_wac->pen_fifo);
2792
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002793 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04002794}
2795
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002796#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002797static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002798{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002799 struct wacom *wacom = hid_get_drvdata(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002800
2801 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002802
2803 /* switch to wacom mode first */
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002804 _wacom_query_tablet_data(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002805 wacom_led_control(wacom);
2806
Oliver Neukume7224092008-04-15 01:31:57 -04002807 mutex_unlock(&wacom->lock);
2808
2809 return 0;
2810}
2811
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002812static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002813{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002814 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002815}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002816#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04002817
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002818static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07002819 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08002820 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07002821 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002822 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002823 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002824#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04002825 .resume = wacom_resume,
2826 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002827#endif
2828 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07002829};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002830module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07002831
2832MODULE_VERSION(DRIVER_VERSION);
2833MODULE_AUTHOR(DRIVER_AUTHOR);
2834MODULE_DESCRIPTION(DRIVER_DESC);
Grant Grundler7021b602017-01-05 11:07:04 -08002835MODULE_LICENSE("GPL");