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