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