wacom_sys.c 71 KB
Newer Older
1
/*
2
 * drivers/input/tablet/wacom_sys.c
3
 *
4
 *  USB Wacom tablet support - system specific code
5
6
7
8
9
10
11
12
13
14
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include "wacom_wac.h"
15
#include "wacom.h"
16
#include <linux/input/mt.h>
17

18
#define WAC_MSG_RETRIES		5
19
20
#define WAC_CMD_RETRIES		10

21
22
#define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
#define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
23
#define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
24

25
26
static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
			    size_t size, unsigned int retries)
27
{
28
29
30
	int retval;

	do {
31
		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
32
				HID_REQ_GET_REPORT);
33
34
35
36
37
	} while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);

	if (retval < 0)
		hid_err(hdev, "wacom_get_report: ran out of retries "
			"(last error = %d)\n", retval);
38
39

	return retval;
40
41
}

42
43
static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
			    size_t size, unsigned int retries)
44
{
45
46
47
	int retval;

	do {
48
		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
49
				HID_REQ_SET_REPORT);
50
51
52
53
54
	} while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);

	if (retval < 0)
		hid_err(hdev, "wacom_set_report: ran out of retries "
			"(last error = %d)\n", retval);
55
56

	return retval;
57
58
}

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
static void wacom_wac_queue_insert(struct hid_device *hdev,
				   struct kfifo_rec_ptr_2 *fifo,
				   u8 *raw_data, int size)
{
	bool warned = false;

	while (kfifo_avail(fifo) < size) {
		if (!warned)
			hid_warn(hdev, "%s: kfifo has filled, starting to drop events\n", __func__);
		warned = true;

		kfifo_skip(fifo);
	}

	kfifo_in(fifo, raw_data, size);
}

static void wacom_wac_queue_flush(struct hid_device *hdev,
				  struct kfifo_rec_ptr_2 *fifo)
{
	while (!kfifo_is_empty(fifo)) {
		u8 buf[WACOM_PKGLEN_MAX];
		int size;
		int err;

		size = kfifo_out(fifo, buf, sizeof(buf));
		err = hid_report_raw_event(hdev, HID_INPUT_REPORT, buf, size, false);
		if (err) {
			hid_warn(hdev, "%s: unable to flush event due to error %d\n",
				 __func__, err);
		}
	}
}

static int wacom_wac_pen_serial_enforce(struct hid_device *hdev,
94
		struct hid_report *report, u8 *raw_data, int report_size)
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
{
	struct wacom *wacom = hid_get_drvdata(hdev);
	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
	struct wacom_features *features = &wacom_wac->features;
	bool flush = false;
	bool insert = false;
	int i, j;

	if (wacom_wac->serial[0] || !(features->quirks & WACOM_QUIRK_TOOLSERIAL))
		return 0;

	/* Queue events which have invalid tool type or serial number */
	for (i = 0; i < report->maxfield; i++) {
		for (j = 0; j < report->field[i]->maxusage; j++) {
			struct hid_field *field = report->field[i];
			struct hid_usage *usage = &field->usage[j];
			unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
			unsigned int offset;
			unsigned int size;
			unsigned int value;

			if (equivalent_usage != HID_DG_INRANGE &&
			    equivalent_usage != HID_DG_TOOLSERIALNUMBER &&
			    equivalent_usage != WACOM_HID_WD_SERIALHI &&
			    equivalent_usage != WACOM_HID_WD_TOOLTYPE)
				continue;

			offset = field->report_offset;
			size = field->report_size;
			value = hid_field_extract(hdev, raw_data+1, offset + j * size, size);

			/* If we go out of range, we need to flush the queue ASAP */
			if (equivalent_usage == HID_DG_INRANGE)
				value = !value;

			if (value) {
				flush = true;
				switch (equivalent_usage) {
				case HID_DG_TOOLSERIALNUMBER:
					wacom_wac->serial[0] = value;
					break;

				case WACOM_HID_WD_SERIALHI:
					wacom_wac->serial[0] |= ((__u64)value) << 32;
					break;

				case WACOM_HID_WD_TOOLTYPE:
					wacom_wac->id[0] = value;
					break;
				}
			}
			else {
				insert = true;
			}
		}
	}

	if (flush)
		wacom_wac_queue_flush(hdev, &wacom_wac->pen_fifo);
	else if (insert)
155
156
		wacom_wac_queue_insert(hdev, &wacom_wac->pen_fifo,
				       raw_data, report_size);
157
158
159
160

	return insert && !flush;
}

161
162
static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
		u8 *raw_data, int size)
163
{
164
	struct wacom *wacom = hid_get_drvdata(hdev);
165

166
167
	if (size > WACOM_PKGLEN_MAX)
		return 1;
168

169
170
171
	if (wacom_wac_pen_serial_enforce(hdev, report, raw_data, size))
		return -1;

172
	memcpy(wacom->wacom_wac.data, raw_data, size);
173

174
175
176
	wacom_wac_irq(&wacom->wacom_wac, size);

	return 0;
177
178
179
180
}

static int wacom_open(struct input_dev *dev)
{
181
	struct wacom *wacom = input_get_drvdata(dev);
182

183
	return hid_hw_open(wacom->hdev);
184
185
186
187
}

