HID: core: Fix size as type u32
[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;
345857bb 222 int 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;
522 int 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);
1216 if (error)
1217 return error;
1218
1219 devres_add(&wacom->hdev->dev, devres);
1220
1221 return 0;
1222}
1223
f9036bd4
BT
1224static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1225 struct attribute_group *group)
1226{
1227 return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1228 group);
1229}
1230
34736aa9 1231enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
97f5541f
BT
1232{
1233 struct wacom *wacom = led->wacom;
1234
1235 if (wacom->led.max_hlv)
1236 return led->hlv * LED_FULL / wacom->led.max_hlv;
1237
1238 if (wacom->led.max_llv)
1239 return led->llv * LED_FULL / wacom->led.max_llv;
1240
1241 /* device doesn't support brightness tuning */
1242 return LED_FULL;
1243}
1244
1245static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
1246{
1247 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1248 struct wacom *wacom = led->wacom;
1249
1250 if (wacom->led.groups[led->group].select != led->id)
1251 return LED_OFF;
1252
1253 return wacom_leds_brightness_get(led);
1254}
1255
1256static int wacom_led_brightness_set(struct led_classdev *cdev,
1257 enum led_brightness brightness)
1258{
1259 struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
1260 struct wacom *wacom = led->wacom;
1261 int error;
1262
1263 mutex_lock(&wacom->lock);
1264
1265 if (!wacom->led.groups || (brightness == LED_OFF &&
1266 wacom->led.groups[led->group].select != led->id)) {
1267 error = 0;
1268 goto out;
1269 }
1270
1271 led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
1272 led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
1273
1274 wacom->led.groups[led->group].select = led->id;
1275
1276 error = wacom_led_control(wacom);
1277
1278out:
1279 mutex_unlock(&wacom->lock);
1280
1281 return error;
1282}
1283
1284static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
1285 enum led_brightness brightness)
1286{
1287}
1288
1289static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
1290 struct wacom_led *led, unsigned int group,
1291 unsigned int id, bool read_only)
1292{
1293 int error;
1294 char *name;
1295
1296 name = devm_kasprintf(dev, GFP_KERNEL,
1297 "%s::wacom-%d.%d",
1298 dev_name(dev),
1299 group,
1300 id);
1301 if (!name)
1302 return -ENOMEM;
1303
34736aa9
BT
1304 if (!read_only) {
1305 led->trigger.name = name;
1306 error = devm_led_trigger_register(dev, &led->trigger);
1307 if (error) {
1308 hid_err(wacom->hdev,
1309 "failed to register LED trigger %s: %d\n",
1310 led->cdev.name, error);
1311 return error;
1312 }
1313 }
1314
97f5541f
BT
1315 led->group = group;
1316 led->id = id;
1317 led->wacom = wacom;
1318 led->llv = wacom->led.llv;
1319 led->hlv = wacom->led.hlv;
1320 led->cdev.name = name;
1321 led->cdev.max_brightness = LED_FULL;
1322 led->cdev.flags = LED_HW_PLUGGABLE;
1323 led->cdev.brightness_get = __wacom_led_brightness_get;
34736aa9 1324 if (!read_only) {
97f5541f 1325 led->cdev.brightness_set_blocking = wacom_led_brightness_set;
34736aa9
BT
1326 led->cdev.default_trigger = led->cdev.name;
1327 } else {
97f5541f 1328 led->cdev.brightness_set = wacom_led_readonly_brightness_set;
34736aa9 1329 }
97f5541f
BT
1330
1331 error = devm_led_classdev_register(dev, &led->cdev);
1332 if (error) {
1333 hid_err(wacom->hdev,
1334 "failed to register LED %s: %d\n",
1335 led->cdev.name, error);
1336 led->cdev.name = NULL;
1337 return error;
1338 }
1339
1340 return 0;
1341}
1342
589e5060
BT
1343static void wacom_led_groups_release_one(void *data)
1344{
1345 struct wacom_group_leds *group = data;
1346
1347 devres_release_group(group->dev, group);
1348}
1349
97f5541f
BT
1350static int wacom_led_groups_alloc_and_register_one(struct device *dev,
1351 struct wacom *wacom,
1352 int group_id, int count,
1353 bool read_only)
1354{
1355 struct wacom_led *leds;
1356 int i, error;
1357
1358 if (group_id >= wacom->led.count || count <= 0)
1359 return -EINVAL;
1360
1361 if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
1362 return -ENOMEM;
1363
1364 leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
1365 if (!leds) {
1366 error = -ENOMEM;
1367 goto err;
1368 }
1369
1370 wacom->led.groups[group_id].leds = leds;
1371 wacom->led.groups[group_id].count = count;
1372
1373 for (i = 0; i < count; i++) {
1374 error = wacom_led_register_one(dev, wacom, &leds[i],
1375 group_id, i, read_only);
1376 if (error)
1377 goto err;
1378 }
1379
589e5060
BT
1380 wacom->led.groups[group_id].dev = dev;
1381
1382 devres_close_group(dev, &wacom->led.groups[group_id]);
1383
1384 /*
1385 * There is a bug (?) in devm_led_classdev_register() in which its
1386 * increments the refcount of the parent. If the parent is an input
1387 * device, that means the ref count never reaches 0 when
1388 * devm_input_device_release() gets called.
1389 * This means that the LEDs are still there after disconnect.
1390 * Manually force the release of the group so that the leds are released
1391 * once we are done using them.
1392 */
1393 error = devm_add_action_or_reset(&wacom->hdev->dev,
1394 wacom_led_groups_release_one,
1395 &wacom->led.groups[group_id]);
1396 if (error)
1397 return error;
1398
97f5541f
BT
1399 return 0;
1400
1401err:
1402 devres_release_group(dev, &wacom->led.groups[group_id]);
1403 return error;
1404}
1405
34736aa9
BT
1406struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
1407 unsigned int id)
1408{
1409 struct wacom_group_leds *group;
1410
1411 if (group_id >= wacom->led.count)
1412 return NULL;
1413
1414 group = &wacom->led.groups[group_id];
1415
1416 if (!group->leds)
1417 return NULL;
1418
1419 id %= group->count;
1420
1421 return &group->leds[id];
1422}
1423
1424/**
1425 * wacom_led_next: gives the next available led with a wacom trigger.
1426 *
1427 * returns the next available struct wacom_led which has its default trigger
1428 * or the current one if none is available.
1429 */
1430struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
1431{
1432 struct wacom_led *next_led;
1433 int group, next;
1434
1435 if (!wacom || !cur)
1436 return NULL;
1437
1438 group = cur->group;
1439 next = cur->id;
1440
1441 do {
1442 next_led = wacom_led_find(wacom, group, ++next);
1443 if (!next_led || next_led == cur)
1444 return next_led;
1445 } while (next_led->cdev.trigger != &next_led->trigger);
1446
1447 return next_led;
1448}
1449
a50aac71
BT
1450static void wacom_led_groups_release(void *data)
1451{
1452 struct wacom *wacom = data;
1453
1454 wacom->led.groups = NULL;
97f5541f 1455 wacom->led.count = 0;
a50aac71
BT
1456}
1457
1458static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1459{
97f5541f 1460 struct device *dev = &wacom->hdev->dev;
a50aac71
BT
1461 struct wacom_group_leds *groups;
1462 int error;
1463
97f5541f 1464 groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
a50aac71
BT
1465 GFP_KERNEL);
1466 if (!groups)
1467 return -ENOMEM;
1468
97f5541f 1469 error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
a50aac71
BT
1470 if (error)
1471 return error;
1472
1473 wacom->led.groups = groups;
97f5541f
BT
1474 wacom->led.count = count;
1475
1476 return 0;
1477}
1478
1479static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
1480 int led_per_group, bool read_only)
1481{
1482 struct device *dev;
1483 int i, error;
1484
1485 if (!wacom->wacom_wac.pad_input)
1486 return -EINVAL;
1487
1488 dev = &wacom->wacom_wac.pad_input->dev;
1489
1490 error = wacom_led_groups_allocate(wacom, group_count);
1491 if (error)
1492 return error;
1493
1494 for (i = 0; i < group_count; i++) {
1495 error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
1496 led_per_group,
1497 read_only);
1498 if (error)
1499 return error;
1500 }
a50aac71
BT
1501
1502 return 0;
1503}
1504
10c55cac 1505int wacom_initialize_leds(struct wacom *wacom)
5d7e7d47
EH
1506{
1507 int error;
1508
862cf553
JG
1509 if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1510 return 0;
1511
09e7d941
PC
1512 /* Initialize default values */
1513 switch (wacom->wacom_wac.features.type) {
10c55cac
AAS
1514 case HID_GENERIC:
1515 if (!wacom->generic_has_leds)
1516 return 0;
1517 wacom->led.llv = 100;
1518 wacom->led.max_llv = 100;
1519
1520 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1521 if (error) {
1522 hid_err(wacom->hdev,
1523 "cannot create leds err: %d\n", error);
1524 return error;
1525 }
1526
1527 error = wacom_devm_sysfs_create_group(wacom,
1528 &generic_led_attr_group);
1529 break;
1530
a19fc986 1531 case INTUOS4S:
09e7d941 1532 case INTUOS4:
81af7e61 1533 case INTUOS4WL:
09e7d941 1534 case INTUOS4L:
f4fa9a6d 1535 wacom->led.llv = 10;
5d7e7d47 1536 wacom->led.hlv = 20;
97f5541f
BT
1537 wacom->led.max_llv = 127;
1538 wacom->led.max_hlv = 127;
5d7e7d47 1539 wacom->led.img_lum = 10;
a50aac71 1540
97f5541f 1541 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
a50aac71
BT
1542 if (error) {
1543 hid_err(wacom->hdev,
1544 "cannot create leds err: %d\n", error);
1545 return error;
1546 }
1547
2df68a88
BT
1548 error = wacom_devm_sysfs_create_group(wacom,
1549 &intuos4_led_attr_group);
09e7d941
PC
1550 break;
1551
246835fc 1552 case WACOM_24HD:
09e7d941 1553 case WACOM_21UX2:
09e7d941
PC
1554 wacom->led.llv = 0;
1555 wacom->led.hlv = 0;
1556 wacom->led.img_lum = 0;
5d7e7d47 1557
97f5541f 1558 error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
a50aac71
BT
1559 if (error) {
1560 hid_err(wacom->hdev,
1561 "cannot create leds err: %d\n", error);
1562 return error;
1563 }
1564
2df68a88
BT
1565 error = wacom_devm_sysfs_create_group(wacom,
1566 &cintiq_led_attr_group);
09e7d941
PC
1567 break;
1568
9b5b95dd
JG
1569 case INTUOS5S:
1570 case INTUOS5:
1571 case INTUOS5L:
9a35c411
PC
1572 case INTUOSPS:
1573 case INTUOSPM:
1574 case INTUOSPL:
862cf553 1575 wacom->led.llv = 32;
97f5541f 1576 wacom->led.max_llv = 96;
862cf553 1577
97f5541f 1578 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
a50aac71
BT
1579 if (error) {
1580 hid_err(wacom->hdev,
1581 "cannot create leds err: %d\n", error);
1582 return error;
1583 }
1584
2df68a88
BT
1585 error = wacom_devm_sysfs_create_group(wacom,
1586 &intuos5_led_attr_group);
9b5b95dd
JG
1587 break;
1588
4922cd26
JG
1589 case INTUOSP2_BT:
1590 wacom->led.llv = 50;
1591 wacom->led.max_llv = 100;
1592 error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1593 if (error) {
1594 hid_err(wacom->hdev,
1595 "cannot create leds err: %d\n", error);
1596 return error;
1597 }
1598 return 0;
1599
a50aac71 1600 case REMOTE:
97f5541f
BT
1601 wacom->led.llv = 255;
1602 wacom->led.max_llv = 255;
a50aac71
BT
1603 error = wacom_led_groups_allocate(wacom, 5);
1604 if (error) {
1605 hid_err(wacom->hdev,
1606 "cannot create leds err: %d\n", error);
1607 return error;
1608 }
1609 return 0;
1610
09e7d941
PC
1611 default:
1612 return 0;
5d7e7d47
EH
1613 }
1614
09e7d941 1615 if (error) {
c31a408f 1616 hid_err(wacom->hdev,
09e7d941
PC
1617 "cannot create sysfs group err: %d\n", error);
1618 return error;
1619 }
09e7d941 1620
5d7e7d47
EH
1621 return 0;
1622}
1623
a544c619
BT
1624static void wacom_init_work(struct work_struct *work)
1625{
1626 struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1627
1628 _wacom_query_tablet_data(wacom);
1629 wacom_led_control(wacom);
1630}
1631
1632static void wacom_query_tablet_data(struct wacom *wacom)
1633{
1634 schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1635}
1636
a1d552cc 1637static enum power_supply_property wacom_battery_props[] = {
9956953e 1638 POWER_SUPPLY_PROP_MODEL_NAME,
71fa641e 1639 POWER_SUPPLY_PROP_PRESENT,
ac8d1010 1640 POWER_SUPPLY_PROP_STATUS,
6e2a6e80 1641 POWER_SUPPLY_PROP_SCOPE,
a1d552cc
CB
1642 POWER_SUPPLY_PROP_CAPACITY
1643};
1644
1645static int wacom_battery_get_property(struct power_supply *psy,
1646 enum power_supply_property psp,
1647 union power_supply_propval *val)
1648{
59d69bc8 1649 struct wacom_battery *battery = power_supply_get_drvdata(psy);
a1d552cc
CB
1650 int ret = 0;
1651
1652 switch (psp) {
9956953e
BT
1653 case POWER_SUPPLY_PROP_MODEL_NAME:
1654 val->strval = battery->wacom->wacom_wac.name;
1655 break;
71fa641e 1656 case POWER_SUPPLY_PROP_PRESENT:
59d69bc8 1657 val->intval = battery->bat_connected;
71fa641e 1658 break;
6e2a6e80
BN
1659 case POWER_SUPPLY_PROP_SCOPE:
1660 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1661 break;
a1d552cc 1662 case POWER_SUPPLY_PROP_CAPACITY:
59d69bc8 1663 val->intval = battery->battery_capacity;
ac8d1010
BT
1664 break;
1665 case POWER_SUPPLY_PROP_STATUS:
16e45989
JG
1666 if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
1667 val->intval = battery->bat_status;
1668 else if (battery->bat_charging)
ac8d1010 1669 val->intval = POWER_SUPPLY_STATUS_CHARGING;
59d69bc8
BT
1670 else if (battery->battery_capacity == 100 &&
1671 battery->ps_connected)
ac8d1010 1672 val->intval = POWER_SUPPLY_STATUS_FULL;
59d69bc8 1673 else if (battery->ps_connected)
b0882cb7 1674 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
ac8d1010
BT
1675 else
1676 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
a1d552cc
CB
1677 break;
1678 default:
1679 ret = -EINVAL;
1680 break;
1681 }
1682
1683 return ret;
1684}
1685
59d69bc8
BT
1686static int __wacom_initialize_battery(struct wacom *wacom,
1687 struct wacom_battery *battery)
a1d552cc 1688{
d70420b9 1689 static atomic_t battery_no = ATOMIC_INIT(0);
b189da90 1690 struct device *dev = &wacom->hdev->dev;
59d69bc8 1691 struct power_supply_config psy_cfg = { .drv_data = battery, };
136ae5e9 1692 struct power_supply *ps_bat;
59d69bc8 1693 struct power_supply_desc *bat_desc = &battery->bat_desc;
d70420b9 1694 unsigned long n;
b189da90
BT
1695 int error;
1696
1697 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1698 return -ENOMEM;
a1d552cc 1699
9956953e
BT
1700 battery->wacom = wacom;
1701
59d69bc8
BT
1702 n = atomic_inc_return(&battery_no) - 1;
1703
1704 bat_desc->properties = wacom_battery_props;
1705 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1706 bat_desc->get_property = wacom_battery_get_property;
1707 sprintf(battery->bat_name, "wacom_battery_%ld", n);
1708 bat_desc->name = battery->bat_name;
96983296 1709 bat_desc->type = POWER_SUPPLY_TYPE_USB;
59d69bc8
BT
1710 bat_desc->use_for_apm = 0;
1711
59d69bc8
BT
1712 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1713 if (IS_ERR(ps_bat)) {
1714 error = PTR_ERR(ps_bat);
1715 goto err;
1716 }
297d716f 1717
59d69bc8 1718 power_supply_powers(ps_bat, &wacom->hdev->dev);
7dbd229e 1719
59d69bc8 1720 battery->battery = ps_bat;
a1d552cc 1721
b189da90 1722 devres_close_group(dev, bat_desc);
7dbd229e 1723 return 0;
b189da90
BT
1724
1725err:
1726 devres_release_group(dev, bat_desc);
1727 return error;
a1d552cc
CB
1728}
1729
59d69bc8
BT
1730static int wacom_initialize_battery(struct wacom *wacom)
1731{
1732 if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
1733 return __wacom_initialize_battery(wacom, &wacom->battery);
1734
1735 return 0;
1736}
1737
a1d552cc
CB
1738static void wacom_destroy_battery(struct wacom *wacom)
1739{
59d69bc8
BT
1740 if (wacom->battery.battery) {
1741 devres_release_group(&wacom->hdev->dev,
1742 &wacom->battery.bat_desc);
1743 wacom->battery.battery = NULL;
b7af2bb8 1744 }
a1d552cc
CB
1745}
1746
f81a1295
BT
1747static ssize_t wacom_show_speed(struct device *dev,
1748 struct device_attribute
1749 *attr, char *buf)
1750{
ee79a8f8 1751 struct hid_device *hdev = to_hid_device(dev);
f81a1295
BT
1752 struct wacom *wacom = hid_get_drvdata(hdev);
1753
1754 return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1755}
1756
1757static ssize_t wacom_store_speed(struct device *dev,
1758 struct device_attribute *attr,
1759 const char *buf, size_t count)
1760{
ee79a8f8 1761 struct hid_device *hdev = to_hid_device(dev);
f81a1295
BT
1762 struct wacom *wacom = hid_get_drvdata(hdev);
1763 u8 new_speed;
1764
1765 if (kstrtou8(buf, 0, &new_speed))
1766 return -EINVAL;
1767
1768 if (new_speed != 0 && new_speed != 1)
1769 return -EINVAL;
1770
1771 wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1772
1773 return count;
1774}
1775
e0984bc3 1776static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
f81a1295
BT
1777 wacom_show_speed, wacom_store_speed);
1778
72b236d6
AS
1779
1780static ssize_t wacom_show_remote_mode(struct kobject *kobj,
1781 struct kobj_attribute *kattr,
1782 char *buf, int index)
1783{
2cf83833 1784 struct device *dev = kobj_to_dev(kobj->parent);
ee79a8f8 1785 struct hid_device *hdev = to_hid_device(dev);
72b236d6
AS
1786 struct wacom *wacom = hid_get_drvdata(hdev);
1787 u8 mode;
1788
a50aac71 1789 mode = wacom->led.groups[index].select;
bc35f73a 1790 return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
72b236d6
AS
1791}
1792
1793#define DEVICE_EKR_ATTR_GROUP(SET_ID) \
1794static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj, \
1795 struct kobj_attribute *kattr, char *buf) \
1796{ \
1797 return wacom_show_remote_mode(kobj, kattr, buf, SET_ID); \
1798} \
1799static struct kobj_attribute remote##SET_ID##_mode_attr = { \
1800 .attr = {.name = "remote_mode", \
1801 .mode = DEV_ATTR_RO_PERM}, \
1802 .show = wacom_show_remote##SET_ID##_mode, \
1803}; \
1804static struct attribute *remote##SET_ID##_serial_attrs[] = { \
1805 &remote##SET_ID##_mode_attr.attr, \
1806 NULL \
1807}; \
1808static struct attribute_group remote##SET_ID##_serial_group = { \
1809 .name = NULL, \
1810 .attrs = remote##SET_ID##_serial_attrs, \
1811}
1812
1813DEVICE_EKR_ATTR_GROUP(0);
1814DEVICE_EKR_ATTR_GROUP(1);
1815DEVICE_EKR_ATTR_GROUP(2);
1816DEVICE_EKR_ATTR_GROUP(3);
1817DEVICE_EKR_ATTR_GROUP(4);
1818
e6f2813a
BT
1819static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1820 int index)
72b236d6
AS
1821{
1822 int error = 0;
83e6b40e 1823 struct wacom_remote *remote = wacom->remote;
72b236d6 1824
e7749f6e 1825 remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
83e6b40e
BT
1826 GFP_KERNEL,
1827 "%d", serial);
e7749f6e 1828 if (!remote->remotes[index].group.name)
72b236d6 1829 return -ENOMEM;
72b236d6 1830
f9036bd4 1831 error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
e7749f6e 1832 &remote->remotes[index].group);
72b236d6 1833 if (error) {
e7749f6e 1834 remote->remotes[index].group.name = NULL;
72b236d6
AS
1835 hid_err(wacom->hdev,
1836 "cannot create sysfs group err: %d\n", error);
72b236d6
AS
1837 return error;
1838 }
1839
1840 return 0;
1841}
1842
72b236d6
AS
1843static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
1844{
1845 const size_t buf_size = 2;
1846 unsigned char *buf;
1847 int retval;
1848
1849 buf = kzalloc(buf_size, GFP_KERNEL);
1850 if (!buf)
1851 return -ENOMEM;
1852
1853 buf[0] = WAC_CMD_DELETE_PAIRING;
1854 buf[1] = selector;
1855
1856 retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
1857 buf_size, WAC_CMD_RETRIES);
1858 kfree(buf);
1859
1860 return retval;
1861}
1862
1863static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
1864 struct kobj_attribute *attr,
1865 const char *buf, size_t count)
1866{
1867 unsigned char selector = 0;
2cf83833 1868 struct device *dev = kobj_to_dev(kobj->parent);
ee79a8f8 1869 struct hid_device *hdev = to_hid_device(dev);
72b236d6
AS
1870 struct wacom *wacom = hid_get_drvdata(hdev);
1871 int err;
1872
1873 if (!strncmp(buf, "*\n", 2)) {
1874 selector = WAC_CMD_UNPAIR_ALL;
1875 } else {
1876 hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
1877 buf);
1878 return -1;
1879 }
1880
1881 mutex_lock(&wacom->lock);
1882
1883 err = wacom_cmd_unpair_remote(wacom, selector);
1884 mutex_unlock(&wacom->lock);
1885
1886 return err < 0 ? err : count;
1887}
1888
1889static struct kobj_attribute unpair_remote_attr = {
1890 .attr = {.name = "unpair_remote", .mode = 0200},
1891 .store = wacom_store_unpair_remote,
1892};
1893
1894static const struct attribute *remote_unpair_attrs[] = {
1895 &unpair_remote_attr.attr,
1896 NULL
1897};
1898
83e6b40e
BT
1899static void wacom_remotes_destroy(void *data)
1900{
1901 struct wacom *wacom = data;
1902 struct wacom_remote *remote = wacom->remote;
1903
1904 if (!remote)
1905 return;
1906
1907 kobject_put(remote->remote_dir);
1908 kfifo_free(&remote->remote_fifo);
1909 wacom->remote = NULL;
1910}
1911
1912static int wacom_initialize_remotes(struct wacom *wacom)
72b236d6
AS
1913{
1914 int error = 0;
83e6b40e 1915 struct wacom_remote *remote;
72b236d6
AS
1916 int i;
1917
1918 if (wacom->wacom_wac.features.type != REMOTE)
1919 return 0;
1920
83e6b40e
BT
1921 remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1922 GFP_KERNEL);
1923 if (!remote)
1924 return -ENOMEM;
72b236d6 1925
83e6b40e
BT
1926 wacom->remote = remote;
1927
1928 spin_lock_init(&remote->remote_lock);
1929
1930 error = kfifo_alloc(&remote->remote_fifo,
1931 5 * sizeof(struct wacom_remote_data),
1932 GFP_KERNEL);
1933 if (error) {
1934 hid_err(wacom->hdev, "failed allocating remote_fifo\n");
72b236d6 1935 return -ENOMEM;
83e6b40e 1936 }
72b236d6 1937
e7749f6e
BT
1938 remote->remotes[0].group = remote0_serial_group;
1939 remote->remotes[1].group = remote1_serial_group;
1940 remote->remotes[2].group = remote2_serial_group;
1941 remote->remotes[3].group = remote3_serial_group;
1942 remote->remotes[4].group = remote4_serial_group;
83e6b40e
BT
1943
1944 remote->remote_dir = kobject_create_and_add("wacom_remote",
1945 &wacom->hdev->dev.kobj);
1946 if (!remote->remote_dir)
1947 return -ENOMEM;
1948
1949 error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
72b236d6
AS
1950
1951 if (error) {
1952 hid_err(wacom->hdev,
1953 "cannot create sysfs group err: %d\n", error);
1954 return error;
1955 }
1956
1957 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
a50aac71 1958 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
e7749f6e 1959 remote->remotes[i].serial = 0;
72b236d6
AS
1960 }
1961
83e6b40e
BT
1962 error = devm_add_action_or_reset(&wacom->hdev->dev,
1963 wacom_remotes_destroy, wacom);
1964 if (error)
1965 return error;
1966
72b236d6
AS
1967 return 0;
1968}
1969
d2d13f18 1970static struct input_dev *wacom_allocate_input(struct wacom *wacom)
3aac0ef1
CB
1971{
1972 struct input_dev *input_dev;
b6c79f2c 1973 struct hid_device *hdev = wacom->hdev;
3aac0ef1 1974 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
3aac0ef1 1975
3dad188e 1976 input_dev = devm_input_allocate_device(&hdev->dev);
d2d13f18
BT
1977 if (!input_dev)
1978 return NULL;
3aac0ef1 1979
2bdd163c 1980 input_dev->name = wacom_wac->features.name;
b6c79f2c
BT
1981 input_dev->phys = hdev->phys;
1982 input_dev->dev.parent = &hdev->dev;
3aac0ef1
CB
1983 input_dev->open = wacom_open;
1984 input_dev->close = wacom_close;
b6c79f2c
BT
1985 input_dev->uniq = hdev->uniq;
1986 input_dev->id.bustype = hdev->bus;
1987 input_dev->id.vendor = hdev->vendor;
12969e3b 1988 input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
b6c79f2c 1989 input_dev->id.version = hdev->version;
3aac0ef1
CB
1990 input_set_drvdata(input_dev, wacom);
1991
d2d13f18
BT
1992 return input_dev;
1993}
1994
d9f2d203
JG
1995static int wacom_allocate_inputs(struct wacom *wacom)
1996{
1997 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1998
1999 wacom_wac->pen_input = wacom_allocate_input(wacom);
2000 wacom_wac->touch_input = wacom_allocate_input(wacom);
2001 wacom_wac->pad_input = wacom_allocate_input(wacom);
3dad188e
BT
2002 if (!wacom_wac->pen_input ||
2003 !wacom_wac->touch_input ||
2004 !wacom_wac->pad_input)
d9f2d203 2005 return -ENOMEM;
d9f2d203 2006
2bdd163c 2007 wacom_wac->pen_input->name = wacom_wac->pen_name;
d9f2d203
JG
2008 wacom_wac->touch_input->name = wacom_wac->touch_name;
2009 wacom_wac->pad_input->name = wacom_wac->pad_name;
2010
2011 return 0;
2012}
2013
2546dacd
BT
2014static int wacom_register_inputs(struct wacom *wacom)
2015{
2a6cdbdd 2016 struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2546dacd 2017 struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2636a3f2 2018 int error = 0;
2546dacd 2019
2a6cdbdd
JG
2020 pen_input_dev = wacom_wac->pen_input;
2021 touch_input_dev = wacom_wac->touch_input;
2546dacd
BT
2022 pad_input_dev = wacom_wac->pad_input;
2023
2a6cdbdd 2024 if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
2546dacd
BT
2025 return -EINVAL;
2026
2a6cdbdd
JG
2027 error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
2028 if (error) {
2029 /* no pen in use on this interface */
2030 input_free_device(pen_input_dev);
2031 wacom_wac->pen_input = NULL;
2032 pen_input_dev = NULL;
2033 } else {
2034 error = input_register_device(pen_input_dev);
2035 if (error)
3dad188e 2036 goto fail;
2a6cdbdd
JG
2037 }
2038
2039 error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
2040 if (error) {
2041 /* no touch in use on this interface */
2042 input_free_device(touch_input_dev);
2043 wacom_wac->touch_input = NULL;
2044 touch_input_dev = NULL;
2045 } else {
2046 error = input_register_device(touch_input_dev);
30ebc1ae 2047 if (error)
3dad188e 2048 goto fail;
30ebc1ae 2049 }
1963518b 2050
d2d13f18
BT
2051 error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2052 if (error) {
2053 /* no pad in use on this interface */
2054 input_free_device(pad_input_dev);
2055 wacom_wac->pad_input = NULL;
2056 pad_input_dev = NULL;
2057 } else {
2058 error = input_register_device(pad_input_dev);
2059 if (error)
3dad188e 2060 goto fail;
d2d13f18
BT
2061 }
2062
1963518b 2063 return 0;
3aac0ef1 2064
3dad188e
BT
2065fail:
2066 wacom_wac->pad_input = NULL;
2a6cdbdd 2067 wacom_wac->touch_input = NULL;
2a6cdbdd 2068 wacom_wac->pen_input = NULL;
3aac0ef1
CB
2069 return error;
2070}
2071
0be01712
JG
2072/*
2073 * Not all devices report physical dimensions from HID.
2074 * Compute the default from hardcoded logical dimension
2075 * and resolution before driver overwrites them.
2076 */
2077static void wacom_set_default_phy(struct wacom_features *features)
2078{
2079 if (features->x_resolution) {
2080 features->x_phy = (features->x_max * 100) /
2081 features->x_resolution;
2082 features->y_phy = (features->y_max * 100) /
2083 features->y_resolution;
2084 }
2085}
2086
2087static void wacom_calculate_res(struct wacom_features *features)
2088{
2089 /* set unit to "100th of a mm" for devices not reported by HID */
2090 if (!features->unit) {
2091 features->unit = 0x11;
2092 features->unitExpo = -3;
2093 }
2094
2095 features->x_resolution = wacom_calc_hid_res(features->x_max,
2096 features->x_phy,
2097 features->unit,
2098 features->unitExpo);
2099 features->y_resolution = wacom_calc_hid_res(features->y_max,
2100 features->y_phy,
2101 features->unit,
2102 features->unitExpo);
2103}
2104
fce9957d
JG
2105void wacom_battery_work(struct work_struct *work)
2106{
d17d1f17 2107 struct wacom *wacom = container_of(work, struct wacom, battery_work);
fce9957d
JG
2108
2109 if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
59d69bc8 2110 !wacom->battery.battery) {
fce9957d
JG
2111 wacom_initialize_battery(wacom);
2112 }
2113 else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
59d69bc8 2114 wacom->battery.battery) {
fce9957d
JG
2115 wacom_destroy_battery(wacom);
2116 }
2117}
2118
01c846f9
BT
2119static size_t wacom_compute_pktlen(struct hid_device *hdev)
2120{
2121 struct hid_report_enum *report_enum;
2122 struct hid_report *report;
2123 size_t size = 0;
2124
2125 report_enum = hdev->report_enum + HID_INPUT_REPORT;
2126
2127 list_for_each_entry(report, &report_enum->report_list, list) {
dabb05c6 2128 size_t report_size = hid_report_len(report);
01c846f9
BT
2129 if (report_size > size)
2130 size = report_size;
2131 }
2132
2133 return size;
2134}
2135
fd5f92b6 2136static void wacom_update_name(struct wacom *wacom, const char *suffix)
c24eab4e
PC
2137{
2138 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2139 struct wacom_features *features = &wacom_wac->features;
44b5250b 2140 char name[WACOM_NAME_MAX];
c24eab4e
PC
2141
2142 /* Generic devices name unspecified */
2143 if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
09dc28ac 2144 char *product_name = wacom->hdev->name;
f2209d4a 2145
09dc28ac
JG
2146 if (hid_is_using_ll_driver(wacom->hdev, &usb_hid_driver)) {
2147 struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
2148 struct usb_device *dev = interface_to_usbdev(intf);
2149 product_name = dev->product;
2150 }
f2209d4a 2151
09dc28ac
JG
2152 if (wacom->hdev->bus == BUS_I2C) {
2153 snprintf(name, sizeof(name), "%s %X",
2154 features->name, wacom->hdev->product);
2155 } else if (strstr(product_name, "Wacom") ||
2156 strstr(product_name, "wacom") ||
2157 strstr(product_name, "WACOM")) {
2158 strlcpy(name, product_name, sizeof(name));
c24eab4e 2159 } else {
09dc28ac 2160 snprintf(name, sizeof(name), "Wacom %s", product_name);
c24eab4e 2161 }
09dc28ac
JG
2162
2163 /* strip out excess whitespaces */
2164 while (1) {
2165 char *gap = strstr(name, " ");
2166 if (gap == NULL)
2167 break;
2168 /* shift everything including the terminator */
2169 memmove(gap, gap+1, strlen(gap));
2170 }
2171
2172 /* get rid of trailing whitespace */
2173 if (name[strlen(name)-1] == ' ')
2174 name[strlen(name)-1] = '\0';
c24eab4e 2175 } else {
44b5250b 2176 strlcpy(name, features->name, sizeof(name));
c24eab4e
PC
2177 }
2178
9956953e
BT
2179 snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
2180 name, suffix);
2181
c24eab4e 2182 /* Append the device type to the name */
2a6cdbdd 2183 snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
fd5f92b6 2184 "%s%s Pen", name, suffix);
2a6cdbdd 2185 snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
fd5f92b6 2186 "%s%s Finger", name, suffix);
c24eab4e 2187 snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
fd5f92b6 2188 "%s%s Pad", name, suffix);
c24eab4e
PC
2189}
2190
84dfbd7f
BT
2191static void wacom_release_resources(struct wacom *wacom)
2192{
2193 struct hid_device *hdev = wacom->hdev;
2194
2195 if (!wacom->resources)
2196 return;
2197
2198 devres_release_group(&hdev->dev, wacom);
2199
2200 wacom->resources = false;
2201
2202 wacom->wacom_wac.pen_input = NULL;
2203 wacom->wacom_wac.touch_input = NULL;
2204 wacom->wacom_wac.pad_input = NULL;
2205}
2206
d2ec58ae
AAS
2207static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2208{
2209 if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2210 wacom_wac->shared->type = wacom_wac->features.type;
2211 wacom_wac->shared->touch_input = wacom_wac->touch_input;
2212 }
2213
d793ff81 2214 if (wacom_wac->has_mute_touch_switch) {
d2ec58ae 2215 wacom_wac->shared->has_mute_touch_switch = true;
d793ff81
PC
2216 wacom_wac->shared->is_touch_on = true;
2217 }
d2ec58ae
AAS
2218
2219 if (wacom_wac->shared->has_mute_touch_switch &&
2220 wacom_wac->shared->touch_input) {
2221 set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2222 input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2223 SW_MUTE_DEVICE);
2224 }
2225}
2226
fd5f92b6 2227static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
3bea733a 2228{
c58ac3a8
BT
2229 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2230 struct wacom_features *features = &wacom_wac->features;
2231 struct hid_device *hdev = wacom->hdev;
e33da8a5 2232 int error;
7704ac93 2233 unsigned int connect_mask = HID_CONNECT_HIDRAW;
3bea733a 2234
01c846f9 2235 features->pktlen = wacom_compute_pktlen(hdev);
c58ac3a8
BT
2236 if (features->pktlen > WACOM_PKGLEN_MAX)
2237 return -EINVAL;
3bea733a 2238
84dfbd7f
BT
2239 if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
2240 return -ENOMEM;
2241
2242 wacom->resources = true;
2243
3f14a63a
JG
2244 error = wacom_allocate_inputs(wacom);
2245 if (error)
3888b0d5 2246 goto fail;
494078b0 2247
8c97a765
BT
2248 /*
2249 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
2250 * into debug mode for the touch part.
2251 * We ignore the other interfaces.
2252 */
2253 if (features->type == BAMBOO_PAD) {
2254 if (features->pktlen == WACOM_PKGLEN_PENABLED) {
2255 features->type = HID_GENERIC;
2256 } else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
2257 (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
2258 error = -ENODEV;
3888b0d5 2259 goto fail;
8c97a765
BT
2260 }
2261 }
2262
401d7d10
PC
2263 /* set the default size in case we do not get them from hid */
2264 wacom_set_default_phy(features);
2265
f393ee2b 2266 /* Retrieve the physical and logical size for touch devices */
c669fb2b 2267 wacom_retrieve_hid_descriptor(hdev, features);
42f4f272 2268 wacom_setup_device_quirks(wacom);
042628ab 2269
aa86b18c
JG
2270 if (features->device_type == WACOM_DEVICETYPE_NONE &&
2271 features->type != WIRELESS) {
8e116d31
JG
2272 error = features->type == HID_GENERIC ? -ENODEV : 0;
2273
042628ab 2274 dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
8e116d31
JG
2275 hdev->name,
2276 error ? "Ignoring" : "Assuming pen");
2277
2278 if (error)
3888b0d5 2279 goto fail;
042628ab 2280
aa86b18c 2281 features->device_type |= WACOM_DEVICETYPE_PEN;
042628ab
JG
2282 }
2283
401d7d10
PC
2284 wacom_calculate_res(features);
2285
fd5f92b6 2286 wacom_update_name(wacom, wireless ? " (WL)" : "");
4492efff 2287
8b407359
AAS
2288 /* pen only Bamboo neither support touch nor pad */
2289 if ((features->type == BAMBOO_PEN) &&
2290 ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
2291 (features->device_type & WACOM_DEVICETYPE_PAD))) {
2292 error = -ENODEV;
2293 goto fail;
2294 }
2295
a9ce7856
JG
2296 error = wacom_add_shared_data(hdev);
2297 if (error)
2298 goto fail;
49b764ae 2299
ccad85cc 2300 if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
f81a1295
BT
2301 (features->quirks & WACOM_QUIRK_BATTERY)) {
2302 error = wacom_initialize_battery(wacom);
2303 if (error)
3888b0d5 2304 goto fail;
f81a1295
BT
2305 }
2306
3f14a63a
JG
2307 error = wacom_register_inputs(wacom);
2308 if (error)
3888b0d5 2309 goto fail;
f81a1295 2310
b62f6465 2311 if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
85d2c77b
BT
2312 error = wacom_initialize_leds(wacom);
2313 if (error)
3888b0d5 2314 goto fail;
85d2c77b 2315
83e6b40e 2316 error = wacom_initialize_remotes(wacom);
b62f6465 2317 if (error)
3888b0d5 2318 goto fail;
b62f6465
BT
2319 }
2320
7704ac93
BT
2321 if (features->type == HID_GENERIC)
2322 connect_mask |= HID_CONNECT_DRIVER;
2323
29b47391 2324 /* Regular HID work starts now */
7704ac93 2325 error = hid_hw_start(hdev, connect_mask);
29b47391
BT
2326 if (error) {
2327 hid_err(hdev, "hw start failed\n");
3888b0d5 2328 goto fail;
d3825d51 2329 }
961794a0 2330
fd5f92b6
BT
2331 if (!wireless) {
2332 /* Note that if query fails it is not a hard failure */
a544c619 2333 wacom_query_tablet_data(wacom);
fd5f92b6 2334 }
86e88f0e
JG
2335
2336 /* touch only Bamboo doesn't support pen */
2337 if ((features->type == BAMBOO_TOUCH) &&
2338 (features->device_type & WACOM_DEVICETYPE_PEN)) {
4d20c332
AAS
2339 cancel_delayed_work_sync(&wacom->init_work);
2340 _wacom_query_tablet_data(wacom);
86e88f0e 2341 error = -ENODEV;
b62f6465 2342 goto fail_quirks;
86e88f0e
JG
2343 }
2344
ccad85cc 2345 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
29b47391
BT
2346 error = hid_hw_open(hdev);
2347
d2ec58ae 2348 wacom_set_shared_values(wacom_wac);
84dfbd7f
BT
2349 devres_close_group(&hdev->dev, wacom);
2350
3bea733a
PC
2351 return 0;
2352
b62f6465 2353fail_quirks:
c58ac3a8 2354 hid_hw_stop(hdev);
3888b0d5 2355fail:
84dfbd7f 2356 wacom_release_resources(wacom);
c58ac3a8
BT
2357 return error;
2358}
2359
a2f091af
BT
2360static void wacom_wireless_work(struct work_struct *work)
2361{
d17d1f17 2362 struct wacom *wacom = container_of(work, struct wacom, wireless_work);
a2f091af
BT
2363 struct usb_device *usbdev = wacom->usbdev;
2364 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2365 struct hid_device *hdev1, *hdev2;
2366 struct wacom *wacom1, *wacom2;
2367 struct wacom_wac *wacom_wac1, *wacom_wac2;
2368 int error;
2369
2370 /*
2371 * Regardless if this is a disconnect or a new tablet,
2372 * remove any existing input and battery devices.
2373 */
2374
2375 wacom_destroy_battery(wacom);
2376
2377 /* Stylus interface */
2378 hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2379 wacom1 = hid_get_drvdata(hdev1);
2380 wacom_wac1 = &(wacom1->wacom_wac);
84dfbd7f 2381 wacom_release_resources(wacom1);
a2f091af
BT
2382
2383 /* Touch interface */
2384 hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2385 wacom2 = hid_get_drvdata(hdev2);
2386 wacom_wac2 = &(wacom2->wacom_wac);
84dfbd7f 2387 wacom_release_resources(wacom2);
a2f091af
BT
2388
2389 if (wacom_wac->pid == 0) {
2390 hid_info(wacom->hdev, "wireless tablet disconnected\n");
a2f091af
BT
2391 } else {
2392 const struct hid_device_id *id = wacom_ids;
2393
2394 hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2395 wacom_wac->pid);
2396
2397 while (id->bus) {
2398 if (id->vendor == USB_VENDOR_ID_WACOM &&
2399 id->product == wacom_wac->pid)
2400 break;
2401 id++;
2402 }
2403
2404 if (!id->bus) {
2405 hid_info(wacom->hdev, "ignoring unknown PID.\n");
2406 return;
2407 }
2408
2409 /* Stylus interface */
2410 wacom_wac1->features =
2411 *((struct wacom_features *)id->driver_data);
fd5f92b6 2412
a2f091af 2413 wacom_wac1->pid = wacom_wac->pid;
fd5f92b6
BT
2414 hid_hw_stop(hdev1);
2415 error = wacom_parse_and_register(wacom1, true);
a2f091af
BT
2416 if (error)
2417 goto fail;
2418
2419 /* Touch interface */
2420 if (wacom_wac1->features.touch_max ||
2421 (wacom_wac1->features.type >= INTUOSHT &&
2422 wacom_wac1->features.type <= BAMBOO_PT)) {
2423 wacom_wac2->features =
2424 *((struct wacom_features *)id->driver_data);
a2f091af 2425 wacom_wac2->pid = wacom_wac->pid;
fd5f92b6
BT
2426 hid_hw_stop(hdev2);
2427 error = wacom_parse_and_register(wacom2, true);
a2f091af
BT
2428 if (error)
2429 goto fail;
a2f091af
BT
2430 }
2431
9956953e
BT
2432 strlcpy(wacom_wac->name, wacom_wac1->name,
2433 sizeof(wacom_wac->name));
a2f091af
BT
2434 error = wacom_initialize_battery(wacom);
2435 if (error)
2436 goto fail;
2437 }
2438
2439 return;
2440
2441fail:
84dfbd7f 2442 wacom_release_resources(wacom1);
84dfbd7f 2443 wacom_release_resources(wacom2);
a2f091af
BT
2444 return;
2445}
2446
04bfa27b
BT
2447static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
2448{
2449 struct wacom_remote *remote = wacom->remote;
e7749f6e 2450 u32 serial = remote->remotes[index].serial;
04bfa27b 2451 int i;
7c35dc3c
BT
2452 unsigned long flags;
2453
791ae273
AAS
2454 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2455 if (remote->remotes[i].serial == serial) {
04bfa27b 2456
791ae273
AAS
2457 spin_lock_irqsave(&remote->remote_lock, flags);
2458 remote->remotes[i].registered = false;
2459 spin_unlock_irqrestore(&remote->remote_lock, flags);
9f1015d4 2460
791ae273
AAS
2461 if (remote->remotes[i].battery.battery)
2462 devres_release_group(&wacom->hdev->dev,
2463 &remote->remotes[i].battery.bat_desc);
2464
2465 if (remote->remotes[i].group.name)
2466 devres_release_group(&wacom->hdev->dev,
2467 &remote->remotes[i]);
04bfa27b 2468
e7749f6e
BT
2469 remote->remotes[i].serial = 0;
2470 remote->remotes[i].group.name = NULL;
9f1015d4 2471 remote->remotes[i].battery.battery = NULL;
04bfa27b
BT
2472 wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2473 }
2474 }
2475}
2476
2477static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
2478 unsigned int index)
2479{
2480 struct wacom_remote *remote = wacom->remote;
f9036bd4 2481 struct device *dev = &wacom->hdev->dev;
04bfa27b
BT
2482 int error, k;
2483
2484 /* A remote can pair more than once with an EKR,
2485 * check to make sure this serial isn't already paired.
2486 */
2487 for (k = 0; k < WACOM_MAX_REMOTES; k++) {
e7749f6e 2488 if (remote->remotes[k].serial == serial)
04bfa27b
BT
2489 break;
2490 }
2491
2492 if (k < WACOM_MAX_REMOTES) {
e7749f6e 2493 remote->remotes[index].serial = serial;
04bfa27b
BT
2494 return 0;
2495 }
2496
e7749f6e 2497 if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
f9036bd4
BT
2498 return -ENOMEM;
2499
04bfa27b
BT
2500 error = wacom_remote_create_attr_group(wacom, serial, index);
2501 if (error)
f9036bd4 2502 goto fail;
04bfa27b 2503
7c35dc3c
BT
2504 remote->remotes[index].input = wacom_allocate_input(wacom);
2505 if (!remote->remotes[index].input) {
2506 error = -ENOMEM;
2507 goto fail;
2508 }
2509 remote->remotes[index].input->uniq = remote->remotes[index].group.name;
2510 remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
2511
2512 if (!remote->remotes[index].input->name) {
2513 error = -EINVAL;
2514 goto fail;
2515 }
2516
2517 error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
2518 &wacom->wacom_wac);
2519 if (error)
2520 goto fail;
2521
e7749f6e 2522 remote->remotes[index].serial = serial;
04bfa27b 2523
7c35dc3c
BT
2524 error = input_register_device(remote->remotes[index].input);
2525 if (error)
2526 goto fail;
2527
97f5541f
BT
2528 error = wacom_led_groups_alloc_and_register_one(
2529 &remote->remotes[index].input->dev,
2530 wacom, index, 3, true);
2531 if (error)
2532 goto fail;
2533
7c35dc3c
BT
2534 remote->remotes[index].registered = true;
2535
e7749f6e 2536 devres_close_group(dev, &remote->remotes[index]);
04bfa27b 2537 return 0;
f9036bd4
BT
2538
2539fail:
e7749f6e
BT
2540 devres_release_group(dev, &remote->remotes[index]);
2541 remote->remotes[index].serial = 0;
f9036bd4 2542 return error;
04bfa27b
BT
2543}
2544
9f1015d4
BT
2545static int wacom_remote_attach_battery(struct wacom *wacom, int index)
2546{
2547 struct wacom_remote *remote = wacom->remote;
2548 int error;
2549
2550 if (!remote->remotes[index].registered)
2551 return 0;
2552
2553 if (remote->remotes[index].battery.battery)
2554 return 0;
2555
2556 if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
2557 return 0;
2558
2559 error = __wacom_initialize_battery(wacom,
2560 &wacom->remote->remotes[index].battery);
2561 if (error)
2562 return error;
2563
2564 return 0;
2565}
2566
e6f2813a
BT
2567static void wacom_remote_work(struct work_struct *work)
2568{
2569 struct wacom *wacom = container_of(work, struct wacom, remote_work);
83e6b40e 2570 struct wacom_remote *remote = wacom->remote;
e6f2813a
BT
2571 struct wacom_remote_data data;
2572 unsigned long flags;
2573 unsigned int count;
2574 u32 serial;
04bfa27b 2575 int i;
e6f2813a 2576
83e6b40e 2577 spin_lock_irqsave(&remote->remote_lock, flags);
e6f2813a 2578
83e6b40e 2579 count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
e6f2813a
BT
2580
2581 if (count != sizeof(data)) {
2582 hid_err(wacom->hdev,
2583 "workitem triggered without status available\n");
83e6b40e 2584 spin_unlock_irqrestore(&remote->remote_lock, flags);
e6f2813a
BT
2585 return;
2586 }
2587
83e6b40e 2588 if (!kfifo_is_empty(&remote->remote_fifo))
e6f2813a
BT
2589 wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2590
83e6b40e 2591 spin_unlock_irqrestore(&remote->remote_lock, flags);
e6f2813a
BT
2592
2593 for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2594 serial = data.remote[i].serial;
2595 if (data.remote[i].connected) {
2596
9f1015d4
BT
2597 if (remote->remotes[i].serial == serial) {
2598 wacom_remote_attach_battery(wacom, i);
e6f2813a 2599 continue;
9f1015d4 2600 }
e6f2813a 2601
e7749f6e 2602 if (remote->remotes[i].serial)
04bfa27b 2603 wacom_remote_destroy_one(wacom, i);
e6f2813a 2604
04bfa27b 2605 wacom_remote_create_one(wacom, serial, i);
e6f2813a 2606
e7749f6e 2607 } else if (remote->remotes[i].serial) {
04bfa27b 2608 wacom_remote_destroy_one(wacom, i);
e6f2813a
BT
2609 }
2610 }
2611}
2612
4082da80
BT
2613static void wacom_mode_change_work(struct work_struct *work)
2614{
2615 struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
2616 struct wacom_shared *shared = wacom->wacom_wac.shared;
2617 struct wacom *wacom1 = NULL;
2618 struct wacom *wacom2 = NULL;
2619 bool is_direct = wacom->wacom_wac.is_direct_mode;
2620 int error = 0;
2621
2622 if (shared->pen) {
2623 wacom1 = hid_get_drvdata(shared->pen);
2624 wacom_release_resources(wacom1);
2625 hid_hw_stop(wacom1->hdev);
2626 wacom1->wacom_wac.has_mode_change = true;
2627 wacom1->wacom_wac.is_direct_mode = is_direct;
2628 }
2629
2630 if (shared->touch) {
2631 wacom2 = hid_get_drvdata(shared->touch);
2632 wacom_release_resources(wacom2);
2633 hid_hw_stop(wacom2->hdev);
2634 wacom2->wacom_wac.has_mode_change = true;
2635 wacom2->wacom_wac.is_direct_mode = is_direct;
2636 }
2637
2638 if (wacom1) {
2639 error = wacom_parse_and_register(wacom1, false);
2640 if (error)
2641 return;
2642 }
2643
2644 if (wacom2) {
2645 error = wacom_parse_and_register(wacom2, false);
2646 if (error)
2647 return;
2648 }
2649
a2f091af
BT
2650 return;
2651}
2652
c58ac3a8
BT
2653static int wacom_probe(struct hid_device *hdev,
2654 const struct hid_device_id *id)
2655{
2656 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2657 struct usb_device *dev = interface_to_usbdev(intf);
2658 struct wacom *wacom;
2659 struct wacom_wac *wacom_wac;
2660 struct wacom_features *features;
2661 int error;
2662
2663 if (!id->driver_data)
2664 return -EINVAL;
2665
2666 hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2667
2668 /* hid-core sets this quirk for the boot interface */
2669 hdev->quirks &= ~HID_QUIRK_NOGET;
2670
19b64330 2671 wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
c58ac3a8
BT
2672 if (!wacom)
2673 return -ENOMEM;
2674
2675 hid_set_drvdata(hdev, wacom);
2676 wacom->hdev = hdev;
2677
2678 wacom_wac = &wacom->wacom_wac;
2679 wacom_wac->features = *((struct wacom_features *)id->driver_data);
2680 features = &wacom_wac->features;
2681
2682 if (features->check_for_hid_type && features->hid_type != hdev->type) {
2683 error = -ENODEV;
3888b0d5 2684 goto fail;
c58ac3a8
BT
2685 }
2686
83417206
JG
2687 error = kfifo_alloc(&wacom_wac->pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
2688 if (error)
2689 goto fail;
2690
c6fa1aeb 2691 wacom_wac->hid_data.inputmode = -1;
326ea2a9 2692 wacom_wac->mode_report = -1;
c6fa1aeb 2693
c58ac3a8
BT
2694 wacom->usbdev = dev;
2695 wacom->intf = intf;
2696 mutex_init(&wacom->lock);
a544c619 2697 INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
d17d1f17
BT
2698 INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2699 INIT_WORK(&wacom->battery_work, wacom_battery_work);
e6f2813a 2700 INIT_WORK(&wacom->remote_work, wacom_remote_work);
4082da80 2701 INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
c58ac3a8
BT
2702
2703 /* ask for the report descriptor to be loaded by HID */
2704 error = hid_parse(hdev);
2705 if (error) {
2706 hid_err(hdev, "parse failed\n");
3888b0d5 2707 goto fail;
c58ac3a8
BT
2708 }
2709
fd5f92b6 2710 error = wacom_parse_and_register(wacom, false);
c58ac3a8 2711 if (error)
3888b0d5 2712 goto fail;
c58ac3a8
BT
2713
2714 if (hdev->bus == BUS_BLUETOOTH) {
2715 error = device_create_file(&hdev->dev, &dev_attr_speed);
2716 if (error)
2717 hid_warn(hdev,
2718 "can't create sysfs speed attribute err: %d\n",
2719 error);
2720 }
2721
2722 return 0;
2723
3888b0d5 2724fail:
29b47391 2725 hid_set_drvdata(hdev, NULL);
5014186d 2726 return error;
3bea733a
PC
2727}
2728
29b47391 2729static void wacom_remove(struct hid_device *hdev)
3bea733a 2730{
29b47391 2731 struct wacom *wacom = hid_get_drvdata(hdev);
f6205161
BT
2732 struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2733 struct wacom_features *features = &wacom_wac->features;
2734
2735 if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2736 hid_hw_close(hdev);
3bea733a 2737
29b47391 2738 hid_hw_stop(hdev);
e7224094 2739
a544c619 2740 cancel_delayed_work_sync(&wacom->init_work);
d17d1f17
BT
2741 cancel_work_sync(&wacom->wireless_work);
2742 cancel_work_sync(&wacom->battery_work);
e6f2813a 2743 cancel_work_sync(&wacom->remote_work);
4082da80 2744 cancel_work_sync(&wacom->mode_change_work);
f81a1295
BT
2745 if (hdev->bus == BUS_BLUETOOTH)
2746 device_remove_file(&hdev->dev, &dev_attr_speed);
e7224094 2747
c0265a94
BT
2748 /* make sure we don't trigger the LEDs */
2749 wacom_led_groups_release(wacom);
b6b1f19b
AAS
2750
2751 if (wacom->wacom_wac.features.type != REMOTE)
2752 wacom_release_resources(wacom);
5b779fc5 2753
83417206
JG
2754 kfifo_free(&wacom_wac->pen_fifo);
2755
29b47391 2756 hid_set_drvdata(hdev, NULL);
e7224094
ON
2757}
2758
41a74581 2759#ifdef CONFIG_PM
29b47391 2760static int wacom_resume(struct hid_device *hdev)
e7224094 2761{
29b47391 2762 struct wacom *wacom = hid_get_drvdata(hdev);
e7224094
ON
2763
2764 mutex_lock(&wacom->lock);
38101475
PC
2765
2766 /* switch to wacom mode first */
a544c619 2767 _wacom_query_tablet_data(wacom);
5d7e7d47 2768 wacom_led_control(wacom);
38101475 2769
e7224094
ON
2770 mutex_unlock(&wacom->lock);
2771
29b47391 2772 return 0;
e7224094
ON
2773}
2774
29b47391 2775static int wacom_reset_resume(struct hid_device *hdev)
e7224094 2776{
29b47391 2777 return wacom_resume(hdev);
3bea733a 2778}
41a74581 2779#endif /* CONFIG_PM */
3bea733a 2780
29b47391 2781static struct hid_driver wacom_driver = {
3bea733a 2782 .name = "wacom",
b036f6fb 2783 .id_table = wacom_ids,
3bea733a 2784 .probe = wacom_probe,
29b47391 2785 .remove = wacom_remove,
7704ac93 2786 .report = wacom_wac_report,
29b47391 2787#ifdef CONFIG_PM
e7224094
ON
2788 .resume = wacom_resume,
2789 .reset_resume = wacom_reset_resume,
29b47391
BT
2790#endif
2791 .raw_event = wacom_raw_event,
3bea733a 2792};
29b47391 2793module_hid_driver(wacom_driver);
f2e0a7d4
BT
2794
2795MODULE_VERSION(DRIVER_VERSION);
2796MODULE_AUTHOR(DRIVER_AUTHOR);
2797MODULE_DESCRIPTION(DRIVER_DESC);
7021b600 2798MODULE_LICENSE("GPL");