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