static void wacom_close(struct input_dev *dev)
{
188
	struct wacom *wacom = input_get_drvdata(dev);
189

190
191
192
193
194
195
	/*
	 * wacom->hdev should never be null, but surprisingly, I had the case
	 * once while unplugging the Wacom Wireless Receiver.
	 */
	if (wacom->hdev)
		hid_hw_close(wacom->hdev);
196
197
}

198
/*
199
 * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
200
201
 */
static int wacom_calc_hid_res(int logical_extents, int physical_extents,
202
			       unsigned unit, int exponent)
203
{
204
205
206
207
208
209
210
211
	struct hid_field field = {
		.logical_maximum = logical_extents,
		.physical_maximum = physical_extents,
		.unit = unit,
		.unit_exponent = exponent,
	};

	return hidinput_calc_abs_res(&field, ABS_X);
212
213
}

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
static void wacom_hid_usage_quirk(struct hid_device *hdev,
		struct hid_field *field, struct hid_usage *usage)
{
	struct wacom *wacom = hid_get_drvdata(hdev);
	struct wacom_features *features = &wacom->wacom_wac.features;
	unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);

	/*
	 * The Dell Canvas 27 needs to be switched to its vendor-defined
	 * report to provide the best resolution.
	 */
	if (hdev->vendor == USB_VENDOR_ID_WACOM &&
	    hdev->product == 0x4200 &&
	    field->application == HID_UP_MSVENDOR) {
		wacom->wacom_wac.mode_report = field->report->id;
		wacom->wacom_wac.mode_value = 2;
	}

	/*
	 * ISDv4 devices which predate HID's adoption of the
	 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
	 * position instead. We can accurately detect if a
	 * usage with that value should be HID_DG_BARRELSWITCH2
	 * based on the surrounding usages, which have remained
	 * constant across generations.
	 */
	if (features->type == HID_GENERIC &&
	    usage->hid == 0x000D0000 &&
	    field->application == HID_DG_PEN &&
	    field->physical == HID_DG_STYLUS) {
		int i = usage->usage_index;

		if (i-4 >= 0 && i+1 < field->maxusage &&
		    field->usage[i-4].hid == HID_DG_TIPSWITCH &&
		    field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
		    field->usage[i-2].hid == HID_DG_ERASER &&
		    field->usage[i-1].hid == HID_DG_INVERT &&
		    field->usage[i+1].hid == HID_DG_INRANGE) {
			usage->hid = HID_DG_BARRELSWITCH2;
		}
	}

	/* 2nd-generation Intuos Pro Large has incorrect Y maximum */
	if (hdev->vendor == USB_VENDOR_ID_WACOM &&
	    hdev->product == 0x0358 &&
	    WACOM_PEN_FIELD(field) &&
	    equivalent_usage == HID_GD_Y) {
		field->logical_maximum = 43200;
	}
}

265
266
static void wacom_feature_mapping(struct hid_device *hdev,
		struct hid_field *field, struct hid_usage *usage)
267
{
268
269
	struct wacom *wacom = hid_get_drvdata(hdev);
	struct wacom_features *features = &wacom->wacom_wac.features;
270
	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
271
	unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
272
273
	u8 *data;
	int ret;
Aaron Ma's avatar
Aaron Ma committed
274
	u32 n;
275

276
277
	wacom_hid_usage_quirk(hdev, field, usage);

278
	switch (equivalent_usage) {
279
280
281
	case WACOM_HID_WD_TOUCH_RING_SETTING:
		wacom->generic_has_leds = true;
		break;
282
283
	case HID_DG_CONTACTMAX:
		/* leave touch_max as is if predefined */
284
285
		if (!features->touch_max) {
			/* read manually */
286
287
			n = hid_report_len(field->report);
			data = hid_alloc_report_buf(field->report, GFP_KERNEL);
288
289
290
291
			if (!data)
				break;
			data[0] = field->report->id;
			ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
292
293
294
295
					       data, n, WAC_CMD_RETRIES);
			if (ret == n) {
				ret = hid_report_raw_event(hdev,
					HID_FEATURE_REPORT, data, n, 0);
296
297
298
299
300
301
302
			} else {
				features->touch_max = 16;
				hid_warn(hdev, "wacom_feature_mapping: "
					 "could not get HID_DG_CONTACTMAX, "
					 "defaulting to %d\n",
					  features->touch_max);
			}
303
304
			kfree(data);
		}
305
		break;
306
307
308
309
310
311
312
313
314
315
	case HID_DG_INPUTMODE:
		/* Ignore if value index is out of bounds. */
		if (usage->usage_index >= field->report_count) {
			dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
			break;
		}

		hid_data->inputmode = field->report->id;
		hid_data->inputmode_index = usage->usage_index;
		break;
316
317
318

	case HID_UP_DIGITIZER:
		if (field->report->id == 0x0B &&
319
320
		    (field->application == WACOM_HID_G9_PEN ||
		     field->application == WACOM_HID_G11_PEN)) {
321
322
323
324
325
			wacom->wacom_wac.mode_report = field->report->id;
			wacom->wacom_wac.mode_value = 0;
		}
		break;

326
327
328
329
330
	case WACOM_HID_WD_DATAMODE:
		wacom->wacom_wac.mode_report = field->report->id;
		wacom->wacom_wac.mode_value = 2;
		break;

331
332
	case WACOM_HID_UP_G9:
	case WACOM_HID_UP_G11:
333
		if (field->report->id == 0x03 &&
334
335
		    (field->application == WACOM_HID_G9_TOUCHSCREEN ||
		     field->application == WACOM_HID_G11_TOUCHSCREEN)) {
336
337
338
339
			wacom->wacom_wac.mode_report = field->report->id;
			wacom->wacom_wac.mode_value = 0;
		}
		break;
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
	case WACOM_HID_WD_OFFSETLEFT:
	case WACOM_HID_WD_OFFSETTOP:
	case WACOM_HID_WD_OFFSETRIGHT:
	case WACOM_HID_WD_OFFSETBOTTOM:
		/* read manually */
		n = hid_report_len(field->report);
		data = hid_alloc_report_buf(field->report, GFP_KERNEL);
		if (!data)
			break;
		data[0] = field->report->id;
		ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
					data, n, WAC_CMD_RETRIES);
		if (ret == n) {
			ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
						   data, n, 0);
		} else {
			hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
				 __func__);
		}
		kfree(data);
		break;
