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