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