361
362
363
	}
}

364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 * Interface Descriptor of wacom devices can be incomplete and
 * inconsistent so wacom_features table is used to store stylus
 * device's packet lengths, various maximum values, and tablet
 * resolution based on product ID's.
 *
 * For devices that contain 2 interfaces, wacom_features table is
 * inaccurate for the touch interface.  Since the Interface Descriptor
 * for touch interfaces has pretty complete data, this function exists
 * to query tablet for this missing information instead of hard coding in
 * an additional table.
 *
 * A typical Interface Descriptor for a stylus will contain a
 * boot mouse application collection that is not of interest and this
 * function will ignore it.
 *
 * It also contains a digitizer application collection that also is not
 * of interest since any information it contains would be duplicate
 * of what is in wacom_features. Usually it defines a report of an array
 * of bytes that could be used as max length of the stylus packet returned.
 * If it happens to define a Digitizer-Stylus Physical Collection then
 * the X and Y logical values contain valid data but it is ignored.
 *
 * A typical Interface Descriptor for a touch interface will contain a
 * Digitizer-Finger Physical Collection which will define both logical
 * X/Y maximum as well as the physical size of tablet. Since touch
 * interfaces haven't supported pressure or distance, this is enough
 * information to override invalid values in the wacom_features table.
392
 *
393
394
 * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
 * data. We deal with them after returning from this function.
395
 */
396
397
static void wacom_usage_mapping(struct hid_device *hdev,
		struct hid_field *field, struct hid_usage *usage)
398
{
399
400
	struct wacom *wacom = hid_get_drvdata(hdev);
	struct wacom_features *features = &wacom->wacom_wac.features;
401
402
	bool finger = WACOM_FINGER_FIELD(field);
	bool pen = WACOM_PEN_FIELD(field);
403
	unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
404

405
406
407
408
409
	/*
	* Requiring Stylus Usage will ignore boot mouse
	* X/Y values and some cases of invalid Digitizer X/Y
	* values commonly reported.
	*/
410
	if (pen)
411
		features->device_type |= WACOM_DEVICETYPE_PEN;
412
	else if (finger)
413
		features->device_type |= WACOM_DEVICETYPE_TOUCH;
414
	else
415
416
		return;

417
	wacom_hid_usage_quirk(hdev, field, usage);
418

419
	switch (equivalent_usage) {
420
421
422
423
	case HID_GD_X:
		features->x_max = field->logical_maximum;
		if (finger) {
			features->x_phy = field->physical_maximum;
424
425
			if ((features->type != BAMBOO_PT) &&
			    (features->type != BAMBOO_TOUCH)) {
426
427
				features->unit = field->unit;
				features->unitExpo = field->unit_exponent;
428
			}
429
430
431
432
433
434
		}
		break;
	case HID_GD_Y:
		features->y_max = field->logical_maximum;
		if (finger) {
			features->y_phy = field->physical_maximum;
435
436
			if ((features->type != BAMBOO_PT) &&
			    (features->type != BAMBOO_TOUCH)) {
437
438
439
440
441
442
443
444
445
446
				features->unit = field->unit;
				features->unitExpo = field->unit_exponent;
			}
		}
		break;
	case HID_DG_TIPPRESSURE:
		if (pen)
			features->pressure_max = field->logical_maximum;
		break;
	}
447
448
449

	if (features->type == HID_GENERIC)
		wacom_wac_usage_mapping(hdev, field, usage);
450
}
451

452
453
454
455
456
457
458
459
static void wacom_post_parse_hid(struct hid_device *hdev,
				 struct wacom_features *features)
{
	struct wacom *wacom = hid_get_drvdata(hdev);
	struct wacom_wac *wacom_wac = &wacom->wacom_wac;

	if (features->type == HID_GENERIC) {
		/* Any last-minute generic device setup */
460
461
462
463
464
465
466
		if (wacom_wac->has_mode_change) {
			if (wacom_wac->is_direct_mode)
				features->device_type |= WACOM_DEVICETYPE_DIRECT;
			else
				features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
		}

467
		if (features->touch_max > 1) {
468
469
470
471
472
473
474
475
			if (features->device_type & WACOM_DEVICETYPE_DIRECT)
				input_mt_init_slots(wacom_wac->touch_input,
						    wacom_wac->features.touch_max,
						    INPUT_MT_DIRECT);
			else
				input_mt_init_slots(wacom_wac->touch_input,
						    wacom_wac->features.touch_max,
						    INPUT_MT_POINTER);
476
477
478
479
		}
	}
}

