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