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