480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
static void wacom_parse_hid(struct hid_device *hdev,
			   struct wacom_features *features)
{
	struct hid_report_enum *rep_enum;
	struct hid_report *hreport;
	int i, j;

	/* check features first */
	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
	list_for_each_entry(hreport, &rep_enum->report_list, list) {
		for (i = 0; i < hreport->maxfield; i++) {
			/* Ignore if report count is out of bounds. */
			if (hreport->field[i]->report_count < 1)
				continue;

			for (j = 0; j < hreport->field[i]->maxusage; j++) {
				wacom_feature_mapping(hdev, hreport->field[i],
						hreport->field[i]->usage + j);
498
			}
499
500
501
		}
	}

502
503
504
505
506
507
508
509
510
511
512
513
	/* now check the input usages */
	rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
	list_for_each_entry(hreport, &rep_enum->report_list, list) {

		if (!hreport->maxfield)
			continue;

		for (i = 0; i < hreport->maxfield; i++)
			for (j = 0; j < hreport->field[i]->maxusage; j++)
				wacom_usage_mapping(hdev, hreport->field[i],
						hreport->field[i]->usage + j);
	}
514
515

	wacom_post_parse_hid(hdev, features);
516
517
}

518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
static int wacom_hid_set_device_mode(struct hid_device *hdev)
{
	struct wacom *wacom = hid_get_drvdata(hdev);
	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
	struct hid_report *r;
	struct hid_report_enum *re;

	if (hid_data->inputmode < 0)
		return 0;

	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
	r = re->report_id_hash[hid_data->inputmode];
	if (r) {
		r->field[0]->value[hid_data->inputmode_index] = 2;
		hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
	}
	return 0;
}

537
538
static int wacom_set_device_mode(struct hid_device *hdev,
				 struct wacom_wac *wacom_wac)
539
{
540
541
542
	u8 *rep_data;
	struct hid_report *r;
	struct hid_report_enum *re;
Aaron Ma's avatar
Aaron Ma committed
543
	u32 length;
544
	int error = -ENOMEM, limit = 0;
545

546
547
548
549
550
551
552
553
554
	if (wacom_wac->mode_report < 0)
		return 0;

	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
	r = re->report_id_hash[wacom_wac->mode_report];
	if (!r)
		return -EINVAL;

	rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
555
	if (!rep_data)
556
557
558
		return -ENOMEM;

	length = hid_report_len(r);
559

560
	do {
561
562
		rep_data[0] = wacom_wac->mode_report;
		rep_data[1] = wacom_wac->mode_value;
563

564
565
		error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
					 length, 1);
566
		if (error >= 0)
567
			error = wacom_get_report(hdev, HID_FEATURE_REPORT,
568
			                         rep_data, length, 1);
569
570
571
	} while (error >= 0 &&
		 rep_data[1] != wacom_wac->mode_report &&
		 limit++ < WAC_MSG_RETRIES);
572
573
574
575
576
577

	kfree(rep_data);

	return error < 0 ? error : 0;
}

578
579
580
static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
		struct wacom_features *features)
{
581
582
583
584
585
586
587
588
	struct wacom *wacom = hid_get_drvdata(hdev);
	int ret;
	u8 rep_data[2];

	switch (features->type) {
	case GRAPHIRE_BT:
		rep_data[0] = 0x03;
		rep_data[1] = 0x00;
589
590
		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
					3);
591
592
593
594
595
596

		if (ret >= 0) {
			rep_data[0] = speed == 0 ? 0x05 : 0x06;
			rep_data[1] = 0x00;

			ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
597
						rep_data, 2, 3);
598
599
600
601
602
603
604
605
606
607
608
609
610
611

			if (ret >= 0) {
				wacom->wacom_wac.bt_high_speed = speed;
				return 0;
			}
		}

		/*
		 * Note that if the raw queries fail, it's not a hard failure
		 * and it is safe to continue
		 */
		hid_warn(hdev, "failed to poke device, command %d, err %d\n",
			 rep_data[0], ret);
		break;
612
613
614
615
616
617
618
619
620
	case INTUOS4WL:
		if (speed == 1)
			wacom->wacom_wac.bt_features &= ~0x20;
		else
			wacom->wacom_wac.bt_features |= 0x20;

		rep_data[0] = 0x03;
		rep_data[1] = wacom->wacom_wac.bt_features;

621
622
		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
					1);
623
624
625
		if (ret >= 0)
			wacom->wacom_wac.bt_high_speed = speed;
		break;
626
627
	}

628
629
630
	return 0;
}

631
632
633
634
635
636
637
/*
 * Switch the tablet into its most-capable mode. Wacom tablets are
 * typically configured to power-up in a mode which sends mouse-like
 * reports to the OS. To get absolute position, pressure data, etc.
 * from the tablet, it is necessary to switch the tablet out of this
 * mode and into one which sends the full range of tablet data.
 */
