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