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