638
static int _wacom_query_tablet_data(struct wacom *wacom)
639
{
640
	struct hid_device *hdev = wacom->hdev;
641
	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
642
	struct wacom_features *features = &wacom_wac->features;
643

644
645
646
	if (hdev->bus == BUS_BLUETOOTH)
		return wacom_bt_query_tablet_data(hdev, 1, features);

647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
	if (features->type != HID_GENERIC) {
		if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
			if (features->type > TABLETPC) {
				/* MT Tablet PC touch */
				wacom_wac->mode_report = 3;
				wacom_wac->mode_value = 4;
			} else if (features->type == WACOM_24HDT) {
				wacom_wac->mode_report = 18;
				wacom_wac->mode_value = 2;
			} else if (features->type == WACOM_27QHDT) {
				wacom_wac->mode_report = 131;
				wacom_wac->mode_value = 2;
			} else if (features->type == BAMBOO_PAD) {
				wacom_wac->mode_report = 2;
				wacom_wac->mode_value = 2;
			}
		} else if (features->device_type & WACOM_DEVICETYPE_PEN) {
			if (features->type <= BAMBOO_PT) {
				wacom_wac->mode_report = 2;
				wacom_wac->mode_value = 2;
			}
668
		}
669
	}
670

671
672
673
674
675
	wacom_set_device_mode(hdev, wacom_wac);

	if (features->type == HID_GENERIC)
		return wacom_hid_set_device_mode(hdev);

676
	return 0;
677
678
}

679
static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
680
					 struct wacom_features *features)
681
{
682
683
	struct wacom *wacom = hid_get_drvdata(hdev);
	struct usb_interface *intf = wacom->intf;
684

685
686
687
688
	/* default features */
	features->x_fuzz = 4;
	features->y_fuzz = 4;
	features->pressure_fuzz = 0;
689
690
	features->distance_fuzz = 1;
	features->tilt_fuzz = 1;
691

692
693
694
695
696
697
698
	/*
	 * The wireless device HID is basic and layout conflicts with
	 * other tablets (monitor and touch interface can look like pen).
	 * Skip the query for this type and modify defaults based on
	 * interface number.
	 */
	if (features->type == WIRELESS) {
699
		if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
700
			features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
701
		else
702
			features->device_type = WACOM_DEVICETYPE_NONE;
703
		return;
704
705
	}

706
	wacom_parse_hid(hdev, features);
707
708
}

709
struct wacom_hdev_data {
710
711
	struct list_head list;
	struct kref kref;
712
	struct hid_device *dev;
713
714
715
716
717
718
	struct wacom_shared shared;
};

static LIST_HEAD(wacom_udev_list);
static DEFINE_MUTEX(wacom_udev_list_lock);

719
720
static bool wacom_are_sibling(struct hid_device *hdev,
		struct hid_device *sibling)
721
{
722
723
	struct wacom *wacom = hid_get_drvdata(hdev);
	struct wacom_features *features = &wacom->wacom_wac.features;
724
725
726
727
	struct wacom *sibling_wacom = hid_get_drvdata(sibling);
	struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
	__u32 oVid = features->oVid ? features->oVid : hdev->vendor;
	__u32 oPid = features->oPid ? features->oPid : hdev->product;
728

729
730
731
732
733
734
735
736
737
738
739
740
	/* The defined oVid/oPid must match that of the sibling */
	if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
		return false;
	if (features->oPid != HID_ANY_ID && sibling->product != oPid)
		return false;

	/*
	 * Devices with the same VID/PID must share the same physical
	 * device path, while those with different VID/PID must share
	 * the same physical parent device path.
	 */
	if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
741
		if (!hid_compare_device_paths(hdev, sibling, '/'))
742
743
			return false;
	} else {
744
		if (!hid_compare_device_paths(hdev, sibling, '.'))
745
			return false;
746
	}
747

748
749
750
751
752
753
754
755
756
757
	/* Skip the remaining heuristics unless you are a HID_GENERIC device */
	if (features->type != HID_GENERIC)
		return true;

	/*
	 * Direct-input devices may not be siblings of indirect-input
	 * devices.
	 */
	if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
	    !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
758
		return false;
759

760
761
762
763
764
765
	/*
	 * Indirect-input devices may not be siblings of direct-input
	 * devices.
	 */
	if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
	    (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
766
		return false;
767

768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
	/* Pen devices may only be siblings of touch devices */
	if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
	    !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
		return false;

	/* Touch devices may only be siblings of pen devices */
	if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
	    !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
		return false;

	/*
	 * No reason could be found for these two devices to NOT be
	 * siblings, so there's a good chance they ARE siblings
	 */
	return true;
783
784
}

785
static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
786
{
787
	struct wacom_hdev_data *data;
788

789
790
	/* Try to find an already-probed interface from the same device */
	list_for_each_entry(data, &wacom_udev_list, list) {
791
		if (hid_compare_device_paths(hdev, data->dev, '/')) {
792
			kref_get(&data->kref);
793
			return data;
794
		}
795
796
797
	}

	/* Fallback to finding devices that appear to be "siblings" */
798
	list_for_each_entry(data, &wacom_udev_list, list) {
799
		if (wacom_are_sibling(hdev, data->dev)) {
800
801
802
803
804
805
806
807
			kref_get(&data->kref);
			return data;
		}
	}

	return NULL;
}

808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
static void wacom_release_shared_data(struct kref *kref)
{
	struct wacom_hdev_data *data =
		container_of(kref, struct wacom_hdev_data, kref);

	mutex_lock(&wacom_udev_list_lock);
	list_del(&data->list);
	mutex_unlock(&wacom_udev_list_lock);

	kfree(data);
}

