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