blob: b54ef1ffcbec329b99520365a63bf347d16a66d2 [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
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700213static void wacom_feature_mapping(struct hid_device *hdev,
214 struct hid_field *field, struct hid_usage *usage)
Chris Bagwell41343612011-10-26 22:32:52 -0700215{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700216 struct wacom *wacom = hid_get_drvdata(hdev);
217 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400218 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
Aaron Armstrong Skomraac2423c2017-01-25 12:08:40 -0800219 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400220 u8 *data;
221 int ret;
Aaron Ma3064a032018-02-03 23:57:15 +0800222 u32 n;
Chris Bagwell41343612011-10-26 22:32:52 -0700223
Aaron Armstrong Skomraac2423c2017-01-25 12:08:40 -0800224 switch (equivalent_usage) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700225 case HID_DG_CONTACTMAX:
226 /* leave touch_max as is if predefined */
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400227 if (!features->touch_max) {
228 /* read manually */
229 data = kzalloc(2, GFP_KERNEL);
230 if (!data)
231 break;
232 data[0] = field->report->id;
233 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700234 data, 2, WAC_CMD_RETRIES);
235 if (ret == 2) {
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400236 features->touch_max = data[1];
Jason Gerecke05e8fd92015-05-21 10:44:32 -0700237 } else {
238 features->touch_max = 16;
239 hid_warn(hdev, "wacom_feature_mapping: "
240 "could not get HID_DG_CONTACTMAX, "
241 "defaulting to %d\n",
242 features->touch_max);
243 }
Benjamin Tissoires8ffffd52014-09-16 16:56:39 -0400244 kfree(data);
245 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700246 break;
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400247 case HID_DG_INPUTMODE:
248 /* Ignore if value index is out of bounds. */
249 if (usage->usage_index >= field->report_count) {
250 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
251 break;
252 }
253
254 hid_data->inputmode = field->report->id;
255 hid_data->inputmode_index = usage->usage_index;
256 break;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700257
258 case HID_UP_DIGITIZER:
259 if (field->report->id == 0x0B &&
Jason Gerecke8de82282016-10-19 18:03:37 -0700260 (field->application == WACOM_HID_G9_PEN ||
261 field->application == WACOM_HID_G11_PEN)) {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700262 wacom->wacom_wac.mode_report = field->report->id;
263 wacom->wacom_wac.mode_value = 0;
264 }
265 break;
266
Jason Gereckec9c09582016-10-19 18:03:43 -0700267 case WACOM_HID_WD_DATAMODE:
268 wacom->wacom_wac.mode_report = field->report->id;
269 wacom->wacom_wac.mode_value = 2;
270 break;
271
Jason Gerecke8de82282016-10-19 18:03:37 -0700272 case WACOM_HID_UP_G9:
273 case WACOM_HID_UP_G11:
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700274 if (field->report->id == 0x03 &&
Jason Gerecke8de82282016-10-19 18:03:37 -0700275 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
276 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700277 wacom->wacom_wac.mode_report = field->report->id;
278 wacom->wacom_wac.mode_value = 0;
279 }
280 break;
Jason Gerecke345857b2016-10-19 18:03:50 -0700281 case WACOM_HID_WD_OFFSETLEFT:
282 case WACOM_HID_WD_OFFSETTOP:
283 case WACOM_HID_WD_OFFSETRIGHT:
284 case WACOM_HID_WD_OFFSETBOTTOM:
285 /* read manually */
286 n = hid_report_len(field->report);
287 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
288 if (!data)
289 break;
290 data[0] = field->report->id;
291 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
292 data, n, WAC_CMD_RETRIES);
293 if (ret == n) {
294 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
295 data, n, 0);
296 } else {
297 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
298 __func__);
299 }
300 kfree(data);
301 break;
Ping Chengf393ee22012-04-29 21:09:17 -0700302 }
Benjamin Tissoires5b01b3b2017-11-03 18:29:47 +0100303
304 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
305 hdev->product == 0x4200 /* Dell Canvas 27 */ &&
306 field->application == HID_UP_MSVENDOR) {
307 wacom->wacom_wac.mode_report = field->report->id;
308 wacom->wacom_wac.mode_value = 2;
309 }
Ping Chengf393ee22012-04-29 21:09:17 -0700310}
311
Chris Bagwell428f8582011-10-26 22:26:59 -0700312/*
313 * Interface Descriptor of wacom devices can be incomplete and
314 * inconsistent so wacom_features table is used to store stylus
315 * device's packet lengths, various maximum values, and tablet
316 * resolution based on product ID's.
317 *
318 * For devices that contain 2 interfaces, wacom_features table is
319 * inaccurate for the touch interface. Since the Interface Descriptor
320 * for touch interfaces has pretty complete data, this function exists
321 * to query tablet for this missing information instead of hard coding in
322 * an additional table.
323 *
324 * A typical Interface Descriptor for a stylus will contain a
325 * boot mouse application collection that is not of interest and this
326 * function will ignore it.
327 *
328 * It also contains a digitizer application collection that also is not
329 * of interest since any information it contains would be duplicate
330 * of what is in wacom_features. Usually it defines a report of an array
331 * of bytes that could be used as max length of the stylus packet returned.
332 * If it happens to define a Digitizer-Stylus Physical Collection then
333 * the X and Y logical values contain valid data but it is ignored.
334 *
335 * A typical Interface Descriptor for a touch interface will contain a
336 * Digitizer-Finger Physical Collection which will define both logical
337 * X/Y maximum as well as the physical size of tablet. Since touch
338 * interfaces haven't supported pressure or distance, this is enough
339 * information to override invalid values in the wacom_features table.
Chris Bagwell41343612011-10-26 22:32:52 -0700340 *
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700341 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
342 * data. We deal with them after returning from this function.
Chris Bagwell428f8582011-10-26 22:26:59 -0700343 */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700344static void wacom_usage_mapping(struct hid_device *hdev,
345 struct hid_field *field, struct hid_usage *usage)
346{
347 struct wacom *wacom = hid_get_drvdata(hdev);
348 struct wacom_features *features = &wacom->wacom_wac.features;
Benjamin Tissoiresd97a5522015-01-05 16:32:12 -0500349 bool finger = WACOM_FINGER_FIELD(field);
350 bool pen = WACOM_PEN_FIELD(field);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700351
352 /*
353 * Requiring Stylus Usage will ignore boot mouse
354 * X/Y values and some cases of invalid Digitizer X/Y
355 * values commonly reported.
356 */
Jason Gerecke042628a2015-04-30 17:51:54 -0700357 if (pen)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700358 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -0700359 else if (finger)
Jason Gereckeaa86b182015-06-15 18:01:42 -0700360 features->device_type |= WACOM_DEVICETYPE_TOUCH;
Jason Gerecke042628a2015-04-30 17:51:54 -0700361 else
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700362 return;
363
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800364 /*
365 * Bamboo models do not support HID_DG_CONTACTMAX.
366 * And, Bamboo Pen only descriptor contains touch.
367 */
Ping Cheng3b164a02015-09-23 09:59:10 -0700368 if (features->type > BAMBOO_PT) {
Ping Cheng30ebc1a2014-11-18 13:29:16 -0800369 /* ISDv4 touch devices at least supports one touch point */
370 if (finger && !features->touch_max)
371 features->touch_max = 1;
372 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700373
Jason Gerecke6005a132016-10-19 18:03:40 -0700374 /*
375 * ISDv4 devices which predate HID's adoption of the
376 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
377 * position instead. We can accurately detect if a
378 * usage with that value should be HID_DG_BARRELSWITCH2
379 * based on the surrounding usages, which have remained
380 * constant across generations.
381 */
382 if (features->type == HID_GENERIC &&
383 usage->hid == 0x000D0000 &&
384 field->application == HID_DG_PEN &&
385 field->physical == HID_DG_STYLUS) {
386 int i = usage->usage_index;
387
388 if (i-4 >= 0 && i+1 < field->maxusage &&
389 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
390 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
391 field->usage[i-2].hid == HID_DG_ERASER &&
392 field->usage[i-1].hid == HID_DG_INVERT &&
393 field->usage[i+1].hid == HID_DG_INRANGE) {
394 usage->hid = HID_DG_BARRELSWITCH2;
395 }
396 }
397
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700398 switch (usage->hid) {
399 case HID_GD_X:
400 features->x_max = field->logical_maximum;
401 if (finger) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700402 features->x_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700403 if ((features->type != BAMBOO_PT) &&
404 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700405 features->unit = field->unit;
406 features->unitExpo = field->unit_exponent;
407 }
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700408 }
409 break;
410 case HID_GD_Y:
411 features->y_max = field->logical_maximum;
412 if (finger) {
413 features->y_phy = field->physical_maximum;
Ping Cheng3b164a02015-09-23 09:59:10 -0700414 if ((features->type != BAMBOO_PT) &&
415 (features->type != BAMBOO_TOUCH)) {
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700416 features->unit = field->unit;
417 features->unitExpo = field->unit_exponent;
418 }
419 }
420 break;
421 case HID_DG_TIPPRESSURE:
422 if (pen)
423 features->pressure_max = field->logical_maximum;
424 break;
425 }
Benjamin Tissoires7704ac92014-09-23 12:08:08 -0400426
427 if (features->type == HID_GENERIC)
428 wacom_wac_usage_mapping(hdev, field, usage);
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700429}
430
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800431static void wacom_post_parse_hid(struct hid_device *hdev,
432 struct wacom_features *features)
433{
434 struct wacom *wacom = hid_get_drvdata(hdev);
435 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
436
437 if (features->type == HID_GENERIC) {
438 /* Any last-minute generic device setup */
Benjamin Tissoires4082da82017-02-14 21:27:18 -0800439 if (wacom_wac->has_mode_change) {
440 if (wacom_wac->is_direct_mode)
441 features->device_type |= WACOM_DEVICETYPE_DIRECT;
442 else
443 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
444 }
445
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800446 if (features->touch_max > 1) {
Aaron Armstrong Skomraac2423c2017-01-25 12:08:40 -0800447 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
448 input_mt_init_slots(wacom_wac->touch_input,
449 wacom_wac->features.touch_max,
450 INPUT_MT_DIRECT);
451 else
452 input_mt_init_slots(wacom_wac->touch_input,
453 wacom_wac->features.touch_max,
454 INPUT_MT_POINTER);
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800455 }
456 }
457}
458
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700459static void wacom_parse_hid(struct hid_device *hdev,
Ping Chengec67bbe2009-12-15 00:35:24 -0800460 struct wacom_features *features)
Ping Cheng545f4e92008-11-24 11:44:27 -0500461{
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700462 struct hid_report_enum *rep_enum;
463 struct hid_report *hreport;
464 int i, j;
Ping Cheng545f4e92008-11-24 11:44:27 -0500465
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700466 /* check features first */
467 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
468 list_for_each_entry(hreport, &rep_enum->report_list, list) {
469 for (i = 0; i < hreport->maxfield; i++) {
470 /* Ignore if report count is out of bounds. */
471 if (hreport->field[i]->report_count < 1)
472 continue;
Ping Cheng545f4e92008-11-24 11:44:27 -0500473
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700474 for (j = 0; j < hreport->field[i]->maxusage; j++) {
475 wacom_feature_mapping(hdev, hreport->field[i],
476 hreport->field[i]->usage + j);
Ping Cheng545f4e92008-11-24 11:44:27 -0500477 }
Ping Cheng545f4e92008-11-24 11:44:27 -0500478 }
479 }
480
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700481 /* now check the input usages */
482 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
483 list_for_each_entry(hreport, &rep_enum->report_list, list) {
484
485 if (!hreport->maxfield)
486 continue;
487
488 for (i = 0; i < hreport->maxfield; i++)
489 for (j = 0; j < hreport->field[i]->maxusage; j++)
490 wacom_usage_mapping(hdev, hreport->field[i],
491 hreport->field[i]->usage + j);
492 }
Jason Gereckeb58ba1b2014-12-05 13:37:32 -0800493
494 wacom_post_parse_hid(hdev, features);
Ping Cheng545f4e92008-11-24 11:44:27 -0500495}
496
Benjamin Tissoires5ae6e892014-09-23 12:08:09 -0400497static int wacom_hid_set_device_mode(struct hid_device *hdev)
498{
499 struct wacom *wacom = hid_get_drvdata(hdev);
500 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
501 struct hid_report *r;
502 struct hid_report_enum *re;
503
504 if (hid_data->inputmode < 0)
505 return 0;
506
507 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
508 r = re->report_id_hash[hid_data->inputmode];
509 if (r) {
510 r->field[0]->value[hid_data->inputmode_index] = 2;
511 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
512 }
513 return 0;
514}
515
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700516static int wacom_set_device_mode(struct hid_device *hdev,
517 struct wacom_wac *wacom_wac)
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700518{
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700519 u8 *rep_data;
520 struct hid_report *r;
521 struct hid_report_enum *re;
Aaron Ma3064a032018-02-03 23:57:15 +0800522 u32 length;
Jason Gereckefe494bc2012-10-03 17:25:35 -0700523 int error = -ENOMEM, limit = 0;
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700524
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700525 if (wacom_wac->mode_report < 0)
526 return 0;
527
528 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
529 r = re->report_id_hash[wacom_wac->mode_report];
530 if (!r)
531 return -EINVAL;
532
533 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700534 if (!rep_data)
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700535 return -ENOMEM;
536
537 length = hid_report_len(r);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700538
Jason Gereckefe494bc2012-10-03 17:25:35 -0700539 do {
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700540 rep_data[0] = wacom_wac->mode_report;
541 rep_data[1] = wacom_wac->mode_value;
Chris Bagwell9937c022013-01-23 19:37:34 -0800542
Przemo Firszt296b7372014-08-06 14:00:38 -0700543 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
544 length, 1);
Benjamin Tissoires3cb83152014-07-24 12:47:47 -0700545 if (error >= 0)
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700546 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
Ping Chengc64d8832014-09-10 12:41:04 -0700547 rep_data, length, 1);
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700548 } while (error >= 0 &&
549 rep_data[1] != wacom_wac->mode_report &&
550 limit++ < WAC_MSG_RETRIES);
Dmitry Torokhov3b7307c2009-08-20 21:41:04 -0700551
552 kfree(rep_data);
553
554 return error < 0 ? error : 0;
555}
556
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700557static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
558 struct wacom_features *features)
559{
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700560 struct wacom *wacom = hid_get_drvdata(hdev);
561 int ret;
562 u8 rep_data[2];
563
564 switch (features->type) {
565 case GRAPHIRE_BT:
566 rep_data[0] = 0x03;
567 rep_data[1] = 0x00;
Przemo Firszt296b7372014-08-06 14:00:38 -0700568 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
569 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700570
571 if (ret >= 0) {
572 rep_data[0] = speed == 0 ? 0x05 : 0x06;
573 rep_data[1] = 0x00;
574
575 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700576 rep_data, 2, 3);
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700577
578 if (ret >= 0) {
579 wacom->wacom_wac.bt_high_speed = speed;
580 return 0;
581 }
582 }
583
584 /*
585 * Note that if the raw queries fail, it's not a hard failure
586 * and it is safe to continue
587 */
588 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
589 rep_data[0], ret);
590 break;
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700591 case INTUOS4WL:
592 if (speed == 1)
593 wacom->wacom_wac.bt_features &= ~0x20;
594 else
595 wacom->wacom_wac.bt_features |= 0x20;
596
597 rep_data[0] = 0x03;
598 rep_data[1] = wacom->wacom_wac.bt_features;
599
Przemo Firszt296b7372014-08-06 14:00:38 -0700600 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
601 1);
Benjamin Tissoires81af7e62014-08-06 13:55:56 -0700602 if (ret >= 0)
603 wacom->wacom_wac.bt_high_speed = speed;
604 break;
Benjamin Tissoires387142b2014-08-06 13:52:56 -0700605 }
606
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700607 return 0;
608}
609
Jason Gereckefe494bc2012-10-03 17:25:35 -0700610/*
611 * Switch the tablet into its most-capable mode. Wacom tablets are
612 * typically configured to power-up in a mode which sends mouse-like
613 * reports to the OS. To get absolute position, pressure data, etc.
614 * from the tablet, it is necessary to switch the tablet out of this
615 * mode and into one which sends the full range of tablet data.
616 */
Benjamin Tissoiresa544c612017-01-20 16:20:13 +0100617static int _wacom_query_tablet_data(struct wacom *wacom)
Jason Gereckefe494bc2012-10-03 17:25:35 -0700618{
Benjamin Tissoiresa544c612017-01-20 16:20:13 +0100619 struct hid_device *hdev = wacom->hdev;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700620 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
Benjamin Tissoiresa544c612017-01-20 16:20:13 +0100621 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700622
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -0700623 if (hdev->bus == BUS_BLUETOOTH)
624 return wacom_bt_query_tablet_data(hdev, 1, features);
625
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700626 if (features->type != HID_GENERIC) {
627 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
628 if (features->type > TABLETPC) {
629 /* MT Tablet PC touch */
630 wacom_wac->mode_report = 3;
631 wacom_wac->mode_value = 4;
632 } else if (features->type == WACOM_24HDT) {
633 wacom_wac->mode_report = 18;
634 wacom_wac->mode_value = 2;
635 } else if (features->type == WACOM_27QHDT) {
636 wacom_wac->mode_report = 131;
637 wacom_wac->mode_value = 2;
638 } else if (features->type == BAMBOO_PAD) {
639 wacom_wac->mode_report = 2;
640 wacom_wac->mode_value = 2;
641 }
642 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
643 if (features->type <= BAMBOO_PT) {
644 wacom_wac->mode_report = 2;
645 wacom_wac->mode_value = 2;
646 }
Jason Gereckefe494bc2012-10-03 17:25:35 -0700647 }
648 }
649
Jason Gerecke326ea2a2016-04-04 11:26:52 -0700650 wacom_set_device_mode(hdev, wacom_wac);
651
652 if (features->type == HID_GENERIC)
653 return wacom_hid_set_device_mode(hdev);
654
Jason Gereckefe494bc2012-10-03 17:25:35 -0700655 return 0;
656}
657
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700658static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
Ping Cheng19635182012-04-29 21:09:18 -0700659 struct wacom_features *features)
Ping Chengec67bbe2009-12-15 00:35:24 -0800660{
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700661 struct wacom *wacom = hid_get_drvdata(hdev);
662 struct usb_interface *intf = wacom->intf;
Ping Chengec67bbe2009-12-15 00:35:24 -0800663
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700664 /* default features */
Henrik Rydbergfed87e62010-09-05 12:25:11 -0700665 features->x_fuzz = 4;
666 features->y_fuzz = 4;
667 features->pressure_fuzz = 0;
Jason Gereckebef7e202016-04-22 14:30:53 -0700668 features->distance_fuzz = 1;
669 features->tilt_fuzz = 1;
Ping Chengec67bbe2009-12-15 00:35:24 -0800670
Chris Bagwelld3825d52012-03-25 23:26:11 -0700671 /*
672 * The wireless device HID is basic and layout conflicts with
673 * other tablets (monitor and touch interface can look like pen).
674 * Skip the query for this type and modify defaults based on
675 * interface number.
676 */
677 if (features->type == WIRELESS) {
Jason Gerecke3f14a632015-08-03 10:17:05 -0700678 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
Jason Gereckeccad85c2015-08-03 10:17:04 -0700679 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700680 else
Jason Gereckeaa86b182015-06-15 18:01:42 -0700681 features->device_type = WACOM_DEVICETYPE_NONE;
Jason Gerecke3f14a632015-08-03 10:17:05 -0700682 return;
Chris Bagwelld3825d52012-03-25 23:26:11 -0700683 }
684
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -0700685 wacom_parse_hid(hdev, features);
Ping Chengec67bbe2009-12-15 00:35:24 -0800686}
687
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700688struct wacom_hdev_data {
Ping Cheng4492eff2010-03-19 22:18:15 -0700689 struct list_head list;
690 struct kref kref;
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700691 struct hid_device *dev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700692 struct wacom_shared shared;
693};
694
695static LIST_HEAD(wacom_udev_list);
696static DEFINE_MUTEX(wacom_udev_list_lock);
697
Jason Gerecke41372d52016-08-08 12:06:30 -0700698static bool compare_device_paths(struct hid_device *hdev_a,
699 struct hid_device *hdev_b, char separator)
700{
701 int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
702 int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
703
704 if (n1 != n2 || n1 <= 0 || n2 <= 0)
705 return false;
706
707 return !strncmp(hdev_a->phys, hdev_b->phys, n1);
708}
709
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700710static bool wacom_are_sibling(struct hid_device *hdev,
711 struct hid_device *sibling)
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700712{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700713 struct wacom *wacom = hid_get_drvdata(hdev);
714 struct wacom_features *features = &wacom->wacom_wac.features;
Jason Gerecke41372d52016-08-08 12:06:30 -0700715 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
716 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
717 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
718 __u32 oPid = features->oPid ? features->oPid : hdev->product;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700719
Jason Gerecke41372d52016-08-08 12:06:30 -0700720 /* The defined oVid/oPid must match that of the sibling */
721 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
722 return false;
723 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
724 return false;
725
726 /*
727 * Devices with the same VID/PID must share the same physical
728 * device path, while those with different VID/PID must share
729 * the same physical parent device path.
730 */
731 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
732 if (!compare_device_paths(hdev, sibling, '/'))
733 return false;
734 } else {
735 if (!compare_device_paths(hdev, sibling, '.'))
736 return false;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700737 }
738
Jason Gerecke41372d52016-08-08 12:06:30 -0700739 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
740 if (features->type != HID_GENERIC)
741 return true;
742
743 /*
744 * Direct-input devices may not be siblings of indirect-input
745 * devices.
746 */
747 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
748 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700749 return false;
750
Jason Gerecke41372d52016-08-08 12:06:30 -0700751 /*
752 * Indirect-input devices may not be siblings of direct-input
753 * devices.
754 */
755 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
756 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700757 return false;
758
Jason Gerecke41372d52016-08-08 12:06:30 -0700759 /* Pen devices may only be siblings of touch devices */
760 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
761 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
762 return false;
763
764 /* Touch devices may only be siblings of pen devices */
765 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
766 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
767 return false;
768
769 /*
770 * No reason could be found for these two devices to NOT be
771 * siblings, so there's a good chance they ARE siblings
772 */
773 return true;
Jason Gereckeaea2bf62012-10-21 00:38:03 -0700774}
775
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700776static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700777{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700778 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700779
Jason Gerecke41372d52016-08-08 12:06:30 -0700780 /* Try to find an already-probed interface from the same device */
781 list_for_each_entry(data, &wacom_udev_list, list) {
Jason Gerecke2a5e5972017-09-18 09:27:42 -0700782 if (compare_device_paths(hdev, data->dev, '/')) {
783 kref_get(&data->kref);
Jason Gerecke41372d52016-08-08 12:06:30 -0700784 return data;
Jason Gerecke2a5e5972017-09-18 09:27:42 -0700785 }
Jason Gerecke41372d52016-08-08 12:06:30 -0700786 }
787
788 /* Fallback to finding devices that appear to be "siblings" */
Ping Cheng4492eff2010-03-19 22:18:15 -0700789 list_for_each_entry(data, &wacom_udev_list, list) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700790 if (wacom_are_sibling(hdev, data->dev)) {
Ping Cheng4492eff2010-03-19 22:18:15 -0700791 kref_get(&data->kref);
792 return data;
793 }
794 }
795
796 return NULL;
797}
798
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200799static void wacom_release_shared_data(struct kref *kref)
800{
801 struct wacom_hdev_data *data =
802 container_of(kref, struct wacom_hdev_data, kref);
803
804 mutex_lock(&wacom_udev_list_lock);
805 list_del(&data->list);
806 mutex_unlock(&wacom_udev_list_lock);
807
808 kfree(data);
809}
810
811static void wacom_remove_shared_data(void *res)
812{
813 struct wacom *wacom = res;
814 struct wacom_hdev_data *data;
815 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
816
817 if (wacom_wac->shared) {
818 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
819 shared);
820
821 if (wacom_wac->shared->touch == wacom->hdev)
822 wacom_wac->shared->touch = NULL;
823 else if (wacom_wac->shared->pen == wacom->hdev)
824 wacom_wac->shared->pen = NULL;
825
826 kref_put(&data->kref, wacom_release_shared_data);
827 wacom_wac->shared = NULL;
828 }
829}
830
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700831static int wacom_add_shared_data(struct hid_device *hdev)
Ping Cheng4492eff2010-03-19 22:18:15 -0700832{
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700833 struct wacom *wacom = hid_get_drvdata(hdev);
834 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
835 struct wacom_hdev_data *data;
Ping Cheng4492eff2010-03-19 22:18:15 -0700836 int retval = 0;
837
838 mutex_lock(&wacom_udev_list_lock);
839
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700840 data = wacom_get_hdev_data(hdev);
Ping Cheng4492eff2010-03-19 22:18:15 -0700841 if (!data) {
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700842 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
Ping Cheng4492eff2010-03-19 22:18:15 -0700843 if (!data) {
844 retval = -ENOMEM;
845 goto out;
846 }
847
848 kref_init(&data->kref);
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700849 data->dev = hdev;
Ping Cheng4492eff2010-03-19 22:18:15 -0700850 list_add_tail(&data->list, &wacom_udev_list);
851 }
852
Benjamin Tissoires4451e082014-07-24 13:01:05 -0700853 wacom_wac->shared = &data->shared;
Ping Cheng4492eff2010-03-19 22:18:15 -0700854
Benjamin Tissoires1c817c82016-07-13 18:05:59 +0200855 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
856 if (retval) {
857 mutex_unlock(&wacom_udev_list_lock);
858 wacom_remove_shared_data(wacom);
859 return retval;
860 }
861
Jason Gereckea9ce7852017-01-17 15:38:58 -0800862 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
863 wacom_wac->shared->touch = hdev;
864 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
865 wacom_wac->shared->pen = hdev;
866
Ping Cheng4492eff2010-03-19 22:18:15 -0700867out:
868 mutex_unlock(&wacom_udev_list_lock);
869 return retval;
870}
871
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700872static int wacom_led_control(struct wacom *wacom)
873{
874 unsigned char *buf;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700875 int retval;
Ping Cheng912ca212014-09-10 12:41:31 -0700876 unsigned char report_id = WAC_CMD_LED_CONTROL;
877 int buf_size = 9;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700878
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200879 if (!wacom->led.groups)
880 return -ENOTSUPP;
881
Aaron Armstrong Skomra74aebed62017-08-28 14:15:39 -0700882 if (wacom->wacom_wac.features.type == REMOTE)
883 return -ENOTSUPP;
884
Ping Cheng912ca212014-09-10 12:41:31 -0700885 if (wacom->wacom_wac.pid) { /* wireless connected */
886 report_id = WAC_CMD_WL_LED_CONTROL;
887 buf_size = 13;
888 }
Jason Gerecke4922cd22017-01-25 12:08:37 -0800889 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
890 report_id = WAC_CMD_WL_INTUOSP2;
891 buf_size = 51;
892 }
Ping Cheng912ca212014-09-10 12:41:31 -0700893 buf = kzalloc(buf_size, GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700894 if (!buf)
895 return -ENOMEM;
896
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -0800897 if (wacom->wacom_wac.features.type == HID_GENERIC) {
898 buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
899 buf[1] = wacom->led.llv;
900 buf[2] = wacom->led.groups[0].select & 0x03;
901
902 } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
903 wacom->wacom_wac.features.type <= INTUOSPL)) {
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700904 /*
905 * Touch Ring and crop mark LED luminance may take on
906 * one of four values:
907 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
908 */
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200909 int ring_led = wacom->led.groups[0].select & 0x03;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700910 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
911 int crop_lum = 0;
Ping Cheng912ca212014-09-10 12:41:31 -0700912 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
Ping Cheng09e7d942011-10-04 23:51:14 -0700913
Ping Cheng912ca212014-09-10 12:41:31 -0700914 buf[0] = report_id;
915 if (wacom->wacom_wac.pid) {
916 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
917 buf, buf_size, WAC_CMD_RETRIES);
918 buf[0] = report_id;
919 buf[4] = led_bits;
920 } else
921 buf[1] = led_bits;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700922 }
Jason Gerecke4922cd22017-01-25 12:08:37 -0800923 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
924 buf[0] = report_id;
925 buf[4] = 100; // Power Connection LED (ORANGE)
926 buf[5] = 100; // BT Connection LED (BLUE)
927 buf[6] = 100; // Paper Mode (RED?)
928 buf[7] = 100; // Paper Mode (GREEN?)
929 buf[8] = 100; // Paper Mode (BLUE?)
930 buf[9] = wacom->led.llv;
931 buf[10] = wacom->led.groups[0].select & 0x03;
932 }
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700933 else {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200934 int led = wacom->led.groups[0].select | 0x4;
Ping Cheng09e7d942011-10-04 23:51:14 -0700935
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700936 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
937 wacom->wacom_wac.features.type == WACOM_24HD)
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +0200938 led |= (wacom->led.groups[1].select << 4) | 0x40;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700939
Ping Cheng912ca212014-09-10 12:41:31 -0700940 buf[0] = report_id;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -0700941 buf[1] = led;
942 buf[2] = wacom->led.llv;
943 buf[3] = wacom->led.hlv;
944 buf[4] = wacom->led.img_lum;
945 }
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700946
Ping Cheng912ca212014-09-10 12:41:31 -0700947 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
Przemo Firszt296b7372014-08-06 14:00:38 -0700948 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700949 kfree(buf);
950
951 return retval;
952}
953
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700954static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
955 const unsigned len, const void *img)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700956{
957 unsigned char *buf;
958 int i, retval;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700959 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700960
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700961 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700962 if (!buf)
963 return -ENOMEM;
964
965 /* Send 'start' command */
966 buf[0] = WAC_CMD_ICON_START;
967 buf[1] = 1;
Przemo Firszt296b7372014-08-06 14:00:38 -0700968 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
969 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700970 if (retval < 0)
971 goto out;
972
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700973 buf[0] = xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700974 buf[1] = button_id & 0x07;
975 for (i = 0; i < 4; i++) {
976 buf[2] = i;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -0700977 memcpy(buf + 3, img + i * chunk_len, chunk_len);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700978
Benjamin Tissoires27b20a92014-07-24 12:56:22 -0700979 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
Przemo Firszt296b7372014-08-06 14:00:38 -0700980 buf, chunk_len + 3, WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700981 if (retval < 0)
982 break;
983 }
984
985 /* Send 'stop' */
986 buf[0] = WAC_CMD_ICON_START;
987 buf[1] = 0;
Przemo Firszt296b7372014-08-06 14:00:38 -0700988 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
989 WAC_CMD_RETRIES);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700990
991out:
992 kfree(buf);
993 return retval;
994}
995
Ping Cheng09e7d942011-10-04 23:51:14 -0700996static ssize_t wacom_led_select_store(struct device *dev, int set_id,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -0700997 const char *buf, size_t count)
998{
Geliang Tangee79a8f2015-12-27 17:25:21 +0800999 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001000 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001001 unsigned int id;
1002 int err;
1003
1004 err = kstrtouint(buf, 10, &id);
1005 if (err)
1006 return err;
1007
1008 mutex_lock(&wacom->lock);
1009
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001010 wacom->led.groups[set_id].select = id & 0x3;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001011 err = wacom_led_control(wacom);
1012
1013 mutex_unlock(&wacom->lock);
1014
1015 return err < 0 ? err : count;
1016}
1017
Ping Cheng09e7d942011-10-04 23:51:14 -07001018#define DEVICE_LED_SELECT_ATTR(SET_ID) \
1019static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
1020 struct device_attribute *attr, const char *buf, size_t count) \
1021{ \
1022 return wacom_led_select_store(dev, SET_ID, buf, count); \
1023} \
Ping Cheng04c59ab2011-10-04 23:51:49 -07001024static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
1025 struct device_attribute *attr, char *buf) \
1026{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +08001027 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001028 struct wacom *wacom = hid_get_drvdata(hdev); \
Ping Cheng37449ad2014-09-10 12:40:30 -07001029 return scnprintf(buf, PAGE_SIZE, "%d\n", \
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001030 wacom->led.groups[SET_ID].select); \
Ping Cheng04c59ab2011-10-04 23:51:49 -07001031} \
Ping Chenge0984bc2014-09-10 12:40:05 -07001032static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
Ping Cheng04c59ab2011-10-04 23:51:49 -07001033 wacom_led##SET_ID##_select_show, \
Ping Cheng09e7d942011-10-04 23:51:14 -07001034 wacom_led##SET_ID##_select_store)
1035
1036DEVICE_LED_SELECT_ATTR(0);
1037DEVICE_LED_SELECT_ATTR(1);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001038
1039static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
1040 const char *buf, size_t count)
1041{
1042 unsigned int value;
1043 int err;
1044
1045 err = kstrtouint(buf, 10, &value);
1046 if (err)
1047 return err;
1048
1049 mutex_lock(&wacom->lock);
1050
1051 *dest = value & 0x7f;
1052 err = wacom_led_control(wacom);
1053
1054 mutex_unlock(&wacom->lock);
1055
1056 return err < 0 ? err : count;
1057}
1058
1059#define DEVICE_LUMINANCE_ATTR(name, field) \
1060static ssize_t wacom_##name##_luminance_store(struct device *dev, \
1061 struct device_attribute *attr, const char *buf, size_t count) \
1062{ \
Geliang Tangee79a8f2015-12-27 17:25:21 +08001063 struct hid_device *hdev = to_hid_device(dev);\
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001064 struct wacom *wacom = hid_get_drvdata(hdev); \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001065 \
1066 return wacom_luminance_store(wacom, &wacom->led.field, \
1067 buf, count); \
1068} \
Ping Cheng37449ad2014-09-10 12:40:30 -07001069static ssize_t wacom_##name##_luminance_show(struct device *dev, \
1070 struct device_attribute *attr, char *buf) \
1071{ \
1072 struct wacom *wacom = dev_get_drvdata(dev); \
1073 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
1074} \
Ping Chenge0984bc2014-09-10 12:40:05 -07001075static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
Ping Cheng37449ad2014-09-10 12:40:30 -07001076 wacom_##name##_luminance_show, \
1077 wacom_##name##_luminance_store)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001078
1079DEVICE_LUMINANCE_ATTR(status0, llv);
1080DEVICE_LUMINANCE_ATTR(status1, hlv);
1081DEVICE_LUMINANCE_ATTR(buttons, img_lum);
1082
1083static ssize_t wacom_button_image_store(struct device *dev, int button_id,
1084 const char *buf, size_t count)
1085{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001086 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07001087 struct wacom *wacom = hid_get_drvdata(hdev);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001088 int err;
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001089 unsigned len;
1090 u8 xfer_id;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001091
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001092 if (hdev->bus == BUS_BLUETOOTH) {
1093 len = 256;
1094 xfer_id = WAC_CMD_ICON_BT_XFER;
1095 } else {
1096 len = 1024;
1097 xfer_id = WAC_CMD_ICON_XFER;
1098 }
1099
1100 if (count != len)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001101 return -EINVAL;
1102
1103 mutex_lock(&wacom->lock);
1104
Benjamin Tissoires849e2f02014-08-06 13:58:25 -07001105 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001106
1107 mutex_unlock(&wacom->lock);
1108
1109 return err < 0 ? err : count;
1110}
1111
1112#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1113static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1114 struct device_attribute *attr, const char *buf, size_t count) \
1115{ \
1116 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1117} \
Ping Chenge0984bc2014-09-10 12:40:05 -07001118static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001119 NULL, wacom_btnimg##BUTTON_ID##_store)
1120
1121DEVICE_BTNIMG_ATTR(0);
1122DEVICE_BTNIMG_ATTR(1);
1123DEVICE_BTNIMG_ATTR(2);
1124DEVICE_BTNIMG_ATTR(3);
1125DEVICE_BTNIMG_ATTR(4);
1126DEVICE_BTNIMG_ATTR(5);
1127DEVICE_BTNIMG_ATTR(6);
1128DEVICE_BTNIMG_ATTR(7);
1129
Ping Cheng09e7d942011-10-04 23:51:14 -07001130static struct attribute *cintiq_led_attrs[] = {
1131 &dev_attr_status_led0_select.attr,
1132 &dev_attr_status_led1_select.attr,
1133 NULL
1134};
1135
1136static struct attribute_group cintiq_led_attr_group = {
1137 .name = "wacom_led",
1138 .attrs = cintiq_led_attrs,
1139};
1140
1141static struct attribute *intuos4_led_attrs[] = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001142 &dev_attr_status0_luminance.attr,
1143 &dev_attr_status1_luminance.attr,
Ping Cheng09e7d942011-10-04 23:51:14 -07001144 &dev_attr_status_led0_select.attr,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001145 &dev_attr_buttons_luminance.attr,
1146 &dev_attr_button0_rawimg.attr,
1147 &dev_attr_button1_rawimg.attr,
1148 &dev_attr_button2_rawimg.attr,
1149 &dev_attr_button3_rawimg.attr,
1150 &dev_attr_button4_rawimg.attr,
1151 &dev_attr_button5_rawimg.attr,
1152 &dev_attr_button6_rawimg.attr,
1153 &dev_attr_button7_rawimg.attr,
1154 NULL
1155};
1156
Ping Cheng09e7d942011-10-04 23:51:14 -07001157static struct attribute_group intuos4_led_attr_group = {
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001158 .name = "wacom_led",
Ping Cheng09e7d942011-10-04 23:51:14 -07001159 .attrs = intuos4_led_attrs,
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001160};
1161
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001162static struct attribute *intuos5_led_attrs[] = {
1163 &dev_attr_status0_luminance.attr,
1164 &dev_attr_status_led0_select.attr,
1165 NULL
1166};
1167
1168static struct attribute_group intuos5_led_attr_group = {
1169 .name = "wacom_led",
1170 .attrs = intuos5_led_attrs,
1171};
1172
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001173static struct attribute *generic_led_attrs[] = {
1174 &dev_attr_status0_luminance.attr,
1175 &dev_attr_status_led0_select.attr,
1176 NULL
1177};
1178
1179static struct attribute_group generic_led_attr_group = {
1180 .name = "wacom_led",
1181 .attrs = generic_led_attrs,
1182};
1183
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001184struct wacom_sysfs_group_devres {
1185 struct attribute_group *group;
1186 struct kobject *root;
1187};
1188
1189static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1190{
1191 struct wacom_sysfs_group_devres *devres = res;
1192 struct kobject *kobj = devres->root;
1193
1194 dev_dbg(dev, "%s: dropping reference to %s\n",
1195 __func__, devres->group->name);
1196 sysfs_remove_group(kobj, devres->group);
1197}
1198
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001199static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1200 struct kobject *root,
1201 struct attribute_group *group)
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001202{
1203 struct wacom_sysfs_group_devres *devres;
1204 int error;
1205
1206 devres = devres_alloc(wacom_devm_sysfs_group_release,
1207 sizeof(struct wacom_sysfs_group_devres),
1208 GFP_KERNEL);
1209 if (!devres)
1210 return -ENOMEM;
1211
1212 devres->group = group;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001213 devres->root = root;
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001214
1215 error = sysfs_create_group(devres->root, group);
1216 if (error)
1217 return error;
1218
1219 devres_add(&wacom->hdev->dev, devres);
1220
1221 return 0;
1222}
1223
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001224static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1225 struct attribute_group *group)
1226{
1227 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1228 group);
1229}
1230
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001231enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001232{
1233 struct wacom *wacom = led->wacom;
1234
1235 if (wacom->led.max_hlv)
1236 return led->hlv * LED_FULL / wacom->led.max_hlv;
1237
1238 if (wacom->led.max_llv)
1239 return led->llv * LED_FULL / wacom->led.max_llv;
1240
1241 /* device doesn't support brightness tuning */
1242 return LED_FULL;
1243}
1244
1245static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1246{
1247 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1248 struct wacom *wacom = led->wacom;
1249
1250 if (wacom->led.groups[led->group].select != led->id)
1251 return LED_OFF;
1252
1253 return wacom_leds_brightness_get(led);
1254}
1255
1256static int wacom_led_brightness_set(struct led_classdev *cdev,
1257 enum led_brightness brightness)
1258{
1259 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1260 struct wacom *wacom = led->wacom;
1261 int error;
1262
1263 mutex_lock(&wacom->lock);
1264
1265 if (!wacom->led.groups || (brightness == LED_OFF &&
1266 wacom->led.groups[led->group].select != led->id)) {
1267 error = 0;
1268 goto out;
1269 }
1270
1271 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1272 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1273
1274 wacom->led.groups[led->group].select = led->id;
1275
1276 error = wacom_led_control(wacom);
1277
1278out:
1279 mutex_unlock(&wacom->lock);
1280
1281 return error;
1282}
1283
1284static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1285 enum led_brightness brightness)
1286{
1287}
1288
1289static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1290 struct wacom_led *led, unsigned int group,
1291 unsigned int id, bool read_only)
1292{
1293 int error;
1294 char *name;
1295
1296 name = devm_kasprintf(dev, GFP_KERNEL,
1297 "%s::wacom-%d.%d",
1298 dev_name(dev),
1299 group,
1300 id);
1301 if (!name)
1302 return -ENOMEM;
1303
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001304 if (!read_only) {
1305 led->trigger.name = name;
1306 error = devm_led_trigger_register(dev, &led->trigger);
1307 if (error) {
1308 hid_err(wacom->hdev,
1309 "failed to register LED trigger %s: %d\n",
1310 led->cdev.name, error);
1311 return error;
1312 }
1313 }
1314
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001315 led->group = group;
1316 led->id = id;
1317 led->wacom = wacom;
1318 led->llv = wacom->led.llv;
1319 led->hlv = wacom->led.hlv;
1320 led->cdev.name = name;
1321 led->cdev.max_brightness = LED_FULL;
1322 led->cdev.flags = LED_HW_PLUGGABLE;
1323 led->cdev.brightness_get = __wacom_led_brightness_get;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001324 if (!read_only) {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001325 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001326 led->cdev.default_trigger = led->cdev.name;
1327 } else {
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001328 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001329 }
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001330
1331 error = devm_led_classdev_register(dev, &led->cdev);
1332 if (error) {
1333 hid_err(wacom->hdev,
1334 "failed to register LED %s: %d\n",
1335 led->cdev.name, error);
1336 led->cdev.name = NULL;
1337 return error;
1338 }
1339
1340 return 0;
1341}
1342
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001343static void wacom_led_groups_release_one(void *data)
1344{
1345 struct wacom_group_leds *group = data;
1346
1347 devres_release_group(group->dev, group);
1348}
1349
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001350static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1351 struct wacom *wacom,
1352 int group_id, int count,
1353 bool read_only)
1354{
1355 struct wacom_led *leds;
1356 int i, error;
1357
1358 if (group_id >= wacom->led.count || count <= 0)
1359 return -EINVAL;
1360
1361 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1362 return -ENOMEM;
1363
1364 leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
1365 if (!leds) {
1366 error = -ENOMEM;
1367 goto err;
1368 }
1369
1370 wacom->led.groups[group_id].leds = leds;
1371 wacom->led.groups[group_id].count = count;
1372
1373 for (i = 0; i < count; i++) {
1374 error = wacom_led_register_one(dev, wacom, &leds[i],
1375 group_id, i, read_only);
1376 if (error)
1377 goto err;
1378 }
1379
Benjamin Tissoires589e5062016-07-13 18:06:11 +02001380 wacom->led.groups[group_id].dev = dev;
1381
1382 devres_close_group(dev, &wacom->led.groups[group_id]);
1383
1384 /*
1385 * There is a bug (?) in devm_led_classdev_register() in which its
1386 * increments the refcount of the parent. If the parent is an input
1387 * device, that means the ref count never reaches 0 when
1388 * devm_input_device_release() gets called.
1389 * This means that the LEDs are still there after disconnect.
1390 * Manually force the release of the group so that the leds are released
1391 * once we are done using them.
1392 */
1393 error = devm_add_action_or_reset(&wacom->hdev->dev,
1394 wacom_led_groups_release_one,
1395 &wacom->led.groups[group_id]);
1396 if (error)
1397 return error;
1398
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001399 return 0;
1400
1401err:
1402 devres_release_group(dev, &wacom->led.groups[group_id]);
1403 return error;
1404}
1405
Benjamin Tissoires34736aa2016-07-13 18:06:12 +02001406struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1407 unsigned int id)
1408{
1409 struct wacom_group_leds *group;
1410
1411 if (group_id >= wacom->led.count)
1412 return NULL;
1413
1414 group = &wacom->led.groups[group_id];
1415
1416 if (!group->leds)
1417 return NULL;
1418
1419 id %= group->count;
1420
1421 return &group->leds[id];
1422}
1423
1424/**
1425 * wacom_led_next: gives the next available led with a wacom trigger.
1426 *
1427 * returns the next available struct wacom_led which has its default trigger
1428 * or the current one if none is available.
1429 */
1430struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1431{
1432 struct wacom_led *next_led;
1433 int group, next;
1434
1435 if (!wacom || !cur)
1436 return NULL;
1437
1438 group = cur->group;
1439 next = cur->id;
1440
1441 do {
1442 next_led = wacom_led_find(wacom, group, ++next);
1443 if (!next_led || next_led == cur)
1444 return next_led;
1445 } while (next_led->cdev.trigger != &next_led->trigger);
1446
1447 return next_led;
1448}
1449
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001450static void wacom_led_groups_release(void *data)
1451{
1452 struct wacom *wacom = data;
1453
1454 wacom->led.groups = NULL;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001455 wacom->led.count = 0;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001456}
1457
1458static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1459{
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001460 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001461 struct wacom_group_leds *groups;
1462 int error;
1463
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001464 groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001465 GFP_KERNEL);
1466 if (!groups)
1467 return -ENOMEM;
1468
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001469 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001470 if (error)
1471 return error;
1472
1473 wacom->led.groups = groups;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001474 wacom->led.count = count;
1475
1476 return 0;
1477}
1478
1479static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1480 int led_per_group, bool read_only)
1481{
1482 struct device *dev;
1483 int i, error;
1484
1485 if (!wacom->wacom_wac.pad_input)
1486 return -EINVAL;
1487
1488 dev = &wacom->wacom_wac.pad_input->dev;
1489
1490 error = wacom_led_groups_allocate(wacom, group_count);
1491 if (error)
1492 return error;
1493
1494 for (i = 0; i < group_count; i++) {
1495 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1496 led_per_group,
1497 read_only);
1498 if (error)
1499 return error;
1500 }
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001501
1502 return 0;
1503}
1504
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001505int wacom_initialize_leds(struct wacom *wacom)
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001506{
1507 int error;
1508
Jason Gerecke862cf552015-06-15 18:01:43 -07001509 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1510 return 0;
1511
Ping Cheng09e7d942011-10-04 23:51:14 -07001512 /* Initialize default values */
1513 switch (wacom->wacom_wac.features.type) {
Aaron Armstrong Skomra10c55ca2017-01-25 12:08:42 -08001514 case HID_GENERIC:
1515 if (!wacom->generic_has_leds)
1516 return 0;
1517 wacom->led.llv = 100;
1518 wacom->led.max_llv = 100;
1519
1520 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1521 if (error) {
1522 hid_err(wacom->hdev,
1523 "cannot create leds err: %d\n", error);
1524 return error;
1525 }
1526
1527 error = wacom_devm_sysfs_create_group(wacom,
1528 &generic_led_attr_group);
1529 break;
1530
Jason Gereckea19fc982012-06-12 00:27:53 -07001531 case INTUOS4S:
Ping Cheng09e7d942011-10-04 23:51:14 -07001532 case INTUOS4:
Benjamin Tissoires81af7e62014-08-06 13:55:56 -07001533 case INTUOS4WL:
Ping Cheng09e7d942011-10-04 23:51:14 -07001534 case INTUOS4L:
Ping Chengf4fa9a62011-10-04 23:49:42 -07001535 wacom->led.llv = 10;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001536 wacom->led.hlv = 20;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001537 wacom->led.max_llv = 127;
1538 wacom->led.max_hlv = 127;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001539 wacom->led.img_lum = 10;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001540
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001541 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001542 if (error) {
1543 hid_err(wacom->hdev,
1544 "cannot create leds err: %d\n", error);
1545 return error;
1546 }
1547
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001548 error = wacom_devm_sysfs_create_group(wacom,
1549 &intuos4_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001550 break;
1551
Jason Gerecke246835f2011-12-12 00:12:04 -08001552 case WACOM_24HD:
Ping Cheng09e7d942011-10-04 23:51:14 -07001553 case WACOM_21UX2:
Ping Cheng09e7d942011-10-04 23:51:14 -07001554 wacom->led.llv = 0;
1555 wacom->led.hlv = 0;
1556 wacom->led.img_lum = 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001557
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001558 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001559 if (error) {
1560 hid_err(wacom->hdev,
1561 "cannot create leds err: %d\n", error);
1562 return error;
1563 }
1564
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001565 error = wacom_devm_sysfs_create_group(wacom,
1566 &cintiq_led_attr_group);
Ping Cheng09e7d942011-10-04 23:51:14 -07001567 break;
1568
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001569 case INTUOS5S:
1570 case INTUOS5:
1571 case INTUOS5L:
Ping Cheng9a35c412013-09-20 09:51:56 -07001572 case INTUOSPS:
1573 case INTUOSPM:
1574 case INTUOSPL:
Jason Gerecke862cf552015-06-15 18:01:43 -07001575 wacom->led.llv = 32;
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001576 wacom->led.max_llv = 96;
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001577
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001578 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001579 if (error) {
1580 hid_err(wacom->hdev,
1581 "cannot create leds err: %d\n", error);
1582 return error;
1583 }
1584
Benjamin Tissoires2df68a82016-07-13 18:05:56 +02001585 error = wacom_devm_sysfs_create_group(wacom,
1586 &intuos5_led_attr_group);
Jason Gerecke9b5b95d2012-04-03 15:50:37 -07001587 break;
1588
Jason Gerecke4922cd22017-01-25 12:08:37 -08001589 case INTUOSP2_BT:
1590 wacom->led.llv = 50;
1591 wacom->led.max_llv = 100;
1592 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1593 if (error) {
1594 hid_err(wacom->hdev,
1595 "cannot create leds err: %d\n", error);
1596 return error;
1597 }
1598 return 0;
1599
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001600 case REMOTE:
Benjamin Tissoires97f55412016-07-13 18:06:10 +02001601 wacom->led.llv = 255;
1602 wacom->led.max_llv = 255;
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001603 error = wacom_led_groups_allocate(wacom, 5);
1604 if (error) {
1605 hid_err(wacom->hdev,
1606 "cannot create leds err: %d\n", error);
1607 return error;
1608 }
1609 return 0;
1610
Ping Cheng09e7d942011-10-04 23:51:14 -07001611 default:
1612 return 0;
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001613 }
1614
Ping Cheng09e7d942011-10-04 23:51:14 -07001615 if (error) {
Benjamin Tissoiresc31a4082014-07-24 12:59:45 -07001616 hid_err(wacom->hdev,
Ping Cheng09e7d942011-10-04 23:51:14 -07001617 "cannot create sysfs group err: %d\n", error);
1618 return error;
1619 }
Ping Cheng09e7d942011-10-04 23:51:14 -07001620
Eduard Hasenleithner5d7e7d42011-09-07 14:08:54 -07001621 return 0;
1622}
1623
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01001624static void wacom_init_work(struct work_struct *work)
1625{
1626 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1627
1628 _wacom_query_tablet_data(wacom);
1629 wacom_led_control(wacom);
1630}
1631
1632static void wacom_query_tablet_data(struct wacom *wacom)
1633{
1634 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1635}
1636
Chris Bagwella1d552c2012-03-25 23:26:30 -07001637static enum power_supply_property wacom_battery_props[] = {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001638 POWER_SUPPLY_PROP_MODEL_NAME,
Jason Gerecke71fa6412015-03-11 10:25:41 -07001639 POWER_SUPPLY_PROP_PRESENT,
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001640 POWER_SUPPLY_PROP_STATUS,
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001641 POWER_SUPPLY_PROP_SCOPE,
Chris Bagwella1d552c2012-03-25 23:26:30 -07001642 POWER_SUPPLY_PROP_CAPACITY
1643};
1644
1645static int wacom_battery_get_property(struct power_supply *psy,
1646 enum power_supply_property psp,
1647 union power_supply_propval *val)
1648{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001649 struct wacom_battery *battery = power_supply_get_drvdata(psy);
Chris Bagwella1d552c2012-03-25 23:26:30 -07001650 int ret = 0;
1651
1652 switch (psp) {
Benjamin Tissoires99569532016-07-13 18:06:17 +02001653 case POWER_SUPPLY_PROP_MODEL_NAME:
1654 val->strval = battery->wacom->wacom_wac.name;
1655 break;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001656 case POWER_SUPPLY_PROP_PRESENT:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001657 val->intval = battery->bat_connected;
Jason Gerecke71fa6412015-03-11 10:25:41 -07001658 break;
Bastien Nocera6e2a6e82013-10-15 23:33:00 -07001659 case POWER_SUPPLY_PROP_SCOPE:
1660 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1661 break;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001662 case POWER_SUPPLY_PROP_CAPACITY:
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001663 val->intval = battery->battery_capacity;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001664 break;
1665 case POWER_SUPPLY_PROP_STATUS:
Jason Gerecke16e45982017-04-28 09:25:33 -07001666 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1667 val->intval = battery->bat_status;
1668 else if (battery->bat_charging)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001669 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001670 else if (battery->battery_capacity == 100 &&
1671 battery->ps_connected)
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001672 val->intval = POWER_SUPPLY_STATUS_FULL;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001673 else if (battery->ps_connected)
Jason Gereckeb0882cb2015-03-06 11:47:43 -08001674 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
Benjamin Tissoiresac8d1012014-07-25 17:29:48 -07001675 else
1676 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001677 break;
1678 default:
1679 ret = -EINVAL;
1680 break;
1681 }
1682
1683 return ret;
1684}
1685
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001686static int __wacom_initialize_battery(struct wacom *wacom,
1687 struct wacom_battery *battery)
Chris Bagwella1d552c2012-03-25 23:26:30 -07001688{
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001689 static atomic_t battery_no = ATOMIC_INIT(0);
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001690 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001691 struct power_supply_config psy_cfg = { .drv_data = battery, };
Benjamin Tissoires136ae5e2016-07-13 18:06:16 +02001692 struct power_supply *ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001693 struct power_supply_desc *bat_desc = &battery->bat_desc;
Benjamin Tissoiresd70420b92014-07-25 17:31:51 -07001694 unsigned long n;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001695 int error;
1696
1697 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1698 return -ENOMEM;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001699
Benjamin Tissoires99569532016-07-13 18:06:17 +02001700 battery->wacom = wacom;
1701
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001702 n = atomic_inc_return(&battery_no) - 1;
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001703
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001704 bat_desc->properties = wacom_battery_props;
1705 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1706 bat_desc->get_property = wacom_battery_get_property;
1707 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1708 bat_desc->name = battery->bat_name;
Benjamin Tissoires96983292016-07-13 18:06:15 +02001709 bat_desc->type = POWER_SUPPLY_TYPE_USB;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001710 bat_desc->use_for_apm = 0;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001711
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001712 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1713 if (IS_ERR(ps_bat)) {
1714 error = PTR_ERR(ps_bat);
1715 goto err;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001716 }
1717
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001718 power_supply_powers(ps_bat, &wacom->hdev->dev);
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001719
1720 battery->battery = ps_bat;
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001721
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001722 devres_close_group(dev, bat_desc);
Benjamin Tissoires7dbd2292014-07-25 17:32:41 -07001723 return 0;
Benjamin Tissoiresb189da92016-07-13 18:05:53 +02001724
1725err:
1726 devres_release_group(dev, bat_desc);
1727 return error;
Chris Bagwella1d552c2012-03-25 23:26:30 -07001728}
1729
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001730static int wacom_initialize_battery(struct wacom *wacom)
1731{
1732 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1733 return __wacom_initialize_battery(wacom, &wacom->battery);
1734
1735 return 0;
1736}
1737
Chris Bagwella1d552c2012-03-25 23:26:30 -07001738static void wacom_destroy_battery(struct wacom *wacom)
1739{
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02001740 if (wacom->battery.battery) {
1741 devres_release_group(&wacom->hdev->dev,
1742 &wacom->battery.bat_desc);
1743 wacom->battery.battery = NULL;
Chris Bagwellb7af2bb2012-06-12 00:25:23 -07001744 }
Chris Bagwella1d552c2012-03-25 23:26:30 -07001745}
1746
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001747static ssize_t wacom_show_speed(struct device *dev,
1748 struct device_attribute
1749 *attr, char *buf)
1750{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001751 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001752 struct wacom *wacom = hid_get_drvdata(hdev);
1753
1754 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1755}
1756
1757static ssize_t wacom_store_speed(struct device *dev,
1758 struct device_attribute *attr,
1759 const char *buf, size_t count)
1760{
Geliang Tangee79a8f2015-12-27 17:25:21 +08001761 struct hid_device *hdev = to_hid_device(dev);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001762 struct wacom *wacom = hid_get_drvdata(hdev);
1763 u8 new_speed;
1764
1765 if (kstrtou8(buf, 0, &new_speed))
1766 return -EINVAL;
1767
1768 if (new_speed != 0 && new_speed != 1)
1769 return -EINVAL;
1770
1771 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1772
1773 return count;
1774}
1775
Ping Chenge0984bc2014-09-10 12:40:05 -07001776static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07001777 wacom_show_speed, wacom_store_speed);
1778
Aaron Skomra72b236d2015-08-20 16:05:17 -07001779
1780static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1781 struct kobj_attribute *kattr,
1782 char *buf, int index)
1783{
Geliang Tang2cf83832015-12-27 17:25:24 +08001784 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001785 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001786 struct wacom *wacom = hid_get_drvdata(hdev);
1787 u8 mode;
1788
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001789 mode = wacom->led.groups[index].select;
Christos Gkekasbc35f732017-07-08 20:25:48 +01001790 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001791}
1792
1793#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1794static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1795 struct kobj_attribute *kattr, char *buf) \
1796{ \
1797 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1798} \
1799static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1800 .attr = {.name = "remote_mode", \
1801 .mode = DEV_ATTR_RO_PERM}, \
1802 .show = wacom_show_remote##SET_ID##_mode, \
1803}; \
1804static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1805 &remote##SET_ID##_mode_attr.attr, \
1806 NULL \
1807}; \
1808static struct attribute_group remote##SET_ID##_serial_group = { \
1809 .name = NULL, \
1810 .attrs = remote##SET_ID##_serial_attrs, \
1811}
1812
1813DEVICE_EKR_ATTR_GROUP(0);
1814DEVICE_EKR_ATTR_GROUP(1);
1815DEVICE_EKR_ATTR_GROUP(2);
1816DEVICE_EKR_ATTR_GROUP(3);
1817DEVICE_EKR_ATTR_GROUP(4);
1818
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02001819static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1820 int index)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001821{
1822 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001823 struct wacom_remote *remote = wacom->remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001824
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001825 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001826 GFP_KERNEL,
1827 "%d", serial);
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001828 if (!remote->remotes[index].group.name)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001829 return -ENOMEM;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001830
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02001831 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001832 &remote->remotes[index].group);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001833 if (error) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001834 remote->remotes[index].group.name = NULL;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001835 hid_err(wacom->hdev,
1836 "cannot create sysfs group err: %d\n", error);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001837 return error;
1838 }
1839
1840 return 0;
1841}
1842
Aaron Skomra72b236d2015-08-20 16:05:17 -07001843static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1844{
1845 const size_t buf_size = 2;
1846 unsigned char *buf;
1847 int retval;
1848
1849 buf = kzalloc(buf_size, GFP_KERNEL);
1850 if (!buf)
1851 return -ENOMEM;
1852
1853 buf[0] = WAC_CMD_DELETE_PAIRING;
1854 buf[1] = selector;
1855
1856 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1857 buf_size, WAC_CMD_RETRIES);
1858 kfree(buf);
1859
1860 return retval;
1861}
1862
1863static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1864 struct kobj_attribute *attr,
1865 const char *buf, size_t count)
1866{
1867 unsigned char selector = 0;
Geliang Tang2cf83832015-12-27 17:25:24 +08001868 struct device *dev = kobj_to_dev(kobj->parent);
Geliang Tangee79a8f2015-12-27 17:25:21 +08001869 struct hid_device *hdev = to_hid_device(dev);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001870 struct wacom *wacom = hid_get_drvdata(hdev);
1871 int err;
1872
1873 if (!strncmp(buf, "*\n", 2)) {
1874 selector = WAC_CMD_UNPAIR_ALL;
1875 } else {
1876 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1877 buf);
1878 return -1;
1879 }
1880
1881 mutex_lock(&wacom->lock);
1882
1883 err = wacom_cmd_unpair_remote(wacom, selector);
1884 mutex_unlock(&wacom->lock);
1885
1886 return err < 0 ? err : count;
1887}
1888
1889static struct kobj_attribute unpair_remote_attr = {
1890 .attr = {.name = "unpair_remote", .mode = 0200},
1891 .store = wacom_store_unpair_remote,
1892};
1893
1894static const struct attribute *remote_unpair_attrs[] = {
1895 &unpair_remote_attr.attr,
1896 NULL
1897};
1898
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001899static void wacom_remotes_destroy(void *data)
1900{
1901 struct wacom *wacom = data;
1902 struct wacom_remote *remote = wacom->remote;
1903
1904 if (!remote)
1905 return;
1906
1907 kobject_put(remote->remote_dir);
1908 kfifo_free(&remote->remote_fifo);
1909 wacom->remote = NULL;
1910}
1911
1912static int wacom_initialize_remotes(struct wacom *wacom)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001913{
1914 int error = 0;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001915 struct wacom_remote *remote;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001916 int i;
1917
1918 if (wacom->wacom_wac.features.type != REMOTE)
1919 return 0;
1920
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001921 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1922 GFP_KERNEL);
1923 if (!remote)
Aaron Skomra72b236d2015-08-20 16:05:17 -07001924 return -ENOMEM;
1925
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001926 wacom->remote = remote;
1927
1928 spin_lock_init(&remote->remote_lock);
1929
1930 error = kfifo_alloc(&remote->remote_fifo,
1931 5 * sizeof(struct wacom_remote_data),
1932 GFP_KERNEL);
1933 if (error) {
1934 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1935 return -ENOMEM;
1936 }
1937
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001938 remote->remotes[0].group = remote0_serial_group;
1939 remote->remotes[1].group = remote1_serial_group;
1940 remote->remotes[2].group = remote2_serial_group;
1941 remote->remotes[3].group = remote3_serial_group;
1942 remote->remotes[4].group = remote4_serial_group;
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001943
1944 remote->remote_dir = kobject_create_and_add("wacom_remote",
1945 &wacom->hdev->dev.kobj);
1946 if (!remote->remote_dir)
1947 return -ENOMEM;
1948
1949 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
Aaron Skomra72b236d2015-08-20 16:05:17 -07001950
1951 if (error) {
1952 hid_err(wacom->hdev,
1953 "cannot create sysfs group err: %d\n", error);
1954 return error;
1955 }
1956
1957 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoiresa50aac72016-07-13 18:06:00 +02001958 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02001959 remote->remotes[i].serial = 0;
Aaron Skomra72b236d2015-08-20 16:05:17 -07001960 }
1961
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02001962 error = devm_add_action_or_reset(&wacom->hdev->dev,
1963 wacom_remotes_destroy, wacom);
1964 if (error)
1965 return error;
1966
Aaron Skomra72b236d2015-08-20 16:05:17 -07001967 return 0;
1968}
1969
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001970static struct input_dev *wacom_allocate_input(struct wacom *wacom)
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001971{
1972 struct input_dev *input_dev;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001973 struct hid_device *hdev = wacom->hdev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001974 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001975
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02001976 input_dev = devm_input_allocate_device(&hdev->dev);
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001977 if (!input_dev)
1978 return NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001979
Jason Gerecke2bdd1632015-07-13 18:03:45 -07001980 input_dev->name = wacom_wac->features.name;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001981 input_dev->phys = hdev->phys;
1982 input_dev->dev.parent = &hdev->dev;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001983 input_dev->open = wacom_open;
1984 input_dev->close = wacom_close;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001985 input_dev->uniq = hdev->uniq;
1986 input_dev->id.bustype = hdev->bus;
1987 input_dev->id.vendor = hdev->vendor;
Benjamin Tissoires12969e32014-09-11 13:14:04 -04001988 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
Benjamin Tissoiresb6c79f22014-07-24 13:00:03 -07001989 input_dev->id.version = hdev->version;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07001990 input_set_drvdata(input_dev, wacom);
1991
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07001992 return input_dev;
1993}
1994
Jason Gerecked9f2d202015-07-13 18:03:44 -07001995static int wacom_allocate_inputs(struct wacom *wacom)
1996{
1997 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1998
1999 wacom_wac->pen_input = wacom_allocate_input(wacom);
2000 wacom_wac->touch_input = wacom_allocate_input(wacom);
2001 wacom_wac->pad_input = wacom_allocate_input(wacom);
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002002 if (!wacom_wac->pen_input ||
2003 !wacom_wac->touch_input ||
2004 !wacom_wac->pad_input)
Jason Gerecked9f2d202015-07-13 18:03:44 -07002005 return -ENOMEM;
Jason Gerecked9f2d202015-07-13 18:03:44 -07002006
Jason Gerecke2bdd1632015-07-13 18:03:45 -07002007 wacom_wac->pen_input->name = wacom_wac->pen_name;
Jason Gerecked9f2d202015-07-13 18:03:44 -07002008 wacom_wac->touch_input->name = wacom_wac->touch_name;
2009 wacom_wac->pad_input->name = wacom_wac->pad_name;
2010
2011 return 0;
2012}
2013
Benjamin Tissoires008f4d92014-07-24 12:51:26 -07002014static int wacom_register_inputs(struct wacom *wacom)
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002015{
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002016 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002017 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
Jason Gerecke2636a3f2015-06-15 18:01:44 -07002018 int error = 0;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002019
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002020 pen_input_dev = wacom_wac->pen_input;
2021 touch_input_dev = wacom_wac->touch_input;
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04002022 pad_input_dev = wacom_wac->pad_input;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002023
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002024 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
Benjamin Tissoires2546dac2014-09-23 12:08:06 -04002025 return -EINVAL;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002026
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002027 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
2028 if (error) {
2029 /* no pen in use on this interface */
2030 input_free_device(pen_input_dev);
2031 wacom_wac->pen_input = NULL;
2032 pen_input_dev = NULL;
2033 } else {
2034 error = input_register_device(pen_input_dev);
Ping Cheng30ebc1a2014-11-18 13:29:16 -08002035 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002036 goto fail;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002037 }
2038
2039 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
2040 if (error) {
2041 /* no touch in use on this interface */
2042 input_free_device(touch_input_dev);
2043 wacom_wac->touch_input = NULL;
2044 touch_input_dev = NULL;
2045 } else {
2046 error = input_register_device(touch_input_dev);
2047 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002048 goto fail;
Ping Cheng30ebc1a2014-11-18 13:29:16 -08002049 }
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002050
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002051 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2052 if (error) {
2053 /* no pad in use on this interface */
2054 input_free_device(pad_input_dev);
2055 wacom_wac->pad_input = NULL;
2056 pad_input_dev = NULL;
2057 } else {
2058 error = input_register_device(pad_input_dev);
2059 if (error)
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002060 goto fail;
Benjamin Tissoiresd2d13f12014-07-24 12:48:28 -07002061 }
2062
Ping Cheng19635182012-04-29 21:09:18 -07002063 return 0;
2064
Benjamin Tissoires3dad1882016-07-13 18:05:54 +02002065fail:
2066 wacom_wac->pad_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002067 wacom_wac->touch_input = NULL;
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002068 wacom_wac->pen_input = NULL;
Chris Bagwell3aac0ef2012-03-25 23:25:45 -07002069 return error;
2070}
2071
Jason Gerecke0be01712015-08-05 15:44:53 -07002072/*
2073 * Not all devices report physical dimensions from HID.
2074 * Compute the default from hardcoded logical dimension
2075 * and resolution before driver overwrites them.
2076 */
2077static void wacom_set_default_phy(struct wacom_features *features)
2078{
2079 if (features->x_resolution) {
2080 features->x_phy = (features->x_max * 100) /
2081 features->x_resolution;
2082 features->y_phy = (features->y_max * 100) /
2083 features->y_resolution;
2084 }
2085}
2086
2087static void wacom_calculate_res(struct wacom_features *features)
2088{
2089 /* set unit to "100th of a mm" for devices not reported by HID */
2090 if (!features->unit) {
2091 features->unit = 0x11;
2092 features->unitExpo = -3;
2093 }
2094
2095 features->x_resolution = wacom_calc_hid_res(features->x_max,
2096 features->x_phy,
2097 features->unit,
2098 features->unitExpo);
2099 features->y_resolution = wacom_calc_hid_res(features->y_max,
2100 features->y_phy,
2101 features->unit,
2102 features->unitExpo);
2103}
2104
Jason Gereckefce99572015-03-06 11:47:40 -08002105void wacom_battery_work(struct work_struct *work)
2106{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002107 struct wacom *wacom = container_of(work, struct wacom, battery_work);
Jason Gereckefce99572015-03-06 11:47:40 -08002108
2109 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02002110 !wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08002111 wacom_initialize_battery(wacom);
2112 }
2113 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
Benjamin Tissoires59d69bc2016-07-13 18:06:08 +02002114 wacom->battery.battery) {
Jason Gereckefce99572015-03-06 11:47:40 -08002115 wacom_destroy_battery(wacom);
2116 }
2117}
2118
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002119static size_t wacom_compute_pktlen(struct hid_device *hdev)
2120{
2121 struct hid_report_enum *report_enum;
2122 struct hid_report *report;
2123 size_t size = 0;
2124
2125 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2126
2127 list_for_each_entry(report, &report_enum->report_list, list) {
Mathieu Magnaudetdabb05c62014-11-27 16:02:36 +01002128 size_t report_size = hid_report_len(report);
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002129 if (report_size > size)
2130 size = report_size;
2131 }
2132
2133 return size;
2134}
2135
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002136static void wacom_update_name(struct wacom *wacom, const char *suffix)
Ping Chengc24eab42015-04-24 15:32:51 -07002137{
2138 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2139 struct wacom_features *features = &wacom_wac->features;
Jason Gerecke44b52502015-06-15 18:01:41 -07002140 char name[WACOM_NAME_MAX];
Ping Chengc24eab42015-04-24 15:32:51 -07002141
2142 /* Generic devices name unspecified */
2143 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
Jason Gerecke09dc28a2017-07-24 09:46:19 -07002144 char *product_name = wacom->hdev->name;
Ping Chengc24eab42015-04-24 15:32:51 -07002145
Jason Gerecke09dc28a2017-07-24 09:46:19 -07002146 if (hid_is_using_ll_driver(wacom->hdev, &usb_hid_driver)) {
2147 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2148 struct usb_device *dev = interface_to_usbdev(intf);
2149 product_name = dev->product;
Ping Chengc24eab42015-04-24 15:32:51 -07002150 }
Jason Gerecke09dc28a2017-07-24 09:46:19 -07002151
2152 if (wacom->hdev->bus == BUS_I2C) {
2153 snprintf(name, sizeof(name), "%s %X",
2154 features->name, wacom->hdev->product);
2155 } else if (strstr(product_name, "Wacom") ||
2156 strstr(product_name, "wacom") ||
2157 strstr(product_name, "WACOM")) {
2158 strlcpy(name, product_name, sizeof(name));
2159 } else {
2160 snprintf(name, sizeof(name), "Wacom %s", product_name);
2161 }
2162
2163 /* strip out excess whitespaces */
2164 while (1) {
2165 char *gap = strstr(name, " ");
2166 if (gap == NULL)
2167 break;
2168 /* shift everything including the terminator */
2169 memmove(gap, gap+1, strlen(gap));
2170 }
2171
2172 /* get rid of trailing whitespace */
2173 if (name[strlen(name)-1] == ' ')
2174 name[strlen(name)-1] = '\0';
Ping Chengc24eab42015-04-24 15:32:51 -07002175 } else {
Jason Gerecke44b52502015-06-15 18:01:41 -07002176 strlcpy(name, features->name, sizeof(name));
Ping Chengc24eab42015-04-24 15:32:51 -07002177 }
2178
Benjamin Tissoires99569532016-07-13 18:06:17 +02002179 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2180 name, suffix);
2181
Ping Chengc24eab42015-04-24 15:32:51 -07002182 /* Append the device type to the name */
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002183 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002184 "%s%s Pen", name, suffix);
Jason Gerecke2a6cdbd2015-06-15 18:01:45 -07002185 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002186 "%s%s Finger", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07002187 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002188 "%s%s Pad", name, suffix);
Ping Chengc24eab42015-04-24 15:32:51 -07002189}
2190
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002191static void wacom_release_resources(struct wacom *wacom)
2192{
2193 struct hid_device *hdev = wacom->hdev;
2194
2195 if (!wacom->resources)
2196 return;
2197
2198 devres_release_group(&hdev->dev, wacom);
2199
2200 wacom->resources = false;
2201
2202 wacom->wacom_wac.pen_input = NULL;
2203 wacom->wacom_wac.touch_input = NULL;
2204 wacom->wacom_wac.pad_input = NULL;
2205}
2206
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002207static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2208{
2209 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2210 wacom_wac->shared->type = wacom_wac->features.type;
2211 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2212 }
2213
Ping Chengd793ff82017-02-14 21:27:45 -08002214 if (wacom_wac->has_mute_touch_switch) {
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002215 wacom_wac->shared->has_mute_touch_switch = true;
Ping Chengd793ff82017-02-14 21:27:45 -08002216 wacom_wac->shared->is_touch_on = true;
2217 }
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002218
2219 if (wacom_wac->shared->has_mute_touch_switch &&
2220 wacom_wac->shared->touch_input) {
2221 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2222 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2223 SW_MUTE_DEVICE);
2224 }
2225}
2226
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002227static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
Ping Cheng3bea7332006-07-13 18:01:36 -07002228{
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002229 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2230 struct wacom_features *features = &wacom_wac->features;
2231 struct hid_device *hdev = wacom->hdev;
Jason Childse33da8a2010-02-17 22:38:31 -08002232 int error;
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002233 unsigned int connect_mask = HID_CONNECT_HIDRAW;
Ping Cheng3bea7332006-07-13 18:01:36 -07002234
Benjamin Tissoires01c846f2014-07-24 12:59:11 -07002235 features->pktlen = wacom_compute_pktlen(hdev);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002236 if (features->pktlen > WACOM_PKGLEN_MAX)
2237 return -EINVAL;
Ping Cheng3bea7332006-07-13 18:01:36 -07002238
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002239 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2240 return -ENOMEM;
2241
2242 wacom->resources = true;
2243
Jason Gerecke3f14a632015-08-03 10:17:05 -07002244 error = wacom_allocate_inputs(wacom);
2245 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002246 goto fail;
Benjamin Tissoires494078b2014-09-23 12:08:07 -04002247
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05002248 /*
2249 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2250 * into debug mode for the touch part.
2251 * We ignore the other interfaces.
2252 */
2253 if (features->type == BAMBOO_PAD) {
2254 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2255 features->type = HID_GENERIC;
2256 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2257 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2258 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002259 goto fail;
Benjamin Tissoires8c97a762015-02-26 11:28:50 -05002260 }
2261 }
2262
Ping Cheng401d7d12013-07-28 00:38:54 -07002263 /* set the default size in case we do not get them from hid */
2264 wacom_set_default_phy(features);
2265
Ping Chengf393ee22012-04-29 21:09:17 -07002266 /* Retrieve the physical and logical size for touch devices */
Benjamin Tissoiresc669fb22014-07-24 13:02:14 -07002267 wacom_retrieve_hid_descriptor(hdev, features);
Ping Cheng42f4f272015-04-15 16:53:54 -07002268 wacom_setup_device_quirks(wacom);
Jason Gerecke042628a2015-04-30 17:51:54 -07002269
Jason Gereckeaa86b182015-06-15 18:01:42 -07002270 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2271 features->type != WIRELESS) {
Jason Gerecke8e116d32015-04-30 17:51:55 -07002272 error = features->type == HID_GENERIC ? -ENODEV : 0;
2273
Jason Gerecke042628a2015-04-30 17:51:54 -07002274 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
Jason Gerecke8e116d32015-04-30 17:51:55 -07002275 hdev->name,
2276 error ? "Ignoring" : "Assuming pen");
2277
2278 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002279 goto fail;
Jason Gerecke042628a2015-04-30 17:51:54 -07002280
Jason Gereckeaa86b182015-06-15 18:01:42 -07002281 features->device_type |= WACOM_DEVICETYPE_PEN;
Jason Gerecke042628a2015-04-30 17:51:54 -07002282 }
2283
Ping Cheng401d7d12013-07-28 00:38:54 -07002284 wacom_calculate_res(features);
2285
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002286 wacom_update_name(wacom, wireless ? " (WL)" : "");
Ping Cheng4492eff2010-03-19 22:18:15 -07002287
Aaron Armstrong Skomra8b407352017-03-29 10:35:39 -07002288 /* pen only Bamboo neither support touch nor pad */
2289 if ((features->type == BAMBOO_PEN) &&
2290 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2291 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2292 error = -ENODEV;
2293 goto fail;
2294 }
2295
Jason Gereckea9ce7852017-01-17 15:38:58 -08002296 error = wacom_add_shared_data(hdev);
2297 if (error)
2298 goto fail;
Ping Cheng49b764a2010-02-20 00:53:49 -08002299
Jason Gereckeccad85c2015-08-03 10:17:04 -07002300 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002301 (features->quirks & WACOM_QUIRK_BATTERY)) {
2302 error = wacom_initialize_battery(wacom);
2303 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002304 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002305 }
2306
Jason Gerecke3f14a632015-08-03 10:17:05 -07002307 error = wacom_register_inputs(wacom);
2308 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002309 goto fail;
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002310
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002311 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002312 error = wacom_initialize_leds(wacom);
2313 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002314 goto fail;
Benjamin Tissoires85d2c772016-07-13 18:05:51 +02002315
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002316 error = wacom_initialize_remotes(wacom);
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002317 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002318 goto fail;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002319 }
2320
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002321 if (features->type == HID_GENERIC)
2322 connect_mask |= HID_CONNECT_DRIVER;
2323
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002324 /* Regular HID work starts now */
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002325 error = hid_hw_start(hdev, connect_mask);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002326 if (error) {
2327 hid_err(hdev, "hw start failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002328 goto fail;
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002329 }
2330
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002331 if (!wireless) {
2332 /* Note that if query fails it is not a hard failure */
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002333 wacom_query_tablet_data(wacom);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002334 }
Jason Gerecke86e88f02015-11-03 16:57:58 -08002335
2336 /* touch only Bamboo doesn't support pen */
2337 if ((features->type == BAMBOO_TOUCH) &&
2338 (features->device_type & WACOM_DEVICETYPE_PEN)) {
Aaron Armstrong Skomra4d20c332017-03-29 11:41:28 -07002339 cancel_delayed_work_sync(&wacom->init_work);
2340 _wacom_query_tablet_data(wacom);
Jason Gerecke86e88f02015-11-03 16:57:58 -08002341 error = -ENODEV;
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002342 goto fail_quirks;
Jason Gerecke86e88f02015-11-03 16:57:58 -08002343 }
2344
Jason Gereckeccad85c2015-08-03 10:17:04 -07002345 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002346 error = hid_hw_open(hdev);
2347
Aaron Armstrong Skomrad2ec58a2017-01-25 12:08:41 -08002348 wacom_set_shared_values(wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002349 devres_close_group(&hdev->dev, wacom);
2350
Ping Cheng3bea7332006-07-13 18:01:36 -07002351 return 0;
2352
Benjamin Tissoiresb62f6462016-07-13 18:05:50 +02002353fail_quirks:
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002354 hid_hw_stop(hdev);
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002355fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002356 wacom_release_resources(wacom);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002357 return error;
2358}
2359
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002360static void wacom_wireless_work(struct work_struct *work)
2361{
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002362 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002363 struct usb_device *usbdev = wacom->usbdev;
2364 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2365 struct hid_device *hdev1, *hdev2;
2366 struct wacom *wacom1, *wacom2;
2367 struct wacom_wac *wacom_wac1, *wacom_wac2;
2368 int error;
2369
2370 /*
2371 * Regardless if this is a disconnect or a new tablet,
2372 * remove any existing input and battery devices.
2373 */
2374
2375 wacom_destroy_battery(wacom);
2376
2377 /* Stylus interface */
2378 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2379 wacom1 = hid_get_drvdata(hdev1);
2380 wacom_wac1 = &(wacom1->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002381 wacom_release_resources(wacom1);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002382
2383 /* Touch interface */
2384 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2385 wacom2 = hid_get_drvdata(hdev2);
2386 wacom_wac2 = &(wacom2->wacom_wac);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002387 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002388
2389 if (wacom_wac->pid == 0) {
2390 hid_info(wacom->hdev, "wireless tablet disconnected\n");
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002391 } else {
2392 const struct hid_device_id *id = wacom_ids;
2393
2394 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2395 wacom_wac->pid);
2396
2397 while (id->bus) {
2398 if (id->vendor == USB_VENDOR_ID_WACOM &&
2399 id->product == wacom_wac->pid)
2400 break;
2401 id++;
2402 }
2403
2404 if (!id->bus) {
2405 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2406 return;
2407 }
2408
2409 /* Stylus interface */
2410 wacom_wac1->features =
2411 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002412
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002413 wacom_wac1->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002414 hid_hw_stop(hdev1);
2415 error = wacom_parse_and_register(wacom1, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002416 if (error)
2417 goto fail;
2418
2419 /* Touch interface */
2420 if (wacom_wac1->features.touch_max ||
2421 (wacom_wac1->features.type >= INTUOSHT &&
2422 wacom_wac1->features.type <= BAMBOO_PT)) {
2423 wacom_wac2->features =
2424 *((struct wacom_features *)id->driver_data);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002425 wacom_wac2->pid = wacom_wac->pid;
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002426 hid_hw_stop(hdev2);
2427 error = wacom_parse_and_register(wacom2, true);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002428 if (error)
2429 goto fail;
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002430 }
2431
Benjamin Tissoires99569532016-07-13 18:06:17 +02002432 strlcpy(wacom_wac->name, wacom_wac1->name,
2433 sizeof(wacom_wac->name));
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002434 error = wacom_initialize_battery(wacom);
2435 if (error)
2436 goto fail;
2437 }
2438
2439 return;
2440
2441fail:
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002442 wacom_release_resources(wacom1);
Benjamin Tissoires84dfbd72016-07-13 18:05:55 +02002443 wacom_release_resources(wacom2);
Benjamin Tissoiresa2f091a2016-02-12 17:27:42 +01002444 return;
2445}
2446
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002447static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2448{
2449 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002450 u32 serial = remote->remotes[index].serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002451 int i;
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002452 unsigned long flags;
2453
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002454 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002455 if (remote->remotes[i].serial == serial) {
Aaron Armstrong Skomra791ae272017-12-07 12:31:56 -08002456
2457 spin_lock_irqsave(&remote->remote_lock, flags);
2458 remote->remotes[i].registered = false;
2459 spin_unlock_irqrestore(&remote->remote_lock, flags);
2460
2461 if (remote->remotes[i].battery.battery)
2462 devres_release_group(&wacom->hdev->dev,
2463 &remote->remotes[i].battery.bat_desc);
2464
2465 if (remote->remotes[i].group.name)
2466 devres_release_group(&wacom->hdev->dev,
2467 &remote->remotes[i]);
2468
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002469 remote->remotes[i].serial = 0;
2470 remote->remotes[i].group.name = NULL;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002471 remote->remotes[i].battery.battery = NULL;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002472 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2473 }
2474 }
2475}
2476
2477static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2478 unsigned int index)
2479{
2480 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002481 struct device *dev = &wacom->hdev->dev;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002482 int error, k;
2483
2484 /* A remote can pair more than once with an EKR,
2485 * check to make sure this serial isn't already paired.
2486 */
2487 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002488 if (remote->remotes[k].serial == serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002489 break;
2490 }
2491
2492 if (k < WACOM_MAX_REMOTES) {
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002493 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002494 return 0;
2495 }
2496
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002497 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002498 return -ENOMEM;
2499
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002500 error = wacom_remote_create_attr_group(wacom, serial, index);
2501 if (error)
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002502 goto fail;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002503
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002504 remote->remotes[index].input = wacom_allocate_input(wacom);
2505 if (!remote->remotes[index].input) {
2506 error = -ENOMEM;
2507 goto fail;
2508 }
2509 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2510 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2511
2512 if (!remote->remotes[index].input->name) {
2513 error = -EINVAL;
2514 goto fail;
2515 }
2516
2517 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2518 &wacom->wacom_wac);
2519 if (error)
2520 goto fail;
2521
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002522 remote->remotes[index].serial = serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002523
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002524 error = input_register_device(remote->remotes[index].input);
2525 if (error)
2526 goto fail;
2527
Benjamin Tissoires97f55412016-07-13 18:06:10 +02002528 error = wacom_led_groups_alloc_and_register_one(
2529 &remote->remotes[index].input->dev,
2530 wacom, index, 3, true);
2531 if (error)
2532 goto fail;
2533
Benjamin Tissoires7c35dc32016-07-13 18:06:07 +02002534 remote->remotes[index].registered = true;
2535
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002536 devres_close_group(dev, &remote->remotes[index]);
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002537 return 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002538
2539fail:
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002540 devres_release_group(dev, &remote->remotes[index]);
2541 remote->remotes[index].serial = 0;
Benjamin Tissoiresf9036bd2016-07-13 18:06:05 +02002542 return error;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002543}
2544
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002545static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2546{
2547 struct wacom_remote *remote = wacom->remote;
2548 int error;
2549
2550 if (!remote->remotes[index].registered)
2551 return 0;
2552
2553 if (remote->remotes[index].battery.battery)
2554 return 0;
2555
2556 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2557 return 0;
2558
2559 error = __wacom_initialize_battery(wacom,
2560 &wacom->remote->remotes[index].battery);
2561 if (error)
2562 return error;
2563
2564 return 0;
2565}
2566
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002567static void wacom_remote_work(struct work_struct *work)
2568{
2569 struct wacom *wacom = container_of(work, struct wacom, remote_work);
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002570 struct wacom_remote *remote = wacom->remote;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002571 struct wacom_remote_data data;
2572 unsigned long flags;
2573 unsigned int count;
2574 u32 serial;
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002575 int i;
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002576
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002577 spin_lock_irqsave(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002578
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002579 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002580
2581 if (count != sizeof(data)) {
2582 hid_err(wacom->hdev,
2583 "workitem triggered without status available\n");
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002584 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002585 return;
2586 }
2587
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002588 if (!kfifo_is_empty(&remote->remote_fifo))
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002589 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2590
Benjamin Tissoires83e6b402016-07-13 18:06:02 +02002591 spin_unlock_irqrestore(&remote->remote_lock, flags);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002592
2593 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2594 serial = data.remote[i].serial;
2595 if (data.remote[i].connected) {
2596
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002597 if (remote->remotes[i].serial == serial) {
2598 wacom_remote_attach_battery(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002599 continue;
Benjamin Tissoires9f1015d42016-07-13 18:06:09 +02002600 }
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002601
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002602 if (remote->remotes[i].serial)
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002603 wacom_remote_destroy_one(wacom, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002604
Benjamin Tissoires04bfa272016-07-13 18:06:04 +02002605 wacom_remote_create_one(wacom, serial, i);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002606
Benjamin Tissoirese7749f62016-07-13 18:06:06 +02002607 } else 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 }
2610 }
2611}
2612
Benjamin Tissoires4082da82017-02-14 21:27:18 -08002613static void wacom_mode_change_work(struct work_struct *work)
2614{
2615 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2616 struct wacom_shared *shared = wacom->wacom_wac.shared;
2617 struct wacom *wacom1 = NULL;
2618 struct wacom *wacom2 = NULL;
2619 bool is_direct = wacom->wacom_wac.is_direct_mode;
2620 int error = 0;
2621
2622 if (shared->pen) {
2623 wacom1 = hid_get_drvdata(shared->pen);
2624 wacom_release_resources(wacom1);
2625 hid_hw_stop(wacom1->hdev);
2626 wacom1->wacom_wac.has_mode_change = true;
2627 wacom1->wacom_wac.is_direct_mode = is_direct;
2628 }
2629
2630 if (shared->touch) {
2631 wacom2 = hid_get_drvdata(shared->touch);
2632 wacom_release_resources(wacom2);
2633 hid_hw_stop(wacom2->hdev);
2634 wacom2->wacom_wac.has_mode_change = true;
2635 wacom2->wacom_wac.is_direct_mode = is_direct;
2636 }
2637
2638 if (wacom1) {
2639 error = wacom_parse_and_register(wacom1, false);
2640 if (error)
2641 return;
2642 }
2643
2644 if (wacom2) {
2645 error = wacom_parse_and_register(wacom2, false);
2646 if (error)
2647 return;
2648 }
2649
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002650 return;
2651}
2652
2653static int wacom_probe(struct hid_device *hdev,
2654 const struct hid_device_id *id)
2655{
2656 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2657 struct usb_device *dev = interface_to_usbdev(intf);
2658 struct wacom *wacom;
2659 struct wacom_wac *wacom_wac;
2660 struct wacom_features *features;
2661 int error;
2662
2663 if (!id->driver_data)
2664 return -EINVAL;
2665
2666 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2667
2668 /* hid-core sets this quirk for the boot interface */
2669 hdev->quirks &= ~HID_QUIRK_NOGET;
2670
Benjamin Tissoires19b64332016-07-13 18:05:58 +02002671 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002672 if (!wacom)
2673 return -ENOMEM;
2674
2675 hid_set_drvdata(hdev, wacom);
2676 wacom->hdev = hdev;
2677
2678 wacom_wac = &wacom->wacom_wac;
2679 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2680 features = &wacom_wac->features;
2681
2682 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2683 error = -ENODEV;
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002684 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002685 }
2686
Jason Gerecke83417202017-11-10 11:50:01 -08002687 error = kfifo_alloc(&wacom_wac->pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
2688 if (error)
2689 goto fail;
2690
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002691 wacom_wac->hid_data.inputmode = -1;
Jason Gerecke326ea2a2016-04-04 11:26:52 -07002692 wacom_wac->mode_report = -1;
Jason Gereckec6fa1ae2016-04-04 11:26:51 -07002693
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002694 wacom->usbdev = dev;
2695 wacom->intf = intf;
2696 mutex_init(&wacom->lock);
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002697 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002698 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2699 INIT_WORK(&wacom->battery_work, wacom_battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002700 INIT_WORK(&wacom->remote_work, wacom_remote_work);
Benjamin Tissoires4082da82017-02-14 21:27:18 -08002701 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002702
2703 /* ask for the report descriptor to be loaded by HID */
2704 error = hid_parse(hdev);
2705 if (error) {
2706 hid_err(hdev, "parse failed\n");
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002707 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002708 }
2709
Benjamin Tissoiresfd5f92b2016-02-12 17:27:43 +01002710 error = wacom_parse_and_register(wacom, false);
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002711 if (error)
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002712 goto fail;
Benjamin Tissoiresc58ac3a2016-02-12 17:27:41 +01002713
2714 if (hdev->bus == BUS_BLUETOOTH) {
2715 error = device_create_file(&hdev->dev, &dev_attr_speed);
2716 if (error)
2717 hid_warn(hdev,
2718 "can't create sysfs speed attribute err: %d\n",
2719 error);
2720 }
2721
2722 return 0;
2723
Benjamin Tissoires3888b0d2016-07-13 18:06:03 +02002724fail:
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002725 hid_set_drvdata(hdev, NULL);
Dmitry Torokhov50141862007-04-12 01:33:39 -04002726 return error;
Ping Cheng3bea7332006-07-13 18:01:36 -07002727}
2728
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002729static void wacom_remove(struct hid_device *hdev)
Ping Cheng3bea7332006-07-13 18:01:36 -07002730{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002731 struct wacom *wacom = hid_get_drvdata(hdev);
Benjamin Tissoiresf6205162016-02-12 17:27:45 +01002732 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2733 struct wacom_features *features = &wacom_wac->features;
2734
2735 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2736 hid_hw_close(hdev);
Ping Cheng3bea7332006-07-13 18:01:36 -07002737
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002738 hid_hw_stop(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002739
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002740 cancel_delayed_work_sync(&wacom->init_work);
Benjamin Tissoiresd17d1f12016-07-13 18:05:52 +02002741 cancel_work_sync(&wacom->wireless_work);
2742 cancel_work_sync(&wacom->battery_work);
Benjamin Tissoirese6f28132016-07-13 18:06:01 +02002743 cancel_work_sync(&wacom->remote_work);
Benjamin Tissoires4082da82017-02-14 21:27:18 -08002744 cancel_work_sync(&wacom->mode_change_work);
Benjamin Tissoiresf81a1292014-08-06 13:48:01 -07002745 if (hdev->bus == BUS_BLUETOOTH)
2746 device_remove_file(&hdev->dev, &dev_attr_speed);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002747
Benjamin Tissoiresc0265a942017-01-20 16:20:12 +01002748 /* make sure we don't trigger the LEDs */
2749 wacom_led_groups_release(wacom);
Aaron Armstrong Skomrab6b1f192017-03-06 10:54:58 -08002750
2751 if (wacom->wacom_wac.features.type != REMOTE)
2752 wacom_release_resources(wacom);
Benjamin Tissoires5b779fc2017-01-20 16:20:11 +01002753
Jason Gerecke83417202017-11-10 11:50:01 -08002754 kfifo_free(&wacom_wac->pen_fifo);
2755
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002756 hid_set_drvdata(hdev, NULL);
Oliver Neukume7224092008-04-15 01:31:57 -04002757}
2758
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002759#ifdef CONFIG_PM
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002760static int wacom_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002761{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002762 struct wacom *wacom = hid_get_drvdata(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002763
2764 mutex_lock(&wacom->lock);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002765
2766 /* switch to wacom mode first */
Benjamin Tissoiresa544c612017-01-20 16:20:13 +01002767 _wacom_query_tablet_data(wacom);
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002768 wacom_led_control(wacom);
2769
Oliver Neukume7224092008-04-15 01:31:57 -04002770 mutex_unlock(&wacom->lock);
2771
2772 return 0;
2773}
2774
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002775static int wacom_reset_resume(struct hid_device *hdev)
Oliver Neukume7224092008-04-15 01:31:57 -04002776{
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002777 return wacom_resume(hdev);
Oliver Neukume7224092008-04-15 01:31:57 -04002778}
Geert Uytterhoeven41a74582014-08-11 11:03:19 -07002779#endif /* CONFIG_PM */
Oliver Neukume7224092008-04-15 01:31:57 -04002780
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002781static struct hid_driver wacom_driver = {
Ping Cheng3bea7332006-07-13 18:01:36 -07002782 .name = "wacom",
Bastian Blankb036f6f2010-02-10 23:06:23 -08002783 .id_table = wacom_ids,
Ping Cheng3bea7332006-07-13 18:01:36 -07002784 .probe = wacom_probe,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002785 .remove = wacom_remove,
Benjamin Tissoires7704ac92014-09-23 12:08:08 -04002786 .report = wacom_wac_report,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002787#ifdef CONFIG_PM
Oliver Neukume7224092008-04-15 01:31:57 -04002788 .resume = wacom_resume,
2789 .reset_resume = wacom_reset_resume,
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002790#endif
2791 .raw_event = wacom_raw_event,
Ping Cheng3bea7332006-07-13 18:01:36 -07002792};
Benjamin Tissoires29b47392014-07-24 12:52:23 -07002793module_hid_driver(wacom_driver);
Benjamin Tissoiresf2e0a7d2014-08-06 14:07:49 -07002794
2795MODULE_VERSION(DRIVER_VERSION);
2796MODULE_AUTHOR(DRIVER_AUTHOR);
2797MODULE_DESCRIPTION(DRIVER_DESC);
Grant Grundler7021b602017-01-05 11:07:04 -08002798MODULE_LICENSE("GPL");