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