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