static void wacom_remove_shared_data(void *res)
{
	struct wacom *wacom = res;
	struct wacom_hdev_data *data;
	struct wacom_wac *wacom_wac = &wacom->wacom_wac;

	if (wacom_wac->shared) {
		data = container_of(wacom_wac->shared, struct wacom_hdev_data,
				    shared);

		if (wacom_wac->shared->touch == wacom->hdev)
			wacom_wac->shared->touch = NULL;
		else if (wacom_wac->shared->pen == wacom->hdev)
			wacom_wac->shared->pen = NULL;

		kref_put(&data->kref, wacom_release_shared_data);
		wacom_wac->shared = NULL;
	}
}

840
static int wacom_add_shared_data(struct hid_device *hdev)
841
{
842
843
844
	struct wacom *wacom = hid_get_drvdata(hdev);
	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
	struct wacom_hdev_data *data;
845
846
847
848
	int retval = 0;

	mutex_lock(&wacom_udev_list_lock);

849
	data = wacom_get_hdev_data(hdev);
850
	if (!data) {
851
		data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
852
853
854
855
856
857
		if (!data) {
			retval = -ENOMEM;
			goto out;
		}

		kref_init(&data->kref);
858
		data->dev = hdev;
859
860
861
		list_add_tail(&data->list, &wacom_udev_list);
	}

862
	wacom_wac->shared = &data->shared;
863

864
865
866
867
868
869
870
	retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
	if (retval) {
		mutex_unlock(&wacom_udev_list_lock);
		wacom_remove_shared_data(wacom);
		return retval;
	}

871
872
873
874
875
	if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
		wacom_wac->shared->touch = hdev;
	else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
		wacom_wac->shared->pen = hdev;

876
877
878
879
880
out:
	mutex_unlock(&wacom_udev_list_lock);
	return retval;
}

881
882
883
static int wacom_led_control(struct wacom *wacom)
{
	unsigned char *buf;
884
	int retval;
885
886
	unsigned char report_id = WAC_CMD_LED_CONTROL;
	int buf_size = 9;
887

888
889
890
	if (!wacom->led.groups)
		return -ENOTSUPP;

891
892
893
	if (wacom->wacom_wac.features.type == REMOTE)
		return -ENOTSUPP;

894
895
896
897
	if (wacom->wacom_wac.pid) { /* wireless connected */
		report_id = WAC_CMD_WL_LED_CONTROL;
		buf_size = 13;
	}
898
899
900
901
	else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
		report_id = WAC_CMD_WL_INTUOSP2;
		buf_size = 51;
	}
902
	buf = kzalloc(buf_size, GFP_KERNEL);
903
904
905
	if (!buf)
		return -ENOMEM;

906
907
908
909
910
911
912
	if (wacom->wacom_wac.features.type == HID_GENERIC) {
		buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
		buf[1] = wacom->led.llv;
		buf[2] = wacom->led.groups[0].select & 0x03;

	} else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
	    wacom->wacom_wac.features.type <= INTUOSPL)) {
913
914
915
916
917
		/*
		 * Touch Ring and crop mark LED luminance may take on
		 * one of four values:
		 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
		 */
918
		int ring_led = wacom->led.groups[0].select & 0x03;
919
920
		int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
		int crop_lum = 0;
921
922
923
924
925
926
927
928
929
930
		unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);

		buf[0] = report_id;
		if (wacom->wacom_wac.pid) {
			wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
					 buf, buf_size, WAC_CMD_RETRIES);
			buf[0] = report_id;
			buf[4] = led_bits;
		} else
			buf[1] = led_bits;
931
	}
932
933
934
935
936
937
938
939
940
941
	else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
		buf[0] = report_id;
		buf[4] = 100; // Power Connection LED (ORANGE)
		buf[5] = 100; // BT Connection LED (BLUE)
		buf[6] = 100; // Paper Mode (RED?)
		buf[7] = 100; // Paper Mode (GREEN?)
		buf[8] = 100; // Paper Mode (BLUE?)
		buf[9] = wacom->led.llv;
		buf[10] = wacom->led.groups[0].select & 0x03;
	}
942
	else {
943
		int led = wacom->led.groups[0].select | 0x4;
944
945
946

		if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
		    wacom->wacom_wac.features.type == WACOM_24HD)
947
			led |= (wacom->led.groups[1].select << 4) | 0x40;
948

949
		buf[0] = report_id;
950
951
952
953
954
		buf[1] = led;
		buf[2] = wacom->led.llv;
		buf[3] = wacom->led.hlv;
		buf[4] = wacom->led.img_lum;
	}
955

956
	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
957
				  WAC_CMD_RETRIES);
958
959
960
961
962
	kfree(buf);

	return retval;
}

963
964
static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
		const unsigned len, const void *img)
965
966
967
{
	unsigned char *buf;
	int i, retval;
968
	const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
969

970
	buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
971
972
973
974
975
976
	if (!buf)
		return -ENOMEM;

	/* Send 'start' command */
	buf[0] = WAC_CMD_ICON_START;
	buf[1] = 1;
977
978
	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
				  WAC_CMD_RETRIES);
979
980
981
	if (retval < 0)
		goto out;

