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