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