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