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