982
	buf[0] = xfer_id;
983
984
985
	buf[1] = button_id & 0x07;
	for (i = 0; i < 4; i++) {
		buf[2] = i;
986
		memcpy(buf + 3, img + i * chunk_len, chunk_len);
987

988
		retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
989
					  buf, chunk_len + 3, WAC_CMD_RETRIES);
990
991
992
993
994
995
996
		if (retval < 0)
			break;
	}

	/* Send 'stop' */
	buf[0] = WAC_CMD_ICON_START;
	buf[1] = 0;
997
998
	wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
			 WAC_CMD_RETRIES);
999
1000
1001
1002
1003
1004

out:
	kfree(buf);
	return retval;
}

1005
static ssize_t wacom_led_select_store(struct device *dev, int set_id,
1006
1007
				      const char *buf, size_t count)
{
Geliang Tang's avatar
Geliang Tang committed
1008
	struct hid_device *hdev = to_hid_device(dev);
1009
	struct wacom *wacom = hid_get_drvdata(hdev);
1010
1011
1012
1013
1014
1015
1016
1017
1018
	unsigned int id;
	int err;

	err = kstrtouint(buf, 10, &id);
	if (err)
		return err;

	mutex_lock(&wacom->lock);

1019
	wacom->led.groups[set_id].select = id & 0x3;
1020
1021
1022
1023
1024
1025
1026
	err = wacom_led_control(wacom);

	mutex_unlock(&wacom->lock);

	return err < 0 ? err : count;
}

1027
1028
1029
1030
1031
1032
#define DEVICE_LED_SELECT_ATTR(SET_ID)					\
static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
	struct device_attribute *attr, const char *buf, size_t count)	\
{									\
	return wacom_led_select_store(dev, SET_ID, buf, count);		\
}									\
1033
1034
1035
static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
	struct device_attribute *attr, char *buf)			\
{									\
Geliang Tang's avatar
Geliang Tang committed
1036
	struct hid_device *hdev = to_hid_device(dev);\
1037
	struct wacom *wacom = hid_get_drvdata(hdev);			\
Ping Cheng's avatar
Ping Cheng committed
1038
	return scnprintf(buf, PAGE_SIZE, "%d\n",			\
1039
			 wacom->led.groups[SET_ID].select);		\
1040
}									\
1041
static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,	\
1042
		    wacom_led##SET_ID##_select_show,			\
1043
1044
1045
1046
		    wacom_led##SET_ID##_select_store)

DEVICE_LED_SELECT_ATTR(0);
DEVICE_LED_SELECT_ATTR(1);
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071

static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
				     const char *buf, size_t count)
{
	unsigned int value;
	int err;

	err = kstrtouint(buf, 10, &value);
	if (err)
		return err;

	mutex_lock(&wacom->lock);

	*dest = value & 0x7f;
	err = wacom_led_control(wacom);

	mutex_unlock(&wacom->lock);

	return err < 0 ? err : count;
}

#define DEVICE_LUMINANCE_ATTR(name, field)				\
static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
	struct device_attribute *attr, const char *buf, size_t count)	\
{									\
Geliang Tang's avatar
Geliang Tang committed
1072
	struct hid_device *hdev = to_hid_device(dev);\
1073
	struct wacom *wacom = hid_get_drvdata(hdev);			\
1074
1075
1076
1077
									\
	return wacom_luminance_store(wacom, &wacom->led.field,		\
				     buf, count);			\
}									\
Ping Cheng's avatar
Ping Cheng committed
1078
1079
1080
1081
1082
1083
static ssize_t wacom_##name##_luminance_show(struct device *dev,	\
	struct device_attribute *attr, char *buf)			\
{									\
	struct wacom *wacom = dev_get_drvdata(dev);			\
	return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);	\
}									\
1084
static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,			\
Ping Cheng's avatar
Ping Cheng committed
1085
1086
		   wacom_##name##_luminance_show,			\
		   wacom_##name##_luminance_store)
1087
1088
1089
1090
1091
1092
1093
1094

DEVICE_LUMINANCE_ATTR(status0, llv);
DEVICE_LUMINANCE_ATTR(status1, hlv);
DEVICE_LUMINANCE_ATTR(buttons, img_lum);

static ssize_t wacom_button_image_store(struct device *dev, int button_id,
					const char *buf, size_t count)
{
Geliang Tang's avatar
Geliang Tang committed
1095
	struct hid_device *hdev = to_hid_device(dev);
1096
	struct wacom *wacom = hid_get_drvdata(hdev);
1097
	int err;
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
	unsigned len;
	u8 xfer_id;

	if (hdev->bus == BUS_BLUETOOTH) {
		len = 256;
		xfer_id = WAC_CMD_ICON_BT_XFER;
	} else {
		len = 1024;
		xfer_id = WAC_CMD_ICON_XFER;
	}
1108

1109
	if (count != len)
1110
1111
1112
1113
		return -EINVAL;

	mutex_lock(&wacom->lock);

1114
	err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126

	mutex_unlock(&wacom->lock);

	return err < 0 ? err : count;
}

#define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
	struct device_attribute *attr, const char *buf, size_t count)	\
{									\
	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
}									\
1127
static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,	\
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
		   NULL, wacom_btnimg##BUTTON_ID##_store)

