]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hid/wacom_sys.c
HID: wacom: fix bit shift for Cintiq Companion 2
[mirror_ubuntu-bionic-kernel.git] / drivers / hid / wacom_sys.c
CommitLineData
3bea733a 1/*
4104d13f 2 * drivers/input/tablet/wacom_sys.c
3bea733a 3 *
232f5693 4 * USB Wacom tablet support - system specific code
3bea733a
PC
5 */
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
3bea733a 14#include "wacom_wac.h"
51269fe8 15#include "wacom.h"
b58ba1ba 16#include <linux/input/mt.h>
3bea733a 17
a417ea44 18#define WAC_MSG_RETRIES 5
5d7e7d47
EH
19#define WAC_CMD_RETRIES 10
20
e0984bc3
PC
21#define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
22#define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
72b236d6 23#define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
e0984bc3 24
c64d8834
PC
25static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
26 size_t size, unsigned int retries)
3bea733a 27{
5d7e7d47
EH
28 int retval;
29
30 do {
c64d8834 31 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
27b20a9d 32 HID_REQ_GET_REPORT);
aef3156d
JG
33 } 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);
5d7e7d47
EH
38
39 return retval;
3bea733a
PC
40}
41
296b7378
PF
42static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
43 size_t size, unsigned int retries)
3bea733a 44{
5d7e7d47
EH
45 int retval;
46
47 do {
296b7378 48 retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
27b20a9d 49 HID_REQ_SET_REPORT);
aef3156d
JG
50 } 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);
5d7e7d47
EH
55
56 return retval;
3bea733a
PC
57}
58
29b47391
BT
59static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
60 u8 *raw_data, int size)
3bea733a 61{
29b47391 62 struct wacom *wacom = hid_get_drvdata(hdev);
3bea733a 63
29b47391
BT
64 if (size > WACOM_PKGLEN_MAX)
65 return 1;
3bea733a 66
29b47391 67 memcpy(wacom->wacom_wac.data, raw_data, size);
3bea733a 68
29b47391
BT
69 wacom_wac_irq(&wacom->wacom_wac, size);
70
71 return 0;
3bea733a
PC
72}
73
3bea733a
PC
74static int wacom_open(struct input_dev *dev)
75{
7791bdae 76 struct wacom *wacom = input_get_drvdata(dev);
29b47391 77
dff67416 78 return hid_hw_open(wacom->hdev);
3bea733a
PC
79}
80
81static void wacom_close(struct input_dev *dev)
82{
7791bdae 83 struct wacom *wacom = input_get_drvdata(dev);
3bea733a 84
3dad188e
BT
85 /*
86 * wacom->hdev should never be null, but surprisingly, I had the case
87 * once while unplugging the Wacom Wireless Receiver.
88 */
89 if (wacom->hdev)
90 hid_hw_close(wacom->hdev);
3bea733a
PC
91}
92
115d5e12 93/*
198fdee2 94 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
115d5e12
JG
95 */
96static int wacom_calc_hid_res(int logical_extents, int physical_extents,
c669fb2b 97 unsigned unit, int exponent)
115d5e12 98{
198fdee2
BT
99 struct hid_field field = {
100 .logical_maximum = logical_extents,
101 .physical_maximum = physical_extents,
102 .unit = unit,
103 .unit_exponent = exponent,
104 };
105
106 return hidinput_calc_abs_res(&field, ABS_X);
115d5e12
JG
107}
108
c669fb2b
BT
109static void wacom_feature_mapping(struct hid_device *hdev,
110 struct hid_field *field, struct hid_usage *usage)
f393ee2b 111{
c669fb2b
BT
112 struct wacom *wacom = hid_get_drvdata(hdev);
113 struct wacom_features *features = &wacom->wacom_wac.features;
5ae6e89f 114 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
ac2423c9 115 unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
8ffffd52
BT
116 u8 *data;
117 int ret;
efa93189 118 u32 n;
f393ee2b 119
ac2423c9 120 switch (equivalent_usage) {
bc335d4c
AAS
121 case WACOM_HID_WD_TOUCH_RING_SETTING:
122 wacom->generic_has_leds = true;
123 break;
c669fb2b
BT
124 case HID_DG_CONTACTMAX:
125 /* leave touch_max as is if predefined */
8ffffd52
BT
126 if (!features->touch_max) {
127 /* read manually */
128 data = kzalloc(2, GFP_KERNEL);
129 if (!data)
130 break;
131 data[0] = field->report->id;
132 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
05e8fd92
JG
133 data, 2, WAC_CMD_RETRIES);
134 if (ret == 2) {
8ffffd52 135 features->touch_max = data[1];
05e8fd92
JG
136 } else {
137 features->touch_max = 16;
138 hid_warn(hdev, "wacom_feature_mapping: "
139 "could not get HID_DG_CONTACTMAX, "
140 "defaulting to %d\n",
141 features->touch_max);
142 }
8ffffd52
BT
143 kfree(data);
144 }
c669fb2b 145 break;
5ae6e89f
BT
146 case HID_DG_INPUTMODE:
147 /* Ignore if value index is out of bounds. */
148 if (usage->usage_index >= field->report_count) {
149 dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
150 break;
151 }
152
153 hid_data->inputmode = field->report->id;
154 hid_data->inputmode_index = usage->usage_index;
155 break;
326ea2a9
JG
156
157 case HID_UP_DIGITIZER:
158 if (field->report->id == 0x0B &&
8de82280
JG
159 (field->application == WACOM_HID_G9_PEN ||
160 field->application == WACOM_HID_G11_PEN)) {
326ea2a9
JG
161 wacom->wacom_wac.mode_report = field->report->id;
162 wacom->wacom_wac.mode_value = 0;
163 }
164 break;
165
c9c09587
JG
166 case WACOM_HID_WD_DATAMODE:
167 wacom->wacom_wac.mode_report = field->report->id;
168 wacom->wacom_wac.mode_value = 2;
169 break;
170
8de82280
JG
171 case WACOM_HID_UP_G9:
172 case WACOM_HID_UP_G11:
326ea2a9 173 if (field->report->id == 0x03 &&
8de82280
JG
174 (field->application == WACOM_HID_G9_TOUCHSCREEN ||
175 field->application == WACOM_HID_G11_TOUCHSCREEN)) {
326ea2a9
JG
176 wacom->wacom_wac.mode_report = field->report->id;
177 wacom->wacom_wac.mode_value = 0;
178 }
179 break;
345857bb
JG
180 case WACOM_HID_WD_OFFSETLEFT:
181 case WACOM_HID_WD_OFFSETTOP:
182 case WACOM_HID_WD_OFFSETRIGHT:
183 case WACOM_HID_WD_OFFSETBOTTOM:
184 /* read manually */
185 n = hid_report_len(field->report);
186 data = hid_alloc_report_buf(field->report, GFP_KERNEL);
187 if (!data)
188 break;
189 data[0] = field->report->id;
190 ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
191 data, n, WAC_CMD_RETRIES);
192 if (ret == n) {
193 ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
194 data, n, 0);
195 } else {
196 hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
197 __func__);
198 }
199 kfree(data);
200 break;
f393ee2b 201 }
5b01b3b8
BT
202
203 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
204 hdev->product == 0x4200 /* Dell Canvas 27 */ &&
205 field->application == HID_UP_MSVENDOR) {
206 wacom->wacom_wac.mode_report = field->report->id;
207 wacom->wacom_wac.mode_value = 2;
208 }
f393ee2b
PC
209}
210
428f8588
CB
211/*
212 * Interface Descriptor of wacom devices can be incomplete and
213 * inconsistent so wacom_features table is used to store stylus
214 * device's packet lengths, various maximum values, and tablet
215 * resolution based on product ID's.
216 *
217 * For devices that contain 2 interfaces, wacom_features table is
218 * inaccurate for the touch interface. Since the Interface Descriptor
219 * for touch interfaces has pretty complete data, this function exists
220 * to query tablet for this missing information instead of hard coding in
221 * an additional table.
222 *
223 * A typical Interface Descriptor for a stylus will contain a
224 * boot mouse application collection that is not of interest and this
225 * function will ignore it.
226 *
227 * It also contains a digitizer application collection that also is not
228 * of interest since any information it contains would be duplicate
229 * of what is in wacom_features. Usually it defines a report of an array
230 * of bytes that could be used as max length of the stylus packet returned.
231 * If it happens to define a Digitizer-Stylus Physical Collection then
232 * the X and Y logical values contain valid data but it is ignored.
233 *
234 * A typical Interface Descriptor for a touch interface will contain a
235 * Digitizer-Finger Physical Collection which will define both logical
236 * X/Y maximum as well as the physical size of tablet. Since touch
237 * interfaces haven't supported pressure or distance, this is enough
238 * information to override invalid values in the wacom_features table.
4134361a 239 *
c669fb2b
BT
240 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
241 * data. We deal with them after returning from this function.
428f8588 242 */
c669fb2b
BT
243static void wacom_usage_mapping(struct hid_device *hdev,
244 struct hid_field *field, struct hid_usage *usage)
545f4e99 245{
c669fb2b
BT
246 struct wacom *wacom = hid_get_drvdata(hdev);
247 struct wacom_features *features = &wacom->wacom_wac.features;
d97a5522
BT
248 bool finger = WACOM_FINGER_FIELD(field);
249 bool pen = WACOM_PEN_FIELD(field);
e9fc413f 250
c669fb2b
BT
251 /*
252 * Requiring Stylus Usage will ignore boot mouse
253 * X/Y values and some cases of invalid Digitizer X/Y
254 * values commonly reported.
255 */
042628ab 256 if (pen)
aa86b18c 257 features->device_type |= WACOM_DEVICETYPE_PEN;
042628ab 258 else if (finger)
aa86b18c 259 features->device_type |= WACOM_DEVICETYPE_TOUCH;
042628ab 260 else
c669fb2b
BT
261 return;
262
30ebc1ae
PC
263 /*
264 * Bamboo models do not support HID_DG_CONTACTMAX.
265 * And, Bamboo Pen only descriptor contains touch.
266 */
3b164a00 267 if (features->type > BAMBOO_PT) {
30ebc1ae
PC
268 /* ISDv4 touch devices at least supports one touch point */
269 if (finger && !features->touch_max)
270 features->touch_max = 1;
271 }
c669fb2b 272
6005a13c
JG
273 /*
274 * ISDv4 devices which predate HID's adoption of the
275 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
276 * position instead. We can accurately detect if a
277 * usage with that value should be HID_DG_BARRELSWITCH2
278 * based on the surrounding usages, which have remained
279 * constant across generations.
280 */
281 if (features->type == HID_GENERIC &&
282 usage->hid == 0x000D0000 &&
283 field->application == HID_DG_PEN &&
284 field->physical == HID_DG_STYLUS) {
285 int i = usage->usage_index;
286
287 if (i-4 >= 0 && i+1 < field->maxusage &&
288 field->usage[i-4].hid == HID_DG_TIPSWITCH &&
289 field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
290 field->usage[i-2].hid == HID_DG_ERASER &&
291 field->usage[i-1].hid == HID_DG_INVERT &&
292 field->usage[i+1].hid == HID_DG_INRANGE) {
293 usage->hid = HID_DG_BARRELSWITCH2;
294 }
295 }
296
553bc6c3
JG
297 /* 2nd-generation Intuos Pro Large has incorrect Y maximum */
298 if (hdev->vendor == USB_VENDOR_ID_WACOM &&
299 hdev->product == 0x0358 &&
300 WACOM_PEN_FIELD(field) &&
301 wacom_equivalent_usage(usage->hid) == HID_GD_Y) {
302 field->logical_maximum = 43200;
303 }
304
c669fb2b
BT
305 switch (usage->hid) {
306 case HID_GD_X:
307 features->x_max = field->logical_maximum;
308 if (finger) {
c669fb2b 309 features->x_phy = field->physical_maximum;
3b164a00
PC
310 if ((features->type != BAMBOO_PT) &&
311 (features->type != BAMBOO_TOUCH)) {
c669fb2b
BT
312 features->unit = field->unit;
313 features->unitExpo = field->unit_exponent;
545f4e99 314 }
c669fb2b
BT
315 }
316 break;
317 case HID_GD_Y:
318 features->y_max = field->logical_maximum;
319 if (finger) {
320 features->y_phy = field->physical_maximum;
3b164a00
PC
321 if ((features->type != BAMBOO_PT) &&
322 (features->type != BAMBOO_TOUCH)) {
c669fb2b
BT
323 features->unit = field->unit;
324 features->unitExpo = field->unit_exponent;
325 }
326 }
327 break;
328 case HID_DG_TIPPRESSURE:
329 if (pen)
330 features->pressure_max = field->logical_maximum;
331 break;
332 }
7704ac93
BT
333
334 if (features->type == HID_GENERIC)
335 wacom_wac_usage_mapping(hdev, field, usage);
c669fb2b 336}
4134361a 337
b58ba1ba
JG
338static void wacom_post_parse_hid(struct hid_device *hdev,
339 struct wacom_features *features)
340{
341 struct wacom *wacom = hid_get_drvdata(hdev);
342 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
343
344 if (features->type == HID_GENERIC) {
345 /* Any last-minute generic device setup */
4082da80
BT
346 if (wacom_wac->has_mode_change) {
347 if (wacom_wac->is_direct_mode)
348 features->device_type |= WACOM_DEVICETYPE_DIRECT;
349 else
350 features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
351 }
352
b58ba1ba 353 if (features->touch_max > 1) {
ac2423c9
AAS
354 if (features->device_type & WACOM_DEVICETYPE_DIRECT)
355 input_mt_init_slots(wacom_wac->touch_input,
356 wacom_wac->features.touch_max,
357 INPUT_MT_DIRECT);
358 else
359 input_mt_init_slots(wacom_wac->touch_input,
360 wacom_wac->features.touch_max,
361 INPUT_MT_POINTER);
b58ba1ba
JG
362 }
363 }
364}
365
c669fb2b
BT
366static void wacom_parse_hid(struct hid_device *hdev,
367 struct wacom_features *features)
368{
369 struct hid_report_enum *rep_enum;
370 struct hid_report *hreport;
371 int i, j;
372
373 /* check features first */
374 rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
375 list_for_each_entry(hreport, &rep_enum->report_list, list) {
376 for (i = 0; i < hreport->maxfield; i++) {
377 /* Ignore if report count is out of bounds. */
378 if (hreport->field[i]->report_count < 1)
379 continue;
380
381 for (j = 0; j < hreport->field[i]->maxusage; j++) {
382 wacom_feature_mapping(hdev, hreport->field[i],
383 hreport->field[i]->usage + j);
4134361a 384 }
545f4e99
PC
385 }
386 }
387
c669fb2b
BT
388 /* now check the input usages */
389 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
390 list_for_each_entry(hreport, &rep_enum->report_list, list) {
391
392 if (!hreport->maxfield)
393 continue;
394
395 for (i = 0; i < hreport->maxfield; i++)
396 for (j = 0; j < hreport->field[i]->maxusage; j++)
397 wacom_usage_mapping(hdev, hreport->field[i],
398 hreport->field[i]->usage + j);
399 }
b58ba1ba
JG
400
401 wacom_post_parse_hid(hdev, features);
545f4e99
PC
402}
403
5ae6e89f
BT
404static int wacom_hid_set_device_mode(struct hid_device *hdev)
405{
406 struct wacom *wacom = hid_get_drvdata(hdev);
407 struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
408 struct hid_report *r;
409 struct hid_report_enum *re;
410
411 if (hid_data->inputmode < 0)
412 return 0;
413
414 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
415 r = re->report_id_hash[hid_data->inputmode];
416 if (r) {
417 r->field[0]->value[hid_data->inputmode_index] = 2;
418 hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
419 }
420 return 0;
421}
422
326ea2a9
JG
423static int wacom_set_device_mode(struct hid_device *hdev,
424 struct wacom_wac *wacom_wac)
3b7307c2 425{
326ea2a9
JG
426 u8 *rep_data;
427 struct hid_report *r;
428 struct hid_report_enum *re;
efa93189 429 u32 length;
fe494bc2 430 int error = -ENOMEM, limit = 0;
3b7307c2 431
326ea2a9
JG
432 if (wacom_wac->mode_report < 0)
433 return 0;
434
435 re = &(hdev->report_enum[HID_FEATURE_REPORT]);
436 r = re->report_id_hash[wacom_wac->mode_report];
437 if (!r)
438 return -EINVAL;
439
440 rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
3b7307c2 441 if (!rep_data)
326ea2a9
JG
442 return -ENOMEM;
443
444 length = hid_report_len(r);
ec67bbed 445
fe494bc2 446 do {
326ea2a9
JG
447 rep_data[0] = wacom_wac->mode_report;
448 rep_data[1] = wacom_wac->mode_value;
9937c026 449
296b7378
PF
450 error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
451 length, 1);
3cb83157 452 if (error >= 0)
27b20a9d 453 error = wacom_get_report(hdev, HID_FEATURE_REPORT,
c64d8834 454 rep_data, length, 1);
326ea2a9
JG
455 } while (error >= 0 &&
456 rep_data[1] != wacom_wac->mode_report &&
457 limit++ < WAC_MSG_RETRIES);
fe494bc2
JG
458
459 kfree(rep_data);
460
461 return error < 0 ? error : 0;
462}
463
f81a1295
BT
464static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
465 struct wacom_features *features)
466{
387142bb
BT
467 struct wacom *wacom = hid_get_drvdata(hdev);
468 int ret;
469 u8 rep_data[2];
470
471 switch (features->type) {
472 case GRAPHIRE_BT:
473 rep_data[0] = 0x03;
474 rep_data[1] = 0x00;
296b7378
PF
475 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
476 3);
387142bb
BT
477
478 if (ret >= 0) {
479 rep_data[0] = speed == 0 ? 0x05 : 0x06;
480 rep_data[1] = 0x00;
481
482 ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
296b7378 483 rep_data, 2, 3);
387142bb
BT
484
485 if (ret >= 0) {
486 wacom->wacom_wac.bt_high_speed = speed;
487 return 0;
488 }
489 }
490
491 /*
492 * Note that if the raw queries fail, it's not a hard failure
493 * and it is safe to continue
494 */
495 hid_warn(hdev, "failed to poke device, command %d, err %d\n",
496 rep_data[0], ret);
497 break;
81af7e61
BT
498 case INTUOS4WL:
499 if (speed == 1)
500 wacom->wacom_wac.bt_features &= ~0x20;
501 else
502 wacom->wacom_wac.bt_features |= 0x20;
503
504 rep_data[0] = 0x03;
505 rep_data[1] = wacom->wacom_wac.bt_features;
506
296b7378
PF
507 ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
508 1);
81af7e61
BT
509 if (ret >= 0)
510 wacom->wacom_wac.bt_high_speed = speed;
511 break;
387142bb
BT
512 }
513
f81a1295
BT
514 return 0;
515}
516
fe494bc2
JG
517/*
518 * Switch the tablet into its most-capable mode. Wacom tablets are
519 * typically configured to power-up in a mode which sends mouse-like
520 * reports to the OS. To get absolute position, pressure data, etc.
521 * from the tablet, it is necessary to switch the tablet out of this
522 * mode and into one which sends the full range of tablet data.
523 */
a544c619 524static int _wacom_query_tablet_data(struct wacom *wacom)
fe494bc2 525{
a544c619 526 struct hid_device *hdev = wacom->hdev;
326ea2a9 527 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
a544c619 528 struct wacom_features *features = &wacom_wac->features;
326ea2a9 529
f81a1295
BT
530 if (hdev->bus == BUS_BLUETOOTH)
531 return wacom_bt_query_tablet_data(hdev, 1, features);
532
326ea2a9
JG
533 if (features->type != HID_GENERIC) {
534 if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
535 if (features->type > TABLETPC) {
536 /* MT Tablet PC touch */
537 wacom_wac->mode_report = 3;
538 wacom_wac->mode_value = 4;
539 } else if (features->type == WACOM_24HDT) {
540 wacom_wac->mode_report = 18;
541 wacom_wac->mode_value = 2;
542 } else if (features->type == WACOM_27QHDT) {
543 wacom_wac->mode_report = 131;
544 wacom_wac->mode_value = 2;
545 } else if (features->type == BAMBOO_PAD) {
546 wacom_wac->mode_report = 2;
547 wacom_wac->mode_value = 2;
548 }
549 } else if (features->device_type & WACOM_DEVICETYPE_PEN) {
550 if (features->type <= BAMBOO_PT) {
551 wacom_wac->mode_report = 2;
552 wacom_wac->mode_value = 2;
553 }
1963518b 554 }
ec67bbed 555 }
3b7307c2 556
326ea2a9
JG
557 wacom_set_device_mode(hdev, wacom_wac);
558
559 if (features->type == HID_GENERIC)
560 return wacom_hid_set_device_mode(hdev);
561
fe494bc2 562 return 0;
3b7307c2
DT
563}
564
c669fb2b 565static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
1963518b 566 struct wacom_features *features)
ec67bbed 567{
27b20a9d
BT
568 struct wacom *wacom = hid_get_drvdata(hdev);
569 struct usb_interface *intf = wacom->intf;
ec67bbed 570
fed87e65 571 /* default features */
fed87e65
HR
572 features->x_fuzz = 4;
573 features->y_fuzz = 4;
574 features->pressure_fuzz = 0;
bef7e200
JG
575 features->distance_fuzz = 1;
576 features->tilt_fuzz = 1;
ec67bbed 577
d3825d51
CB
578 /*
579 * The wireless device HID is basic and layout conflicts with
580 * other tablets (monitor and touch interface can look like pen).
581 * Skip the query for this type and modify defaults based on
582 * interface number.
583 */
584 if (features->type == WIRELESS) {
3f14a63a 585 if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
ccad85cc 586 features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
3f14a63a 587 else
aa86b18c 588 features->device_type = WACOM_DEVICETYPE_NONE;
3f14a63a 589 return;
d3825d51
CB
590 }
591
c669fb2b 592 wacom_parse_hid(hdev, features);
ec67bbed
PC
593}
594
4451e088 595struct wacom_hdev_data {
4492efff
PC
596 struct list_head list;
597 struct kref kref;
4451e088 598 struct hid_device *dev;
4492efff
PC
599 struct wacom_shared shared;
600};
601
602static LIST_HEAD(wacom_udev_list);
603static DEFINE_MUTEX(wacom_udev_list_lock);
604
41372d5d
JG
605static bool compare_device_paths(struct hid_device *hdev_a,
606 struct hid_device *hdev_b, char separator)
607{
608 int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
609 int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
610
611 if (n1 != n2 || n1 <= 0 || n2 <= 0)
612 return false;
613
614 return !strncmp(hdev_a->phys, hdev_b->phys, n1);
615}
616
4451e088
BT
617static bool wacom_are_sibling(struct hid_device *hdev,
618 struct hid_device *sibling)
aea2bf6a 619{
4451e088
BT
620 struct wacom *wacom = hid_get_drvdata(hdev);
621 struct wacom_features *features = &wacom->wacom_wac.features;
41372d5d
JG
622 struct wacom *sibling_wacom = hid_get_drvdata(sibling);
623 struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
624 __u32 oVid = features->oVid ? features->oVid : hdev->vendor;
625 __u32 oPid = features->oPid ? features->oPid : hdev->product;
4451e088 626
41372d5d
JG
627 /* The defined oVid/oPid must match that of the sibling */
628 if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
629 return false;
630 if (features->oPid != HID_ANY_ID && sibling->product != oPid)
631 return false;
632
633 /*
634 * Devices with the same VID/PID must share the same physical
635 * device path, while those with different VID/PID must share
636 * the same physical parent device path.
637 */
638 if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
639 if (!compare_device_paths(hdev, sibling, '/'))
640 return false;
641 } else {
642 if (!compare_device_paths(hdev, sibling, '.'))
643 return false;
4451e088 644 }
aea2bf6a 645
41372d5d
JG
646 /* Skip the remaining heuristics unless you are a HID_GENERIC device */
647 if (features->type != HID_GENERIC)
648 return true;
649
650 /*
651 * Direct-input devices may not be siblings of indirect-input
652 * devices.
653 */
654 if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
655 !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
4451e088 656 return false;
aea2bf6a 657
41372d5d
JG
658 /*
659 * Indirect-input devices may not be siblings of direct-input
660 * devices.
661 */
662 if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
663 (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
4451e088 664 return false;
aea2bf6a 665
41372d5d
JG
666 /* Pen devices may only be siblings of touch devices */
667 if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
668 !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
669 return false;
670
671 /* Touch devices may only be siblings of pen devices */
672 if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
673 !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
674 return false;
675
676 /*
677 * No reason could be found for these two devices to NOT be
678 * siblings, so there's a good chance they ARE siblings
679 */
680 return true;
aea2bf6a
JG
681}
682
4451e088 683static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
4492efff 684{
4451e088 685 struct wacom_hdev_data *data;
4492efff 686
41372d5d
JG
687 /* Try to find an already-probed interface from the same device */
688 list_for_each_entry(data, &wacom_udev_list, list) {
2a5e597c
JG
689 if (compare_device_paths(hdev, data->dev, '/')) {
690 kref_get(&data->kref);
41372d5d 691 return data;
2a5e597c 692 }
41372d5d
JG
693 }
694
695 /* Fallback to finding devices that appear to be "siblings" */
4492efff 696 list_for_each_entry(data, &wacom_udev_list, list) {
4451e088 697 if (wacom_are_sibling(hdev, data->dev)) {
4492efff
PC
698 kref_get(&data->kref);
699 return data;
700 }
701 }
702
703 return NULL;
704}
705
1c817c83
BT
706static void wacom_release_shared_data(struct kref *kref)
707{
708 struct wacom_hdev_data *data =
709 container_of(kref, struct wacom_hdev_data, kref);
710
711 mutex_lock(&wacom_udev_list_lock);
712 list_del(&data->list);
713 mutex_unlock(&wacom_udev_list_lock);
714
715 kfree(data);
716}
717
718static void wacom_remove_shared_data(void *res)
719{
720 struct wacom *wacom = res;
721 struct wacom_hdev_data *data;
722 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
723
724 if (wacom_wac->shared) {
725 data = container_of(wacom_wac->shared, struct wacom_hdev_data,
726 shared);
727
728 if (wacom_wac->shared->touch == wacom->hdev)
729 wacom_wac->shared->touch = NULL;
730 else if (wacom_wac->shared->pen == wacom->hdev)
731 wacom_wac->shared->pen = NULL;
732
733 kref_put(&data->kref, wacom_release_shared_data);
734 wacom_wac->shared = NULL;
735 }
736}
737
4451e088 738static int wacom_add_shared_data(struct hid_device *hdev)
4492efff 739{
4451e088
BT
740 struct wacom *wacom = hid_get_drvdata(hdev);
741 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
742 struct wacom_hdev_data *data;
4492efff
PC
743 int retval = 0;
744
745 mutex_lock(&wacom_udev_list_lock);
746
4451e088 747 data = wacom_get_hdev_data(hdev);
4492efff 748 if (!data) {
4451e088 749 data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
4492efff
PC
750 if (!data) {
751 retval = -ENOMEM;
752 goto out;
753 }
754
755 kref_init(&data->kref);
4451e088 756 data->dev = hdev;
4492efff
PC
757 list_add_tail(&data->list, &wacom_udev_list);
758 }
759
4451e088 760 wacom_wac->shared = &data->shared;
4492efff 761
1c817c83
BT
762 retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
763 if (retval) {
764 mutex_unlock(&wacom_udev_list_lock);
765 wacom_remove_shared_data(wacom);
766 return retval;
767 }
768
a9ce7856
JG
769 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
770 wacom_wac->shared->touch = hdev;
771 else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
772 wacom_wac->shared->pen = hdev;
773
4492efff
PC
774out:
775 mutex_unlock(&wacom_udev_list_lock);
776 return retval;
777}
778
5d7e7d47
EH
779static int wacom_led_control(struct wacom *wacom)
780{
781 unsigned char *buf;
9b5b95dd 782 int retval;
912ca216
PC
783 unsigned char report_id = WAC_CMD_LED_CONTROL;
784 int buf_size = 9;
5d7e7d47 785
a50aac71
BT
786 if (!wacom->led.groups)
787 return -ENOTSUPP;
788
74aebed6
AAS
789 if (wacom->wacom_wac.features.type == REMOTE)
790 return -ENOTSUPP;
791
912ca216
PC
792 if (wacom->wacom_wac.pid) { /* wireless connected */
793 report_id = WAC_CMD_WL_LED_CONTROL;
794 buf_size = 13;
795 }
4922cd26
JG
796 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
797 report_id = WAC_CMD_WL_INTUOSP2;
798 buf_size = 51;
799 }
912ca216 800 buf = kzalloc(buf_size, GFP_KERNEL);
5d7e7d47
EH
801 if (!buf)
802 return -ENOMEM;
803
10c55cac
AAS
804 if (wacom->wacom_wac.features.type == HID_GENERIC) {
805 buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
806 buf[1] = wacom->led.llv;
807 buf[2] = wacom->led.groups[0].select & 0x03;
808
809 } else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
810 wacom->wacom_wac.features.type <= INTUOSPL)) {
9b5b95dd
JG
811 /*
812 * Touch Ring and crop mark LED luminance may take on
813 * one of four values:
814 * 0 = Low; 1 = Medium; 2 = High; 3 = Off
815 */
a50aac71 816 int ring_led = wacom->led.groups[0].select & 0x03;
9b5b95dd
JG
817 int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
818 int crop_lum = 0;
912ca216
PC
819 unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
820
821 buf[0] = report_id;
822 if (wacom->wacom_wac.pid) {
823 wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
824 buf, buf_size, WAC_CMD_RETRIES);
825 buf[0] = report_id;
826 buf[4] = led_bits;
827 } else
828 buf[1] = led_bits;
9b5b95dd 829 }
4922cd26
JG
830 else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
831 buf[0] = report_id;
832 buf[4] = 100; // Power Connection LED (ORANGE)
833 buf[5] = 100; // BT Connection LED (BLUE)
834 buf[6] = 100; // Paper Mode (RED?)
835 buf[7] = 100; // Paper Mode (GREEN?)
836 buf[8] = 100; // Paper Mode (BLUE?)
837 buf[9] = wacom->led.llv;
838 buf[10] = wacom->led.groups[0].select & 0x03;
839 }
9b5b95dd 840 else {
a50aac71 841 int led = wacom->led.groups[0].select | 0x4;
9b5b95dd
JG
842
843 if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
844 wacom->wacom_wac.features.type == WACOM_24HD)
a50aac71 845 led |= (wacom->led.groups[1].select << 4) | 0x40;
9b5b95dd 846
912ca216 847 buf[0] = report_id;
9b5b95dd
JG
848 buf[1] = led;
849 buf[2] = wacom->led.llv;
850 buf[3] = wacom->led.hlv;
851 buf[4] = wacom->led.img_lum;
852 }
5d7e7d47 853
912ca216 854 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
296b7378 855 WAC_CMD_RETRIES);
5d7e7d47
EH
856 kfree(buf);
857
858 return retval;
859}
860
849e2f06
BT
861static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
862 const unsigned len, const void *img)
5d7e7d47
EH
863{
864 unsigned char *buf;
865 int i, retval;
849e2f06 866 const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
5d7e7d47 867
849e2f06 868 buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
5d7e7d47
EH
869 if (!buf)
870 return -ENOMEM;
871
872 /* Send 'start' command */
873 buf[0] = WAC_CMD_ICON_START;
874 buf[1] = 1;
296b7378
PF
875 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
876 WAC_CMD_RETRIES);
5d7e7d47
EH
877 if (retval < 0)
878 goto out;
879
849e2f06 880 buf[0] = xfer_id;
5d7e7d47
EH
881 buf[1] = button_id & 0x07;
882 for (i = 0; i < 4; i++) {
883 buf[2] = i;
849e2f06 884 memcpy(buf + 3, img + i * chunk_len, chunk_len);
5d7e7d47 885
27b20a9d 886 retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
296b7378 887 buf, chunk_len + 3, WAC_CMD_RETRIES);
5d7e7d47
EH
888 if (retval < 0)
889 break;
890 }
891
892 /* Send 'stop' */
893 buf[0] = WAC_CMD_ICON_START;
894 buf[1] = 0;
296b7378
PF
895 wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
896 WAC_CMD_RETRIES);
5d7e7d47
EH
897
898out:
899 kfree(buf);
900 return retval;
901}
902
09e7d941 903static ssize_t wacom_led_select_store(struct device *dev, int set_id,
5d7e7d47
EH
904 const char *buf, size_t count)
905{
ee79a8f8 906 struct hid_device *hdev = to_hid_device(dev);
29b47391 907 struct wacom *wacom = hid_get_drvdata(hdev);
5d7e7d47
EH
908 unsigned int id;
909 int err;
910
911 err = kstrtouint(buf, 10, &id);
912 if (err)
913 return err;
914
915 mutex_lock(&wacom->lock);
916
a50aac71 917 wacom->led.groups[set_id].select = id & 0x3;
5d7e7d47
EH
918 err = wacom_led_control(wacom);
919
920 mutex_unlock(&wacom->lock);
921
922 return err < 0 ? err : count;
923}
924
09e7d941
PC
925#define DEVICE_LED_SELECT_ATTR(SET_ID) \
926static ssize_t wacom_led##SET_ID##_select_store(struct device *dev, \
927 struct device_attribute *attr, const char *buf, size_t count) \
928{ \
929 return wacom_led_select_store(dev, SET_ID, buf, count); \
930} \
04c59abd
PC
931static ssize_t wacom_led##SET_ID##_select_show(struct device *dev, \
932 struct device_attribute *attr, char *buf) \
933{ \
ee79a8f8 934 struct hid_device *hdev = to_hid_device(dev);\
29b47391 935 struct wacom *wacom = hid_get_drvdata(hdev); \
37449adc 936 return scnprintf(buf, PAGE_SIZE, "%d\n", \
a50aac71 937 wacom->led.groups[SET_ID].select); \
04c59abd 938} \
e0984bc3 939static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM, \
04c59abd 940 wacom_led##SET_ID##_select_show, \
09e7d941
PC
941 wacom_led##SET_ID##_select_store)
942
943DEVICE_LED_SELECT_ATTR(0);
944DEVICE_LED_SELECT_ATTR(1);
5d7e7d47
EH
945
946static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
947 const char *buf, size_t count)
948{
949 unsigned int value;
950 int err;
951
952 err = kstrtouint(buf, 10, &value);
953 if (err)
954 return err;
955
956 mutex_lock(&wacom->lock);
957
958 *dest = value & 0x7f;
959 err = wacom_led_control(wacom);
960
961 mutex_unlock(&wacom->lock);
962
963 return err < 0 ? err : count;
964}
965
966#define DEVICE_LUMINANCE_ATTR(name, field) \
967static ssize_t wacom_##name##_luminance_store(struct device *dev, \
968 struct device_attribute *attr, const char *buf, size_t count) \
969{ \
ee79a8f8 970 struct hid_device *hdev = to_hid_device(dev);\
29b47391 971 struct wacom *wacom = hid_get_drvdata(hdev); \
5d7e7d47
EH
972 \
973 return wacom_luminance_store(wacom, &wacom->led.field, \
974 buf, count); \
975} \
37449adc
PC
976static ssize_t wacom_##name##_luminance_show(struct device *dev, \
977 struct device_attribute *attr, char *buf) \
978{ \
979 struct wacom *wacom = dev_get_drvdata(dev); \
980 return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field); \
981} \
e0984bc3 982static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM, \
37449adc
PC
983 wacom_##name##_luminance_show, \
984 wacom_##name##_luminance_store)
5d7e7d47
EH
985
986DEVICE_LUMINANCE_ATTR(status0, llv);
987DEVICE_LUMINANCE_ATTR(status1, hlv);
988DEVICE_LUMINANCE_ATTR(buttons, img_lum);
989
990static ssize_t wacom_button_image_store(struct device *dev, int button_id,
991 const char *buf, size_t count)
992{
ee79a8f8 993 struct hid_device *hdev = to_hid_device(dev);
29b47391 994 struct wacom *wacom = hid_get_drvdata(hdev);
5d7e7d47 995 int err;
849e2f06
BT
996 unsigned len;
997 u8 xfer_id;
998
999 if (hdev->bus == BUS_BLUETOOTH) {
1000 len = 256;
1001 xfer_id = WAC_CMD_ICON_BT_XFER;
1002 } else {
1003 len = 1024;
1004 xfer_id = WAC_CMD_ICON_XFER;
1005 }
5d7e7d47 1006
849e2f06 1007 if (count != len)
5d7e7d47
EH
1008 return -EINVAL;
1009
1010 mutex_lock(&wacom->lock);
1011
849e2f06 1012 err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
5d7e7d47
EH
1013
1014 mutex_unlock(&wacom->lock);
1015
1016 return err < 0 ? err : count;
1017}
1018
1019#define DEVICE_BTNIMG_ATTR(BUTTON_ID) \
1020static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev, \
1021 struct device_attribute *attr, const char *buf, size_t count) \
1022{ \
1023 return wacom_button_image_store(dev, BUTTON_ID, buf, count); \
1024} \
e0984bc3 1025static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM, \
5d7e7d47
EH
1026 NULL, wacom_btnimg##BUTTON_ID##_store)
1027
1028DEVICE_BTNIMG_ATTR(0);
1029DEVICE_BTNIMG_ATTR(1);
1030DEVICE_BTNIMG_ATTR(2);
1031DEVICE_BTNIMG_ATTR(3);
1032DEVICE_BTNIMG_ATTR(4);
1033DEVICE_BTNIMG_ATTR(5);
1034DEVICE_BTNIMG_ATTR(6);
1035DEVICE_BTNIMG_ATTR(7);
1036
09e7d941
PC
1037static struct attribute *cintiq_led_attrs[] = {
1038 &dev_attr_status_led0_select.attr,
1039 &dev_attr_status_led1_select.attr,
1040 NULL
1041};
1042
1043static struct attribute_group cintiq_led_attr_group = {
1044 .name = "wacom_led",
1045 .attrs = cintiq_led_attrs,
1046};
1047
1048static struct attribute *intuos4_led_attrs[] = {
5d7e7d47
EH
1049 &dev_attr_status0_luminance.attr,
1050 &dev_attr_status1_luminance.attr,
09e7d941 1051 &dev_attr_status_led0_select.attr,
5d7e7d47
EH
1052 &dev_attr_buttons_luminance.attr,
1053 &dev_attr_button0_rawimg.attr,
1054 &dev_attr_button1_rawimg.attr,
1055 &dev_attr_button2_rawimg.attr,
1056 &dev_attr_button3_rawimg.attr,
1057 &dev_attr_button4_rawimg.attr,
1058 &dev_attr_button5_rawimg.attr,
1059 &dev_attr_button6_rawimg.attr,
1060 &dev_attr_button7_rawimg.attr,
1061 NULL
1062};
1063
09e7d941 1064static struct attribute_group intuos4_led_attr_group = {
5d7e7d47 1065 .name = "wacom_led",
09e7d941 1066 .attrs = intuos4_led_attrs,
5d7e7d47
EH
1067};
1068
9b5b95dd
JG
1069static struct attribute *intuos5_led_attrs[] = {
1070 &dev_attr_status0_luminance.attr,
1071 &dev_attr_status_led0_select.attr,
1072 NULL
1073};
1074
1075static struct attribute_group intuos5_led_attr_group = {
1076 .name = "wacom_led",
1077 .attrs = intuos5_led_attrs,
1078};
1079
10c55cac
AAS
1080static struct attribute *generic_led_attrs[] = {
1081 &dev_attr_status0_luminance.attr,
1082 &dev_attr_status_led0_select.attr,
1083 NULL
1084};
1085
1086static struct attribute_group generic_led_attr_group = {
1087 .name = "wacom_led",
1088 .attrs = generic_led_attrs,
1089};
1090
2df68a88
BT
1091struct wacom_sysfs_group_devres {
1092 struct attribute_group *group;
1093 struct kobject *root;
1094};
1095
1096static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
1097{
1098 struct wacom_sysfs_group_devres *devres = res;
1099 struct kobject *kobj = devres->root;
1100
1101 dev_dbg(dev, "%s: dropping reference to %s\n",
1102 __func__, devres->group->name);
1103 sysfs_remove_group(kobj, devres->group);
1104}
1105
f9036bd4
BT
1106static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1107 struct kobject *root,
1108 struct attribute_group *group)
2df68a88
BT
1109{
1110 struct wacom_sysfs_group_devres *devres;
1111 int error;
1112
1113 devres = devres_alloc(wacom_devm_sysfs_group_release,
1114 sizeof(struct wacom_sysfs_group_devres),
1115 GFP_KERNEL);
1116 if (!devres)
1117 return -ENOMEM;
1118
1119 devres->group = group;
f9036bd4 1120 devres->root = root;
2df68a88
BT
1121
1122 error = sysfs_create_group(devres->root, group);
bf4edfcd
AY
1123 if (error) {
1124 devres_free(devres);
2df68a88 1125 return error;
bf4edfcd 1126 }
2df68a88
BT
1127
1128 devres_add(&wacom->hdev->dev, devres);
1129
1130 return 0;
1131}
1132
f9036bd4
BT
1133static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1134 struct attribute_group *group)
1135{
1136 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1137 group);
1138}
1139
34736aa9 1140enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
97f5541f
BT
1141{
1142 struct wacom *wacom = led->wacom;
1143
1144 if (wacom->led.max_hlv)
1145 return led->hlv * LED_FULL / wacom->led.max_hlv;
1146
1147 if (wacom->led.max_llv)
1148 return led->llv * LED_FULL / wacom->led.max_llv;
1149
1150 /* device doesn't support brightness tuning */
1151 return LED_FULL;
1152}
1153
1154static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1155{
1156 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1157 struct wacom *wacom = led->wacom;
1158
1159 if (wacom->led.groups[led->group].select != led->id)
1160 return LED_OFF;
1161
1162 return wacom_leds_brightness_get(led);
1163}
1164
1165static int wacom_led_brightness_set(struct led_classdev *cdev,
1166 enum led_brightness brightness)
1167{
1168 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1169 struct wacom *wacom = led->wacom;
1170 int error;
1171
1172 mutex_lock(&wacom->lock);
1173
1174 if (!wacom->led.groups || (brightness == LED_OFF &&
1175 wacom->led.groups[led->group].select != led->id)) {
1176 error = 0;
1177 goto out;
1178 }
1179
1180 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1181 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1182
1183 wacom->led.groups[led->group].select = led->id;
1184
1185 error = wacom_led_control(wacom);
1186
1187out:
1188 mutex_unlock(&wacom->lock);
1189
1190 return error;
1191}
1192
1193static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1194 enum led_brightness brightness)
1195{
1196}
1197
1198static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1199 struct wacom_led *led, unsigned int group,
1200 unsigned int id, bool read_only)
1201{
1202 int error;
1203 char *name;
1204
1205 name = devm_kasprintf(dev, GFP_KERNEL,
1206 "%s::wacom-%d.%d",
1207 dev_name(dev),
1208 group,
1209 id);
1210 if (!name)
1211 return -ENOMEM;
1212
34736aa9
BT
1213 if (!read_only) {
1214 led->trigger.name = name;
1215 error = devm_led_trigger_register(dev, &led->trigger);
1216 if (error) {
1217 hid_err(wacom->hdev,
1218 "failed to register LED trigger %s: %d\n",
1219 led->cdev.name, error);
1220 return error;
1221 }
1222 }
1223
97f5541f
BT
1224 led->group = group;
1225 led->id = id;
1226 led->wacom = wacom;
1227 led->llv = wacom->led.llv;
1228 led->hlv = wacom->led.hlv;
1229 led->cdev.name = name;
1230 led->cdev.max_brightness = LED_FULL;
1231 led->cdev.flags = LED_HW_PLUGGABLE;
1232 led->cdev.brightness_get = __wacom_led_brightness_get;
34736aa9 1233 if (!read_only) {
97f5541f 1234 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
34736aa9
BT
1235 led->cdev.default_trigger = led->cdev.name;
1236 } else {
97f5541f 1237 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
34736aa9 1238 }
97f5541f
BT
1239
1240 error = devm_led_classdev_register(dev, &led->cdev);
1241 if (error) {
1242 hid_err(wacom->hdev,
1243 "failed to register LED %s: %d\n",
1244 led->cdev.name, error);
1245 led->cdev.name = NULL;
1246 return error;
1247 }
1248
1249 return 0;
1250}
1251
589e5060
BT
1252static void wacom_led_groups_release_one(void *data)
1253{
1254 struct wacom_group_leds *group = data;
1255
1256 devres_release_group(group->dev, group);
1257}
1258
97f5541f
BT
1259static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1260 struct wacom *wacom,
1261 int group_id, int count,
1262 bool read_only)
1263{
1264 struct wacom_led *leds;
1265 int i, error;
1266
1267 if (group_id >= wacom->led.count || count <= 0)
1268 return -EINVAL;
1269
1270 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1271 return -ENOMEM;
1272
1273 leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
1274 if (!leds) {
1275 error = -ENOMEM;
1276 goto err;
1277 }
1278
1279 wacom->led.groups[group_id].leds = leds;
1280 wacom->led.groups[group_id].count = count;
1281
1282 for (i = 0; i < count; i++) {
1283 error = wacom_led_register_one(dev, wacom, &leds[i],
1284 group_id, i, read_only);
1285 if (error)
1286 goto err;
1287 }
1288
589e5060
BT
1289 wacom->led.groups[group_id].dev = dev;
1290
1291 devres_close_group(dev, &wacom->led.groups[group_id]);
1292
1293 /*
1294 * There is a bug (?) in devm_led_classdev_register() in which its
1295 * increments the refcount of the parent. If the parent is an input
1296 * device, that means the ref count never reaches 0 when
1297 * devm_input_device_release() gets called.
1298 * This means that the LEDs are still there after disconnect.
1299 * Manually force the release of the group so that the leds are released
1300 * once we are done using them.
1301 */
1302 error = devm_add_action_or_reset(&wacom->hdev->dev,
1303 wacom_led_groups_release_one,
1304 &wacom->led.groups[group_id]);
1305 if (error)
1306 return error;
1307
97f5541f
BT
1308 return 0;
1309
1310err:
1311 devres_release_group(dev, &wacom->led.groups[group_id]);
1312 return error;
1313}
1314
34736aa9
BT
1315struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1316 unsigned int id)
1317{
1318 struct wacom_group_leds *group;
1319
1320 if (group_id >= wacom->led.count)
1321 return NULL;
1322
1323 group = &wacom->led.groups[group_id];
1324
1325 if (!group->leds)
1326 return NULL;
1327
1328 id %= group->count;
1329
1330 return &group->leds[id];
1331}
1332
1333/**
1334 * wacom_led_next: gives the next available led with a wacom trigger.
1335 *
1336 * returns the next available struct wacom_led which has its default trigger
1337 * or the current one if none is available.
1338 */
1339struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1340{
1341 struct wacom_led *next_led;
1342 int group, next;
1343
1344 if (!wacom || !cur)
1345 return NULL;
1346
1347 group = cur->group;
1348 next = cur->id;
1349
1350 do {
1351 next_led = wacom_led_find(wacom, group, ++next);
1352 if (!next_led || next_led == cur)
1353 return next_led;
1354 } while (next_led->cdev.trigger != &next_led->trigger);
1355
1356 return next_led;
1357}
1358
a50aac71
BT
1359static void wacom_led_groups_release(void *data)
1360{
1361 struct wacom *wacom = data;
1362
1363 wacom->led.groups = NULL;
97f5541f 1364 wacom->led.count = 0;
a50aac71
BT
1365}
1366
1367static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1368{
97f5541f 1369 struct device *dev = &wacom->hdev->dev;
a50aac71
BT
1370 struct wacom_group_leds *groups;
1371 int error;
1372
97f5541f 1373 groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
a50aac71
BT
1374 GFP_KERNEL);
1375 if (!groups)
1376 return -ENOMEM;
1377
97f5541f 1378 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
a50aac71
BT
1379 if (error)
1380 return error;
1381
1382 wacom->led.groups = groups;
97f5541f
BT
1383 wacom->led.count = count;
1384
1385 return 0;
1386}
1387
1388static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1389 int led_per_group, bool read_only)
1390{
1391 struct device *dev;
1392 int i, error;
1393
1394 if (!wacom->wacom_wac.pad_input)
1395 return -EINVAL;
1396
1397 dev = &wacom->wacom_wac.pad_input->dev;
1398
1399 error = wacom_led_groups_allocate(wacom, group_count);
1400 if (error)
1401 return error;
1402
1403 for (i = 0; i < group_count; i++) {
1404 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1405 led_per_group,
1406 read_only);
1407 if (error)
1408 return error;
1409 }
a50aac71
BT
1410
1411 return 0;
1412}
1413
10c55cac 1414int wacom_initialize_leds(struct wacom *wacom)
5d7e7d47
EH
1415{
1416 int error;
1417
862cf553
JG
1418 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1419 return 0;
1420
09e7d941
PC
1421 /* Initialize default values */
1422 switch (wacom->wacom_wac.features.type) {
10c55cac
AAS
1423 case HID_GENERIC:
1424 if (!wacom->generic_has_leds)
1425 return 0;
1426 wacom->led.llv = 100;
1427 wacom->led.max_llv = 100;
1428
1429 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1430 if (error) {
1431 hid_err(wacom->hdev,
1432 "cannot create leds err: %d\n", error);
1433 return error;
1434 }
1435
1436 error = wacom_devm_sysfs_create_group(wacom,
1437 &generic_led_attr_group);
1438 break;
1439
a19fc986 1440 case INTUOS4S:
09e7d941 1441 case INTUOS4:
81af7e61 1442 case INTUOS4WL:
09e7d941 1443 case INTUOS4L:
f4fa9a6d 1444 wacom->led.llv = 10;
5d7e7d47 1445 wacom->led.hlv = 20;
97f5541f
BT
1446 wacom->led.max_llv = 127;
1447 wacom->led.max_hlv = 127;
5d7e7d47 1448 wacom->led.img_lum = 10;
a50aac71 1449
97f5541f 1450 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
a50aac71
BT
1451 if (error) {
1452 hid_err(wacom->hdev,
1453 "cannot create leds err: %d\n", error);
1454 return error;
1455 }
1456
2df68a88
BT
1457 error = wacom_devm_sysfs_create_group(wacom,
1458 &intuos4_led_attr_group);
09e7d941
PC
1459 break;
1460
246835fc 1461 case WACOM_24HD:
09e7d941 1462 case WACOM_21UX2:
09e7d941
PC
1463 wacom->led.llv = 0;
1464 wacom->led.hlv = 0;
1465 wacom->led.img_lum = 0;
5d7e7d47 1466
97f5541f 1467 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
a50aac71
BT
1468 if (error) {
1469 hid_err(wacom->hdev,
1470 "cannot create leds err: %d\n", error);
1471 return error;
1472 }
1473
2df68a88
BT
1474 error = wacom_devm_sysfs_create_group(wacom,
1475 &cintiq_led_attr_group);
09e7d941
PC
1476 break;
1477
9b5b95dd
JG
1478 case INTUOS5S:
1479 case INTUOS5:
1480 case INTUOS5L:
9a35c411
PC
1481 case INTUOSPS:
1482 case INTUOSPM:
1483 case INTUOSPL:
862cf553 1484 wacom->led.llv = 32;
97f5541f 1485 wacom->led.max_llv = 96;
862cf553 1486
97f5541f 1487 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
a50aac71
BT
1488 if (error) {
1489 hid_err(wacom->hdev,
1490 "cannot create leds err: %d\n", error);
1491 return error;
1492 }
1493
2df68a88
BT
1494 error = wacom_devm_sysfs_create_group(wacom,
1495 &intuos5_led_attr_group);
9b5b95dd
JG
1496 break;
1497
4922cd26
JG
1498 case INTUOSP2_BT:
1499 wacom->led.llv = 50;
1500 wacom->led.max_llv = 100;
1501 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1502 if (error) {
1503 hid_err(wacom->hdev,
1504 "cannot create leds err: %d\n", error);
1505 return error;
1506 }
1507 return 0;
1508
a50aac71 1509 case REMOTE:
97f5541f
BT
1510 wacom->led.llv = 255;
1511 wacom->led.max_llv = 255;
a50aac71
BT
1512 error = wacom_led_groups_allocate(wacom, 5);
1513 if (error) {
1514 hid_err(wacom->hdev,
1515 "cannot create leds err: %d\n", error);
1516 return error;
1517 }
1518 return 0;
1519
09e7d941
PC
1520 default:
1521 return 0;
5d7e7d47
EH
1522 }
1523
09e7d941 1524 if (error) {
c31a408f 1525 hid_err(wacom->hdev,
09e7d941
PC
1526 "cannot create sysfs group err: %d\n", error);
1527 return error;
1528 }
09e7d941 1529
5d7e7d47
EH
1530 return 0;
1531}
1532
a544c619
BT
1533static void wacom_init_work(struct work_struct *work)
1534{
1535 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1536
1537 _wacom_query_tablet_data(wacom);
1538 wacom_led_control(wacom);
1539}
1540
1541static void wacom_query_tablet_data(struct wacom *wacom)
1542{
1543 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1544}
1545
a1d552cc 1546static enum power_supply_property wacom_battery_props[] = {
9956953e 1547 POWER_SUPPLY_PROP_MODEL_NAME,
71fa641e 1548 POWER_SUPPLY_PROP_PRESENT,
ac8d1010 1549 POWER_SUPPLY_PROP_STATUS,
6e2a6e80 1550 POWER_SUPPLY_PROP_SCOPE,
a1d552cc
CB
1551 POWER_SUPPLY_PROP_CAPACITY
1552};
1553
1554static int wacom_battery_get_property(struct power_supply *psy,
1555 enum power_supply_property psp,
1556 union power_supply_propval *val)
1557{
59d69bc8 1558 struct wacom_battery *battery = power_supply_get_drvdata(psy);
a1d552cc
CB
1559 int ret = 0;
1560
1561 switch (psp) {
9956953e
BT
1562 case POWER_SUPPLY_PROP_MODEL_NAME:
1563 val->strval = battery->wacom->wacom_wac.name;
1564 break;
71fa641e 1565 case POWER_SUPPLY_PROP_PRESENT:
59d69bc8 1566 val->intval = battery->bat_connected;
71fa641e 1567 break;
6e2a6e80
BN
1568 case POWER_SUPPLY_PROP_SCOPE:
1569 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1570 break;
a1d552cc 1571 case POWER_SUPPLY_PROP_CAPACITY:
59d69bc8 1572 val->intval = battery->battery_capacity;
ac8d1010
BT
1573 break;
1574 case POWER_SUPPLY_PROP_STATUS:
16e45989
JG
1575 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1576 val->intval = battery->bat_status;
1577 else if (battery->bat_charging)
ac8d1010 1578 val->intval = POWER_SUPPLY_STATUS_CHARGING;
59d69bc8
BT
1579 else if (battery->battery_capacity == 100 &&
1580 battery->ps_connected)
ac8d1010 1581 val->intval = POWER_SUPPLY_STATUS_FULL;
59d69bc8 1582 else if (battery->ps_connected)
b0882cb7 1583 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
ac8d1010
BT
1584 else
1585 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
a1d552cc
CB
1586 break;
1587 default:
1588 ret = -EINVAL;
1589 break;
1590 }
1591
1592 return ret;
1593}
1594
59d69bc8
BT
1595static int __wacom_initialize_battery(struct wacom *wacom,
1596 struct wacom_battery *battery)
a1d552cc 1597{
d70420b9 1598 static atomic_t battery_no = ATOMIC_INIT(0);
b189da90 1599 struct device *dev = &wacom->hdev->dev;
59d69bc8 1600 struct power_supply_config psy_cfg = { .drv_data = battery, };
136ae5e9 1601 struct power_supply *ps_bat;
59d69bc8 1602 struct power_supply_desc *bat_desc = &battery->bat_desc;
d70420b9 1603 unsigned long n;
b189da90
BT
1604 int error;
1605
1606 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1607 return -ENOMEM;
a1d552cc 1608
9956953e
BT
1609 battery->wacom = wacom;
1610
59d69bc8
BT
1611 n = atomic_inc_return(&battery_no) - 1;
1612
1613 bat_desc->properties = wacom_battery_props;
1614 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1615 bat_desc->get_property = wacom_battery_get_property;
1616 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1617 bat_desc->name = battery->bat_name;
96983296 1618 bat_desc->type = POWER_SUPPLY_TYPE_USB;
59d69bc8
BT
1619 bat_desc->use_for_apm = 0;
1620
59d69bc8
BT
1621 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1622 if (IS_ERR(ps_bat)) {
1623 error = PTR_ERR(ps_bat);
1624 goto err;
1625 }
297d716f 1626
59d69bc8 1627 power_supply_powers(ps_bat, &wacom->hdev->dev);
7dbd229e 1628
59d69bc8 1629 battery->battery = ps_bat;
a1d552cc 1630
b189da90 1631 devres_close_group(dev, bat_desc);
7dbd229e 1632 return 0;
b189da90
BT
1633
1634err:
1635 devres_release_group(dev, bat_desc);
1636 return error;
a1d552cc
CB
1637}
1638
59d69bc8
BT
1639static int wacom_initialize_battery(struct wacom *wacom)
1640{
1641 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1642 return __wacom_initialize_battery(wacom, &wacom->battery);
1643
1644 return 0;
1645}
1646
a1d552cc
CB
1647static void wacom_destroy_battery(struct wacom *wacom)
1648{
59d69bc8
BT
1649 if (wacom->battery.battery) {
1650 devres_release_group(&wacom->hdev->dev,
1651 &wacom->battery.bat_desc);
1652 wacom->battery.battery = NULL;
b7af2bb8 1653 }
a1d552cc
CB
1654}
1655
f81a1295
BT
1656static ssize_t wacom_show_speed(struct device *dev,
1657 struct device_attribute
1658 *attr, char *buf)
1659{
ee79a8f8 1660 struct hid_device *hdev = to_hid_device(dev);
f81a1295
BT
1661 struct wacom *wacom = hid_get_drvdata(hdev);
1662
1663 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1664}
1665
1666static ssize_t wacom_store_speed(struct device *dev,
1667 struct device_attribute *attr,
1668 const char *buf, size_t count)
1669{
ee79a8f8 1670 struct hid_device *hdev = to_hid_device(dev);
f81a1295
BT
1671 struct wacom *wacom = hid_get_drvdata(hdev);
1672 u8 new_speed;
1673
1674 if (kstrtou8(buf, 0, &new_speed))
1675 return -EINVAL;
1676
1677 if (new_speed != 0 && new_speed != 1)
1678 return -EINVAL;
1679
1680 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1681
1682 return count;
1683}
1684
e0984bc3 1685static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
f81a1295
BT
1686 wacom_show_speed, wacom_store_speed);
1687
72b236d6
AS
1688
1689static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1690 struct kobj_attribute *kattr,
1691 char *buf, int index)
1692{
2cf83833 1693 struct device *dev = kobj_to_dev(kobj->parent);
ee79a8f8 1694 struct hid_device *hdev = to_hid_device(dev);
72b236d6
AS
1695 struct wacom *wacom = hid_get_drvdata(hdev);
1696 u8 mode;
1697
a50aac71 1698 mode = wacom->led.groups[index].select;
bc35f73a 1699 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
72b236d6
AS
1700}
1701
1702#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1703static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1704 struct kobj_attribute *kattr, char *buf) \
1705{ \
1706 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1707} \
1708static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1709 .attr = {.name = "remote_mode", \
1710 .mode = DEV_ATTR_RO_PERM}, \
1711 .show = wacom_show_remote##SET_ID##_mode, \
1712}; \
1713static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1714 &remote##SET_ID##_mode_attr.attr, \
1715 NULL \
1716}; \
1717static struct attribute_group remote##SET_ID##_serial_group = { \
1718 .name = NULL, \
1719 .attrs = remote##SET_ID##_serial_attrs, \
1720}
1721
1722DEVICE_EKR_ATTR_GROUP(0);
1723DEVICE_EKR_ATTR_GROUP(1);
1724DEVICE_EKR_ATTR_GROUP(2);
1725DEVICE_EKR_ATTR_GROUP(3);
1726DEVICE_EKR_ATTR_GROUP(4);
1727
e6f2813a
BT
1728static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1729 int index)
72b236d6
AS
1730{
1731 int error = 0;
83e6b40e 1732 struct wacom_remote *remote = wacom->remote;
72b236d6 1733
e7749f6e 1734 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
83e6b40e
BT
1735 GFP_KERNEL,
1736 "%d", serial);
e7749f6e 1737 if (!remote->remotes[index].group.name)
72b236d6 1738 return -ENOMEM;
72b236d6 1739
f9036bd4 1740 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
e7749f6e 1741 &remote->remotes[index].group);
72b236d6 1742 if (error) {
e7749f6e 1743 remote->remotes[index].group.name = NULL;
72b236d6
AS
1744 hid_err(wacom->hdev,
1745 "cannot create sysfs group err: %d\n", error);
72b236d6
AS
1746 return error;
1747 }
1748
1749 return 0;
1750}
1751
72b236d6
AS
1752static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1753{
1754 const size_t buf_size = 2;
1755 unsigned char *buf;
1756 int retval;
1757
1758 buf = kzalloc(buf_size, GFP_KERNEL);
1759 if (!buf)
1760 return -ENOMEM;
1761
1762 buf[0] = WAC_CMD_DELETE_PAIRING;
1763 buf[1] = selector;
1764
1765 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1766 buf_size, WAC_CMD_RETRIES);
1767 kfree(buf);
1768
1769 return retval;
1770}
1771
1772static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1773 struct kobj_attribute *attr,
1774 const char *buf, size_t count)
1775{
1776 unsigned char selector = 0;
2cf83833 1777 struct device *dev = kobj_to_dev(kobj->parent);
ee79a8f8 1778 struct hid_device *hdev = to_hid_device(dev);
72b236d6
AS
1779 struct wacom *wacom = hid_get_drvdata(hdev);
1780 int err;
1781
1782 if (!strncmp(buf, "*\n", 2)) {
1783 selector = WAC_CMD_UNPAIR_ALL;
1784 } else {
1785 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1786 buf);
1787 return -1;
1788 }
1789
1790 mutex_lock(&wacom->lock);
1791
1792 err = wacom_cmd_unpair_remote(wacom, selector);
1793 mutex_unlock(&wacom->lock);
1794
1795 return err < 0 ? err : count;
1796}
1797
1798static struct kobj_attribute unpair_remote_attr = {
1799 .attr = {.name = "unpair_remote", .mode = 0200},
1800 .store = wacom_store_unpair_remote,
1801};
1802
1803static const struct attribute *remote_unpair_attrs[] = {
1804 &unpair_remote_attr.attr,
1805 NULL
1806};
1807
83e6b40e
BT
1808static void wacom_remotes_destroy(void *data)
1809{
1810 struct wacom *wacom = data;
1811 struct wacom_remote *remote = wacom->remote;
1812
1813 if (!remote)
1814 return;
1815
1816 kobject_put(remote->remote_dir);
1817 kfifo_free(&remote->remote_fifo);
1818 wacom->remote = NULL;
1819}
1820
1821static int wacom_initialize_remotes(struct wacom *wacom)
72b236d6
AS
1822{
1823 int error = 0;
83e6b40e 1824 struct wacom_remote *remote;
72b236d6
AS
1825 int i;
1826
1827 if (wacom->wacom_wac.features.type != REMOTE)
1828 return 0;
1829
83e6b40e
BT
1830 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1831 GFP_KERNEL);
1832 if (!remote)
1833 return -ENOMEM;
72b236d6 1834
83e6b40e
BT
1835 wacom->remote = remote;
1836
1837 spin_lock_init(&remote->remote_lock);
1838
1839 error = kfifo_alloc(&remote->remote_fifo,
1840 5 * sizeof(struct wacom_remote_data),
1841 GFP_KERNEL);
1842 if (error) {
1843 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
72b236d6 1844 return -ENOMEM;
83e6b40e 1845 }
72b236d6 1846
e7749f6e
BT
1847 remote->remotes[0].group = remote0_serial_group;
1848 remote->remotes[1].group = remote1_serial_group;
1849 remote->remotes[2].group = remote2_serial_group;
1850 remote->remotes[3].group = remote3_serial_group;
1851 remote->remotes[4].group = remote4_serial_group;
83e6b40e
BT
1852
1853 remote->remote_dir = kobject_create_and_add("wacom_remote",
1854 &wacom->hdev->dev.kobj);
1855 if (!remote->remote_dir)
1856 return -ENOMEM;
1857
1858 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
72b236d6
AS
1859
1860 if (error) {
1861 hid_err(wacom->hdev,
1862 "cannot create sysfs group err: %d\n", error);
1863 return error;
1864 }
1865
1866 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
a50aac71 1867 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
e7749f6e 1868 remote->remotes[i].serial = 0;
72b236d6
AS
1869 }
1870
83e6b40e
BT
1871 error = devm_add_action_or_reset(&wacom->hdev->dev,
1872 wacom_remotes_destroy, wacom);
1873 if (error)
1874 return error;
1875
72b236d6
AS
1876 return 0;
1877}
1878
d2d13f18 1879static struct input_dev *wacom_allocate_input(struct wacom *wacom)
3aac0ef1
CB
1880{
1881 struct input_dev *input_dev;
b6c79f2c 1882 struct hid_device *hdev = wacom->hdev;
3aac0ef1 1883 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
3aac0ef1 1884
3dad188e 1885 input_dev = devm_input_allocate_device(&hdev->dev);
d2d13f18
BT
1886 if (!input_dev)
1887 return NULL;
3aac0ef1 1888
2bdd163c 1889 input_dev->name = wacom_wac->features.name;
b6c79f2c
BT
1890 input_dev->phys = hdev->phys;
1891 input_dev->dev.parent = &hdev->dev;
3aac0ef1
CB
1892 input_dev->open = wacom_open;
1893 input_dev->close = wacom_close;
b6c79f2c
BT
1894 input_dev->uniq = hdev->uniq;
1895 input_dev->id.bustype = hdev->bus;
1896 input_dev->id.vendor = hdev->vendor;
12969e3b 1897 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
b6c79f2c 1898 input_dev->id.version = hdev->version;
3aac0ef1
CB
1899 input_set_drvdata(input_dev, wacom);
1900
d2d13f18
BT
1901 return input_dev;
1902}
1903
d9f2d203
JG
1904static int wacom_allocate_inputs(struct wacom *wacom)
1905{
1906 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1907
1908 wacom_wac->pen_input = wacom_allocate_input(wacom);
1909 wacom_wac->touch_input = wacom_allocate_input(wacom);
1910 wacom_wac->pad_input = wacom_allocate_input(wacom);
3dad188e
BT
1911 if (!wacom_wac->pen_input ||
1912 !wacom_wac->touch_input ||
1913 !wacom_wac->pad_input)
d9f2d203 1914 return -ENOMEM;
d9f2d203 1915
2bdd163c 1916 wacom_wac->pen_input->name = wacom_wac->pen_name;
d9f2d203
JG
1917 wacom_wac->touch_input->name = wacom_wac->touch_name;
1918 wacom_wac->pad_input->name = wacom_wac->pad_name;
1919
1920 return 0;
1921}
1922
2546dacd
BT
1923static int wacom_register_inputs(struct wacom *wacom)
1924{
2a6cdbdd 1925 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2546dacd 1926 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2636a3f2 1927 int error = 0;
2546dacd 1928
2a6cdbdd
JG
1929 pen_input_dev = wacom_wac->pen_input;
1930 touch_input_dev = wacom_wac->touch_input;
2546dacd
BT
1931 pad_input_dev = wacom_wac->pad_input;
1932
2a6cdbdd 1933 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
2546dacd
BT
1934 return -EINVAL;
1935
2a6cdbdd
JG
1936 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
1937 if (error) {
1938 /* no pen in use on this interface */
1939 input_free_device(pen_input_dev);
1940 wacom_wac->pen_input = NULL;
1941 pen_input_dev = NULL;
1942 } else {
1943 error = input_register_device(pen_input_dev);
1944 if (error)
3dad188e 1945 goto fail;
2a6cdbdd
JG
1946 }
1947
1948 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
1949 if (error) {
1950 /* no touch in use on this interface */
1951 input_free_device(touch_input_dev);
1952 wacom_wac->touch_input = NULL;
1953 touch_input_dev = NULL;
1954 } else {
1955 error = input_register_device(touch_input_dev);
30ebc1ae 1956 if (error)
3dad188e 1957 goto fail;
30ebc1ae 1958 }
1963518b 1959
d2d13f18
BT
1960 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1961 if (error) {
1962 /* no pad in use on this interface */
1963 input_free_device(pad_input_dev);
1964 wacom_wac->pad_input = NULL;
1965 pad_input_dev = NULL;
1966 } else {
1967 error = input_register_device(pad_input_dev);
1968 if (error)
3dad188e 1969 goto fail;
d2d13f18
BT
1970 }
1971
1963518b 1972 return 0;
3aac0ef1 1973
3dad188e
BT
1974fail:
1975 wacom_wac->pad_input = NULL;
2a6cdbdd 1976 wacom_wac->touch_input = NULL;
2a6cdbdd 1977 wacom_wac->pen_input = NULL;
3aac0ef1
CB
1978 return error;
1979}
1980
0be01712
JG
1981/*
1982 * Not all devices report physical dimensions from HID.
1983 * Compute the default from hardcoded logical dimension
1984 * and resolution before driver overwrites them.
1985 */
1986static void wacom_set_default_phy(struct wacom_features *features)
1987{
1988 if (features->x_resolution) {
1989 features->x_phy = (features->x_max * 100) /
1990 features->x_resolution;
1991 features->y_phy = (features->y_max * 100) /
1992 features->y_resolution;
1993 }
1994}
1995
1996static void wacom_calculate_res(struct wacom_features *features)
1997{
1998 /* set unit to "100th of a mm" for devices not reported by HID */
1999 if (!features->unit) {
2000 features->unit = 0x11;
2001 features->unitExpo = -3;
2002 }
2003
2004 features->x_resolution = wacom_calc_hid_res(features->x_max,
2005 features->x_phy,
2006 features->unit,
2007 features->unitExpo);
2008 features->y_resolution = wacom_calc_hid_res(features->y_max,
2009 features->y_phy,
2010 features->unit,
2011 features->unitExpo);
2012}
2013
fce9957d
JG
2014void wacom_battery_work(struct work_struct *work)
2015{
d17d1f17 2016 struct wacom *wacom = container_of(work, struct wacom, battery_work);
fce9957d
JG
2017
2018 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
59d69bc8 2019 !wacom->battery.battery) {
fce9957d
JG
2020 wacom_initialize_battery(wacom);
2021 }
2022 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
59d69bc8 2023 wacom->battery.battery) {
fce9957d
JG
2024 wacom_destroy_battery(wacom);
2025 }
2026}
2027
01c846f9
BT
2028static size_t wacom_compute_pktlen(struct hid_device *hdev)
2029{
2030 struct hid_report_enum *report_enum;
2031 struct hid_report *report;
2032 size_t size = 0;
2033
2034 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2035
2036 list_for_each_entry(report, &report_enum->report_list, list) {
dabb05c6 2037 size_t report_size = hid_report_len(report);
01c846f9
BT
2038 if (report_size > size)
2039 size = report_size;
2040 }
2041
2042 return size;
2043}
2044
fd5f92b6 2045static void wacom_update_name(struct wacom *wacom, const char *suffix)
c24eab4e
PC
2046{
2047 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2048 struct wacom_features *features = &wacom_wac->features;
44b5250b 2049 char name[WACOM_NAME_MAX];
c24eab4e
PC
2050
2051 /* Generic devices name unspecified */
2052 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
09dc28ac 2053 char *product_name = wacom->hdev->name;
f2209d4a 2054
09dc28ac
JG
2055 if (hid_is_using_ll_driver(wacom->hdev, &usb_hid_driver)) {
2056 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2057 struct usb_device *dev = interface_to_usbdev(intf);
2058 product_name = dev->product;
2059 }
f2209d4a 2060
09dc28ac
JG
2061 if (wacom->hdev->bus == BUS_I2C) {
2062 snprintf(name, sizeof(name), "%s %X",
2063 features->name, wacom->hdev->product);
2064 } else if (strstr(product_name, "Wacom") ||
2065 strstr(product_name, "wacom") ||
2066 strstr(product_name, "WACOM")) {
2067 strlcpy(name, product_name, sizeof(name));
c24eab4e 2068 } else {
09dc28ac 2069 snprintf(name, sizeof(name), "Wacom %s", product_name);
c24eab4e 2070 }
09dc28ac
JG
2071
2072 /* strip out excess whitespaces */
2073 while (1) {
2074 char *gap = strstr(name, " ");
2075 if (gap == NULL)
2076 break;
2077 /* shift everything including the terminator */
2078 memmove(gap, gap+1, strlen(gap));
2079 }
2080
2081 /* get rid of trailing whitespace */
2082 if (name[strlen(name)-1] == ' ')
2083 name[strlen(name)-1] = '\0';
c24eab4e 2084 } else {
44b5250b 2085 strlcpy(name, features->name, sizeof(name));
c24eab4e
PC
2086 }
2087
9956953e
BT
2088 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2089 name, suffix);
2090
c24eab4e 2091 /* Append the device type to the name */
2a6cdbdd 2092 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
fd5f92b6 2093 "%s%s Pen", name, suffix);
2a6cdbdd 2094 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
fd5f92b6 2095 "%s%s Finger", name, suffix);
c24eab4e 2096 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
fd5f92b6 2097 "%s%s Pad", name, suffix);
c24eab4e
PC
2098}
2099
84dfbd7f
BT
2100static void wacom_release_resources(struct wacom *wacom)
2101{
2102 struct hid_device *hdev = wacom->hdev;
2103
2104 if (!wacom->resources)
2105 return;
2106
2107 devres_release_group(&hdev->dev, wacom);
2108
2109 wacom->resources = false;
2110
2111 wacom->wacom_wac.pen_input = NULL;
2112 wacom->wacom_wac.touch_input = NULL;
2113 wacom->wacom_wac.pad_input = NULL;
2114}
2115
d2ec58ae
AAS
2116static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2117{
2118 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2119 wacom_wac->shared->type = wacom_wac->features.type;
2120 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2121 }
2122
d793ff81 2123 if (wacom_wac->has_mute_touch_switch) {
d2ec58ae 2124 wacom_wac->shared->has_mute_touch_switch = true;
d793ff81
PC
2125 wacom_wac->shared->is_touch_on = true;
2126 }
d2ec58ae
AAS
2127
2128 if (wacom_wac->shared->has_mute_touch_switch &&
2129 wacom_wac->shared->touch_input) {
2130 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2131 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2132 SW_MUTE_DEVICE);
2133 }
2134}
2135
fd5f92b6 2136static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
3bea733a 2137{
c58ac3a8
BT
2138 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2139 struct wacom_features *features = &wacom_wac->features;
2140 struct hid_device *hdev = wacom->hdev;
e33da8a5 2141 int error;
7704ac93 2142 unsigned int connect_mask = HID_CONNECT_HIDRAW;
3bea733a 2143
01c846f9 2144 features->pktlen = wacom_compute_pktlen(hdev);
c58ac3a8
BT
2145 if (features->pktlen > WACOM_PKGLEN_MAX)
2146 return -EINVAL;
3bea733a 2147
84dfbd7f
BT
2148 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2149 return -ENOMEM;
2150
2151 wacom->resources = true;
2152
3f14a63a
JG
2153 error = wacom_allocate_inputs(wacom);
2154 if (error)
3888b0d5 2155 goto fail;
494078b0 2156
8c97a765
BT
2157 /*
2158 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2159 * into debug mode for the touch part.
2160 * We ignore the other interfaces.
2161 */
2162 if (features->type == BAMBOO_PAD) {
2163 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2164 features->type = HID_GENERIC;
2165 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2166 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2167 error = -ENODEV;
3888b0d5 2168 goto fail;
8c97a765
BT
2169 }
2170 }
2171
401d7d10
PC
2172 /* set the default size in case we do not get them from hid */
2173 wacom_set_default_phy(features);
2174
f393ee2b 2175 /* Retrieve the physical and logical size for touch devices */
c669fb2b 2176 wacom_retrieve_hid_descriptor(hdev, features);
42f4f272 2177 wacom_setup_device_quirks(wacom);
042628ab 2178
aa86b18c
JG
2179 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2180 features->type != WIRELESS) {
8e116d31
JG
2181 error = features->type == HID_GENERIC ? -ENODEV : 0;
2182
042628ab 2183 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
8e116d31
JG
2184 hdev->name,
2185 error ? "Ignoring" : "Assuming pen");
2186
2187 if (error)
3888b0d5 2188 goto fail;
042628ab 2189
aa86b18c 2190 features->device_type |= WACOM_DEVICETYPE_PEN;
042628ab
JG
2191 }
2192
401d7d10
PC
2193 wacom_calculate_res(features);
2194
fd5f92b6 2195 wacom_update_name(wacom, wireless ? " (WL)" : "");
4492efff 2196
8b407359
AAS
2197 /* pen only Bamboo neither support touch nor pad */
2198 if ((features->type == BAMBOO_PEN) &&
2199 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2200 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2201 error = -ENODEV;
2202 goto fail;
2203 }
2204
a9ce7856
JG
2205 error = wacom_add_shared_data(hdev);
2206 if (error)
2207 goto fail;
49b764ae 2208
ccad85cc 2209 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
f81a1295
BT
2210 (features->quirks & WACOM_QUIRK_BATTERY)) {
2211 error = wacom_initialize_battery(wacom);
2212 if (error)
3888b0d5 2213 goto fail;
f81a1295
BT
2214 }
2215
3f14a63a
JG
2216 error = wacom_register_inputs(wacom);
2217 if (error)
3888b0d5 2218 goto fail;
f81a1295 2219
b62f6465 2220 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
85d2c77b
BT
2221 error = wacom_initialize_leds(wacom);
2222 if (error)
3888b0d5 2223 goto fail;
85d2c77b 2224
83e6b40e 2225 error = wacom_initialize_remotes(wacom);
b62f6465 2226 if (error)
3888b0d5 2227 goto fail;
b62f6465
BT
2228 }
2229
7704ac93
BT
2230 if (features->type == HID_GENERIC)
2231 connect_mask |= HID_CONNECT_DRIVER;
2232
29b47391 2233 /* Regular HID work starts now */
7704ac93 2234 error = hid_hw_start(hdev, connect_mask);
29b47391
BT
2235 if (error) {
2236 hid_err(hdev, "hw start failed\n");
3888b0d5 2237 goto fail;
d3825d51 2238 }
961794a0 2239
fd5f92b6
BT
2240 if (!wireless) {
2241 /* Note that if query fails it is not a hard failure */
a544c619 2242 wacom_query_tablet_data(wacom);
fd5f92b6 2243 }
86e88f0e
JG
2244
2245 /* touch only Bamboo doesn't support pen */
2246 if ((features->type == BAMBOO_TOUCH) &&
2247 (features->device_type & WACOM_DEVICETYPE_PEN)) {
4d20c332
AAS
2248 cancel_delayed_work_sync(&wacom->init_work);
2249 _wacom_query_tablet_data(wacom);
86e88f0e 2250 error = -ENODEV;
b62f6465 2251 goto fail_quirks;
86e88f0e
JG
2252 }
2253
ccad85cc 2254 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
29b47391
BT
2255 error = hid_hw_open(hdev);
2256
d2ec58ae 2257 wacom_set_shared_values(wacom_wac);
84dfbd7f
BT
2258 devres_close_group(&hdev->dev, wacom);
2259
3bea733a
PC
2260 return 0;
2261
b62f6465 2262fail_quirks:
c58ac3a8 2263 hid_hw_stop(hdev);
3888b0d5 2264fail:
84dfbd7f 2265 wacom_release_resources(wacom);
c58ac3a8
BT
2266 return error;
2267}
2268
a2f091af
BT
2269static void wacom_wireless_work(struct work_struct *work)
2270{
d17d1f17 2271 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
a2f091af
BT
2272 struct usb_device *usbdev = wacom->usbdev;
2273 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2274 struct hid_device *hdev1, *hdev2;
2275 struct wacom *wacom1, *wacom2;
2276 struct wacom_wac *wacom_wac1, *wacom_wac2;
2277 int error;
2278
2279 /*
2280 * Regardless if this is a disconnect or a new tablet,
2281 * remove any existing input and battery devices.
2282 */
2283
2284 wacom_destroy_battery(wacom);
2285
2286 /* Stylus interface */
2287 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2288 wacom1 = hid_get_drvdata(hdev1);
2289 wacom_wac1 = &(wacom1->wacom_wac);
84dfbd7f 2290 wacom_release_resources(wacom1);
a2f091af
BT
2291
2292 /* Touch interface */
2293 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2294 wacom2 = hid_get_drvdata(hdev2);
2295 wacom_wac2 = &(wacom2->wacom_wac);
84dfbd7f 2296 wacom_release_resources(wacom2);
a2f091af
BT
2297
2298 if (wacom_wac->pid == 0) {
2299 hid_info(wacom->hdev, "wireless tablet disconnected\n");
a2f091af
BT
2300 } else {
2301 const struct hid_device_id *id = wacom_ids;
2302
2303 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2304 wacom_wac->pid);
2305
2306 while (id->bus) {
2307 if (id->vendor == USB_VENDOR_ID_WACOM &&
2308 id->product == wacom_wac->pid)
2309 break;
2310 id++;
2311 }
2312
2313 if (!id->bus) {
2314 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2315 return;
2316 }
2317
2318 /* Stylus interface */
2319 wacom_wac1->features =
2320 *((struct wacom_features *)id->driver_data);
fd5f92b6 2321
a2f091af 2322 wacom_wac1->pid = wacom_wac->pid;
fd5f92b6
BT
2323 hid_hw_stop(hdev1);
2324 error = wacom_parse_and_register(wacom1, true);
a2f091af
BT
2325 if (error)
2326 goto fail;
2327
2328 /* Touch interface */
2329 if (wacom_wac1->features.touch_max ||
2330 (wacom_wac1->features.type >= INTUOSHT &&
2331 wacom_wac1->features.type <= BAMBOO_PT)) {
2332 wacom_wac2->features =
2333 *((struct wacom_features *)id->driver_data);
a2f091af 2334 wacom_wac2->pid = wacom_wac->pid;
fd5f92b6
BT
2335 hid_hw_stop(hdev2);
2336 error = wacom_parse_and_register(wacom2, true);
a2f091af
BT
2337 if (error)
2338 goto fail;
a2f091af
BT
2339 }
2340
9956953e
BT
2341 strlcpy(wacom_wac->name, wacom_wac1->name,
2342 sizeof(wacom_wac->name));
a2f091af
BT
2343 error = wacom_initialize_battery(wacom);
2344 if (error)
2345 goto fail;
2346 }
2347
2348 return;
2349
2350fail:
84dfbd7f 2351 wacom_release_resources(wacom1);
84dfbd7f 2352 wacom_release_resources(wacom2);
a2f091af
BT
2353 return;
2354}
2355
04bfa27b
BT
2356static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2357{
2358 struct wacom_remote *remote = wacom->remote;
e7749f6e 2359 u32 serial = remote->remotes[index].serial;
04bfa27b 2360 int i;
7c35dc3c
BT
2361 unsigned long flags;
2362
326859d8
AAS
2363 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2364 if (remote->remotes[i].serial == serial) {
04bfa27b 2365
326859d8
AAS
2366 spin_lock_irqsave(&remote->remote_lock, flags);
2367 remote->remotes[i].registered = false;
2368 spin_unlock_irqrestore(&remote->remote_lock, flags);
9f1015d4 2369
326859d8
AAS
2370 if (remote->remotes[i].battery.battery)
2371 devres_release_group(&wacom->hdev->dev,
2372 &remote->remotes[i].battery.bat_desc);
2373
2374 if (remote->remotes[i].group.name)
2375 devres_release_group(&wacom->hdev->dev,
2376 &remote->remotes[i]);
04bfa27b 2377
e7749f6e
BT
2378 remote->remotes[i].serial = 0;
2379 remote->remotes[i].group.name = NULL;
9f1015d4 2380 remote->remotes[i].battery.battery = NULL;
04bfa27b
BT
2381 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2382 }
2383 }
2384}
2385
2386static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2387 unsigned int index)
2388{
2389 struct wacom_remote *remote = wacom->remote;
f9036bd4 2390 struct device *dev = &wacom->hdev->dev;
04bfa27b
BT
2391 int error, k;
2392
2393 /* A remote can pair more than once with an EKR,
2394 * check to make sure this serial isn't already paired.
2395 */
2396 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
e7749f6e 2397 if (remote->remotes[k].serial == serial)
04bfa27b
BT
2398 break;
2399 }
2400
2401 if (k < WACOM_MAX_REMOTES) {
e7749f6e 2402 remote->remotes[index].serial = serial;
04bfa27b
BT
2403 return 0;
2404 }
2405
e7749f6e 2406 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
f9036bd4
BT
2407 return -ENOMEM;
2408
04bfa27b
BT
2409 error = wacom_remote_create_attr_group(wacom, serial, index);
2410 if (error)
f9036bd4 2411 goto fail;
04bfa27b 2412
7c35dc3c
BT
2413 remote->remotes[index].input = wacom_allocate_input(wacom);
2414 if (!remote->remotes[index].input) {
2415 error = -ENOMEM;
2416 goto fail;
2417 }
2418 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2419 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2420
2421 if (!remote->remotes[index].input->name) {
2422 error = -EINVAL;
2423 goto fail;
2424 }
2425
2426 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2427 &wacom->wacom_wac);
2428 if (error)
2429 goto fail;
2430
e7749f6e 2431 remote->remotes[index].serial = serial;
04bfa27b 2432
7c35dc3c
BT
2433 error = input_register_device(remote->remotes[index].input);
2434 if (error)
2435 goto fail;
2436
97f5541f
BT
2437 error = wacom_led_groups_alloc_and_register_one(
2438 &remote->remotes[index].input->dev,
2439 wacom, index, 3, true);
2440 if (error)
2441 goto fail;
2442
7c35dc3c
BT
2443 remote->remotes[index].registered = true;
2444
e7749f6e 2445 devres_close_group(dev, &remote->remotes[index]);
04bfa27b 2446 return 0;
f9036bd4
BT
2447
2448fail:
e7749f6e
BT
2449 devres_release_group(dev, &remote->remotes[index]);
2450 remote->remotes[index].serial = 0;
f9036bd4 2451 return error;
04bfa27b
BT
2452}
2453
9f1015d4
BT
2454static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2455{
2456 struct wacom_remote *remote = wacom->remote;
2457 int error;
2458
2459 if (!remote->remotes[index].registered)
2460 return 0;
2461
2462 if (remote->remotes[index].battery.battery)
2463 return 0;
2464
2465 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2466 return 0;
2467
2468 error = __wacom_initialize_battery(wacom,
2469 &wacom->remote->remotes[index].battery);
2470 if (error)
2471 return error;
2472
2473 return 0;
2474}
2475
e6f2813a
BT
2476static void wacom_remote_work(struct work_struct *work)
2477{
2478 struct wacom *wacom = container_of(work, struct wacom, remote_work);
83e6b40e 2479 struct wacom_remote *remote = wacom->remote;
e6f2813a
BT
2480 struct wacom_remote_data data;
2481 unsigned long flags;
2482 unsigned int count;
2483 u32 serial;
04bfa27b 2484 int i;
e6f2813a 2485
83e6b40e 2486 spin_lock_irqsave(&remote->remote_lock, flags);
e6f2813a 2487
83e6b40e 2488 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
e6f2813a
BT
2489
2490 if (count != sizeof(data)) {
2491 hid_err(wacom->hdev,
2492 "workitem triggered without status available\n");
83e6b40e 2493 spin_unlock_irqrestore(&remote->remote_lock, flags);
e6f2813a
BT
2494 return;
2495 }
2496
83e6b40e 2497 if (!kfifo_is_empty(&remote->remote_fifo))
e6f2813a
BT
2498 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2499
83e6b40e 2500 spin_unlock_irqrestore(&remote->remote_lock, flags);
e6f2813a
BT
2501
2502 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2503 serial = data.remote[i].serial;
2504 if (data.remote[i].connected) {
2505
9f1015d4
BT
2506 if (remote->remotes[i].serial == serial) {
2507 wacom_remote_attach_battery(wacom, i);
e6f2813a 2508 continue;
9f1015d4 2509 }
e6f2813a 2510
e7749f6e 2511 if (remote->remotes[i].serial)
04bfa27b 2512 wacom_remote_destroy_one(wacom, i);
e6f2813a 2513
04bfa27b 2514 wacom_remote_create_one(wacom, serial, i);
e6f2813a 2515
e7749f6e 2516 } else if (remote->remotes[i].serial) {
04bfa27b 2517 wacom_remote_destroy_one(wacom, i);
e6f2813a
BT
2518 }
2519 }
2520}
2521
4082da80
BT
2522static void wacom_mode_change_work(struct work_struct *work)
2523{
2524 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2525 struct wacom_shared *shared = wacom->wacom_wac.shared;
2526 struct wacom *wacom1 = NULL;
2527 struct wacom *wacom2 = NULL;
2528 bool is_direct = wacom->wacom_wac.is_direct_mode;
2529 int error = 0;
2530
2531 if (shared->pen) {
2532 wacom1 = hid_get_drvdata(shared->pen);
2533 wacom_release_resources(wacom1);
2534 hid_hw_stop(wacom1->hdev);
2535 wacom1->wacom_wac.has_mode_change = true;
2536 wacom1->wacom_wac.is_direct_mode = is_direct;
2537 }
2538
2539 if (shared->touch) {
2540 wacom2 = hid_get_drvdata(shared->touch);
2541 wacom_release_resources(wacom2);
2542 hid_hw_stop(wacom2->hdev);
2543 wacom2->wacom_wac.has_mode_change = true;
2544 wacom2->wacom_wac.is_direct_mode = is_direct;
2545 }
2546
2547 if (wacom1) {
2548 error = wacom_parse_and_register(wacom1, false);
2549 if (error)
2550 return;
2551 }
2552
2553 if (wacom2) {
2554 error = wacom_parse_and_register(wacom2, false);
2555 if (error)
2556 return;
2557 }
2558
a2f091af
BT
2559 return;
2560}
2561
c58ac3a8
BT
2562static int wacom_probe(struct hid_device *hdev,
2563 const struct hid_device_id *id)
2564{
2565 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2566 struct usb_device *dev = interface_to_usbdev(intf);
2567 struct wacom *wacom;
2568 struct wacom_wac *wacom_wac;
2569 struct wacom_features *features;
2570 int error;
2571
2572 if (!id->driver_data)
2573 return -EINVAL;
2574
2575 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2576
2577 /* hid-core sets this quirk for the boot interface */
2578 hdev->quirks &= ~HID_QUIRK_NOGET;
2579
19b64330 2580 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
c58ac3a8
BT
2581 if (!wacom)
2582 return -ENOMEM;
2583
2584 hid_set_drvdata(hdev, wacom);
2585 wacom->hdev = hdev;
2586
2587 wacom_wac = &wacom->wacom_wac;
2588 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2589 features = &wacom_wac->features;
2590
2591 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2592 error = -ENODEV;
3888b0d5 2593 goto fail;
c58ac3a8
BT
2594 }
2595
c6fa1aeb 2596 wacom_wac->hid_data.inputmode = -1;
326ea2a9 2597 wacom_wac->mode_report = -1;
c6fa1aeb 2598
c58ac3a8
BT
2599 wacom->usbdev = dev;
2600 wacom->intf = intf;
2601 mutex_init(&wacom->lock);
a544c619 2602 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
d17d1f17
BT
2603 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2604 INIT_WORK(&wacom->battery_work, wacom_battery_work);
e6f2813a 2605 INIT_WORK(&wacom->remote_work, wacom_remote_work);
4082da80 2606 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
c58ac3a8
BT
2607
2608 /* ask for the report descriptor to be loaded by HID */
2609 error = hid_parse(hdev);
2610 if (error) {
2611 hid_err(hdev, "parse failed\n");
3888b0d5 2612 goto fail;
c58ac3a8
BT
2613 }
2614
fd5f92b6 2615 error = wacom_parse_and_register(wacom, false);
c58ac3a8 2616 if (error)
3888b0d5 2617 goto fail;
c58ac3a8
BT
2618
2619 if (hdev->bus == BUS_BLUETOOTH) {
2620 error = device_create_file(&hdev->dev, &dev_attr_speed);
2621 if (error)
2622 hid_warn(hdev,
2623 "can't create sysfs speed attribute err: %d\n",
2624 error);
2625 }
2626
2627 return 0;
2628
3888b0d5 2629fail:
29b47391 2630 hid_set_drvdata(hdev, NULL);
5014186d 2631 return error;
3bea733a
PC
2632}
2633
29b47391 2634static void wacom_remove(struct hid_device *hdev)
3bea733a 2635{
29b47391 2636 struct wacom *wacom = hid_get_drvdata(hdev);
f6205161
BT
2637 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2638 struct wacom_features *features = &wacom_wac->features;
2639
2640 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2641 hid_hw_close(hdev);
3bea733a 2642
29b47391 2643 hid_hw_stop(hdev);
e7224094 2644
a544c619 2645 cancel_delayed_work_sync(&wacom->init_work);
d17d1f17
BT
2646 cancel_work_sync(&wacom->wireless_work);
2647 cancel_work_sync(&wacom->battery_work);
e6f2813a 2648 cancel_work_sync(&wacom->remote_work);
4082da80 2649 cancel_work_sync(&wacom->mode_change_work);
f81a1295
BT
2650 if (hdev->bus == BUS_BLUETOOTH)
2651 device_remove_file(&hdev->dev, &dev_attr_speed);
e7224094 2652
c0265a94
BT
2653 /* make sure we don't trigger the LEDs */
2654 wacom_led_groups_release(wacom);
b6b1f19b
AAS
2655
2656 if (wacom->wacom_wac.features.type != REMOTE)
2657 wacom_release_resources(wacom);
5b779fc5 2658
29b47391 2659 hid_set_drvdata(hdev, NULL);
e7224094
ON
2660}
2661
41a74581 2662#ifdef CONFIG_PM
29b47391 2663static int wacom_resume(struct hid_device *hdev)
e7224094 2664{
29b47391 2665 struct wacom *wacom = hid_get_drvdata(hdev);
e7224094
ON
2666
2667 mutex_lock(&wacom->lock);
38101475
PC
2668
2669 /* switch to wacom mode first */
a544c619 2670 _wacom_query_tablet_data(wacom);
5d7e7d47 2671 wacom_led_control(wacom);
38101475 2672
e7224094
ON
2673 mutex_unlock(&wacom->lock);
2674
29b47391 2675 return 0;
e7224094
ON
2676}
2677
29b47391 2678static int wacom_reset_resume(struct hid_device *hdev)
e7224094 2679{
29b47391 2680 return wacom_resume(hdev);
3bea733a 2681}
41a74581 2682#endif /* CONFIG_PM */
3bea733a 2683
29b47391 2684static struct hid_driver wacom_driver = {
3bea733a 2685 .name = "wacom",
b036f6fb 2686 .id_table = wacom_ids,
3bea733a 2687 .probe = wacom_probe,
29b47391 2688 .remove = wacom_remove,
7704ac93 2689 .report = wacom_wac_report,
29b47391 2690#ifdef CONFIG_PM
e7224094
ON
2691 .resume = wacom_resume,
2692 .reset_resume = wacom_reset_resume,
29b47391
BT
2693#endif
2694 .raw_event = wacom_raw_event,
3bea733a 2695};
29b47391 2696module_hid_driver(wacom_driver);
f2e0a7d4
BT
2697
2698MODULE_VERSION(DRIVER_VERSION);
2699MODULE_AUTHOR(DRIVER_AUTHOR);
2700MODULE_DESCRIPTION(DRIVER_DESC);
7021b600 2701MODULE_LICENSE("GPL");