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