DEVICE_BTNIMG_ATTR(0);
DEVICE_BTNIMG_ATTR(1);
DEVICE_BTNIMG_ATTR(2);
DEVICE_BTNIMG_ATTR(3);
DEVICE_BTNIMG_ATTR(4);
DEVICE_BTNIMG_ATTR(5);
DEVICE_BTNIMG_ATTR(6);
DEVICE_BTNIMG_ATTR(7);

1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
static struct attribute *cintiq_led_attrs[] = {
	&dev_attr_status_led0_select.attr,
	&dev_attr_status_led1_select.attr,
	NULL
};

static struct attribute_group cintiq_led_attr_group = {
	.name = "wacom_led",
	.attrs = cintiq_led_attrs,
};

static struct attribute *intuos4_led_attrs[] = {
1151
1152
	&dev_attr_status0_luminance.attr,
	&dev_attr_status1_luminance.attr,
1153
	&dev_attr_status_led0_select.attr,
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
	&dev_attr_buttons_luminance.attr,
	&dev_attr_button0_rawimg.attr,
	&dev_attr_button1_rawimg.attr,
	&dev_attr_button2_rawimg.attr,
	&dev_attr_button3_rawimg.attr,
	&dev_attr_button4_rawimg.attr,
	&dev_attr_button5_rawimg.attr,
	&dev_attr_button6_rawimg.attr,
	&dev_attr_button7_rawimg.attr,
	NULL
};

1166
static struct attribute_group intuos4_led_attr_group = {
1167
	.name = "wacom_led",
1168
	.attrs = intuos4_led_attrs,
1169
1170
};

1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
static struct attribute *intuos5_led_attrs[] = {
	&dev_attr_status0_luminance.attr,
	&dev_attr_status_led0_select.attr,
	NULL
};

static struct attribute_group intuos5_led_attr_group = {
	.name = "wacom_led",
	.attrs = intuos5_led_attrs,
};

1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
static struct attribute *generic_led_attrs[] = {
	&dev_attr_status0_luminance.attr,
	&dev_attr_status_led0_select.attr,
	NULL
};

static struct attribute_group generic_led_attr_group = {
	.name = "wacom_led",
	.attrs = generic_led_attrs,
};

1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
struct wacom_sysfs_group_devres {
	struct attribute_group *group;
	struct kobject *root;
};

static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
{
	struct wacom_sysfs_group_devres *devres = res;
	struct kobject *kobj = devres->root;

	dev_dbg(dev, "%s: dropping reference to %s\n",
		__func__, devres->group->name);
	sysfs_remove_group(kobj, devres->group);
}

1208
1209
1210
static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
					   struct kobject *root,
					   struct attribute_group *group)
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
{
	struct wacom_sysfs_group_devres *devres;
	int error;

	devres = devres_alloc(wacom_devm_sysfs_group_release,
			      sizeof(struct wacom_sysfs_group_devres),
			      GFP_KERNEL);
	if (!devres)
		return -ENOMEM;

	devres->group = group;
1222
	devres->root = root;
1223
1224

	error = sysfs_create_group(devres->root, group);
1225
1226
	if (error) {
		devres_free(devres);
1227
		return error;
1228
	}
1229
1230
1231
1232
1233
1234

	devres_add(&wacom->hdev->dev, devres);

	return 0;
}

1235
1236
1237
1238
1239
1240
1241
static int wacom_devm_sysfs_create_group(struct wacom *wacom,
					 struct attribute_group *group)
{
	return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
					       group);
}

1242
enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
{
	struct wacom *wacom = led->wacom;

	if (wacom->led.max_hlv)
		return led->hlv * LED_FULL / wacom->led.max_hlv;

	if (wacom->led.max_llv)
		return led->llv * LED_FULL / wacom->led.max_llv;

	/* device doesn't support brightness tuning */
	return LED_FULL;
}

static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
{
	struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
	struct wacom *wacom = led->wacom;

	if (wacom->led.groups[led->group].select != led->id)
		return LED_OFF;

	return wacom_leds_brightness_get(led);
}

static int wacom_led_brightness_set(struct led_classdev *cdev,
				    enum led_brightness brightness)
{
	struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
	struct wacom *wacom = led->wacom;
	int error;

	mutex_lock(&wacom->lock);

	if (!wacom->led.groups || (brightness == LED_OFF &&
	    wacom->led.groups[led->group].select != led->id)) {
		error = 0;
		goto out;
	}

	led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
	led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;

	wacom->led.groups[led->group].select = led->id;

	error = wacom_led_control(wacom);

out:
	mutex_unlock(&wacom->lock);

	return error;
}

static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
					       enum led_brightness brightness)
{
}

static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
				  struct wacom_led *led, unsigned int group,
				  unsigned int id, bool read_only)
{
	int error;
	char *name;

	name = devm_kasprintf(dev, GFP_KERNEL,
			      "%s::wacom-%d.%d",
			      dev_name(dev),
			      group,
			      id);
	if (!name)
		return -ENOMEM;

1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
	if (!read_only) {
		led->trigger.name = name;
		error = devm_led_trigger_register(dev, &led->trigger);
		if (error) {
			hid_err(wacom->hdev,
				"failed to register LED trigger %s: %d\n",
				led->cdev.name, error);
			return error;
		}
	}

1326
1327
1328
1329
1330
1331
1332
1333
1334
	led->group = group;
	led->