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