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