blob: 0bdd85d486feeb2fec33f7651f201df146c8cd71 [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
255 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
256 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
257 hdev->product == 0x0358 &&
258 WACOM_PEN_FIELD(field) &&
259 equivalent_usage == HID_GD_Y) {
260 field->logical_maximum = 43200;
261 }
262}
263
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700264static void wacom_feature_mapping(struct hid_device *hdev,
265 struct hid_field *field, struct hid_usage *usage)
Chris Bagwell41343612011-10-26 22:32:52 -0700266{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700267 struct wacom *wacom = hid_get_drvdata(hdev);
268 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400269 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
Aaron Armstrong Skomraac2423c92017-01-25 12:08:40 -0800270 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400271 u8 *data;
272 int ret;
Aaron Ma3064a032018-02-03 23:57:15 +0800273 u32 n;
Chris Bagwell41343612011-10-26 22:32:52 -0700274
Jason Gerecke57832512018-06-25 13:24:35 -0700275 wacom_hid_usage_quirk(hdev, field, usage);
276
Aaron Armstrong Skomraac2423c92017-01-25 12:08:40 -0800277 switch (equivalent_usage) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700278 case HID_DG_CONTACTMAX:
279 /* leave touch_max as is if predefined */
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400280 if (!features->touch_max) {
281 /* read manually */
282 data = kzalloc(2, GFP_KERNEL);
283 if (!data)
284 break;
285 data[0] = field->report->id;
286 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700287 data, 2, WAC_CMD_RETRIES);
288 if (ret == 2) {
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400289 features->touch_max = data[1];
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700290 } else {
291 features->touch_max = 16;
292 hid_warn(hdev, "wacom_feature_mapping: "
293 "could not get HID_DG_CONTACTMAX, "
294 "defaulting to %d\n",
295 features->touch_max);
296 }
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400297 kfree(data);
298 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700299 break;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400300 case HID_DG_INPUTMODE:
301 /* Ignore if value index is out of bounds. */
302 if (usage->usage_index >= field->report_count) {
303 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
304 break;
305 }
306
307 hid_data->inputmode = field->report->id;
308 hid_data->inputmode_index = usage->usage_index;
309 break;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700310
311 case HID_UP_DIGITIZER:
312 if (field->report->id == 0x0B &&
Jason Gerecke8de82282016-10-19 18:03:37 -0700313 (field->application == WACOM_HID_G9_PEN ||
314 field->application == WACOM_HID_G11_PEN)) {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700315 wacom->wacom_wac.mode_report = field->report->id;
316 wacom->wacom_wac.mode_value = 0;
317 }
318 break;
319
Jason Gereckec9c09582016-10-19 18:03:43 -0700320 case WACOM_HID_WD_DATAMODE:
321 wacom->wacom_wac.mode_report = field->report->id;
322 wacom->wacom_wac.mode_value = 2;
323 break;
324
Jason Gerecke8de82282016-10-19 18:03:37 -0700325 case WACOM_HID_UP_G9:
326 case WACOM_HID_UP_G11:
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700327 if (field->report->id == 0x03 &&
Jason Gerecke8de82282016-10-19 18:03:37 -0700328 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
329 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700330 wacom->wacom_wac.mode_report = field->report->id;
331 wacom->wacom_wac.mode_value = 0;
332 }
333 break;
Jason Gerecke345857b2016-10-19 18:03:50 -0700334 case WACOM_HID_WD_OFFSETLEFT:
335 case WACOM_HID_WD_OFFSETTOP:
336 case WACOM_HID_WD_OFFSETRIGHT:
337 case WACOM_HID_WD_OFFSETBOTTOM:
338 /* read manually */
339 n = hid_report_len(field->report);
340 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
341 if (!data)
342 break;
343 data[0] = field->report->id;
344 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
345 data, n, WAC_CMD_RETRIES);
346 if (ret == n) {
347 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
348 data, n, 0);
349 } else {
350 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
351 __func__);
352 }
353 kfree(data);
354 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700355 }
356}
357
Chris Bagwell428f8582011-10-26 22:26:59 -0700358/*
359 * Interface Descriptor of wacom devices can be incomplete and
360 * inconsistent so wacom_features table is used to store stylus
361 * device's packet lengths, various maximum values, and tablet
362 * resolution based on product ID's.
363 *
364 * For devices that contain 2 interfaces, wacom_features table is
365 * inaccurate for the touch interface. Since the Interface Descriptor
366 * for touch interfaces has pretty complete data, this function exists
367 * to query tablet for this missing information instead of hard coding in
368 * an additional table.
369 *
370 * A typical Interface Descriptor for a stylus will contain a
371 * boot mouse application collection that is not of interest and this
372 * function will ignore it.
373 *
374 * It also contains a digitizer application collection that also is not
375 * of interest since any information it contains would be duplicate
376 * of what is in wacom_features. Usually it defines a report of an array
377 * of bytes that could be used as max length of the stylus packet returned.
378 * If it happens to define a Digitizer-Stylus Physical Collection then
379 * the X and Y logical values contain valid data but it is ignored.
380 *
381 * A typical Interface Descriptor for a touch interface will contain a
382 * Digitizer-Finger Physical Collection which will define both logical
383 * X/Y maximum as well as the physical size of tablet. Since touch
384 * interfaces haven't supported pressure or distance, this is enough
385 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700386 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700387 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
388 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700389 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700390static void wacom_usage_mapping(struct hid_device *hdev,
391 struct hid_field *field, struct hid_usage *usage)
392{
393 struct wacom *wacom = hid_get_drvdata(hdev);
394 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoiresd97a5522015-01-05 16:32:12 -0500395 bool finger = WACOM_FINGER_FIELD(field);
396 bool pen = WACOM_PEN_FIELD(field);
Ping Cheng418b5732018-06-25 13:24:36 -0700397 unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700398
399 /*
400 * Requiring Stylus Usage will ignore boot mouse
401 * X/Y values and some cases of invalid Digitizer X/Y
402 * values commonly reported.
403 */
Jason Gerecke042628a2015-04-30 17:51:54 -0700404 if (pen)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700405 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -0700406 else if (finger)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700407 features->device_type |= WACOM_DEVICETYPE_TOUCH;
Jason Gerecke042628a2015-04-30 17:51:54 -0700408 else
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700409 return;
410
Jason Gerecke57832512018-06-25 13:24:35 -0700411 wacom_hid_usage_quirk(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700412
Ping Cheng418b5732018-06-25 13:24:36 -0700413 switch (equivalent_usage) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700414 case HID_GD_X:
415 features->x_max = field->logical_maximum;
416 if (finger) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700417 features->x_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700418 if ((features->type != BAMBOO_PT) &&
419 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700420 features->unit = field->unit;
421 features->unitExpo = field->unit_exponent;
422 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700423 }
424 break;
425 case HID_GD_Y:
426 features->y_max = field->logical_maximum;
427 if (finger) {
428 features->y_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700429 if ((features->type != BAMBOO_PT) &&
430 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700431 features->unit = field->unit;
432 features->unitExpo = field->unit_exponent;
433 }
434 }
435 break;
436 case HID_DG_TIPPRESSURE:
437 if (pen)
438 features->pressure_max = field->logical_maximum;
439 break;
440 }
Benjamin Tissoires7704ac92014-09-23 12:08:08 -0400441
442 if (features->type == HID_GENERIC)
443 wacom_wac_usage_mapping(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700444}
445
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800446static void wacom_post_parse_hid(struct hid_device *hdev,
447 struct wacom_features *features)
448{
449 struct wacom *wacom = hid_get_drvdata(hdev);
450 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
451
452 if (features->type == HID_GENERIC) {
453 /* Any last-minute generic device setup */
Benjamin Tissoires4082da82017-02-14 21:27:18 -0800454 if (wacom_wac->has_mode_change) {
455 if (wacom_wac->is_direct_mode)
456 features->device_type |= WACOM_DEVICETYPE_DIRECT;
457 else
458 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
459 }
460
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800461 if (features->touch_max > 1) {
Aaron Armstrong Skomraac2423c92017-01-25 12:08:40 -0800462 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
463 input_mt_init_slots(wacom_wac->touch_input,
464 wacom_wac->features.touch_max,
465 INPUT_MT_DIRECT);
466 else
467 input_mt_init_slots(wacom_wac->touch_input,
468 wacom_wac->features.touch_max,
469 INPUT_MT_POINTER);
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800470 }
471 }
472}
473
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700474static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800475 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500476{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700477 struct hid_report_enum *rep_enum;
478 struct hid_report *hreport;
479 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500480
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700481 /* check features first */
482 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
483 list_for_each_entry(hreport, &rep_enum->report_list, list) {
484 for (i = 0; i < hreport->maxfield; i++) {
485 /* Ignore if report count is out of bounds. */
486 if (hreport->field[i]->report_count < 1)
487 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500488
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700489 for (j = 0; j < hreport->field[i]->maxusage; j++) {
490 wacom_feature_mapping(hdev, hreport->field[i],
491 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500492 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500493 }
494 }
495
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700496 /* now check the input usages */
497 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
498 list_for_each_entry(hreport, &rep_enum->report_list, list) {
499
500 if (!hreport->maxfield)
501 continue;
502
503 for (i = 0; i < hreport->maxfield; i++)
504 for (j = 0; j < hreport->field[i]->maxusage; j++)
505 wacom_usage_mapping(hdev, hreport->field[i],
506 hreport->field[i]->usage + j);
507 }
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800508
509 wacom_post_parse_hid(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -0500510}
511
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400512static int wacom_hid_set_device_mode(struct hid_device *hdev)
513{
514 struct wacom *wacom = hid_get_drvdata(hdev);
515 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
516 struct hid_report *r;
517 struct hid_report_enum *re;
518
519 if (hid_data->inputmode < 0)
520 return 0;
521
522 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
523 r = re->report_id_hash[hid_data->inputmode];
524 if (r) {
525 r->field[0]->value[hid_data->inputmode_index] = 2;
526 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
527 }
528 return 0;
529}
530
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700531static int wacom_set_device_mode(struct hid_device *hdev,
532 struct wacom_wac *wacom_wac)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700533{
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700534 u8 *rep_data;
535 struct hid_report *r;
536 struct hid_report_enum *re;
Aaron Ma3064a032018-02-03 23:57:15 +0800537 u32 length;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700538 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700539
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700540 if (wacom_wac->mode_report < 0)
541 return 0;
542
543 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
544 r = re->report_id_hash[wacom_wac->mode_report];
545 if (!r)
546 return -EINVAL;
547
548 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700549 if (!rep_data)
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700550 return -ENOMEM;
551
552 length = hid_report_len(r);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700553
Jason Gereckefe494bc2012-10-03 17:25:35 -0700554 do {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700555 rep_data[0] = wacom_wac->mode_report;
556 rep_data[1] = wacom_wac->mode_value;
Chris Bagwell9937c022013-01-23 19:37:34 -0800557
Przemo Firszt296b7372014-08-06 14:00:38 -0700558 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
559 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700560 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700561 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700562 rep_data, length, 1);
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700563 } while (error >= 0 &&
564 rep_data[1] != wacom_wac->mode_report &&
565 limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700566
567 kfree(rep_data);
568
569 return error < 0 ? error : 0;
570}
571
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700572static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
573 struct wacom_features *features)
574{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700575 struct wacom *wacom = hid_get_drvdata(hdev);
576 int ret;
577 u8 rep_data[2];
578
579 switch (features->type) {
580 case GRAPHIRE_BT:
581 rep_data[0] = 0x03;
582 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700583 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
584 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700585
586 if (ret >= 0) {
587 rep_data[0] = speed == 0 ? 0x05 : 0x06;
588 rep_data[1] = 0x00;
589
590 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700591 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700592
593 if (ret >= 0) {
594 wacom->wacom_wac.bt_high_speed = speed;
595 return 0;
596 }
597 }
598
599 /*
600 * Note that if the raw queries fail, it's not a hard failure
601 * and it is safe to continue
602 */
603 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
604 rep_data[0], ret);
605 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700606 case INTUOS4WL:
607 if (speed == 1)
608 wacom->wacom_wac.bt_features &= ~0x20;
609 else
610 wacom->wacom_wac.bt_features |= 0x20;
611
612 rep_data[0] = 0x03;
613 rep_data[1] = wacom->wacom_wac.bt_features;
614
Przemo Firszt296b7372014-08-06 14:00:38 -0700615 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
616 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700617 if (ret >= 0)
618 wacom->wacom_wac.bt_high_speed = speed;
619 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700620 }
621
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700622 return 0;
623}
624
Jason Gereckefe494bc2012-10-03 17:25:35 -0700625/*
626 * Switch the tablet into its most-capable mode. Wacom tablets are
627 * typically configured to power-up in a mode which sends mouse-like
628 * reports to the OS. To get absolute position, pressure data, etc.
629 * from the tablet, it is necessary to switch the tablet out of this
630 * mode and into one which sends the full range of tablet data.
631 */
Benjamin Tissoiresa544c612017-01-20 16:20:13 +0100632static int _wacom_query_tablet_data(struct wacom *wacom)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700633{
Benjamin Tissoiresa544c612017-01-20 16:20:13 +0100634 struct hid_device *hdev = wacom->hdev;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700635 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoiresa544c612017-01-20 16:20:13 +0100636 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700637
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700638 if (hdev->bus == BUS_BLUETOOTH)
639 return wacom_bt_query_tablet_data(hdev, 1, features);
640
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700641 if (features->type != HID_GENERIC) {
642 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
643 if (features->type > TABLETPC) {
644 /* MT Tablet PC touch */
645 wacom_wac->mode_report = 3;
646 wacom_wac->mode_value = 4;
647 } else if (features->type == WACOM_24HDT) {
648 wacom_wac->mode_report = 18;
649 wacom_wac->mode_value = 2;
650 } else if (features->type == WACOM_27QHDT) {
651 wacom_wac->mode_report = 131;
652 wacom_wac->mode_value = 2;
653 } else if (features->type == BAMBOO_PAD) {
654 wacom_wac->mode_report = 2;
655 wacom_wac->mode_value = 2;
656 }
657 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
658 if (features->type <= BAMBOO_PT) {
659 wacom_wac->mode_report = 2;
660 wacom_wac->mode_value = 2;
661 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700662 }
663 }
664
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700665 wacom_set_device_mode(hdev, wacom_wac);
666
667 if (features->type == HID_GENERIC)
668 return wacom_hid_set_device_mode(hdev);
669
Jason Gereckefe494bc2012-10-03 17:25:35 -0700670 return 0;
671}
672
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700673static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700674 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800675{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700676 struct wacom *wacom = hid_get_drvdata(hdev);
677 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800678
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700679 /* default features */
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700680 features->x_fuzz = 4;
681 features->y_fuzz = 4;
682 features->pressure_fuzz = 0;
Jason Gereckebef7e202016-04-22 14:30:53 -0700683 features->distance_fuzz = 1;
684 features->tilt_fuzz = 1;
Ping Chengec67bbe2009-12-15 00:35:24 -0800685
Chris Bagwelld3825d52012-03-25 23:26:11 -0700686 /*
687 * The wireless device HID is basic and layout conflicts with
688 * other tablets (monitor and touch interface can look like pen).
689 * Skip the query for this type and modify defaults based on
690 * interface number.
691 */
692 if (features->type == WIRELESS) {
Jason Gerecke3f14a632015-08-03 10:17:05 -0700693 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
Jason Gereckeccad85c2015-08-03 10:17:04 -0700694 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700695 else
Jason Gereckeaa86b182015-06-15 18:01:42 -0700696 features->device_type = WACOM_DEVICETYPE_NONE;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700697 return;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700698 }
699
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700700 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800701}
702
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700703struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700704 struct list_head list;
705 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700706 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700707 struct wacom_shared shared;
708};
709
710static LIST_HEAD(wacom_udev_list);
711static DEFINE_MUTEX(wacom_udev_list_lock);
712
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700713static bool wacom_are_sibling(struct hid_device *hdev,
714 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700715{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700716 struct wacom *wacom = hid_get_drvdata(hdev);
717 struct wacom_features *features = &wacom->wacom_wac.features;
Jason Gerecke41372d52016-08-08 12:06:30 -0700718 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
719 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
720 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
721 __u32 oPid = features->oPid ? features->oPid : hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700722
Jason Gerecke41372d52016-08-08 12:06:30 -0700723 /* The defined oVid/oPid must match that of the sibling */
724 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
725 return false;
726 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
727 return false;
728
729 /*
730 * Devices with the same VID/PID must share the same physical
731 * device path, while those with different VID/PID must share
732 * the same physical parent device path.
733 */
734 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
Daniel M. Lambea1a8861f2018-07-17 22:35:36 +0100735 if (!hid_compare_device_paths(hdev, sibling, '/'))
Jason Gerecke41372d52016-08-08 12:06:30 -0700736 return false;
737 } else {
Daniel M. Lambea1a8861f2018-07-17 22:35:36 +0100738 if (!hid_compare_device_paths(hdev, sibling, '.'))
Jason Gerecke41372d52016-08-08 12:06:30 -0700739 return false;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700740 }
741
Jason Gerecke41372d52016-08-08 12:06:30 -0700742 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
743 if (features->type != HID_GENERIC)
744 return true;
745
746 /*
747 * Direct-input devices may not be siblings of indirect-input
748 * devices.
749 */
750 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
751 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700752 return false;
753
Jason Gerecke41372d52016-08-08 12:06:30 -0700754 /*
755 * Indirect-input devices may not be siblings of direct-input
756 * devices.
757 */
758 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
759 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700760 return false;
761
Jason Gerecke41372d52016-08-08 12:06:30 -0700762 /* Pen devices may only be siblings of touch devices */
763 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
764 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
765 return false;
766
767 /* Touch devices may only be siblings of pen devices */
768 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
769 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
770 return false;
771
772 /*
773 * No reason could be found for these two devices to NOT be
774 * siblings, so there's a good chance they ARE siblings
775 */
776 return true;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700777}
778
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700779static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700780{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700781 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700782
Jason Gerecke41372d52016-08-08 12:06:30 -0700783 /* Try to find an already-probed interface from the same device */
784 list_for_each_entry(data, &wacom_udev_list, list) {
Daniel M. Lambea1a8861f2018-07-17 22:35:36 +0100785 if (hid_compare_device_paths(hdev, data->dev, '/')) {
Jason Gerecke2a5e5972017-09-18 09:27:42 -0700786 kref_get(&data->kref);
Jason Gerecke41372d52016-08-08 12:06:30 -0700787 return data;
Jason Gerecke2a5e5972017-09-18 09:27:42 -0700788 }
Jason Gerecke41372d52016-08-08 12:06:30 -0700789 }
790
791 /* Fallback to finding devices that appear to be "siblings" */
Ping Cheng4492eff2010-03-19 22:18:15 -0700792 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700793 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700794 kref_get(&data->kref);
795 return data;
796 }
797 }
798
799 return NULL;
800}
801
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200802static void wacom_release_shared_data(struct kref *kref)
803{
804 struct wacom_hdev_data *data =
805 container_of(kref, struct wacom_hdev_data, kref);
806
807 mutex_lock(&wacom_udev_list_lock);
808 list_del(&data->list);
809 mutex_unlock(&wacom_udev_list_lock);
810
811 kfree(data);
812}
813
814static void wacom_remove_shared_data(void *res)
815{
816 struct wacom *wacom = res;
817 struct wacom_hdev_data *data;
818 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
819
820 if (wacom_wac->shared) {
821 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
822 shared);
823
824 if (wacom_wac->shared->touch == wacom->hdev)
825 wacom_wac->shared->touch = NULL;
826 else if (wacom_wac->shared->pen == wacom->hdev)
827 wacom_wac->shared->pen = NULL;
828
829 kref_put(&data->kref, wacom_release_shared_data);
830 wacom_wac->shared = NULL;
831 }
832}
833
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700834static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700835{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700836 struct wacom *wacom = hid_get_drvdata(hdev);
837 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
838 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700839 int retval = 0;
840
841 mutex_lock(&wacom_udev_list_lock);
842
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700843 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700844 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700845 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700846 if (!data) {
847 retval = -ENOMEM;
848 goto out;
849 }
850
851 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700852 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700853 list_add_tail(&data->list, &wacom_udev_list);
854 }
855
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700856 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700857
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200858 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
859 if (retval) {
860 mutex_unlock(&wacom_udev_list_lock);
861 wacom_remove_shared_data(wacom);
862 return retval;
863 }
864
Jason Gereckea9ce7852017-01-17 15:38:58 -0800865 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
866 wacom_wac->shared->touch = hdev;
867 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
868 wacom_wac->shared->pen = hdev;
869
Ping Cheng4492eff2010-03-19 22:18:15 -0700870out:
871 mutex_unlock(&wacom_udev_list_lock);
872 return retval;
873}
874
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700875static int wacom_led_control(struct wacom *wacom)
876{
877 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700878 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700879 unsigned char report_id = WAC_CMD_LED_CONTROL;
880 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700881
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200882 if (!wacom->led.groups)
883 return -ENOTSUPP;
884
Aaron Armstrong Skomra74aebed62017-08-28 14:15:39 -0700885 if (wacom->wacom_wac.features.type == REMOTE)
886 return -ENOTSUPP;
887
Ping Cheng912ca212014-09-10 12:41:31 -0700888 if (wacom->wacom_wac.pid) { /* wireless connected */
889 report_id = WAC_CMD_WL_LED_CONTROL;
890 buf_size = 13;
891 }
Jason Gerecke4922cd22017-01-25 12:08:37 -0800892 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
893 report_id = WAC_CMD_WL_INTUOSP2;
894 buf_size = 51;
895 }
Ping Cheng912ca212014-09-10 12:41:31 -0700896 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700897 if (!buf)
898 return -ENOMEM;
899
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -0800900 if (wacom->wacom_wac.features.type == HID_GENERIC) {
901 buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
902 buf[1] = wacom->led.llv;
903 buf[2] = wacom->led.groups[0].select & 0x03;
904
905 } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
906 wacom->wacom_wac.features.type <= INTUOSPL)) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700907 /*
908 * Touch Ring and crop mark LED luminance may take on
909 * one of four values:
910 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
911 */
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200912 int ring_led = wacom->led.groups[0].select & 0x03;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700913 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
914 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700915 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700916
Ping Cheng912ca212014-09-10 12:41:31 -0700917 buf[0] = report_id;
918 if (wacom->wacom_wac.pid) {
919 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
920 buf, buf_size, WAC_CMD_RETRIES);
921 buf[0] = report_id;
922 buf[4] = led_bits;
923 } else
924 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700925 }
Jason Gerecke4922cd22017-01-25 12:08:37 -0800926 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
927 buf[0] = report_id;
928 buf[4] = 100; // Power Connection LED (ORANGE)
929 buf[5] = 100; // BT Connection LED (BLUE)
930 buf[6] = 100; // Paper Mode (RED?)
931 buf[7] = 100; // Paper Mode (GREEN?)
932 buf[8] = 100; // Paper Mode (BLUE?)
933 buf[9] = wacom->led.llv;
934 buf[10] = wacom->led.groups[0].select & 0x03;
935 }
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700936 else {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200937 int led = wacom->led.groups[0].select | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700938
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700939 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
940 wacom->wacom_wac.features.type == WACOM_24HD)
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200941 led |= (wacom->led.groups[1].select << 4) | 0x40;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700942
Ping Cheng912ca212014-09-10 12:41:31 -0700943 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700944 buf[1] = led;
945 buf[2] = wacom->led.llv;
946 buf[3] = wacom->led.hlv;
947 buf[4] = wacom->led.img_lum;
948 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700949
Ping Cheng912ca212014-09-10 12:41:31 -0700950 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700951 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700952 kfree(buf);
953
954 return retval;
955}
956
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700957static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
958 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700959{
960 unsigned char *buf;
961 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700962 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700963
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700964 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700965 if (!buf)
966 return -ENOMEM;
967
968 /* Send 'start' command */
969 buf[0] = WAC_CMD_ICON_START;
970 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700971 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
972 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700973 if (retval < 0)
974 goto out;
975
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700976 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700977 buf[1] = button_id & 0x07;
978 for (i = 0; i < 4; i++) {
979 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700980 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700981
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700982 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700983 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700984 if (retval < 0)
985 break;
986 }
987
988 /* Send 'stop' */
989 buf[0] = WAC_CMD_ICON_START;
990 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700991 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
992 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700993
994out:
995 kfree(buf);
996 return retval;
997}
998
Ping Cheng09e7d942011-10-04 23:51:14 -0700999static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001000 const char *buf, size_t count)
1001{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001002 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001003 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001004 unsigned int id;
1005 int err;
1006
1007 err = kstrtouint(buf, 10, &id);
1008 if (err)
1009 return err;
1010
1011 mutex_lock(&wacom->lock);
1012
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001013 wacom->led.groups[set_id].select = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001014 err = wacom_led_control(wacom);
1015
1016 mutex_unlock(&wacom->lock);
1017
1018 return err < 0 ? err : count;
1019}
1020
Ping Cheng09e7d942011-10-04 23:51:14 -07001021#define DEVICE_LED_SELECT_ATTR(SET_ID) \
1022static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
1023 struct device_attribute *attr, const char *buf, size_t count) \
1024{ \
1025 return wacom_led_select_store(dev, SET_ID, buf, count); \
1026} \
Ping Cheng04c59ab2011-10-04 23:51:49 -07001027static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
1028 struct device_attribute *attr, char *buf) \
1029{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +08001030 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001031 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -07001032 return scnprintf(buf, PAGE_SIZE, "%d\n", \
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001033 wacom->led.groups[SET_ID].select); \
Ping Cheng04c59ab2011-10-04 23:51:49 -07001034} \
Ping Chenge0984bc2014-09-10 12:40:05 -07001035static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -07001036 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -07001037 wacom_led##SET_ID##_select_store)
1038
1039DEVICE_LED_SELECT_ATTR(0);
1040DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001041
1042static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
1043 const char *buf, size_t count)
1044{
1045 unsigned int value;
1046 int err;
1047
1048 err = kstrtouint(buf, 10, &value);
1049 if (err)
1050 return err;
1051
1052 mutex_lock(&wacom->lock);
1053
1054 *dest = value & 0x7f;
1055 err = wacom_led_control(wacom);
1056
1057 mutex_unlock(&wacom->lock);
1058
1059 return err < 0 ? err : count;
1060}
1061
1062#define DEVICE_LUMINANCE_ATTR(name, field) \
1063static ssize_t wacom_##name##_luminance_store(struct device *dev, \
1064 struct device_attribute *attr, const char *buf, size_t count) \
1065{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +08001066 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001067 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001068 \
1069 return wacom_luminance_store(wacom, &wacom->led.field, \
1070 buf, count); \
1071} \
Ping Cheng37449ad2014-09-10 12:40:30 -07001072static ssize_t wacom_##name##_luminance_show(struct device *dev, \
1073 struct device_attribute *attr, char *buf) \
1074{ \
1075 struct wacom *wacom = dev_get_drvdata(dev); \
1076 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
1077} \
Ping Chenge0984bc2014-09-10 12:40:05 -07001078static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -07001079 wacom_##name##_luminance_show, \
1080 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001081
1082DEVICE_LUMINANCE_ATTR(status0, llv);
1083DEVICE_LUMINANCE_ATTR(status1, hlv);
1084DEVICE_LUMINANCE_ATTR(buttons, img_lum);
1085
1086static ssize_t wacom_button_image_store(struct device *dev, int button_id,
1087 const char *buf, size_t count)
1088{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001089 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001090 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001091 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001092 unsigned len;
1093 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001094
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001095 if (hdev->bus == BUS_BLUETOOTH) {
1096 len = 256;
1097 xfer_id = WAC_CMD_ICON_BT_XFER;
1098 } else {
1099 len = 1024;
1100 xfer_id = WAC_CMD_ICON_XFER;
1101 }
1102
1103 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001104 return -EINVAL;
1105
1106 mutex_lock(&wacom->lock);
1107
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001108 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001109
1110 mutex_unlock(&wacom->lock);
1111
1112 return err < 0 ? err : count;
1113}
1114
1115#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1116static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1117 struct device_attribute *attr, const char *buf, size_t count) \
1118{ \
1119 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1120} \
Ping Chenge0984bc2014-09-10 12:40:05 -07001121static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001122 NULL, wacom_btnimg##BUTTON_ID##_store)
1123
1124DEVICE_BTNIMG_ATTR(0);
1125DEVICE_BTNIMG_ATTR(1);
1126DEVICE_BTNIMG_ATTR(2);
1127DEVICE_BTNIMG_ATTR(3);
1128DEVICE_BTNIMG_ATTR(4);
1129DEVICE_BTNIMG_ATTR(5);
1130DEVICE_BTNIMG_ATTR(6);
1131DEVICE_BTNIMG_ATTR(7);
1132
Ping Cheng09e7d942011-10-04 23:51:14 -07001133static struct attribute *cintiq_led_attrs[] = {
1134 &dev_attr_status_led0_select.attr,
1135 &dev_attr_status_led1_select.attr,
1136 NULL
1137};
1138
1139static struct attribute_group cintiq_led_attr_group = {
1140 .name = "wacom_led",
1141 .attrs = cintiq_led_attrs,
1142};
1143
1144static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001145 &dev_attr_status0_luminance.attr,
1146 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -07001147 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001148 &dev_attr_buttons_luminance.attr,
1149 &dev_attr_button0_rawimg.attr,
1150 &dev_attr_button1_rawimg.attr,
1151 &dev_attr_button2_rawimg.attr,
1152 &dev_attr_button3_rawimg.attr,
1153 &dev_attr_button4_rawimg.attr,
1154 &dev_attr_button5_rawimg.attr,
1155 &dev_attr_button6_rawimg.attr,
1156 &dev_attr_button7_rawimg.attr,
1157 NULL
1158};
1159
Ping Cheng09e7d942011-10-04 23:51:14 -07001160static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001161 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -07001162 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001163};
1164
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001165static struct attribute *intuos5_led_attrs[] = {
1166 &dev_attr_status0_luminance.attr,
1167 &dev_attr_status_led0_select.attr,
1168 NULL
1169};
1170
1171static struct attribute_group intuos5_led_attr_group = {
1172 .name = "wacom_led",
1173 .attrs = intuos5_led_attrs,
1174};
1175
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001176static struct attribute *generic_led_attrs[] = {
1177 &dev_attr_status0_luminance.attr,
1178 &dev_attr_status_led0_select.attr,
1179 NULL
1180};
1181
1182static struct attribute_group generic_led_attr_group = {
1183 .name = "wacom_led",
1184 .attrs = generic_led_attrs,
1185};
1186
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001187struct wacom_sysfs_group_devres {
1188 struct attribute_group *group;
1189 struct kobject *root;
1190};
1191
1192static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1193{
1194 struct wacom_sysfs_group_devres *devres = res;
1195 struct kobject *kobj = devres->root;
1196
1197 dev_dbg(dev, "%s: dropping reference to %s\n",
1198 __func__, devres->group->name);
1199 sysfs_remove_group(kobj, devres->group);
1200}
1201
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001202static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1203 struct kobject *root,
1204 struct attribute_group *group)
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001205{
1206 struct wacom_sysfs_group_devres *devres;
1207 int error;
1208
1209 devres = devres_alloc(wacom_devm_sysfs_group_release,
1210 sizeof(struct wacom_sysfs_group_devres),
1211 GFP_KERNEL);
1212 if (!devres)
1213 return -ENOMEM;
1214
1215 devres->group = group;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001216 devres->root = root;
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001217
1218 error = sysfs_create_group(devres->root, group);
Arvind Yadav097b8f62018-04-24 13:33:03 +05301219 if (error) {
1220 devres_free(devres);
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001221 return error;
Arvind Yadav097b8f62018-04-24 13:33:03 +05301222 }
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001223
1224 devres_add(&wacom->hdev->dev, devres);
1225
1226 return 0;
1227}
1228
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001229static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1230 struct attribute_group *group)
1231{
1232 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1233 group);
1234}
1235
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001236enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001237{
1238 struct wacom *wacom = led->wacom;
1239
1240 if (wacom->led.max_hlv)
1241 return led->hlv * LED_FULL / wacom->led.max_hlv;
1242
1243 if (wacom->led.max_llv)
1244 return led->llv * LED_FULL / wacom->led.max_llv;
1245
1246 /* device doesn't support brightness tuning */
1247 return LED_FULL;
1248}
1249
1250static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1251{
1252 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1253 struct wacom *wacom = led->wacom;
1254
1255 if (wacom->led.groups[led->group].select != led->id)
1256 return LED_OFF;
1257
1258 return wacom_leds_brightness_get(led);
1259}
1260
1261static int wacom_led_brightness_set(struct led_classdev *cdev,
1262 enum led_brightness brightness)
1263{
1264 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1265 struct wacom *wacom = led->wacom;
1266 int error;
1267
1268 mutex_lock(&wacom->lock);
1269
1270 if (!wacom->led.groups || (brightness == LED_OFF &&
1271 wacom->led.groups[led->group].select != led->id)) {
1272 error = 0;
1273 goto out;
1274 }
1275
1276 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1277 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1278
1279 wacom->led.groups[led->group].select = led->id;
1280
1281 error = wacom_led_control(wacom);
1282
1283out:
1284 mutex_unlock(&wacom->lock);
1285
1286 return error;
1287}
1288
1289static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1290 enum led_brightness brightness)
1291{
1292}
1293
1294static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1295 struct wacom_led *led, unsigned int group,
1296 unsigned int id, bool read_only)
1297{
1298 int error;
1299 char *name;
1300
1301 name = devm_kasprintf(dev, GFP_KERNEL,
1302 "%s::wacom-%d.%d",
1303 dev_name(dev),
1304 group,
1305 id);
1306 if (!name)
1307 return -ENOMEM;
1308
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001309 if (!read_only) {
1310 led->trigger.name = name;
1311 error = devm_led_trigger_register(dev, &led->trigger);
1312 if (error) {
1313 hid_err(wacom->hdev,
1314 "failed to register LED trigger %s: %d\n",
1315 led->cdev.name, error);
1316 return error;
1317 }
1318 }
1319
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001320 led->group = group;
1321 led->id = id;
1322 led->wacom = wacom;
1323 led->llv = wacom->led.llv;
1324 led->hlv = wacom->led.hlv;
1325 led->cdev.name = name;
1326 led->cdev.max_brightness = LED_FULL;
1327 led->cdev.flags = LED_HW_PLUGGABLE;
1328 led->cdev.brightness_get = __wacom_led_brightness_get;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001329 if (!read_only) {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001330 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001331 led->cdev.default_trigger = led->cdev.name;
1332 } else {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001333 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001334 }
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001335
1336 error = devm_led_classdev_register(dev, &led->cdev);
1337 if (error) {
1338 hid_err(wacom->hdev,
1339 "failed to register LED %s: %d\n",
1340 led->cdev.name, error);
1341 led->cdev.name = NULL;
1342 return error;
1343 }
1344
1345 return 0;
1346}
1347
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001348static void wacom_led_groups_release_one(void *data)
1349{
1350 struct wacom_group_leds *group = data;
1351
1352 devres_release_group(group->dev, group);
1353}
1354
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001355static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1356 struct wacom *wacom,
1357 int group_id, int count,
1358 bool read_only)
1359{
1360 struct wacom_led *leds;
1361 int i, error;
1362
1363 if (group_id >= wacom->led.count || count <= 0)
1364 return -EINVAL;
1365
1366 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1367 return -ENOMEM;
1368
Kees Cooka86854d2018-06-12 14:07:58 -07001369 leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL);
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001370 if (!leds) {
1371 error = -ENOMEM;
1372 goto err;
1373 }
1374
1375 wacom->led.groups[group_id].leds = leds;
1376 wacom->led.groups[group_id].count = count;
1377
1378 for (i = 0; i < count; i++) {
1379 error = wacom_led_register_one(dev, wacom, &leds[i],
1380 group_id, i, read_only);
1381 if (error)
1382 goto err;
1383 }
1384
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001385 wacom->led.groups[group_id].dev = dev;
1386
1387 devres_close_group(dev, &wacom->led.groups[group_id]);
1388
1389 /*
1390 * There is a bug (?) in devm_led_classdev_register() in which its
1391 * increments the refcount of the parent. If the parent is an input
1392 * device, that means the ref count never reaches 0 when
1393 * devm_input_device_release() gets called.
1394 * This means that the LEDs are still there after disconnect.
1395 * Manually force the release of the group so that the leds are released
1396 * once we are done using them.
1397 */
1398 error = devm_add_action_or_reset(&wacom->hdev->dev,
1399 wacom_led_groups_release_one,
1400 &wacom->led.groups[group_id]);
1401 if (error)
1402 return error;
1403
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001404 return 0;
1405
1406err:
1407 devres_release_group(dev, &wacom->led.groups[group_id]);
1408 return error;
1409}
1410
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001411struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1412 unsigned int id)
1413{
1414 struct wacom_group_leds *group;
1415
1416 if (group_id >= wacom->led.count)
1417 return NULL;
1418
1419 group = &wacom->led.groups[group_id];
1420
1421 if (!group->leds)
1422 return NULL;
1423
1424 id %= group->count;
1425
1426 return &group->leds[id];
1427}
1428
1429/**
1430 * wacom_led_next: gives the next available led with a wacom trigger.
1431 *
1432 * returns the next available struct wacom_led which has its default trigger
1433 * or the current one if none is available.
1434 */
1435struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1436{
1437 struct wacom_led *next_led;
1438 int group, next;
1439
1440 if (!wacom || !cur)
1441 return NULL;
1442
1443 group = cur->group;
1444 next = cur->id;
1445
1446 do {
1447 next_led = wacom_led_find(wacom, group, ++next);
1448 if (!next_led || next_led == cur)
1449 return next_led;
1450 } while (next_led->cdev.trigger != &next_led->trigger);
1451
1452 return next_led;
1453}
1454
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001455static void wacom_led_groups_release(void *data)
1456{
1457 struct wacom *wacom = data;
1458
1459 wacom->led.groups = NULL;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001460 wacom->led.count = 0;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001461}
1462
1463static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1464{
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001465 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001466 struct wacom_group_leds *groups;
1467 int error;
1468
Kees Cooka86854d2018-06-12 14:07:58 -07001469 groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds),
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001470 GFP_KERNEL);
1471 if (!groups)
1472 return -ENOMEM;
1473
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001474 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001475 if (error)
1476 return error;
1477
1478 wacom->led.groups = groups;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001479 wacom->led.count = count;
1480
1481 return 0;
1482}
1483
1484static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1485 int led_per_group, bool read_only)
1486{
1487 struct device *dev;
1488 int i, error;
1489
1490 if (!wacom->wacom_wac.pad_input)
1491 return -EINVAL;
1492
1493 dev = &wacom->wacom_wac.pad_input->dev;
1494
1495 error = wacom_led_groups_allocate(wacom, group_count);
1496 if (error)
1497 return error;
1498
1499 for (i = 0; i < group_count; i++) {
1500 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1501 led_per_group,
1502 read_only);
1503 if (error)
1504 return error;
1505 }
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001506
1507 return 0;
1508}
1509
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001510int wacom_initialize_leds(struct wacom *wacom)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001511{
1512 int error;
1513
Jason Gerecke862cf552015-06-15 18:01:43 -07001514 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1515 return 0;
1516
Ping Cheng09e7d942011-10-04 23:51:14 -07001517 /* Initialize default values */
1518 switch (wacom->wacom_wac.features.type) {
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001519 case HID_GENERIC:
1520 if (!wacom->generic_has_leds)
1521 return 0;
1522 wacom->led.llv = 100;
1523 wacom->led.max_llv = 100;
1524
1525 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1526 if (error) {
1527 hid_err(wacom->hdev,
1528 "cannot create leds err: %d\n", error);
1529 return error;
1530 }
1531
1532 error = wacom_devm_sysfs_create_group(wacom,
1533 &generic_led_attr_group);
1534 break;
1535
Jason Gereckea19fc982012-06-12 00:27:53 -07001536 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -07001537 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -07001538 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -07001539 case INTUOS4L:
Ping Chengf4fa9a62011-10-04 23:49:42 -07001540 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001541 wacom->led.hlv = 20;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001542 wacom->led.max_llv = 127;
1543 wacom->led.max_hlv = 127;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001544 wacom->led.img_lum = 10;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001545
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001546 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001547 if (error) {
1548 hid_err(wacom->hdev,
1549 "cannot create leds err: %d\n", error);
1550 return error;
1551 }
1552
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001553 error = wacom_devm_sysfs_create_group(wacom,
1554 &intuos4_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001555 break;
1556
Jason Gerecke246835f2011-12-12 00:12:04 -08001557 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -07001558 case WACOM_21UX2:
Ping Cheng09e7d942011-10-04 23:51:14 -07001559 wacom->led.llv = 0;
1560 wacom->led.hlv = 0;
1561 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001562
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001563 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001564 if (error) {
1565 hid_err(wacom->hdev,
1566 "cannot create leds err: %d\n", error);
1567 return error;
1568 }
1569
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001570 error = wacom_devm_sysfs_create_group(wacom,
1571 &cintiq_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001572 break;
1573
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001574 case INTUOS5S:
1575 case INTUOS5:
1576 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -07001577 case INTUOSPS:
1578 case INTUOSPM:
1579 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -07001580 wacom->led.llv = 32;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001581 wacom->led.max_llv = 96;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001582
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001583 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001584 if (error) {
1585 hid_err(wacom->hdev,
1586 "cannot create leds err: %d\n", error);
1587 return error;
1588 }
1589
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001590 error = wacom_devm_sysfs_create_group(wacom,
1591 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001592 break;
1593
Jason Gerecke4922cd22017-01-25 12:08:37 -08001594 case INTUOSP2_BT:
1595 wacom->led.llv = 50;
1596 wacom->led.max_llv = 100;
1597 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1598 if (error) {
1599 hid_err(wacom->hdev,
1600 "cannot create leds err: %d\n", error);
1601 return error;
1602 }
1603 return 0;
1604
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001605 case REMOTE:
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001606 wacom->led.llv = 255;
1607 wacom->led.max_llv = 255;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001608 error = wacom_led_groups_allocate(wacom, 5);
1609 if (error) {
1610 hid_err(wacom->hdev,
1611 "cannot create leds err: %d\n", error);
1612 return error;
1613 }
1614 return 0;
1615
Ping Cheng09e7d942011-10-04 23:51:14 -07001616 default:
1617 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001618 }
1619
Ping Cheng09e7d942011-10-04 23:51:14 -07001620 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -07001621 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -07001622 "cannot create sysfs group err: %d\n", error);
1623 return error;
1624 }
Ping Cheng09e7d942011-10-04 23:51:14 -07001625
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001626 return 0;
1627}
1628
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01001629static void wacom_init_work(struct work_struct *work)
1630{
1631 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1632
1633 _wacom_query_tablet_data(wacom);
1634 wacom_led_control(wacom);
1635}
1636
1637static void wacom_query_tablet_data(struct wacom *wacom)
1638{
1639 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1640}
1641
Chris Bagwella1d552c2012-03-25 23:26:30 -07001642static enum power_supply_property wacom_battery_props[] = {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001643 POWER_SUPPLY_PROP_MODEL_NAME,
Jason Gerecke71fa6412015-03-11 10:25:41 -07001644 POWER_SUPPLY_PROP_PRESENT,
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001645 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001646 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001647 POWER_SUPPLY_PROP_CAPACITY
1648};
1649
1650static int wacom_battery_get_property(struct power_supply *psy,
1651 enum power_supply_property psp,
1652 union power_supply_propval *val)
1653{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001654 struct wacom_battery *battery = power_supply_get_drvdata(psy);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001655 int ret = 0;
1656
1657 switch (psp) {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001658 case POWER_SUPPLY_PROP_MODEL_NAME:
1659 val->strval = battery->wacom->wacom_wac.name;
1660 break;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001661 case POWER_SUPPLY_PROP_PRESENT:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001662 val->intval = battery->bat_connected;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001663 break;
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001664 case POWER_SUPPLY_PROP_SCOPE:
1665 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1666 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001667 case POWER_SUPPLY_PROP_CAPACITY:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001668 val->intval = battery->battery_capacity;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001669 break;
1670 case POWER_SUPPLY_PROP_STATUS:
Jason Gerecke16e45982017-04-28 09:25:33 -07001671 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1672 val->intval = battery->bat_status;
1673 else if (battery->bat_charging)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001674 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001675 else if (battery->battery_capacity == 100 &&
1676 battery->ps_connected)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001677 val->intval = POWER_SUPPLY_STATUS_FULL;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001678 else if (battery->ps_connected)
Jason Gereckeb0882cb2015-03-06 11:47:43 -08001679 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001680 else
1681 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001682 break;
1683 default:
1684 ret = -EINVAL;
1685 break;
1686 }
1687
1688 return ret;
1689}
1690
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001691static int __wacom_initialize_battery(struct wacom *wacom,
1692 struct wacom_battery *battery)
Chris Bagwella1d552c2012-03-25 23:26:30 -07001693{
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001694 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001695 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001696 struct power_supply_config psy_cfg = { .drv_data = battery, };
Benjamin Tissoires136ae5e2016-07-13 18:06:16 +02001697 struct power_supply *ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001698 struct power_supply_desc *bat_desc = &battery->bat_desc;
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001699 unsigned long n;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001700 int error;
1701
1702 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1703 return -ENOMEM;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001704
Benjamin Tissoires99569532016-07-13 18:06:17 +02001705 battery->wacom = wacom;
1706
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001707 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001708
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001709 bat_desc->properties = wacom_battery_props;
1710 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1711 bat_desc->get_property = wacom_battery_get_property;
1712 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1713 bat_desc->name = battery->bat_name;
Benjamin Tissoires96983292016-07-13 18:06:15 +02001714 bat_desc->type = POWER_SUPPLY_TYPE_USB;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001715 bat_desc->use_for_apm = 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001716
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001717 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1718 if (IS_ERR(ps_bat)) {
1719 error = PTR_ERR(ps_bat);
1720 goto err;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001721 }
1722
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001723 power_supply_powers(ps_bat, &wacom->hdev->dev);
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001724
1725 battery->battery = ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001726
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001727 devres_close_group(dev, bat_desc);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001728 return 0;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001729
1730err:
1731 devres_release_group(dev, bat_desc);
1732 return error;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001733}
1734
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001735static int wacom_initialize_battery(struct wacom *wacom)
1736{
1737 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1738 return __wacom_initialize_battery(wacom, &wacom->battery);
1739
1740 return 0;
1741}
1742
Chris Bagwella1d552c2012-03-25 23:26:30 -07001743static void wacom_destroy_battery(struct wacom *wacom)
1744{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001745 if (wacom->battery.battery) {
1746 devres_release_group(&wacom->hdev->dev,
1747 &wacom->battery.bat_desc);
1748 wacom->battery.battery = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001749 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001750}
1751
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001752static ssize_t wacom_show_speed(struct device *dev,
1753 struct device_attribute
1754 *attr, char *buf)
1755{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001756 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001757 struct wacom *wacom = hid_get_drvdata(hdev);
1758
1759 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1760}
1761
1762static ssize_t wacom_store_speed(struct device *dev,
1763 struct device_attribute *attr,
1764 const char *buf, size_t count)
1765{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001766 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001767 struct wacom *wacom = hid_get_drvdata(hdev);
1768 u8 new_speed;
1769
1770 if (kstrtou8(buf, 0, &new_speed))
1771 return -EINVAL;
1772
1773 if (new_speed != 0 && new_speed != 1)
1774 return -EINVAL;
1775
1776 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1777
1778 return count;
1779}
1780
Ping Chenge0984bc2014-09-10 12:40:05 -07001781static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001782 wacom_show_speed, wacom_store_speed);
1783
Aaron Skomra72b236d2015-08-20 16:05:17 -07001784
1785static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1786 struct kobj_attribute *kattr,
1787 char *buf, int index)
1788{
Geliang Tang2cf83832015-12-27 17:25:24 +08001789 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001790 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001791 struct wacom *wacom = hid_get_drvdata(hdev);
1792 u8 mode;
1793
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001794 mode = wacom->led.groups[index].select;
Christos Gkekasbc35f732017-07-08 20:25:48 +01001795 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001796}
1797
1798#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1799static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1800 struct kobj_attribute *kattr, char *buf) \
1801{ \
1802 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1803} \
1804static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1805 .attr = {.name = "remote_mode", \
1806 .mode = DEV_ATTR_RO_PERM}, \
1807 .show = wacom_show_remote##SET_ID##_mode, \
1808}; \
1809static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1810 &remote##SET_ID##_mode_attr.attr, \
1811 NULL \
1812}; \
1813static struct attribute_group remote##SET_ID##_serial_group = { \
1814 .name = NULL, \
1815 .attrs = remote##SET_ID##_serial_attrs, \
1816}
1817
1818DEVICE_EKR_ATTR_GROUP(0);
1819DEVICE_EKR_ATTR_GROUP(1);
1820DEVICE_EKR_ATTR_GROUP(2);
1821DEVICE_EKR_ATTR_GROUP(3);
1822DEVICE_EKR_ATTR_GROUP(4);
1823
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02001824static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1825 int index)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001826{
1827 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001828 struct wacom_remote *remote = wacom->remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001829
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001830 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001831 GFP_KERNEL,
1832 "%d", serial);
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001833 if (!remote->remotes[index].group.name)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001834 return -ENOMEM;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001835
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001836 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001837 &remote->remotes[index].group);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001838 if (error) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001839 remote->remotes[index].group.name = NULL;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001840 hid_err(wacom->hdev,
1841 "cannot create sysfs group err: %d\n", error);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001842 return error;
1843 }
1844
1845 return 0;
1846}
1847
Aaron Skomra72b236d2015-08-20 16:05:17 -07001848static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1849{
1850 const size_t buf_size = 2;
1851 unsigned char *buf;
1852 int retval;
1853
1854 buf = kzalloc(buf_size, GFP_KERNEL);
1855 if (!buf)
1856 return -ENOMEM;
1857
1858 buf[0] = WAC_CMD_DELETE_PAIRING;
1859 buf[1] = selector;
1860
1861 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1862 buf_size, WAC_CMD_RETRIES);
1863 kfree(buf);
1864
1865 return retval;
1866}
1867
1868static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1869 struct kobj_attribute *attr,
1870 const char *buf, size_t count)
1871{
1872 unsigned char selector = 0;
Geliang Tang2cf83832015-12-27 17:25:24 +08001873 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001874 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001875 struct wacom *wacom = hid_get_drvdata(hdev);
1876 int err;
1877
1878 if (!strncmp(buf, "*\n", 2)) {
1879 selector = WAC_CMD_UNPAIR_ALL;
1880 } else {
1881 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1882 buf);
1883 return -1;
1884 }
1885
1886 mutex_lock(&wacom->lock);
1887
1888 err = wacom_cmd_unpair_remote(wacom, selector);
1889 mutex_unlock(&wacom->lock);
1890
1891 return err < 0 ? err : count;
1892}
1893
1894static struct kobj_attribute unpair_remote_attr = {
1895 .attr = {.name = "unpair_remote", .mode = 0200},
1896 .store = wacom_store_unpair_remote,
1897};
1898
1899static const struct attribute *remote_unpair_attrs[] = {
1900 &unpair_remote_attr.attr,
1901 NULL
1902};
1903
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001904static void wacom_remotes_destroy(void *data)
1905{
1906 struct wacom *wacom = data;
1907 struct wacom_remote *remote = wacom->remote;
1908
1909 if (!remote)
1910 return;
1911
1912 kobject_put(remote->remote_dir);
1913 kfifo_free(&remote->remote_fifo);
1914 wacom->remote = NULL;
1915}
1916
1917static int wacom_initialize_remotes(struct wacom *wacom)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001918{
1919 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001920 struct wacom_remote *remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001921 int i;
1922
1923 if (wacom->wacom_wac.features.type != REMOTE)
1924 return 0;
1925
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001926 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1927 GFP_KERNEL);
1928 if (!remote)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001929 return -ENOMEM;
1930
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001931 wacom->remote = remote;
1932
1933 spin_lock_init(&remote->remote_lock);
1934
1935 error = kfifo_alloc(&remote->remote_fifo,
1936 5 * sizeof(struct wacom_remote_data),
1937 GFP_KERNEL);
1938 if (error) {
1939 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1940 return -ENOMEM;
1941 }
1942
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001943 remote->remotes[0].group = remote0_serial_group;
1944 remote->remotes[1].group = remote1_serial_group;
1945 remote->remotes[2].group = remote2_serial_group;
1946 remote->remotes[3].group = remote3_serial_group;
1947 remote->remotes[4].group = remote4_serial_group;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001948
1949 remote->remote_dir = kobject_create_and_add("wacom_remote",
1950 &wacom->hdev->dev.kobj);
1951 if (!remote->remote_dir)
1952 return -ENOMEM;
1953
1954 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001955
1956 if (error) {
1957 hid_err(wacom->hdev,
1958 "cannot create sysfs group err: %d\n", error);
1959 return error;
1960 }
1961
1962 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001963 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001964 remote->remotes[i].serial = 0;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001965 }
1966
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001967 error = devm_add_action_or_reset(&wacom->hdev->dev,
1968 wacom_remotes_destroy, wacom);
1969 if (error)
1970 return error;
1971
Aaron Skomra72b236d2015-08-20 16:05:17 -07001972 return 0;
1973}
1974
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001975static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001976{
1977 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001978 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001979 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001980
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001981 input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001982 if (!input_dev)
1983 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001984
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001985 input_dev->name = wacom_wac->features.name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001986 input_dev->phys = hdev->phys;
1987 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001988 input_dev->open = wacom_open;
1989 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001990 input_dev->uniq = hdev->uniq;
1991 input_dev->id.bustype = hdev->bus;
1992 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04001993 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001994 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001995 input_set_drvdata(input_dev, wacom);
1996
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001997 return input_dev;
1998}
1999
Jason Gerecked9f2d202015-07-13 18:03:44 -07002000static int wacom_allocate_inputs(struct wacom *wacom)
2001{
2002 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2003
2004 wacom_wac->pen_input = wacom_allocate_input(wacom);
2005 wacom_wac->touch_input = wacom_allocate_input(wacom);
2006 wacom_wac->pad_input = wacom_allocate_input(wacom);
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002007 if (!wacom_wac->pen_input ||
2008 !wacom_wac->touch_input ||
2009 !wacom_wac->pad_input)
Jason Gerecked9f2d202015-07-13 18:03:44 -07002010 return -ENOMEM;
Jason Gerecked9f2d202015-07-13 18:03:44 -07002011
Jason Gerecke2bdd1632015-07-13 18:03:45 -07002012 wacom_wac->pen_input->name = wacom_wac->pen_name;
Jason Gerecked9f2d202015-07-13 18:03:44 -07002013 wacom_wac->touch_input->name = wacom_wac->touch_name;
2014 wacom_wac->pad_input->name = wacom_wac->pad_name;
2015
2016 return 0;
2017}
2018
Benjamin Tissoires008f4d9e2014-07-24 12:51:26 -07002019static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002020{
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002021 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002022 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Jason Gerecke2636a3f2015-06-15 18:01:44 -07002023 int error = 0;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002024
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002025 pen_input_dev = wacom_wac->pen_input;
2026 touch_input_dev = wacom_wac->touch_input;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04002027 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002028
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002029 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04002030 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002031
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002032 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
2033 if (error) {
2034 /* no pen in use on this interface */
2035 input_free_device(pen_input_dev);
2036 wacom_wac->pen_input = NULL;
2037 pen_input_dev = NULL;
2038 } else {
2039 error = input_register_device(pen_input_dev);
Ping Cheng30ebc1a2014-11-18 13:29:16 -08002040 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002041 goto fail;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002042 }
2043
2044 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
2045 if (error) {
2046 /* no touch in use on this interface */
2047 input_free_device(touch_input_dev);
2048 wacom_wac->touch_input = NULL;
2049 touch_input_dev = NULL;
2050 } else {
2051 error = input_register_device(touch_input_dev);
2052 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002053 goto fail;
Ping Cheng30ebc1a2014-11-18 13:29:16 -08002054 }
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002055
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002056 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2057 if (error) {
2058 /* no pad in use on this interface */
2059 input_free_device(pad_input_dev);
2060 wacom_wac->pad_input = NULL;
2061 pad_input_dev = NULL;
2062 } else {
2063 error = input_register_device(pad_input_dev);
2064 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002065 goto fail;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002066 }
2067
Ping Cheng19635182012-04-29 21:09:18 -07002068 return 0;
2069
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002070fail:
2071 wacom_wac->pad_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002072 wacom_wac->touch_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002073 wacom_wac->pen_input = NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002074 return error;
2075}
2076
Jason Gerecke0be01712015-08-05 15:44:53 -07002077/*
2078 * Not all devices report physical dimensions from HID.
2079 * Compute the default from hardcoded logical dimension
2080 * and resolution before driver overwrites them.
2081 */
2082static void wacom_set_default_phy(struct wacom_features *features)
2083{
2084 if (features->x_resolution) {
2085 features->x_phy = (features->x_max * 100) /
2086 features->x_resolution;
2087 features->y_phy = (features->y_max * 100) /
2088 features->y_resolution;
2089 }
2090}
2091
2092static void wacom_calculate_res(struct wacom_features *features)
2093{
2094 /* set unit to "100th of a mm" for devices not reported by HID */
2095 if (!features->unit) {
2096 features->unit = 0x11;
2097 features->unitExpo = -3;
2098 }
2099
2100 features->x_resolution = wacom_calc_hid_res(features->x_max,
2101 features->x_phy,
2102 features->unit,
2103 features->unitExpo);
2104 features->y_resolution = wacom_calc_hid_res(features->y_max,
2105 features->y_phy,
2106 features->unit,
2107 features->unitExpo);
2108}
2109
Jason Gereckefce99572015-03-06 11:47:40 -08002110void wacom_battery_work(struct work_struct *work)
2111{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002112 struct wacom *wacom = container_of(work, struct wacom, battery_work);
Jason Gereckefce99572015-03-06 11:47:40 -08002113
2114 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02002115 !wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08002116 wacom_initialize_battery(wacom);
2117 }
2118 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02002119 wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08002120 wacom_destroy_battery(wacom);
2121 }
2122}
2123
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002124static size_t wacom_compute_pktlen(struct hid_device *hdev)
2125{
2126 struct hid_report_enum *report_enum;
2127 struct hid_report *report;
2128 size_t size = 0;
2129
2130 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2131
2132 list_for_each_entry(report, &report_enum->report_list, list) {
Mathieu Magnaudetdabb05c62014-11-27 16:02:36 +01002133 size_t report_size = hid_report_len(report);
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002134 if (report_size > size)
2135 size = report_size;
2136 }
2137
2138 return size;
2139}
2140
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002141static void wacom_update_name(struct wacom *wacom, const char *suffix)
Ping Chengc24eab42015-04-24 15:32:51 -07002142{
2143 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2144 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke44b52502015-06-15 18:01:41 -07002145 char name[WACOM_NAME_MAX];
Ping Chengc24eab42015-04-24 15:32:51 -07002146
2147 /* Generic devices name unspecified */
2148 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
Jason Gerecke09dc28a2017-07-24 09:46:19 -07002149 char *product_name = wacom->hdev->name;
Ping Chengc24eab42015-04-24 15:32:51 -07002150
Jason Gerecke09dc28a2017-07-24 09:46:19 -07002151 if (hid_is_using_ll_driver(wacom->hdev, &usb_hid_driver)) {
2152 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2153 struct usb_device *dev = interface_to_usbdev(intf);
2154 product_name = dev->product;
Ping Chengc24eab42015-04-24 15:32:51 -07002155 }
Jason Gerecke09dc28a2017-07-24 09:46:19 -07002156
2157 if (wacom->hdev->bus == BUS_I2C) {
2158 snprintf(name, sizeof(name), "%s %X",
2159 features->name, wacom->hdev->product);
2160 } else if (strstr(product_name, "Wacom") ||
2161 strstr(product_name, "wacom") ||
2162 strstr(product_name, "WACOM")) {
2163 strlcpy(name, product_name, sizeof(name));
2164 } else {
2165 snprintf(name, sizeof(name), "Wacom %s", product_name);
2166 }
2167
2168 /* strip out excess whitespaces */
2169 while (1) {
2170 char *gap = strstr(name, " ");
2171 if (gap == NULL)
2172 break;
2173 /* shift everything including the terminator */
2174 memmove(gap, gap+1, strlen(gap));
2175 }
2176
2177 /* get rid of trailing whitespace */
2178 if (name[strlen(name)-1] == ' ')
2179 name[strlen(name)-1] = '\0';
Ping Chengc24eab42015-04-24 15:32:51 -07002180 } else {
Jason Gerecke44b52502015-06-15 18:01:41 -07002181 strlcpy(name, features->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07002182 }
2183
Benjamin Tissoires99569532016-07-13 18:06:17 +02002184 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2185 name, suffix);
2186
Ping Chengc24eab42015-04-24 15:32:51 -07002187 /* Append the device type to the name */
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002188 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002189 "%s%s Pen", name, suffix);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002190 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002191 "%s%s Finger", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07002192 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002193 "%s%s Pad", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07002194}
2195
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002196static void wacom_release_resources(struct wacom *wacom)
2197{
2198 struct hid_device *hdev = wacom->hdev;
2199
2200 if (!wacom->resources)
2201 return;
2202
2203 devres_release_group(&hdev->dev, wacom);
2204
2205 wacom->resources = false;
2206
2207 wacom->wacom_wac.pen_input = NULL;
2208 wacom->wacom_wac.touch_input = NULL;
2209 wacom->wacom_wac.pad_input = NULL;
2210}
2211
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002212static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2213{
2214 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2215 wacom_wac->shared->type = wacom_wac->features.type;
2216 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2217 }
2218
Ping Chengd793ff82017-02-14 21:27:45 -08002219 if (wacom_wac->has_mute_touch_switch) {
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002220 wacom_wac->shared->has_mute_touch_switch = true;
Ping Chengd793ff82017-02-14 21:27:45 -08002221 wacom_wac->shared->is_touch_on = true;
2222 }
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002223
2224 if (wacom_wac->shared->has_mute_touch_switch &&
2225 wacom_wac->shared->touch_input) {
2226 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2227 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2228 SW_MUTE_DEVICE);
2229 }
2230}
2231
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002232static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
Ping Cheng3bea7332006-07-13 18:01:36 -07002233{
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002234 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2235 struct wacom_features *features = &wacom_wac->features;
2236 struct hid_device *hdev = wacom->hdev;
Jason Childse33da8a2010-02-17 22:38:31 -08002237 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002238 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07002239
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002240 features->pktlen = wacom_compute_pktlen(hdev);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002241 if (features->pktlen > WACOM_PKGLEN_MAX)
2242 return -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07002243
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002244 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2245 return -ENOMEM;
2246
2247 wacom->resources = true;
2248
Jason Gerecke3f14a632015-08-03 10:17:05 -07002249 error = wacom_allocate_inputs(wacom);
2250 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002251 goto fail;
Benjamin Tissoires494078b2014-09-23 12:08:07 -04002252
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05002253 /*
2254 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2255 * into debug mode for the touch part.
2256 * We ignore the other interfaces.
2257 */
2258 if (features->type == BAMBOO_PAD) {
2259 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2260 features->type = HID_GENERIC;
2261 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2262 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2263 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002264 goto fail;
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05002265 }
2266 }
2267
Ping Cheng401d7d12013-07-28 00:38:54 -07002268 /* set the default size in case we do not get them from hid */
2269 wacom_set_default_phy(features);
2270
Ping Chengf393ee22012-04-29 21:09:17 -07002271 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07002272 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng42f4f272015-04-15 16:53:54 -07002273 wacom_setup_device_quirks(wacom);
Jason Gerecke042628a2015-04-30 17:51:54 -07002274
Jason Gereckeaa86b182015-06-15 18:01:42 -07002275 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2276 features->type != WIRELESS) {
Jason Gerecke8e116d32015-04-30 17:51:55 -07002277 error = features->type == HID_GENERIC ? -ENODEV : 0;
2278
Jason Gerecke042628a2015-04-30 17:51:54 -07002279 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
Jason Gerecke8e116d32015-04-30 17:51:55 -07002280 hdev->name,
2281 error ? "Ignoring" : "Assuming pen");
2282
2283 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002284 goto fail;
Jason Gerecke042628a2015-04-30 17:51:54 -07002285
Jason Gereckeaa86b182015-06-15 18:01:42 -07002286 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -07002287 }
2288
Ping Cheng401d7d12013-07-28 00:38:54 -07002289 wacom_calculate_res(features);
2290
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002291 wacom_update_name(wacom, wireless ? " (WL)" : "");
Ping Cheng4492eff2010-03-19 22:18:15 -07002292
Aaron Armstrong Skomra8b407352017-03-29 10:35:39 -07002293 /* pen only Bamboo neither support touch nor pad */
2294 if ((features->type == BAMBOO_PEN) &&
2295 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2296 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2297 error = -ENODEV;
2298 goto fail;
2299 }
2300
Jason Gereckea9ce7852017-01-17 15:38:58 -08002301 error = wacom_add_shared_data(hdev);
2302 if (error)
2303 goto fail;
Ping Cheng49b764a2010-02-20 00:53:49 -08002304
Jason Gereckeccad85c2015-08-03 10:17:04 -07002305 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002306 (features->quirks & WACOM_QUIRK_BATTERY)) {
2307 error = wacom_initialize_battery(wacom);
2308 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002309 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002310 }
2311
Jason Gerecke3f14a632015-08-03 10:17:05 -07002312 error = wacom_register_inputs(wacom);
2313 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002314 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002315
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002316 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002317 error = wacom_initialize_leds(wacom);
2318 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002319 goto fail;
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002320
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002321 error = wacom_initialize_remotes(wacom);
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002322 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002323 goto fail;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002324 }
2325
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002326 if (features->type == HID_GENERIC)
2327 connect_mask |= HID_CONNECT_DRIVER;
2328
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002329 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002330 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002331 if (error) {
2332 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002333 goto fail;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002334 }
2335
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002336 if (!wireless) {
2337 /* Note that if query fails it is not a hard failure */
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002338 wacom_query_tablet_data(wacom);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002339 }
Jason Gerecke86e88f02015-11-03 16:57:58 -08002340
2341 /* touch only Bamboo doesn't support pen */
2342 if ((features->type == BAMBOO_TOUCH) &&
2343 (features->device_type & WACOM_DEVICETYPE_PEN)) {
Aaron Armstrong Skomra4d20c332017-03-29 11:41:28 -07002344 cancel_delayed_work_sync(&wacom->init_work);
2345 _wacom_query_tablet_data(wacom);
Jason Gerecke86e88f02015-11-03 16:57:58 -08002346 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002347 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08002348 }
2349
Jason Gereckeccad85c2015-08-03 10:17:04 -07002350 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002351 error = hid_hw_open(hdev);
2352
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002353 wacom_set_shared_values(wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002354 devres_close_group(&hdev->dev, wacom);
2355
Ping Cheng3bea7332006-07-13 18:01:36 -07002356 return 0;
2357
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002358fail_quirks:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002359 hid_hw_stop(hdev);
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002360fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002361 wacom_release_resources(wacom);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002362 return error;
2363}
2364
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002365static void wacom_wireless_work(struct work_struct *work)
2366{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002367 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002368 struct usb_device *usbdev = wacom->usbdev;
2369 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2370 struct hid_device *hdev1, *hdev2;
2371 struct wacom *wacom1, *wacom2;
2372 struct wacom_wac *wacom_wac1, *wacom_wac2;
2373 int error;
2374
2375 /*
2376 * Regardless if this is a disconnect or a new tablet,
2377 * remove any existing input and battery devices.
2378 */
2379
2380 wacom_destroy_battery(wacom);
2381
2382 /* Stylus interface */
2383 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2384 wacom1 = hid_get_drvdata(hdev1);
2385 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002386 wacom_release_resources(wacom1);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002387
2388 /* Touch interface */
2389 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2390 wacom2 = hid_get_drvdata(hdev2);
2391 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002392 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002393
2394 if (wacom_wac->pid == 0) {
2395 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002396 } else {
2397 const struct hid_device_id *id = wacom_ids;
2398
2399 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2400 wacom_wac->pid);
2401
2402 while (id->bus) {
2403 if (id->vendor == USB_VENDOR_ID_WACOM &&
2404 id->product == wacom_wac->pid)
2405 break;
2406 id++;
2407 }
2408
2409 if (!id->bus) {
2410 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2411 return;
2412 }
2413
2414 /* Stylus interface */
2415 wacom_wac1->features =
2416 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002417
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002418 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002419 hid_hw_stop(hdev1);
2420 error = wacom_parse_and_register(wacom1, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002421 if (error)
2422 goto fail;
2423
2424 /* Touch interface */
2425 if (wacom_wac1->features.touch_max ||
2426 (wacom_wac1->features.type >= INTUOSHT &&
2427 wacom_wac1->features.type <= BAMBOO_PT)) {
2428 wacom_wac2->features =
2429 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002430 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002431 hid_hw_stop(hdev2);
2432 error = wacom_parse_and_register(wacom2, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002433 if (error)
2434 goto fail;
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002435 }
2436
Benjamin Tissoires99569532016-07-13 18:06:17 +02002437 strlcpy(wacom_wac->name, wacom_wac1->name,
2438 sizeof(wacom_wac->name));
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002439 error = wacom_initialize_battery(wacom);
2440 if (error)
2441 goto fail;
2442 }
2443
2444 return;
2445
2446fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002447 wacom_release_resources(wacom1);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002448 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002449 return;
2450}
2451
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002452static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2453{
2454 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002455 u32 serial = remote->remotes[index].serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002456 int i;
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002457 unsigned long flags;
2458
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002459 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002460 if (remote->remotes[i].serial == serial) {
Aaron Armstrong Skomra791ae272017-12-07 12:31:56 -08002461
2462 spin_lock_irqsave(&remote->remote_lock, flags);
2463 remote->remotes[i].registered = false;
2464 spin_unlock_irqrestore(&remote->remote_lock, flags);
2465
2466 if (remote->remotes[i].battery.battery)
2467 devres_release_group(&wacom->hdev->dev,
2468 &remote->remotes[i].battery.bat_desc);
2469
2470 if (remote->remotes[i].group.name)
2471 devres_release_group(&wacom->hdev->dev,
2472 &remote->remotes[i]);
2473
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002474 remote->remotes[i].serial = 0;
2475 remote->remotes[i].group.name = NULL;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002476 remote->remotes[i].battery.battery = NULL;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002477 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2478 }
2479 }
2480}
2481
2482static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2483 unsigned int index)
2484{
2485 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002486 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002487 int error, k;
2488
2489 /* A remote can pair more than once with an EKR,
2490 * check to make sure this serial isn't already paired.
2491 */
2492 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002493 if (remote->remotes[k].serial == serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002494 break;
2495 }
2496
2497 if (k < WACOM_MAX_REMOTES) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002498 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002499 return 0;
2500 }
2501
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002502 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002503 return -ENOMEM;
2504
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002505 error = wacom_remote_create_attr_group(wacom, serial, index);
2506 if (error)
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002507 goto fail;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002508
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002509 remote->remotes[index].input = wacom_allocate_input(wacom);
2510 if (!remote->remotes[index].input) {
2511 error = -ENOMEM;
2512 goto fail;
2513 }
2514 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2515 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2516
2517 if (!remote->remotes[index].input->name) {
2518 error = -EINVAL;
2519 goto fail;
2520 }
2521
2522 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2523 &wacom->wacom_wac);
2524 if (error)
2525 goto fail;
2526
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002527 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002528
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002529 error = input_register_device(remote->remotes[index].input);
2530 if (error)
2531 goto fail;
2532
Benjamin Tissoires97f55412016-07-13 18:06:10 +02002533 error = wacom_led_groups_alloc_and_register_one(
2534 &remote->remotes[index].input->dev,
2535 wacom, index, 3, true);
2536 if (error)
2537 goto fail;
2538
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002539 remote->remotes[index].registered = true;
2540
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002541 devres_close_group(dev, &remote->remotes[index]);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002542 return 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002543
2544fail:
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002545 devres_release_group(dev, &remote->remotes[index]);
2546 remote->remotes[index].serial = 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002547 return error;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002548}
2549
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002550static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2551{
2552 struct wacom_remote *remote = wacom->remote;
2553 int error;
2554
2555 if (!remote->remotes[index].registered)
2556 return 0;
2557
2558 if (remote->remotes[index].battery.battery)
2559 return 0;
2560
2561 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2562 return 0;
2563
2564 error = __wacom_initialize_battery(wacom,
2565 &wacom->remote->remotes[index].battery);
2566 if (error)
2567 return error;
2568
2569 return 0;
2570}
2571
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002572static void wacom_remote_work(struct work_struct *work)
2573{
2574 struct wacom *wacom = container_of(work, struct wacom, remote_work);
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002575 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002576 struct wacom_remote_data data;
2577 unsigned long flags;
2578 unsigned int count;
2579 u32 serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002580 int i;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002581
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002582 spin_lock_irqsave(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002583
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002584 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002585
2586 if (count != sizeof(data)) {
2587 hid_err(wacom->hdev,
2588 "workitem triggered without status available\n");
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002589 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002590 return;
2591 }
2592
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002593 if (!kfifo_is_empty(&remote->remote_fifo))
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002594 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2595
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002596 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002597
2598 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2599 serial = data.remote[i].serial;
2600 if (data.remote[i].connected) {
2601
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002602 if (remote->remotes[i].serial == serial) {
2603 wacom_remote_attach_battery(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002604 continue;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002605 }
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002606
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002607 if (remote->remotes[i].serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002608 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002609
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002610 wacom_remote_create_one(wacom, serial, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002611
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002612 } else if (remote->remotes[i].serial) {
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002613 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002614 }
2615 }
2616}
2617
Benjamin Tissoires4082da82017-02-14 21:27:18 -08002618static void wacom_mode_change_work(struct work_struct *work)
2619{
2620 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2621 struct wacom_shared *shared = wacom->wacom_wac.shared;
2622 struct wacom *wacom1 = NULL;
2623 struct wacom *wacom2 = NULL;
2624 bool is_direct = wacom->wacom_wac.is_direct_mode;
2625 int error = 0;
2626
2627 if (shared->pen) {
2628 wacom1 = hid_get_drvdata(shared->pen);
2629 wacom_release_resources(wacom1);
2630 hid_hw_stop(wacom1->hdev);
2631 wacom1->wacom_wac.has_mode_change = true;
2632 wacom1->wacom_wac.is_direct_mode = is_direct;
2633 }
2634
2635 if (shared->touch) {
2636 wacom2 = hid_get_drvdata(shared->touch);
2637 wacom_release_resources(wacom2);
2638 hid_hw_stop(wacom2->hdev);
2639 wacom2->wacom_wac.has_mode_change = true;
2640 wacom2->wacom_wac.is_direct_mode = is_direct;
2641 }
2642
2643 if (wacom1) {
2644 error = wacom_parse_and_register(wacom1, false);
2645 if (error)
2646 return;
2647 }
2648
2649 if (wacom2) {
2650 error = wacom_parse_and_register(wacom2, false);
2651 if (error)
2652 return;
2653 }
2654
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002655 return;
2656}
2657
2658static int wacom_probe(struct hid_device *hdev,
2659 const struct hid_device_id *id)
2660{
2661 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2662 struct usb_device *dev = interface_to_usbdev(intf);
2663 struct wacom *wacom;
2664 struct wacom_wac *wacom_wac;
2665 struct wacom_features *features;
2666 int error;
2667
2668 if (!id->driver_data)
2669 return -EINVAL;
2670
2671 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2672
2673 /* hid-core sets this quirk for the boot interface */
2674 hdev->quirks &= ~HID_QUIRK_NOGET;
2675
Benjamin Tissoires19b64332016-07-13 18:05:58 +02002676 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002677 if (!wacom)
2678 return -ENOMEM;
2679
2680 hid_set_drvdata(hdev, wacom);
2681 wacom->hdev = hdev;
2682
2683 wacom_wac = &wacom->wacom_wac;
2684 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2685 features = &wacom_wac->features;
2686
2687 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2688 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002689 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002690 }
2691
Jason Gerecke83417202017-11-10 11:50:01 -08002692 error = kfifo_alloc(&wacom_wac->pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
2693 if (error)
2694 goto fail;
2695
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002696 wacom_wac->hid_data.inputmode = -1;
Jason Gerecke326ea2a2016-04-04 11:26:52 -07002697 wacom_wac->mode_report = -1;
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002698
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002699 wacom->usbdev = dev;
2700 wacom->intf = intf;
2701 mutex_init(&wacom->lock);
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002702 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002703 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2704 INIT_WORK(&wacom->battery_work, wacom_battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002705 INIT_WORK(&wacom->remote_work, wacom_remote_work);
Benjamin Tissoires4082da82017-02-14 21:27:18 -08002706 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002707
2708 /* ask for the report descriptor to be loaded by HID */
2709 error = hid_parse(hdev);
2710 if (error) {
2711 hid_err(hdev, "parse failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002712 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002713 }
2714
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002715 error = wacom_parse_and_register(wacom, false);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002716 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002717 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002718
2719 if (hdev->bus == BUS_BLUETOOTH) {
2720 error = device_create_file(&hdev->dev, &dev_attr_speed);
2721 if (error)
2722 hid_warn(hdev,
2723 "can't create sysfs speed attribute err: %d\n",
2724 error);
2725 }
2726
2727 return 0;
2728
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002729fail:
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002730 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04002731 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07002732}
2733
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002734static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07002735{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002736 struct wacom *wacom = hid_get_drvdata(hdev);
Benjamin Tissoiresf6205162016-02-12 17:27:45 +01002737 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2738 struct wacom_features *features = &wacom_wac->features;
2739
2740 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2741 hid_hw_close(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07002742
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002743 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002744
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002745 cancel_delayed_work_sync(&wacom->init_work);
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002746 cancel_work_sync(&wacom->wireless_work);
2747 cancel_work_sync(&wacom->battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002748 cancel_work_sync(&wacom->remote_work);
Benjamin Tissoires4082da82017-02-14 21:27:18 -08002749 cancel_work_sync(&wacom->mode_change_work);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002750 if (hdev->bus == BUS_BLUETOOTH)
2751 device_remove_file(&hdev->dev, &dev_attr_speed);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002752
Benjamin Tissoiresc0265a942017-01-20 16:20:12 +01002753 /* make sure we don't trigger the LEDs */
2754 wacom_led_groups_release(wacom);
Aaron Armstrong Skomrab6b1f192017-03-06 10:54:58 -08002755
2756 if (wacom->wacom_wac.features.type != REMOTE)
2757 wacom_release_resources(wacom);
Benjamin Tissoires5b779fc2017-01-20 16:20:11 +01002758
Jason Gerecke83417202017-11-10 11:50:01 -08002759 kfifo_free(&wacom_wac->pen_fifo);
2760
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002761 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04002762}
2763
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002764#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002765static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002766{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002767 struct wacom *wacom = hid_get_drvdata(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002768
2769 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002770
2771 /* switch to wacom mode first */
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002772 _wacom_query_tablet_data(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002773 wacom_led_control(wacom);
2774
Oliver Neukume7224092008-04-15 01:31:57 -04002775 mutex_unlock(&wacom->lock);
2776
2777 return 0;
2778}
2779
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002780static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002781{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002782 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002783}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002784#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04002785
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002786static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07002787 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08002788 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07002789 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002790 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002791 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002792#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04002793 .resume = wacom_resume,
2794 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002795#endif
2796 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07002797};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002798module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07002799
2800MODULE_VERSION(DRIVER_VERSION);
2801MODULE_AUTHOR(DRIVER_AUTHOR);
2802MODULE_DESCRIPTION(DRIVER_DESC);
Grant Grundler7021b602017-01-05 11:07:04 -08002803MODULE_LICENSE("GPL");