xref: /openbmc/linux/drivers/hid/wacom_sys.c (revision d883312489df262501d635b706be43080b30f1ec)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2471d1714SBenjamin Tissoires /*
3471d1714SBenjamin Tissoires  * drivers/input/tablet/wacom_sys.c
4471d1714SBenjamin Tissoires  *
5471d1714SBenjamin Tissoires  *  USB Wacom tablet support - system specific code
6471d1714SBenjamin Tissoires  */
7471d1714SBenjamin Tissoires 
8471d1714SBenjamin Tissoires /*
9471d1714SBenjamin Tissoires  */
10471d1714SBenjamin Tissoires 
11471d1714SBenjamin Tissoires #include "wacom_wac.h"
12471d1714SBenjamin Tissoires #include "wacom.h"
13b58ba1baSJason Gerecke #include <linux/input/mt.h>
14471d1714SBenjamin Tissoires 
15471d1714SBenjamin Tissoires #define WAC_MSG_RETRIES		5
16471d1714SBenjamin Tissoires #define WAC_CMD_RETRIES		10
17471d1714SBenjamin Tissoires 
18e0984bc3SPing Cheng #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
19e0984bc3SPing Cheng #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
2072b236d6SAaron Skomra #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
21e0984bc3SPing Cheng 
22c64d8834SPing Cheng static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
23c64d8834SPing Cheng 			    size_t size, unsigned int retries)
24471d1714SBenjamin Tissoires {
25471d1714SBenjamin Tissoires 	int retval;
26471d1714SBenjamin Tissoires 
27471d1714SBenjamin Tissoires 	do {
28c64d8834SPing Cheng 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
29471d1714SBenjamin Tissoires 				HID_REQ_GET_REPORT);
30aef3156dSJason Gerecke 	} while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
31aef3156dSJason Gerecke 
32aef3156dSJason Gerecke 	if (retval < 0)
33aef3156dSJason Gerecke 		hid_err(hdev, "wacom_get_report: ran out of retries "
34aef3156dSJason Gerecke 			"(last error = %d)\n", retval);
35471d1714SBenjamin Tissoires 
36471d1714SBenjamin Tissoires 	return retval;
37471d1714SBenjamin Tissoires }
38471d1714SBenjamin Tissoires 
39296b7378SPrzemo Firszt static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
40296b7378SPrzemo Firszt 			    size_t size, unsigned int retries)
41471d1714SBenjamin Tissoires {
42471d1714SBenjamin Tissoires 	int retval;
43471d1714SBenjamin Tissoires 
44471d1714SBenjamin Tissoires 	do {
45296b7378SPrzemo Firszt 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
46471d1714SBenjamin Tissoires 				HID_REQ_SET_REPORT);
47aef3156dSJason Gerecke 	} while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
48aef3156dSJason Gerecke 
49aef3156dSJason Gerecke 	if (retval < 0)
50aef3156dSJason Gerecke 		hid_err(hdev, "wacom_set_report: ran out of retries "
51aef3156dSJason Gerecke 			"(last error = %d)\n", retval);
52471d1714SBenjamin Tissoires 
53471d1714SBenjamin Tissoires 	return retval;
54471d1714SBenjamin Tissoires }
55471d1714SBenjamin Tissoires 
5683417206SJason Gerecke static void wacom_wac_queue_insert(struct hid_device *hdev,
5783417206SJason Gerecke 				   struct kfifo_rec_ptr_2 *fifo,
5883417206SJason Gerecke 				   u8 *raw_data, int size)
5983417206SJason Gerecke {
6083417206SJason Gerecke 	bool warned = false;
6183417206SJason Gerecke 
6283417206SJason Gerecke 	while (kfifo_avail(fifo) < size) {
6383417206SJason Gerecke 		if (!warned)
6483417206SJason Gerecke 			hid_warn(hdev, "%s: kfifo has filled, starting to drop events\n", __func__);
6583417206SJason Gerecke 		warned = true;
6683417206SJason Gerecke 
6783417206SJason Gerecke 		kfifo_skip(fifo);
6883417206SJason Gerecke 	}
6983417206SJason Gerecke 
7083417206SJason Gerecke 	kfifo_in(fifo, raw_data, size);
7183417206SJason Gerecke }
7283417206SJason Gerecke 
7383417206SJason Gerecke static void wacom_wac_queue_flush(struct hid_device *hdev,
7483417206SJason Gerecke 				  struct kfifo_rec_ptr_2 *fifo)
7583417206SJason Gerecke {
7683417206SJason Gerecke 	while (!kfifo_is_empty(fifo)) {
7783417206SJason Gerecke 		u8 buf[WACOM_PKGLEN_MAX];
7883417206SJason Gerecke 		int size;
7983417206SJason Gerecke 		int err;
8083417206SJason Gerecke 
8183417206SJason Gerecke 		size = kfifo_out(fifo, buf, sizeof(buf));
8283417206SJason Gerecke 		err = hid_report_raw_event(hdev, HID_INPUT_REPORT, buf, size, false);
8383417206SJason Gerecke 		if (err) {
8483417206SJason Gerecke 			hid_warn(hdev, "%s: unable to flush event due to error %d\n",
8583417206SJason Gerecke 				 __func__, err);
8683417206SJason Gerecke 		}
8783417206SJason Gerecke 	}
8883417206SJason Gerecke }
8983417206SJason Gerecke 
9083417206SJason Gerecke static int wacom_wac_pen_serial_enforce(struct hid_device *hdev,
91073b50bcSJason Gerecke 		struct hid_report *report, u8 *raw_data, int report_size)
9283417206SJason Gerecke {
9383417206SJason Gerecke 	struct wacom *wacom = hid_get_drvdata(hdev);
9483417206SJason Gerecke 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
9583417206SJason Gerecke 	struct wacom_features *features = &wacom_wac->features;
9683417206SJason Gerecke 	bool flush = false;
9783417206SJason Gerecke 	bool insert = false;
9883417206SJason Gerecke 	int i, j;
9983417206SJason Gerecke 
10083417206SJason Gerecke 	if (wacom_wac->serial[0] || !(features->quirks & WACOM_QUIRK_TOOLSERIAL))
10183417206SJason Gerecke 		return 0;
10283417206SJason Gerecke 
10383417206SJason Gerecke 	/* Queue events which have invalid tool type or serial number */
10483417206SJason Gerecke 	for (i = 0; i < report->maxfield; i++) {
10583417206SJason Gerecke 		for (j = 0; j < report->field[i]->maxusage; j++) {
10683417206SJason Gerecke 			struct hid_field *field = report->field[i];
10783417206SJason Gerecke 			struct hid_usage *usage = &field->usage[j];
10883417206SJason Gerecke 			unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
10983417206SJason Gerecke 			unsigned int offset;
11083417206SJason Gerecke 			unsigned int size;
11183417206SJason Gerecke 			unsigned int value;
11283417206SJason Gerecke 
11383417206SJason Gerecke 			if (equivalent_usage != HID_DG_INRANGE &&
11483417206SJason Gerecke 			    equivalent_usage != HID_DG_TOOLSERIALNUMBER &&
11583417206SJason Gerecke 			    equivalent_usage != WACOM_HID_WD_SERIALHI &&
11683417206SJason Gerecke 			    equivalent_usage != WACOM_HID_WD_TOOLTYPE)
11783417206SJason Gerecke 				continue;
11883417206SJason Gerecke 
11983417206SJason Gerecke 			offset = field->report_offset;
12083417206SJason Gerecke 			size = field->report_size;
12183417206SJason Gerecke 			value = hid_field_extract(hdev, raw_data+1, offset + j * size, size);
12283417206SJason Gerecke 
12383417206SJason Gerecke 			/* If we go out of range, we need to flush the queue ASAP */
12483417206SJason Gerecke 			if (equivalent_usage == HID_DG_INRANGE)
12583417206SJason Gerecke 				value = !value;
12683417206SJason Gerecke 
12783417206SJason Gerecke 			if (value) {
12883417206SJason Gerecke 				flush = true;
12983417206SJason Gerecke 				switch (equivalent_usage) {
13083417206SJason Gerecke 				case HID_DG_TOOLSERIALNUMBER:
13183417206SJason Gerecke 					wacom_wac->serial[0] = value;
13283417206SJason Gerecke 					break;
13383417206SJason Gerecke 
13483417206SJason Gerecke 				case WACOM_HID_WD_SERIALHI:
13583417206SJason Gerecke 					wacom_wac->serial[0] |= ((__u64)value) << 32;
13683417206SJason Gerecke 					break;
13783417206SJason Gerecke 
13883417206SJason Gerecke 				case WACOM_HID_WD_TOOLTYPE:
13983417206SJason Gerecke 					wacom_wac->id[0] = value;
14083417206SJason Gerecke 					break;
14183417206SJason Gerecke 				}
14283417206SJason Gerecke 			}
14383417206SJason Gerecke 			else {
14483417206SJason Gerecke 				insert = true;
14583417206SJason Gerecke 			}
14683417206SJason Gerecke 		}
14783417206SJason Gerecke 	}
14883417206SJason Gerecke 
14983417206SJason Gerecke 	if (flush)
150179e8e47SJason Gerecke 		wacom_wac_queue_flush(hdev, wacom_wac->pen_fifo);
15183417206SJason Gerecke 	else if (insert)
152179e8e47SJason Gerecke 		wacom_wac_queue_insert(hdev, wacom_wac->pen_fifo,
153073b50bcSJason Gerecke 				       raw_data, report_size);
15483417206SJason Gerecke 
15583417206SJason Gerecke 	return insert && !flush;
15683417206SJason Gerecke }
15783417206SJason Gerecke 
158471d1714SBenjamin Tissoires static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
159471d1714SBenjamin Tissoires 		u8 *raw_data, int size)
160471d1714SBenjamin Tissoires {
161471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
162471d1714SBenjamin Tissoires 
163471d1714SBenjamin Tissoires 	if (size > WACOM_PKGLEN_MAX)
164471d1714SBenjamin Tissoires 		return 1;
165471d1714SBenjamin Tissoires 
16683417206SJason Gerecke 	if (wacom_wac_pen_serial_enforce(hdev, report, raw_data, size))
16783417206SJason Gerecke 		return -1;
16883417206SJason Gerecke 
169471d1714SBenjamin Tissoires 	memcpy(wacom->wacom_wac.data, raw_data, size);
170471d1714SBenjamin Tissoires 
171471d1714SBenjamin Tissoires 	wacom_wac_irq(&wacom->wacom_wac, size);
172471d1714SBenjamin Tissoires 
173471d1714SBenjamin Tissoires 	return 0;
174471d1714SBenjamin Tissoires }
175471d1714SBenjamin Tissoires 
176471d1714SBenjamin Tissoires static int wacom_open(struct input_dev *dev)
177471d1714SBenjamin Tissoires {
178471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
179471d1714SBenjamin Tissoires 
180dff67416SBenjamin Tissoires 	return hid_hw_open(wacom->hdev);
181471d1714SBenjamin Tissoires }
182471d1714SBenjamin Tissoires 
183471d1714SBenjamin Tissoires static void wacom_close(struct input_dev *dev)
184471d1714SBenjamin Tissoires {
185471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
186471d1714SBenjamin Tissoires 
1873dad188eSBenjamin Tissoires 	/*
1883dad188eSBenjamin Tissoires 	 * wacom->hdev should never be null, but surprisingly, I had the case
1893dad188eSBenjamin Tissoires 	 * once while unplugging the Wacom Wireless Receiver.
1903dad188eSBenjamin Tissoires 	 */
1913dad188eSBenjamin Tissoires 	if (wacom->hdev)
192471d1714SBenjamin Tissoires 		hid_hw_close(wacom->hdev);
193471d1714SBenjamin Tissoires }
194471d1714SBenjamin Tissoires 
195471d1714SBenjamin Tissoires /*
196471d1714SBenjamin Tissoires  * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
197471d1714SBenjamin Tissoires  */
198471d1714SBenjamin Tissoires static int wacom_calc_hid_res(int logical_extents, int physical_extents,
199471d1714SBenjamin Tissoires 			       unsigned unit, int exponent)
200471d1714SBenjamin Tissoires {
201471d1714SBenjamin Tissoires 	struct hid_field field = {
202471d1714SBenjamin Tissoires 		.logical_maximum = logical_extents,
203471d1714SBenjamin Tissoires 		.physical_maximum = physical_extents,
204471d1714SBenjamin Tissoires 		.unit = unit,
205471d1714SBenjamin Tissoires 		.unit_exponent = exponent,
206471d1714SBenjamin Tissoires 	};
207471d1714SBenjamin Tissoires 
208471d1714SBenjamin Tissoires 	return hidinput_calc_abs_res(&field, ABS_X);
209471d1714SBenjamin Tissoires }
210471d1714SBenjamin Tissoires 
21157832512SJason Gerecke static void wacom_hid_usage_quirk(struct hid_device *hdev,
21257832512SJason Gerecke 		struct hid_field *field, struct hid_usage *usage)
21357832512SJason Gerecke {
21457832512SJason Gerecke 	struct wacom *wacom = hid_get_drvdata(hdev);
21557832512SJason Gerecke 	struct wacom_features *features = &wacom->wacom_wac.features;
21657832512SJason Gerecke 	unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
21757832512SJason Gerecke 
21857832512SJason Gerecke 	/*
21957832512SJason Gerecke 	 * The Dell Canvas 27 needs to be switched to its vendor-defined
22057832512SJason Gerecke 	 * report to provide the best resolution.
22157832512SJason Gerecke 	 */
22257832512SJason Gerecke 	if (hdev->vendor == USB_VENDOR_ID_WACOM &&
22357832512SJason Gerecke 	    hdev->product == 0x4200 &&
22457832512SJason Gerecke 	    field->application == HID_UP_MSVENDOR) {
22557832512SJason Gerecke 		wacom->wacom_wac.mode_report = field->report->id;
22657832512SJason Gerecke 		wacom->wacom_wac.mode_value = 2;
22757832512SJason Gerecke 	}
22857832512SJason Gerecke 
22957832512SJason Gerecke 	/*
23057832512SJason Gerecke 	 * ISDv4 devices which predate HID's adoption of the
23157832512SJason Gerecke 	 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
23257832512SJason Gerecke 	 * position instead. We can accurately detect if a
23357832512SJason Gerecke 	 * usage with that value should be HID_DG_BARRELSWITCH2
23457832512SJason Gerecke 	 * based on the surrounding usages, which have remained
23557832512SJason Gerecke 	 * constant across generations.
23657832512SJason Gerecke 	 */
23757832512SJason Gerecke 	if (features->type == HID_GENERIC &&
23857832512SJason Gerecke 	    usage->hid == 0x000D0000 &&
23957832512SJason Gerecke 	    field->application == HID_DG_PEN &&
24057832512SJason Gerecke 	    field->physical == HID_DG_STYLUS) {
24157832512SJason Gerecke 		int i = usage->usage_index;
24257832512SJason Gerecke 
24357832512SJason Gerecke 		if (i-4 >= 0 && i+1 < field->maxusage &&
24457832512SJason Gerecke 		    field->usage[i-4].hid == HID_DG_TIPSWITCH &&
24557832512SJason Gerecke 		    field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
24657832512SJason Gerecke 		    field->usage[i-2].hid == HID_DG_ERASER &&
24757832512SJason Gerecke 		    field->usage[i-1].hid == HID_DG_INVERT &&
24857832512SJason Gerecke 		    field->usage[i+1].hid == HID_DG_INRANGE) {
24957832512SJason Gerecke 			usage->hid = HID_DG_BARRELSWITCH2;
25057832512SJason Gerecke 		}
25157832512SJason Gerecke 	}
25257832512SJason Gerecke 
253e9fe0d49SJason Gerecke 	/*
254e9fe0d49SJason Gerecke 	 * Wacom's AES devices use different vendor-defined usages to
255e9fe0d49SJason Gerecke 	 * report serial number information compared to their branded
256e9fe0d49SJason Gerecke 	 * hardware. The usages are also sometimes ill-defined and do
257e9fe0d49SJason Gerecke 	 * not have the correct logical min/max values set. Lets patch
258e9fe0d49SJason Gerecke 	 * the descriptor to use the branded usage convention and fix
259e9fe0d49SJason Gerecke 	 * the errors.
260e9fe0d49SJason Gerecke 	 */
261e9fe0d49SJason Gerecke 	if (usage->hid == WACOM_HID_WT_SERIALNUMBER &&
262e9fe0d49SJason Gerecke 	    field->report_size == 16 &&
263e9fe0d49SJason Gerecke 	    field->index + 2 < field->report->maxfield) {
264e9fe0d49SJason Gerecke 		struct hid_field *a = field->report->field[field->index + 1];
265e9fe0d49SJason Gerecke 		struct hid_field *b = field->report->field[field->index + 2];
266e9fe0d49SJason Gerecke 
267e9fe0d49SJason Gerecke 		if (a->maxusage > 0 &&
268e9fe0d49SJason Gerecke 		    a->usage[0].hid == HID_DG_TOOLSERIALNUMBER &&
269e9fe0d49SJason Gerecke 		    a->report_size == 32 &&
270e9fe0d49SJason Gerecke 		    b->maxusage > 0 &&
271e9fe0d49SJason Gerecke 		    b->usage[0].hid == 0xFF000000 &&
272e9fe0d49SJason Gerecke 		    b->report_size == 8) {
273e9fe0d49SJason Gerecke 			features->quirks |= WACOM_QUIRK_AESPEN;
274e9fe0d49SJason Gerecke 			usage->hid = WACOM_HID_WD_TOOLTYPE;
275e9fe0d49SJason Gerecke 			field->logical_minimum = S16_MIN;
276e9fe0d49SJason Gerecke 			field->logical_maximum = S16_MAX;
277e9fe0d49SJason Gerecke 			a->logical_minimum = S32_MIN;
278e9fe0d49SJason Gerecke 			a->logical_maximum = S32_MAX;
279e9fe0d49SJason Gerecke 			b->usage[0].hid = WACOM_HID_WD_SERIALHI;
280e9fe0d49SJason Gerecke 			b->logical_minimum = 0;
281e9fe0d49SJason Gerecke 			b->logical_maximum = U8_MAX;
282e9fe0d49SJason Gerecke 		}
283e9fe0d49SJason Gerecke 	}
284e9fe0d49SJason Gerecke 
28557832512SJason Gerecke 	/* 2nd-generation Intuos Pro Large has incorrect Y maximum */
28657832512SJason Gerecke 	if (hdev->vendor == USB_VENDOR_ID_WACOM &&
28757832512SJason Gerecke 	    hdev->product == 0x0358 &&
28857832512SJason Gerecke 	    WACOM_PEN_FIELD(field) &&
28957832512SJason Gerecke 	    equivalent_usage == HID_GD_Y) {
29057832512SJason Gerecke 		field->logical_maximum = 43200;
29157832512SJason Gerecke 	}
29257832512SJason Gerecke }
29357832512SJason Gerecke 
294471d1714SBenjamin Tissoires static void wacom_feature_mapping(struct hid_device *hdev,
295471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
296471d1714SBenjamin Tissoires {
297471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
298471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
2995ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
300ac2423c9SAaron Armstrong Skomra 	unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
3018ffffd52SBenjamin Tissoires 	u8 *data;
3028ffffd52SBenjamin Tissoires 	int ret;
3033064a03bSAaron Ma 	u32 n;
304471d1714SBenjamin Tissoires 
30557832512SJason Gerecke 	wacom_hid_usage_quirk(hdev, field, usage);
30657832512SJason Gerecke 
307ac2423c9SAaron Armstrong Skomra 	switch (equivalent_usage) {
308d8e98060SAaron Armstrong Skomra 	case WACOM_HID_WD_TOUCH_RING_SETTING:
309d8e98060SAaron Armstrong Skomra 		wacom->generic_has_leds = true;
310d8e98060SAaron Armstrong Skomra 		break;
311471d1714SBenjamin Tissoires 	case HID_DG_CONTACTMAX:
312471d1714SBenjamin Tissoires 		/* leave touch_max as is if predefined */
3138ffffd52SBenjamin Tissoires 		if (!features->touch_max) {
3148ffffd52SBenjamin Tissoires 			/* read manually */
315184eccd4SAaron Armstrong Skomra 			n = hid_report_len(field->report);
316184eccd4SAaron Armstrong Skomra 			data = hid_alloc_report_buf(field->report, GFP_KERNEL);
3178ffffd52SBenjamin Tissoires 			if (!data)
3188ffffd52SBenjamin Tissoires 				break;
3198ffffd52SBenjamin Tissoires 			data[0] = field->report->id;
3208ffffd52SBenjamin Tissoires 			ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
321184eccd4SAaron Armstrong Skomra 					       data, n, WAC_CMD_RETRIES);
322778fbf41SJason Gerecke 			if (ret == n && features->type == HID_GENERIC) {
323184eccd4SAaron Armstrong Skomra 				ret = hid_report_raw_event(hdev,
324184eccd4SAaron Armstrong Skomra 					HID_FEATURE_REPORT, data, n, 0);
325778fbf41SJason Gerecke 			} else if (ret == 2 && features->type != HID_GENERIC) {
326778fbf41SJason Gerecke 				features->touch_max = data[1];
32705e8fd92SJason Gerecke 			} else {
32805e8fd92SJason Gerecke 				features->touch_max = 16;
32905e8fd92SJason Gerecke 				hid_warn(hdev, "wacom_feature_mapping: "
33005e8fd92SJason Gerecke 					 "could not get HID_DG_CONTACTMAX, "
33105e8fd92SJason Gerecke 					 "defaulting to %d\n",
33205e8fd92SJason Gerecke 					  features->touch_max);
33305e8fd92SJason Gerecke 			}
3348ffffd52SBenjamin Tissoires 			kfree(data);
3358ffffd52SBenjamin Tissoires 		}
336471d1714SBenjamin Tissoires 		break;
3375ae6e89fSBenjamin Tissoires 	case HID_DG_INPUTMODE:
3385ae6e89fSBenjamin Tissoires 		/* Ignore if value index is out of bounds. */
3395ae6e89fSBenjamin Tissoires 		if (usage->usage_index >= field->report_count) {
3405ae6e89fSBenjamin Tissoires 			dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
3415ae6e89fSBenjamin Tissoires 			break;
3425ae6e89fSBenjamin Tissoires 		}
3435ae6e89fSBenjamin Tissoires 
3445ae6e89fSBenjamin Tissoires 		hid_data->inputmode = field->report->id;
3455ae6e89fSBenjamin Tissoires 		hid_data->inputmode_index = usage->usage_index;
3465ae6e89fSBenjamin Tissoires 		break;
347326ea2a9SJason Gerecke 
348326ea2a9SJason Gerecke 	case HID_UP_DIGITIZER:
349326ea2a9SJason Gerecke 		if (field->report->id == 0x0B &&
3508de82280SJason Gerecke 		    (field->application == WACOM_HID_G9_PEN ||
3518de82280SJason Gerecke 		     field->application == WACOM_HID_G11_PEN)) {
352326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_report = field->report->id;
353326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_value = 0;
354326ea2a9SJason Gerecke 		}
355326ea2a9SJason Gerecke 		break;
356326ea2a9SJason Gerecke 
357c9c09587SJason Gerecke 	case WACOM_HID_WD_DATAMODE:
358c9c09587SJason Gerecke 		wacom->wacom_wac.mode_report = field->report->id;
359c9c09587SJason Gerecke 		wacom->wacom_wac.mode_value = 2;
360c9c09587SJason Gerecke 		break;
361c9c09587SJason Gerecke 
3628de82280SJason Gerecke 	case WACOM_HID_UP_G9:
3638de82280SJason Gerecke 	case WACOM_HID_UP_G11:
364326ea2a9SJason Gerecke 		if (field->report->id == 0x03 &&
3658de82280SJason Gerecke 		    (field->application == WACOM_HID_G9_TOUCHSCREEN ||
3668de82280SJason Gerecke 		     field->application == WACOM_HID_G11_TOUCHSCREEN)) {
367326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_report = field->report->id;
368326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_value = 0;
369326ea2a9SJason Gerecke 		}
370326ea2a9SJason Gerecke 		break;
371345857bbSJason Gerecke 	case WACOM_HID_WD_OFFSETLEFT:
372345857bbSJason Gerecke 	case WACOM_HID_WD_OFFSETTOP:
373345857bbSJason Gerecke 	case WACOM_HID_WD_OFFSETRIGHT:
374345857bbSJason Gerecke 	case WACOM_HID_WD_OFFSETBOTTOM:
375345857bbSJason Gerecke 		/* read manually */
376345857bbSJason Gerecke 		n = hid_report_len(field->report);
377345857bbSJason Gerecke 		data = hid_alloc_report_buf(field->report, GFP_KERNEL);
378345857bbSJason Gerecke 		if (!data)
379345857bbSJason Gerecke 			break;
380345857bbSJason Gerecke 		data[0] = field->report->id;
381345857bbSJason Gerecke 		ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
382345857bbSJason Gerecke 					data, n, WAC_CMD_RETRIES);
383345857bbSJason Gerecke 		if (ret == n) {
384345857bbSJason Gerecke 			ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
385345857bbSJason Gerecke 						   data, n, 0);
386345857bbSJason Gerecke 		} else {
387345857bbSJason Gerecke 			hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
388345857bbSJason Gerecke 				 __func__);
389345857bbSJason Gerecke 		}
390345857bbSJason Gerecke 		kfree(data);
391345857bbSJason Gerecke 		break;
392471d1714SBenjamin Tissoires 	}
393471d1714SBenjamin Tissoires }
394471d1714SBenjamin Tissoires 
395471d1714SBenjamin Tissoires /*
396471d1714SBenjamin Tissoires  * Interface Descriptor of wacom devices can be incomplete and
397471d1714SBenjamin Tissoires  * inconsistent so wacom_features table is used to store stylus
398471d1714SBenjamin Tissoires  * device's packet lengths, various maximum values, and tablet
399471d1714SBenjamin Tissoires  * resolution based on product ID's.
400471d1714SBenjamin Tissoires  *
401471d1714SBenjamin Tissoires  * For devices that contain 2 interfaces, wacom_features table is
402471d1714SBenjamin Tissoires  * inaccurate for the touch interface.  Since the Interface Descriptor
403471d1714SBenjamin Tissoires  * for touch interfaces has pretty complete data, this function exists
404471d1714SBenjamin Tissoires  * to query tablet for this missing information instead of hard coding in
405471d1714SBenjamin Tissoires  * an additional table.
406471d1714SBenjamin Tissoires  *
407471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a stylus will contain a
408471d1714SBenjamin Tissoires  * boot mouse application collection that is not of interest and this
409471d1714SBenjamin Tissoires  * function will ignore it.
410471d1714SBenjamin Tissoires  *
411471d1714SBenjamin Tissoires  * It also contains a digitizer application collection that also is not
412471d1714SBenjamin Tissoires  * of interest since any information it contains would be duplicate
413471d1714SBenjamin Tissoires  * of what is in wacom_features. Usually it defines a report of an array
414471d1714SBenjamin Tissoires  * of bytes that could be used as max length of the stylus packet returned.
415471d1714SBenjamin Tissoires  * If it happens to define a Digitizer-Stylus Physical Collection then
416471d1714SBenjamin Tissoires  * the X and Y logical values contain valid data but it is ignored.
417471d1714SBenjamin Tissoires  *
418471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a touch interface will contain a
419471d1714SBenjamin Tissoires  * Digitizer-Finger Physical Collection which will define both logical
420471d1714SBenjamin Tissoires  * X/Y maximum as well as the physical size of tablet. Since touch
421471d1714SBenjamin Tissoires  * interfaces haven't supported pressure or distance, this is enough
422471d1714SBenjamin Tissoires  * information to override invalid values in the wacom_features table.
423471d1714SBenjamin Tissoires  *
424471d1714SBenjamin Tissoires  * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
425471d1714SBenjamin Tissoires  * data. We deal with them after returning from this function.
426471d1714SBenjamin Tissoires  */
427471d1714SBenjamin Tissoires static void wacom_usage_mapping(struct hid_device *hdev,
428471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
429471d1714SBenjamin Tissoires {
430471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
431471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
432d97a5522SBenjamin Tissoires 	bool finger = WACOM_FINGER_FIELD(field);
433d97a5522SBenjamin Tissoires 	bool pen = WACOM_PEN_FIELD(field);
434418b573bSPing Cheng 	unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
435471d1714SBenjamin Tissoires 
436471d1714SBenjamin Tissoires 	/*
437471d1714SBenjamin Tissoires 	* Requiring Stylus Usage will ignore boot mouse
438471d1714SBenjamin Tissoires 	* X/Y values and some cases of invalid Digitizer X/Y
439471d1714SBenjamin Tissoires 	* values commonly reported.
440471d1714SBenjamin Tissoires 	*/
441042628abSJason Gerecke 	if (pen)
442aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_PEN;
443042628abSJason Gerecke 	else if (finger)
444aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_TOUCH;
445042628abSJason Gerecke 	else
446471d1714SBenjamin Tissoires 		return;
447471d1714SBenjamin Tissoires 
44857832512SJason Gerecke 	wacom_hid_usage_quirk(hdev, field, usage);
449471d1714SBenjamin Tissoires 
450418b573bSPing Cheng 	switch (equivalent_usage) {
451471d1714SBenjamin Tissoires 	case HID_GD_X:
452471d1714SBenjamin Tissoires 		features->x_max = field->logical_maximum;
453471d1714SBenjamin Tissoires 		if (finger) {
454471d1714SBenjamin Tissoires 			features->x_phy = field->physical_maximum;
4553b164a00SPing Cheng 			if ((features->type != BAMBOO_PT) &&
4563b164a00SPing Cheng 			    (features->type != BAMBOO_TOUCH)) {
457471d1714SBenjamin Tissoires 				features->unit = field->unit;
458471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
459471d1714SBenjamin Tissoires 			}
460471d1714SBenjamin Tissoires 		}
461471d1714SBenjamin Tissoires 		break;
462471d1714SBenjamin Tissoires 	case HID_GD_Y:
463471d1714SBenjamin Tissoires 		features->y_max = field->logical_maximum;
464471d1714SBenjamin Tissoires 		if (finger) {
465471d1714SBenjamin Tissoires 			features->y_phy = field->physical_maximum;
4663b164a00SPing Cheng 			if ((features->type != BAMBOO_PT) &&
4673b164a00SPing Cheng 			    (features->type != BAMBOO_TOUCH)) {
468471d1714SBenjamin Tissoires 				features->unit = field->unit;
469471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
470471d1714SBenjamin Tissoires 			}
471471d1714SBenjamin Tissoires 		}
472471d1714SBenjamin Tissoires 		break;
473471d1714SBenjamin Tissoires 	case HID_DG_TIPPRESSURE:
474471d1714SBenjamin Tissoires 		if (pen)
475471d1714SBenjamin Tissoires 			features->pressure_max = field->logical_maximum;
476471d1714SBenjamin Tissoires 		break;
477471d1714SBenjamin Tissoires 	}
4787704ac93SBenjamin Tissoires 
4797704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
4807704ac93SBenjamin Tissoires 		wacom_wac_usage_mapping(hdev, field, usage);
481471d1714SBenjamin Tissoires }
482471d1714SBenjamin Tissoires 
483b58ba1baSJason Gerecke static void wacom_post_parse_hid(struct hid_device *hdev,
484b58ba1baSJason Gerecke 				 struct wacom_features *features)
485b58ba1baSJason Gerecke {
486b58ba1baSJason Gerecke 	struct wacom *wacom = hid_get_drvdata(hdev);
487b58ba1baSJason Gerecke 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
488b58ba1baSJason Gerecke 
489b58ba1baSJason Gerecke 	if (features->type == HID_GENERIC) {
490b58ba1baSJason Gerecke 		/* Any last-minute generic device setup */
4914082da80SBenjamin Tissoires 		if (wacom_wac->has_mode_change) {
4924082da80SBenjamin Tissoires 			if (wacom_wac->is_direct_mode)
4934082da80SBenjamin Tissoires 				features->device_type |= WACOM_DEVICETYPE_DIRECT;
4944082da80SBenjamin Tissoires 			else
4954082da80SBenjamin Tissoires 				features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
4964082da80SBenjamin Tissoires 		}
4974082da80SBenjamin Tissoires 
498b58ba1baSJason Gerecke 		if (features->touch_max > 1) {
499ac2423c9SAaron Armstrong Skomra 			if (features->device_type & WACOM_DEVICETYPE_DIRECT)
500ac2423c9SAaron Armstrong Skomra 				input_mt_init_slots(wacom_wac->touch_input,
501ac2423c9SAaron Armstrong Skomra 						    wacom_wac->features.touch_max,
502b58ba1baSJason Gerecke 						    INPUT_MT_DIRECT);
503ac2423c9SAaron Armstrong Skomra 			else
504ac2423c9SAaron Armstrong Skomra 				input_mt_init_slots(wacom_wac->touch_input,
505ac2423c9SAaron Armstrong Skomra 						    wacom_wac->features.touch_max,
506ac2423c9SAaron Armstrong Skomra 						    INPUT_MT_POINTER);
507b58ba1baSJason Gerecke 		}
508b58ba1baSJason Gerecke 	}
509b58ba1baSJason Gerecke }
510b58ba1baSJason Gerecke 
511471d1714SBenjamin Tissoires static void wacom_parse_hid(struct hid_device *hdev,
512471d1714SBenjamin Tissoires 			   struct wacom_features *features)
513471d1714SBenjamin Tissoires {
514471d1714SBenjamin Tissoires 	struct hid_report_enum *rep_enum;
515471d1714SBenjamin Tissoires 	struct hid_report *hreport;
516471d1714SBenjamin Tissoires 	int i, j;
517471d1714SBenjamin Tissoires 
518471d1714SBenjamin Tissoires 	/* check features first */
519471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
520471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
521471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++) {
522471d1714SBenjamin Tissoires 			/* Ignore if report count is out of bounds. */
523471d1714SBenjamin Tissoires 			if (hreport->field[i]->report_count < 1)
524471d1714SBenjamin Tissoires 				continue;
525471d1714SBenjamin Tissoires 
526471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++) {
527471d1714SBenjamin Tissoires 				wacom_feature_mapping(hdev, hreport->field[i],
528471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
529471d1714SBenjamin Tissoires 			}
530471d1714SBenjamin Tissoires 		}
531471d1714SBenjamin Tissoires 	}
532471d1714SBenjamin Tissoires 
533471d1714SBenjamin Tissoires 	/* now check the input usages */
534471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
535471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
536471d1714SBenjamin Tissoires 
537471d1714SBenjamin Tissoires 		if (!hreport->maxfield)
538471d1714SBenjamin Tissoires 			continue;
539471d1714SBenjamin Tissoires 
540471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++)
541471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++)
542471d1714SBenjamin Tissoires 				wacom_usage_mapping(hdev, hreport->field[i],
543471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
544471d1714SBenjamin Tissoires 	}
545b58ba1baSJason Gerecke 
546b58ba1baSJason Gerecke 	wacom_post_parse_hid(hdev, features);
547471d1714SBenjamin Tissoires }
548471d1714SBenjamin Tissoires 
5495ae6e89fSBenjamin Tissoires static int wacom_hid_set_device_mode(struct hid_device *hdev)
5505ae6e89fSBenjamin Tissoires {
5515ae6e89fSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
5525ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
5535ae6e89fSBenjamin Tissoires 	struct hid_report *r;
5545ae6e89fSBenjamin Tissoires 	struct hid_report_enum *re;
5555ae6e89fSBenjamin Tissoires 
5565ae6e89fSBenjamin Tissoires 	if (hid_data->inputmode < 0)
5575ae6e89fSBenjamin Tissoires 		return 0;
5585ae6e89fSBenjamin Tissoires 
5595ae6e89fSBenjamin Tissoires 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
5605ae6e89fSBenjamin Tissoires 	r = re->report_id_hash[hid_data->inputmode];
5615ae6e89fSBenjamin Tissoires 	if (r) {
5625ae6e89fSBenjamin Tissoires 		r->field[0]->value[hid_data->inputmode_index] = 2;
5635ae6e89fSBenjamin Tissoires 		hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
5645ae6e89fSBenjamin Tissoires 	}
5655ae6e89fSBenjamin Tissoires 	return 0;
5665ae6e89fSBenjamin Tissoires }
5675ae6e89fSBenjamin Tissoires 
568326ea2a9SJason Gerecke static int wacom_set_device_mode(struct hid_device *hdev,
569326ea2a9SJason Gerecke 				 struct wacom_wac *wacom_wac)
570471d1714SBenjamin Tissoires {
571326ea2a9SJason Gerecke 	u8 *rep_data;
572326ea2a9SJason Gerecke 	struct hid_report *r;
573326ea2a9SJason Gerecke 	struct hid_report_enum *re;
5743064a03bSAaron Ma 	u32 length;
575471d1714SBenjamin Tissoires 	int error = -ENOMEM, limit = 0;
576471d1714SBenjamin Tissoires 
577326ea2a9SJason Gerecke 	if (wacom_wac->mode_report < 0)
578326ea2a9SJason Gerecke 		return 0;
579326ea2a9SJason Gerecke 
580326ea2a9SJason Gerecke 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
581326ea2a9SJason Gerecke 	r = re->report_id_hash[wacom_wac->mode_report];
582326ea2a9SJason Gerecke 	if (!r)
583326ea2a9SJason Gerecke 		return -EINVAL;
584326ea2a9SJason Gerecke 
585326ea2a9SJason Gerecke 	rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
586471d1714SBenjamin Tissoires 	if (!rep_data)
587326ea2a9SJason Gerecke 		return -ENOMEM;
588326ea2a9SJason Gerecke 
589326ea2a9SJason Gerecke 	length = hid_report_len(r);
590471d1714SBenjamin Tissoires 
591471d1714SBenjamin Tissoires 	do {
592326ea2a9SJason Gerecke 		rep_data[0] = wacom_wac->mode_report;
593326ea2a9SJason Gerecke 		rep_data[1] = wacom_wac->mode_value;
594471d1714SBenjamin Tissoires 
595296b7378SPrzemo Firszt 		error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
596296b7378SPrzemo Firszt 					 length, 1);
597471d1714SBenjamin Tissoires 		if (error >= 0)
598471d1714SBenjamin Tissoires 			error = wacom_get_report(hdev, HID_FEATURE_REPORT,
599c64d8834SPing Cheng 			                         rep_data, length, 1);
600326ea2a9SJason Gerecke 	} while (error >= 0 &&
601326ea2a9SJason Gerecke 		 rep_data[1] != wacom_wac->mode_report &&
602326ea2a9SJason Gerecke 		 limit++ < WAC_MSG_RETRIES);
603471d1714SBenjamin Tissoires 
604471d1714SBenjamin Tissoires 	kfree(rep_data);
605471d1714SBenjamin Tissoires 
606471d1714SBenjamin Tissoires 	return error < 0 ? error : 0;
607471d1714SBenjamin Tissoires }
608471d1714SBenjamin Tissoires 
609f81a1295SBenjamin Tissoires static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
610f81a1295SBenjamin Tissoires 		struct wacom_features *features)
611f81a1295SBenjamin Tissoires {
612387142bbSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
613387142bbSBenjamin Tissoires 	int ret;
614387142bbSBenjamin Tissoires 	u8 rep_data[2];
615387142bbSBenjamin Tissoires 
616387142bbSBenjamin Tissoires 	switch (features->type) {
617387142bbSBenjamin Tissoires 	case GRAPHIRE_BT:
618387142bbSBenjamin Tissoires 		rep_data[0] = 0x03;
619387142bbSBenjamin Tissoires 		rep_data[1] = 0x00;
620296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
621296b7378SPrzemo Firszt 					3);
622387142bbSBenjamin Tissoires 
623387142bbSBenjamin Tissoires 		if (ret >= 0) {
624387142bbSBenjamin Tissoires 			rep_data[0] = speed == 0 ? 0x05 : 0x06;
625387142bbSBenjamin Tissoires 			rep_data[1] = 0x00;
626387142bbSBenjamin Tissoires 
627387142bbSBenjamin Tissoires 			ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
628296b7378SPrzemo Firszt 						rep_data, 2, 3);
629387142bbSBenjamin Tissoires 
630387142bbSBenjamin Tissoires 			if (ret >= 0) {
631387142bbSBenjamin Tissoires 				wacom->wacom_wac.bt_high_speed = speed;
632387142bbSBenjamin Tissoires 				return 0;
633387142bbSBenjamin Tissoires 			}
634387142bbSBenjamin Tissoires 		}
635387142bbSBenjamin Tissoires 
636387142bbSBenjamin Tissoires 		/*
637387142bbSBenjamin Tissoires 		 * Note that if the raw queries fail, it's not a hard failure
638387142bbSBenjamin Tissoires 		 * and it is safe to continue
639387142bbSBenjamin Tissoires 		 */
640387142bbSBenjamin Tissoires 		hid_warn(hdev, "failed to poke device, command %d, err %d\n",
641387142bbSBenjamin Tissoires 			 rep_data[0], ret);
642387142bbSBenjamin Tissoires 		break;
64381af7e61SBenjamin Tissoires 	case INTUOS4WL:
64481af7e61SBenjamin Tissoires 		if (speed == 1)
64581af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features &= ~0x20;
64681af7e61SBenjamin Tissoires 		else
64781af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features |= 0x20;
64881af7e61SBenjamin Tissoires 
64981af7e61SBenjamin Tissoires 		rep_data[0] = 0x03;
65081af7e61SBenjamin Tissoires 		rep_data[1] = wacom->wacom_wac.bt_features;
65181af7e61SBenjamin Tissoires 
652296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
653296b7378SPrzemo Firszt 					1);
65481af7e61SBenjamin Tissoires 		if (ret >= 0)
65581af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_high_speed = speed;
65681af7e61SBenjamin Tissoires 		break;
657387142bbSBenjamin Tissoires 	}
658387142bbSBenjamin Tissoires 
659f81a1295SBenjamin Tissoires 	return 0;
660f81a1295SBenjamin Tissoires }
661f81a1295SBenjamin Tissoires 
662471d1714SBenjamin Tissoires /*
663471d1714SBenjamin Tissoires  * Switch the tablet into its most-capable mode. Wacom tablets are
664471d1714SBenjamin Tissoires  * typically configured to power-up in a mode which sends mouse-like
665471d1714SBenjamin Tissoires  * reports to the OS. To get absolute position, pressure data, etc.
666471d1714SBenjamin Tissoires  * from the tablet, it is necessary to switch the tablet out of this
667471d1714SBenjamin Tissoires  * mode and into one which sends the full range of tablet data.
668471d1714SBenjamin Tissoires  */
669a544c619SBenjamin Tissoires static int _wacom_query_tablet_data(struct wacom *wacom)
670471d1714SBenjamin Tissoires {
671a544c619SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
672326ea2a9SJason Gerecke 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
673a544c619SBenjamin Tissoires 	struct wacom_features *features = &wacom_wac->features;
674326ea2a9SJason Gerecke 
675f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
676f81a1295SBenjamin Tissoires 		return wacom_bt_query_tablet_data(hdev, 1, features);
677f81a1295SBenjamin Tissoires 
678326ea2a9SJason Gerecke 	if (features->type != HID_GENERIC) {
679aa86b18cSJason Gerecke 		if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
680471d1714SBenjamin Tissoires 			if (features->type > TABLETPC) {
681471d1714SBenjamin Tissoires 				/* MT Tablet PC touch */
682326ea2a9SJason Gerecke 				wacom_wac->mode_report = 3;
683326ea2a9SJason Gerecke 				wacom_wac->mode_value = 4;
684326ea2a9SJason Gerecke 			} else if (features->type == WACOM_24HDT) {
685326ea2a9SJason Gerecke 				wacom_wac->mode_report = 18;
686326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
687326ea2a9SJason Gerecke 			} else if (features->type == WACOM_27QHDT) {
688326ea2a9SJason Gerecke 				wacom_wac->mode_report = 131;
689326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
690326ea2a9SJason Gerecke 			} else if (features->type == BAMBOO_PAD) {
691326ea2a9SJason Gerecke 				wacom_wac->mode_report = 2;
692326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
6938c97a765SBenjamin Tissoires 			}
694aa86b18cSJason Gerecke 		} else if (features->device_type & WACOM_DEVICETYPE_PEN) {
6957adb91bdSPing Cheng 			if (features->type <= BAMBOO_PT) {
696326ea2a9SJason Gerecke 				wacom_wac->mode_report = 2;
697326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
698471d1714SBenjamin Tissoires 			}
699471d1714SBenjamin Tissoires 		}
700326ea2a9SJason Gerecke 	}
701326ea2a9SJason Gerecke 
702326ea2a9SJason Gerecke 	wacom_set_device_mode(hdev, wacom_wac);
703326ea2a9SJason Gerecke 
704326ea2a9SJason Gerecke 	if (features->type == HID_GENERIC)
705326ea2a9SJason Gerecke 		return wacom_hid_set_device_mode(hdev);
706471d1714SBenjamin Tissoires 
707471d1714SBenjamin Tissoires 	return 0;
708471d1714SBenjamin Tissoires }
709471d1714SBenjamin Tissoires 
710471d1714SBenjamin Tissoires static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
711471d1714SBenjamin Tissoires 					 struct wacom_features *features)
712471d1714SBenjamin Tissoires {
713471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
714471d1714SBenjamin Tissoires 	struct usb_interface *intf = wacom->intf;
715471d1714SBenjamin Tissoires 
716471d1714SBenjamin Tissoires 	/* default features */
717471d1714SBenjamin Tissoires 	features->x_fuzz = 4;
718471d1714SBenjamin Tissoires 	features->y_fuzz = 4;
719471d1714SBenjamin Tissoires 	features->pressure_fuzz = 0;
720bef7e200SJason Gerecke 	features->distance_fuzz = 1;
721bef7e200SJason Gerecke 	features->tilt_fuzz = 1;
722471d1714SBenjamin Tissoires 
723471d1714SBenjamin Tissoires 	/*
724471d1714SBenjamin Tissoires 	 * The wireless device HID is basic and layout conflicts with
725471d1714SBenjamin Tissoires 	 * other tablets (monitor and touch interface can look like pen).
726471d1714SBenjamin Tissoires 	 * Skip the query for this type and modify defaults based on
727471d1714SBenjamin Tissoires 	 * interface number.
728471d1714SBenjamin Tissoires 	 */
729471d1714SBenjamin Tissoires 	if (features->type == WIRELESS) {
7303f14a63aSJason Gerecke 		if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
731ccad85ccSJason Gerecke 			features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
7323f14a63aSJason Gerecke 		else
733aa86b18cSJason Gerecke 			features->device_type = WACOM_DEVICETYPE_NONE;
7343f14a63aSJason Gerecke 		return;
735471d1714SBenjamin Tissoires 	}
736471d1714SBenjamin Tissoires 
737471d1714SBenjamin Tissoires 	wacom_parse_hid(hdev, features);
738471d1714SBenjamin Tissoires }
739471d1714SBenjamin Tissoires 
740471d1714SBenjamin Tissoires struct wacom_hdev_data {
741471d1714SBenjamin Tissoires 	struct list_head list;
742471d1714SBenjamin Tissoires 	struct kref kref;
743471d1714SBenjamin Tissoires 	struct hid_device *dev;
744471d1714SBenjamin Tissoires 	struct wacom_shared shared;
745471d1714SBenjamin Tissoires };
746471d1714SBenjamin Tissoires 
747471d1714SBenjamin Tissoires static LIST_HEAD(wacom_udev_list);
748471d1714SBenjamin Tissoires static DEFINE_MUTEX(wacom_udev_list_lock);
749471d1714SBenjamin Tissoires 
750471d1714SBenjamin Tissoires static bool wacom_are_sibling(struct hid_device *hdev,
751471d1714SBenjamin Tissoires 		struct hid_device *sibling)
752471d1714SBenjamin Tissoires {
753471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
754471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
75541372d5dSJason Gerecke 	struct wacom *sibling_wacom = hid_get_drvdata(sibling);
75641372d5dSJason Gerecke 	struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
75741372d5dSJason Gerecke 	__u32 oVid = features->oVid ? features->oVid : hdev->vendor;
75841372d5dSJason Gerecke 	__u32 oPid = features->oPid ? features->oPid : hdev->product;
759471d1714SBenjamin Tissoires 
76041372d5dSJason Gerecke 	/* The defined oVid/oPid must match that of the sibling */
76141372d5dSJason Gerecke 	if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
76241372d5dSJason Gerecke 		return false;
76341372d5dSJason Gerecke 	if (features->oPid != HID_ANY_ID && sibling->product != oPid)
76441372d5dSJason Gerecke 		return false;
76541372d5dSJason Gerecke 
76641372d5dSJason Gerecke 	/*
76741372d5dSJason Gerecke 	 * Devices with the same VID/PID must share the same physical
76841372d5dSJason Gerecke 	 * device path, while those with different VID/PID must share
76941372d5dSJason Gerecke 	 * the same physical parent device path.
77041372d5dSJason Gerecke 	 */
77141372d5dSJason Gerecke 	if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
7721a8861f1SDaniel M. Lambea 		if (!hid_compare_device_paths(hdev, sibling, '/'))
77341372d5dSJason Gerecke 			return false;
77441372d5dSJason Gerecke 	} else {
7751a8861f1SDaniel M. Lambea 		if (!hid_compare_device_paths(hdev, sibling, '.'))
77641372d5dSJason Gerecke 			return false;
777471d1714SBenjamin Tissoires 	}
778471d1714SBenjamin Tissoires 
77941372d5dSJason Gerecke 	/* Skip the remaining heuristics unless you are a HID_GENERIC device */
78041372d5dSJason Gerecke 	if (features->type != HID_GENERIC)
78141372d5dSJason Gerecke 		return true;
78241372d5dSJason Gerecke 
78341372d5dSJason Gerecke 	/*
78441372d5dSJason Gerecke 	 * Direct-input devices may not be siblings of indirect-input
78541372d5dSJason Gerecke 	 * devices.
78641372d5dSJason Gerecke 	 */
78741372d5dSJason Gerecke 	if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
78841372d5dSJason Gerecke 	    !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
789471d1714SBenjamin Tissoires 		return false;
790471d1714SBenjamin Tissoires 
79141372d5dSJason Gerecke 	/*
79241372d5dSJason Gerecke 	 * Indirect-input devices may not be siblings of direct-input
79341372d5dSJason Gerecke 	 * devices.
79441372d5dSJason Gerecke 	 */
79541372d5dSJason Gerecke 	if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
79641372d5dSJason Gerecke 	    (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
797471d1714SBenjamin Tissoires 		return false;
798471d1714SBenjamin Tissoires 
79941372d5dSJason Gerecke 	/* Pen devices may only be siblings of touch devices */
80041372d5dSJason Gerecke 	if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
80141372d5dSJason Gerecke 	    !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
80241372d5dSJason Gerecke 		return false;
80341372d5dSJason Gerecke 
80441372d5dSJason Gerecke 	/* Touch devices may only be siblings of pen devices */
80541372d5dSJason Gerecke 	if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
80641372d5dSJason Gerecke 	    !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
80741372d5dSJason Gerecke 		return false;
80841372d5dSJason Gerecke 
80941372d5dSJason Gerecke 	/*
81041372d5dSJason Gerecke 	 * No reason could be found for these two devices to NOT be
81141372d5dSJason Gerecke 	 * siblings, so there's a good chance they ARE siblings
81241372d5dSJason Gerecke 	 */
81341372d5dSJason Gerecke 	return true;
814471d1714SBenjamin Tissoires }
815471d1714SBenjamin Tissoires 
816471d1714SBenjamin Tissoires static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
817471d1714SBenjamin Tissoires {
818471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
819471d1714SBenjamin Tissoires 
82041372d5dSJason Gerecke 	/* Try to find an already-probed interface from the same device */
82141372d5dSJason Gerecke 	list_for_each_entry(data, &wacom_udev_list, list) {
8221a8861f1SDaniel M. Lambea 		if (hid_compare_device_paths(hdev, data->dev, '/')) {
8232a5e597cSJason Gerecke 			kref_get(&data->kref);
82441372d5dSJason Gerecke 			return data;
82541372d5dSJason Gerecke 		}
8262a5e597cSJason Gerecke 	}
82741372d5dSJason Gerecke 
82841372d5dSJason Gerecke 	/* Fallback to finding devices that appear to be "siblings" */
829471d1714SBenjamin Tissoires 	list_for_each_entry(data, &wacom_udev_list, list) {
830471d1714SBenjamin Tissoires 		if (wacom_are_sibling(hdev, data->dev)) {
831471d1714SBenjamin Tissoires 			kref_get(&data->kref);
832471d1714SBenjamin Tissoires 			return data;
833471d1714SBenjamin Tissoires 		}
834471d1714SBenjamin Tissoires 	}
835471d1714SBenjamin Tissoires 
836471d1714SBenjamin Tissoires 	return NULL;
837471d1714SBenjamin Tissoires }
838471d1714SBenjamin Tissoires 
8391c817c83SBenjamin Tissoires static void wacom_release_shared_data(struct kref *kref)
8401c817c83SBenjamin Tissoires {
8411c817c83SBenjamin Tissoires 	struct wacom_hdev_data *data =
8421c817c83SBenjamin Tissoires 		container_of(kref, struct wacom_hdev_data, kref);
8431c817c83SBenjamin Tissoires 
8441c817c83SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
8451c817c83SBenjamin Tissoires 	list_del(&data->list);
8461c817c83SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
8471c817c83SBenjamin Tissoires 
8481c817c83SBenjamin Tissoires 	kfree(data);
8491c817c83SBenjamin Tissoires }
8501c817c83SBenjamin Tissoires 
8511c817c83SBenjamin Tissoires static void wacom_remove_shared_data(void *res)
8521c817c83SBenjamin Tissoires {
8531c817c83SBenjamin Tissoires 	struct wacom *wacom = res;
8541c817c83SBenjamin Tissoires 	struct wacom_hdev_data *data;
8551c817c83SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
8561c817c83SBenjamin Tissoires 
8571c817c83SBenjamin Tissoires 	if (wacom_wac->shared) {
8581c817c83SBenjamin Tissoires 		data = container_of(wacom_wac->shared, struct wacom_hdev_data,
8591c817c83SBenjamin Tissoires 				    shared);
8601c817c83SBenjamin Tissoires 
8611c817c83SBenjamin Tissoires 		if (wacom_wac->shared->touch == wacom->hdev)
8621c817c83SBenjamin Tissoires 			wacom_wac->shared->touch = NULL;
8631c817c83SBenjamin Tissoires 		else if (wacom_wac->shared->pen == wacom->hdev)
8641c817c83SBenjamin Tissoires 			wacom_wac->shared->pen = NULL;
8651c817c83SBenjamin Tissoires 
8661c817c83SBenjamin Tissoires 		kref_put(&data->kref, wacom_release_shared_data);
8671c817c83SBenjamin Tissoires 		wacom_wac->shared = NULL;
8681c817c83SBenjamin Tissoires 	}
8691c817c83SBenjamin Tissoires }
8701c817c83SBenjamin Tissoires 
871471d1714SBenjamin Tissoires static int wacom_add_shared_data(struct hid_device *hdev)
872471d1714SBenjamin Tissoires {
873471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
874471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
875471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
876471d1714SBenjamin Tissoires 	int retval = 0;
877471d1714SBenjamin Tissoires 
878471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
879471d1714SBenjamin Tissoires 
880471d1714SBenjamin Tissoires 	data = wacom_get_hdev_data(hdev);
881471d1714SBenjamin Tissoires 	if (!data) {
882471d1714SBenjamin Tissoires 		data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
883471d1714SBenjamin Tissoires 		if (!data) {
884471d1714SBenjamin Tissoires 			retval = -ENOMEM;
885471d1714SBenjamin Tissoires 			goto out;
886471d1714SBenjamin Tissoires 		}
887471d1714SBenjamin Tissoires 
888471d1714SBenjamin Tissoires 		kref_init(&data->kref);
889471d1714SBenjamin Tissoires 		data->dev = hdev;
890471d1714SBenjamin Tissoires 		list_add_tail(&data->list, &wacom_udev_list);
891471d1714SBenjamin Tissoires 	}
892471d1714SBenjamin Tissoires 
893471d1714SBenjamin Tissoires 	wacom_wac->shared = &data->shared;
894471d1714SBenjamin Tissoires 
8951c817c83SBenjamin Tissoires 	retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
8961c817c83SBenjamin Tissoires 	if (retval) {
8971c817c83SBenjamin Tissoires 		mutex_unlock(&wacom_udev_list_lock);
8981c817c83SBenjamin Tissoires 		wacom_remove_shared_data(wacom);
8991c817c83SBenjamin Tissoires 		return retval;
9001c817c83SBenjamin Tissoires 	}
9011c817c83SBenjamin Tissoires 
902a9ce7856SJason Gerecke 	if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
903a9ce7856SJason Gerecke 		wacom_wac->shared->touch = hdev;
904a9ce7856SJason Gerecke 	else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
905a9ce7856SJason Gerecke 		wacom_wac->shared->pen = hdev;
906a9ce7856SJason Gerecke 
907471d1714SBenjamin Tissoires out:
908471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
909471d1714SBenjamin Tissoires 	return retval;
910471d1714SBenjamin Tissoires }
911471d1714SBenjamin Tissoires 
912471d1714SBenjamin Tissoires static int wacom_led_control(struct wacom *wacom)
913471d1714SBenjamin Tissoires {
914471d1714SBenjamin Tissoires 	unsigned char *buf;
915471d1714SBenjamin Tissoires 	int retval;
916912ca216SPing Cheng 	unsigned char report_id = WAC_CMD_LED_CONTROL;
917912ca216SPing Cheng 	int buf_size = 9;
918471d1714SBenjamin Tissoires 
919a50aac71SBenjamin Tissoires 	if (!wacom->led.groups)
920a50aac71SBenjamin Tissoires 		return -ENOTSUPP;
921a50aac71SBenjamin Tissoires 
92274aebed6SAaron Armstrong Skomra 	if (wacom->wacom_wac.features.type == REMOTE)
92374aebed6SAaron Armstrong Skomra 		return -ENOTSUPP;
92474aebed6SAaron Armstrong Skomra 
925912ca216SPing Cheng 	if (wacom->wacom_wac.pid) { /* wireless connected */
926912ca216SPing Cheng 		report_id = WAC_CMD_WL_LED_CONTROL;
927912ca216SPing Cheng 		buf_size = 13;
928912ca216SPing Cheng 	}
9294922cd26SJason Gerecke 	else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
9304922cd26SJason Gerecke 		report_id = WAC_CMD_WL_INTUOSP2;
9314922cd26SJason Gerecke 		buf_size = 51;
9324922cd26SJason Gerecke 	}
933912ca216SPing Cheng 	buf = kzalloc(buf_size, GFP_KERNEL);
934471d1714SBenjamin Tissoires 	if (!buf)
935471d1714SBenjamin Tissoires 		return -ENOMEM;
936471d1714SBenjamin Tissoires 
93710c55cacSAaron Armstrong Skomra 	if (wacom->wacom_wac.features.type == HID_GENERIC) {
93810c55cacSAaron Armstrong Skomra 		buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
93910c55cacSAaron Armstrong Skomra 		buf[1] = wacom->led.llv;
94010c55cacSAaron Armstrong Skomra 		buf[2] = wacom->led.groups[0].select & 0x03;
94110c55cacSAaron Armstrong Skomra 
94210c55cacSAaron Armstrong Skomra 	} else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
94310c55cacSAaron Armstrong Skomra 	    wacom->wacom_wac.features.type <= INTUOSPL)) {
944471d1714SBenjamin Tissoires 		/*
945471d1714SBenjamin Tissoires 		 * Touch Ring and crop mark LED luminance may take on
946471d1714SBenjamin Tissoires 		 * one of four values:
947471d1714SBenjamin Tissoires 		 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
948471d1714SBenjamin Tissoires 		 */
949a50aac71SBenjamin Tissoires 		int ring_led = wacom->led.groups[0].select & 0x03;
950471d1714SBenjamin Tissoires 		int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
951471d1714SBenjamin Tissoires 		int crop_lum = 0;
952912ca216SPing Cheng 		unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
953471d1714SBenjamin Tissoires 
954912ca216SPing Cheng 		buf[0] = report_id;
955912ca216SPing Cheng 		if (wacom->wacom_wac.pid) {
956912ca216SPing Cheng 			wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
957912ca216SPing Cheng 					 buf, buf_size, WAC_CMD_RETRIES);
958912ca216SPing Cheng 			buf[0] = report_id;
959912ca216SPing Cheng 			buf[4] = led_bits;
960912ca216SPing Cheng 		} else
961912ca216SPing Cheng 			buf[1] = led_bits;
962471d1714SBenjamin Tissoires 	}
9634922cd26SJason Gerecke 	else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
9644922cd26SJason Gerecke 		buf[0] = report_id;
9654922cd26SJason Gerecke 		buf[4] = 100; // Power Connection LED (ORANGE)
9664922cd26SJason Gerecke 		buf[5] = 100; // BT Connection LED (BLUE)
9674922cd26SJason Gerecke 		buf[6] = 100; // Paper Mode (RED?)
9684922cd26SJason Gerecke 		buf[7] = 100; // Paper Mode (GREEN?)
9694922cd26SJason Gerecke 		buf[8] = 100; // Paper Mode (BLUE?)
9704922cd26SJason Gerecke 		buf[9] = wacom->led.llv;
9714922cd26SJason Gerecke 		buf[10] = wacom->led.groups[0].select & 0x03;
9724922cd26SJason Gerecke 	}
973471d1714SBenjamin Tissoires 	else {
974a50aac71SBenjamin Tissoires 		int led = wacom->led.groups[0].select | 0x4;
975471d1714SBenjamin Tissoires 
976471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
977471d1714SBenjamin Tissoires 		    wacom->wacom_wac.features.type == WACOM_24HD)
978a50aac71SBenjamin Tissoires 			led |= (wacom->led.groups[1].select << 4) | 0x40;
979471d1714SBenjamin Tissoires 
980912ca216SPing Cheng 		buf[0] = report_id;
981471d1714SBenjamin Tissoires 		buf[1] = led;
982471d1714SBenjamin Tissoires 		buf[2] = wacom->led.llv;
983471d1714SBenjamin Tissoires 		buf[3] = wacom->led.hlv;
984471d1714SBenjamin Tissoires 		buf[4] = wacom->led.img_lum;
985471d1714SBenjamin Tissoires 	}
986471d1714SBenjamin Tissoires 
987912ca216SPing Cheng 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
988296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
989471d1714SBenjamin Tissoires 	kfree(buf);
990471d1714SBenjamin Tissoires 
991471d1714SBenjamin Tissoires 	return retval;
992471d1714SBenjamin Tissoires }
993471d1714SBenjamin Tissoires 
994849e2f06SBenjamin Tissoires static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
995849e2f06SBenjamin Tissoires 		const unsigned len, const void *img)
996471d1714SBenjamin Tissoires {
997471d1714SBenjamin Tissoires 	unsigned char *buf;
998471d1714SBenjamin Tissoires 	int i, retval;
999849e2f06SBenjamin Tissoires 	const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
1000471d1714SBenjamin Tissoires 
1001849e2f06SBenjamin Tissoires 	buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
1002471d1714SBenjamin Tissoires 	if (!buf)
1003471d1714SBenjamin Tissoires 		return -ENOMEM;
1004471d1714SBenjamin Tissoires 
1005471d1714SBenjamin Tissoires 	/* Send 'start' command */
1006471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
1007471d1714SBenjamin Tissoires 	buf[1] = 1;
1008296b7378SPrzemo Firszt 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1009296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
1010471d1714SBenjamin Tissoires 	if (retval < 0)
1011471d1714SBenjamin Tissoires 		goto out;
1012471d1714SBenjamin Tissoires 
1013849e2f06SBenjamin Tissoires 	buf[0] = xfer_id;
1014471d1714SBenjamin Tissoires 	buf[1] = button_id & 0x07;
1015471d1714SBenjamin Tissoires 	for (i = 0; i < 4; i++) {
1016471d1714SBenjamin Tissoires 		buf[2] = i;
1017849e2f06SBenjamin Tissoires 		memcpy(buf + 3, img + i * chunk_len, chunk_len);
1018471d1714SBenjamin Tissoires 
1019471d1714SBenjamin Tissoires 		retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
1020296b7378SPrzemo Firszt 					  buf, chunk_len + 3, WAC_CMD_RETRIES);
1021471d1714SBenjamin Tissoires 		if (retval < 0)
1022471d1714SBenjamin Tissoires 			break;
1023471d1714SBenjamin Tissoires 	}
1024471d1714SBenjamin Tissoires 
1025471d1714SBenjamin Tissoires 	/* Send 'stop' */
1026471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
1027471d1714SBenjamin Tissoires 	buf[1] = 0;
1028296b7378SPrzemo Firszt 	wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1029296b7378SPrzemo Firszt 			 WAC_CMD_RETRIES);
1030471d1714SBenjamin Tissoires 
1031471d1714SBenjamin Tissoires out:
1032471d1714SBenjamin Tissoires 	kfree(buf);
1033471d1714SBenjamin Tissoires 	return retval;
1034471d1714SBenjamin Tissoires }
1035471d1714SBenjamin Tissoires 
1036471d1714SBenjamin Tissoires static ssize_t wacom_led_select_store(struct device *dev, int set_id,
1037471d1714SBenjamin Tissoires 				      const char *buf, size_t count)
1038471d1714SBenjamin Tissoires {
1039ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1040471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1041471d1714SBenjamin Tissoires 	unsigned int id;
1042471d1714SBenjamin Tissoires 	int err;
1043471d1714SBenjamin Tissoires 
1044471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &id);
1045471d1714SBenjamin Tissoires 	if (err)
1046471d1714SBenjamin Tissoires 		return err;
1047471d1714SBenjamin Tissoires 
1048471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
1049471d1714SBenjamin Tissoires 
1050a50aac71SBenjamin Tissoires 	wacom->led.groups[set_id].select = id & 0x3;
1051471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
1052471d1714SBenjamin Tissoires 
1053471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
1054471d1714SBenjamin Tissoires 
1055471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
1056471d1714SBenjamin Tissoires }
1057471d1714SBenjamin Tissoires 
1058471d1714SBenjamin Tissoires #define DEVICE_LED_SELECT_ATTR(SET_ID)					\
1059471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
1060471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
1061471d1714SBenjamin Tissoires {									\
1062471d1714SBenjamin Tissoires 	return wacom_led_select_store(dev, SET_ID, buf, count);		\
1063471d1714SBenjamin Tissoires }									\
1064471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
1065471d1714SBenjamin Tissoires 	struct device_attribute *attr, char *buf)			\
1066471d1714SBenjamin Tissoires {									\
1067ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);\
1068471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
106937449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n",			\
1070a50aac71SBenjamin Tissoires 			 wacom->led.groups[SET_ID].select);		\
1071471d1714SBenjamin Tissoires }									\
1072e0984bc3SPing Cheng static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,	\
1073471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_show,			\
1074471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_store)
1075471d1714SBenjamin Tissoires 
1076471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(0);
1077471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(1);
1078471d1714SBenjamin Tissoires 
1079471d1714SBenjamin Tissoires static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
1080471d1714SBenjamin Tissoires 				     const char *buf, size_t count)
1081471d1714SBenjamin Tissoires {
1082471d1714SBenjamin Tissoires 	unsigned int value;
1083471d1714SBenjamin Tissoires 	int err;
1084471d1714SBenjamin Tissoires 
1085471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &value);
1086471d1714SBenjamin Tissoires 	if (err)
1087471d1714SBenjamin Tissoires 		return err;
1088471d1714SBenjamin Tissoires 
1089471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
1090471d1714SBenjamin Tissoires 
1091471d1714SBenjamin Tissoires 	*dest = value & 0x7f;
1092471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
1093471d1714SBenjamin Tissoires 
1094471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
1095471d1714SBenjamin Tissoires 
1096471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
1097471d1714SBenjamin Tissoires }
1098471d1714SBenjamin Tissoires 
1099471d1714SBenjamin Tissoires #define DEVICE_LUMINANCE_ATTR(name, field)				\
1100471d1714SBenjamin Tissoires static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
1101471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
1102471d1714SBenjamin Tissoires {									\
1103ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);\
1104471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
1105471d1714SBenjamin Tissoires 									\
1106471d1714SBenjamin Tissoires 	return wacom_luminance_store(wacom, &wacom->led.field,		\
1107471d1714SBenjamin Tissoires 				     buf, count);			\
1108471d1714SBenjamin Tissoires }									\
110937449adcSPing Cheng static ssize_t wacom_##name##_luminance_show(struct device *dev,	\
111037449adcSPing Cheng 	struct device_attribute *attr, char *buf)			\
111137449adcSPing Cheng {									\
111237449adcSPing Cheng 	struct wacom *wacom = dev_get_drvdata(dev);			\
111337449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);	\
111437449adcSPing Cheng }									\
1115e0984bc3SPing Cheng static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,			\
111637449adcSPing Cheng 		   wacom_##name##_luminance_show,			\
111737449adcSPing Cheng 		   wacom_##name##_luminance_store)
1118471d1714SBenjamin Tissoires 
1119471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status0, llv);
1120471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status1, hlv);
1121471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(buttons, img_lum);
1122471d1714SBenjamin Tissoires 
1123471d1714SBenjamin Tissoires static ssize_t wacom_button_image_store(struct device *dev, int button_id,
1124471d1714SBenjamin Tissoires 					const char *buf, size_t count)
1125471d1714SBenjamin Tissoires {
1126ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1127471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1128471d1714SBenjamin Tissoires 	int err;
1129849e2f06SBenjamin Tissoires 	unsigned len;
1130849e2f06SBenjamin Tissoires 	u8 xfer_id;
1131471d1714SBenjamin Tissoires 
1132849e2f06SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
1133849e2f06SBenjamin Tissoires 		len = 256;
1134849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_BT_XFER;
1135849e2f06SBenjamin Tissoires 	} else {
1136849e2f06SBenjamin Tissoires 		len = 1024;
1137849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_XFER;
1138849e2f06SBenjamin Tissoires 	}
1139849e2f06SBenjamin Tissoires 
1140849e2f06SBenjamin Tissoires 	if (count != len)
1141471d1714SBenjamin Tissoires 		return -EINVAL;
1142471d1714SBenjamin Tissoires 
1143471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
1144471d1714SBenjamin Tissoires 
1145849e2f06SBenjamin Tissoires 	err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
1146471d1714SBenjamin Tissoires 
1147471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
1148471d1714SBenjamin Tissoires 
1149471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
1150471d1714SBenjamin Tissoires }
1151471d1714SBenjamin Tissoires 
1152471d1714SBenjamin Tissoires #define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
1153471d1714SBenjamin Tissoires static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
1154471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
1155471d1714SBenjamin Tissoires {									\
1156471d1714SBenjamin Tissoires 	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
1157471d1714SBenjamin Tissoires }									\
1158e0984bc3SPing Cheng static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,	\
1159471d1714SBenjamin Tissoires 		   NULL, wacom_btnimg##BUTTON_ID##_store)
1160471d1714SBenjamin Tissoires 
1161471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(0);
1162471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(1);
1163471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(2);
1164471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(3);
1165471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(4);
1166471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(5);
1167471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(6);
1168471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(7);
1169471d1714SBenjamin Tissoires 
1170471d1714SBenjamin Tissoires static struct attribute *cintiq_led_attrs[] = {
1171471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
1172471d1714SBenjamin Tissoires 	&dev_attr_status_led1_select.attr,
1173471d1714SBenjamin Tissoires 	NULL
1174471d1714SBenjamin Tissoires };
1175471d1714SBenjamin Tissoires 
11762a6a35a9SRikard Falkeborn static const struct attribute_group cintiq_led_attr_group = {
1177471d1714SBenjamin Tissoires 	.name = "wacom_led",
1178471d1714SBenjamin Tissoires 	.attrs = cintiq_led_attrs,
1179471d1714SBenjamin Tissoires };
1180471d1714SBenjamin Tissoires 
1181471d1714SBenjamin Tissoires static struct attribute *intuos4_led_attrs[] = {
1182471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
1183471d1714SBenjamin Tissoires 	&dev_attr_status1_luminance.attr,
1184471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
1185471d1714SBenjamin Tissoires 	&dev_attr_buttons_luminance.attr,
1186471d1714SBenjamin Tissoires 	&dev_attr_button0_rawimg.attr,
1187471d1714SBenjamin Tissoires 	&dev_attr_button1_rawimg.attr,
1188471d1714SBenjamin Tissoires 	&dev_attr_button2_rawimg.attr,
1189471d1714SBenjamin Tissoires 	&dev_attr_button3_rawimg.attr,
1190471d1714SBenjamin Tissoires 	&dev_attr_button4_rawimg.attr,
1191471d1714SBenjamin Tissoires 	&dev_attr_button5_rawimg.attr,
1192471d1714SBenjamin Tissoires 	&dev_attr_button6_rawimg.attr,
1193471d1714SBenjamin Tissoires 	&dev_attr_button7_rawimg.attr,
1194471d1714SBenjamin Tissoires 	NULL
1195471d1714SBenjamin Tissoires };
1196471d1714SBenjamin Tissoires 
11972a6a35a9SRikard Falkeborn static const struct attribute_group intuos4_led_attr_group = {
1198471d1714SBenjamin Tissoires 	.name = "wacom_led",
1199471d1714SBenjamin Tissoires 	.attrs = intuos4_led_attrs,
1200471d1714SBenjamin Tissoires };
1201471d1714SBenjamin Tissoires 
1202471d1714SBenjamin Tissoires static struct attribute *intuos5_led_attrs[] = {
1203471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
1204471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
1205471d1714SBenjamin Tissoires 	NULL
1206471d1714SBenjamin Tissoires };
1207471d1714SBenjamin Tissoires 
12082a6a35a9SRikard Falkeborn static const struct attribute_group intuos5_led_attr_group = {
1209471d1714SBenjamin Tissoires 	.name = "wacom_led",
1210471d1714SBenjamin Tissoires 	.attrs = intuos5_led_attrs,
1211471d1714SBenjamin Tissoires };
1212471d1714SBenjamin Tissoires 
121310c55cacSAaron Armstrong Skomra static struct attribute *generic_led_attrs[] = {
121410c55cacSAaron Armstrong Skomra 	&dev_attr_status0_luminance.attr,
121510c55cacSAaron Armstrong Skomra 	&dev_attr_status_led0_select.attr,
121610c55cacSAaron Armstrong Skomra 	NULL
121710c55cacSAaron Armstrong Skomra };
121810c55cacSAaron Armstrong Skomra 
12192a6a35a9SRikard Falkeborn static const struct attribute_group generic_led_attr_group = {
122010c55cacSAaron Armstrong Skomra 	.name = "wacom_led",
122110c55cacSAaron Armstrong Skomra 	.attrs = generic_led_attrs,
122210c55cacSAaron Armstrong Skomra };
122310c55cacSAaron Armstrong Skomra 
12242df68a88SBenjamin Tissoires struct wacom_sysfs_group_devres {
12252a6a35a9SRikard Falkeborn 	const struct attribute_group *group;
12262df68a88SBenjamin Tissoires 	struct kobject *root;
12272df68a88SBenjamin Tissoires };
12282df68a88SBenjamin Tissoires 
12292df68a88SBenjamin Tissoires static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
12302df68a88SBenjamin Tissoires {
12312df68a88SBenjamin Tissoires 	struct wacom_sysfs_group_devres *devres = res;
12322df68a88SBenjamin Tissoires 	struct kobject *kobj = devres->root;
12332df68a88SBenjamin Tissoires 
12342df68a88SBenjamin Tissoires 	dev_dbg(dev, "%s: dropping reference to %s\n",
12352df68a88SBenjamin Tissoires 		__func__, devres->group->name);
12362df68a88SBenjamin Tissoires 	sysfs_remove_group(kobj, devres->group);
12372df68a88SBenjamin Tissoires }
12382df68a88SBenjamin Tissoires 
1239f9036bd4SBenjamin Tissoires static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1240f9036bd4SBenjamin Tissoires 					   struct kobject *root,
12412a6a35a9SRikard Falkeborn 					   const struct attribute_group *group)
12422df68a88SBenjamin Tissoires {
12432df68a88SBenjamin Tissoires 	struct wacom_sysfs_group_devres *devres;
12442df68a88SBenjamin Tissoires 	int error;
12452df68a88SBenjamin Tissoires 
12462df68a88SBenjamin Tissoires 	devres = devres_alloc(wacom_devm_sysfs_group_release,
12472df68a88SBenjamin Tissoires 			      sizeof(struct wacom_sysfs_group_devres),
12482df68a88SBenjamin Tissoires 			      GFP_KERNEL);
12492df68a88SBenjamin Tissoires 	if (!devres)
12502df68a88SBenjamin Tissoires 		return -ENOMEM;
12512df68a88SBenjamin Tissoires 
12522df68a88SBenjamin Tissoires 	devres->group = group;
1253f9036bd4SBenjamin Tissoires 	devres->root = root;
12542df68a88SBenjamin Tissoires 
12552df68a88SBenjamin Tissoires 	error = sysfs_create_group(devres->root, group);
1256097b8f62SArvind Yadav 	if (error) {
1257097b8f62SArvind Yadav 		devres_free(devres);
12582df68a88SBenjamin Tissoires 		return error;
1259097b8f62SArvind Yadav 	}
12602df68a88SBenjamin Tissoires 
12612df68a88SBenjamin Tissoires 	devres_add(&wacom->hdev->dev, devres);
12622df68a88SBenjamin Tissoires 
12632df68a88SBenjamin Tissoires 	return 0;
12642df68a88SBenjamin Tissoires }
12652df68a88SBenjamin Tissoires 
1266f9036bd4SBenjamin Tissoires static int wacom_devm_sysfs_create_group(struct wacom *wacom,
12672a6a35a9SRikard Falkeborn 					 const struct attribute_group *group)
1268f9036bd4SBenjamin Tissoires {
1269f9036bd4SBenjamin Tissoires 	return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1270f9036bd4SBenjamin Tissoires 					       group);
1271f9036bd4SBenjamin Tissoires }
1272f9036bd4SBenjamin Tissoires 
127337309f47SPing Cheng static void wacom_devm_kfifo_release(struct device *dev, void *res)
127437309f47SPing Cheng {
127537309f47SPing Cheng 	struct kfifo_rec_ptr_2 *devres = res;
127637309f47SPing Cheng 
127737309f47SPing Cheng 	kfifo_free(devres);
127837309f47SPing Cheng }
127937309f47SPing Cheng 
128037309f47SPing Cheng static int wacom_devm_kfifo_alloc(struct wacom *wacom)
128137309f47SPing Cheng {
128237309f47SPing Cheng 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1283179e8e47SJason Gerecke 	struct kfifo_rec_ptr_2 *pen_fifo;
128437309f47SPing Cheng 	int error;
128537309f47SPing Cheng 
128637309f47SPing Cheng 	pen_fifo = devres_alloc(wacom_devm_kfifo_release,
128737309f47SPing Cheng 			      sizeof(struct kfifo_rec_ptr_2),
128837309f47SPing Cheng 			      GFP_KERNEL);
128937309f47SPing Cheng 
129037309f47SPing Cheng 	if (!pen_fifo)
129137309f47SPing Cheng 		return -ENOMEM;
129237309f47SPing Cheng 
129337309f47SPing Cheng 	error = kfifo_alloc(pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
129437309f47SPing Cheng 	if (error) {
129537309f47SPing Cheng 		devres_free(pen_fifo);
129637309f47SPing Cheng 		return error;
129737309f47SPing Cheng 	}
129837309f47SPing Cheng 
129937309f47SPing Cheng 	devres_add(&wacom->hdev->dev, pen_fifo);
1300179e8e47SJason Gerecke 	wacom_wac->pen_fifo = pen_fifo;
130137309f47SPing Cheng 
130237309f47SPing Cheng 	return 0;
130337309f47SPing Cheng }
130437309f47SPing Cheng 
130534736aa9SBenjamin Tissoires enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
130697f5541fSBenjamin Tissoires {
130797f5541fSBenjamin Tissoires 	struct wacom *wacom = led->wacom;
130897f5541fSBenjamin Tissoires 
130997f5541fSBenjamin Tissoires 	if (wacom->led.max_hlv)
131097f5541fSBenjamin Tissoires 		return led->hlv * LED_FULL / wacom->led.max_hlv;
131197f5541fSBenjamin Tissoires 
131297f5541fSBenjamin Tissoires 	if (wacom->led.max_llv)
131397f5541fSBenjamin Tissoires 		return led->llv * LED_FULL / wacom->led.max_llv;
131497f5541fSBenjamin Tissoires 
131597f5541fSBenjamin Tissoires 	/* device doesn't support brightness tuning */
131697f5541fSBenjamin Tissoires 	return LED_FULL;
131797f5541fSBenjamin Tissoires }
131897f5541fSBenjamin Tissoires 
131997f5541fSBenjamin Tissoires static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
132097f5541fSBenjamin Tissoires {
132197f5541fSBenjamin Tissoires 	struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
132297f5541fSBenjamin Tissoires 	struct wacom *wacom = led->wacom;
132397f5541fSBenjamin Tissoires 
132497f5541fSBenjamin Tissoires 	if (wacom->led.groups[led->group].select != led->id)
132597f5541fSBenjamin Tissoires 		return LED_OFF;
132697f5541fSBenjamin Tissoires 
132797f5541fSBenjamin Tissoires 	return wacom_leds_brightness_get(led);
132897f5541fSBenjamin Tissoires }
132997f5541fSBenjamin Tissoires 
133097f5541fSBenjamin Tissoires static int wacom_led_brightness_set(struct led_classdev *cdev,
133197f5541fSBenjamin Tissoires 				    enum led_brightness brightness)
133297f5541fSBenjamin Tissoires {
133397f5541fSBenjamin Tissoires 	struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
133497f5541fSBenjamin Tissoires 	struct wacom *wacom = led->wacom;
133597f5541fSBenjamin Tissoires 	int error;
133697f5541fSBenjamin Tissoires 
133797f5541fSBenjamin Tissoires 	mutex_lock(&wacom->lock);
133897f5541fSBenjamin Tissoires 
133997f5541fSBenjamin Tissoires 	if (!wacom->led.groups || (brightness == LED_OFF &&
134097f5541fSBenjamin Tissoires 	    wacom->led.groups[led->group].select != led->id)) {
134197f5541fSBenjamin Tissoires 		error = 0;
134297f5541fSBenjamin Tissoires 		goto out;
134397f5541fSBenjamin Tissoires 	}
134497f5541fSBenjamin Tissoires 
134597f5541fSBenjamin Tissoires 	led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
134697f5541fSBenjamin Tissoires 	led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
134797f5541fSBenjamin Tissoires 
134897f5541fSBenjamin Tissoires 	wacom->led.groups[led->group].select = led->id;
134997f5541fSBenjamin Tissoires 
135097f5541fSBenjamin Tissoires 	error = wacom_led_control(wacom);
135197f5541fSBenjamin Tissoires 
135297f5541fSBenjamin Tissoires out:
135397f5541fSBenjamin Tissoires 	mutex_unlock(&wacom->lock);
135497f5541fSBenjamin Tissoires 
135597f5541fSBenjamin Tissoires 	return error;
135697f5541fSBenjamin Tissoires }
135797f5541fSBenjamin Tissoires 
135897f5541fSBenjamin Tissoires static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
135997f5541fSBenjamin Tissoires 					       enum led_brightness brightness)
136097f5541fSBenjamin Tissoires {
136197f5541fSBenjamin Tissoires }
136297f5541fSBenjamin Tissoires 
136397f5541fSBenjamin Tissoires static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
136497f5541fSBenjamin Tissoires 				  struct wacom_led *led, unsigned int group,
136597f5541fSBenjamin Tissoires 				  unsigned int id, bool read_only)
136697f5541fSBenjamin Tissoires {
136797f5541fSBenjamin Tissoires 	int error;
136897f5541fSBenjamin Tissoires 	char *name;
136997f5541fSBenjamin Tissoires 
137097f5541fSBenjamin Tissoires 	name = devm_kasprintf(dev, GFP_KERNEL,
137197f5541fSBenjamin Tissoires 			      "%s::wacom-%d.%d",
137297f5541fSBenjamin Tissoires 			      dev_name(dev),
137397f5541fSBenjamin Tissoires 			      group,
137497f5541fSBenjamin Tissoires 			      id);
137597f5541fSBenjamin Tissoires 	if (!name)
137697f5541fSBenjamin Tissoires 		return -ENOMEM;
137797f5541fSBenjamin Tissoires 
137834736aa9SBenjamin Tissoires 	if (!read_only) {
137934736aa9SBenjamin Tissoires 		led->trigger.name = name;
138034736aa9SBenjamin Tissoires 		error = devm_led_trigger_register(dev, &led->trigger);
138134736aa9SBenjamin Tissoires 		if (error) {
138234736aa9SBenjamin Tissoires 			hid_err(wacom->hdev,
138334736aa9SBenjamin Tissoires 				"failed to register LED trigger %s: %d\n",
138434736aa9SBenjamin Tissoires 				led->cdev.name, error);
138534736aa9SBenjamin Tissoires 			return error;
138634736aa9SBenjamin Tissoires 		}
138734736aa9SBenjamin Tissoires 	}
138834736aa9SBenjamin Tissoires 
138997f5541fSBenjamin Tissoires 	led->group = group;
139097f5541fSBenjamin Tissoires 	led->id = id;
139197f5541fSBenjamin Tissoires 	led->wacom = wacom;
139297f5541fSBenjamin Tissoires 	led->llv = wacom->led.llv;
139397f5541fSBenjamin Tissoires 	led->hlv = wacom->led.hlv;
139497f5541fSBenjamin Tissoires 	led->cdev.name = name;
139597f5541fSBenjamin Tissoires 	led->cdev.max_brightness = LED_FULL;
139697f5541fSBenjamin Tissoires 	led->cdev.flags = LED_HW_PLUGGABLE;
139797f5541fSBenjamin Tissoires 	led->cdev.brightness_get = __wacom_led_brightness_get;
139834736aa9SBenjamin Tissoires 	if (!read_only) {
139997f5541fSBenjamin Tissoires 		led->cdev.brightness_set_blocking = wacom_led_brightness_set;
140034736aa9SBenjamin Tissoires 		led->cdev.default_trigger = led->cdev.name;
140134736aa9SBenjamin Tissoires 	} else {
140297f5541fSBenjamin Tissoires 		led->cdev.brightness_set = wacom_led_readonly_brightness_set;
140334736aa9SBenjamin Tissoires 	}
140497f5541fSBenjamin Tissoires 
140597f5541fSBenjamin Tissoires 	error = devm_led_classdev_register(dev, &led->cdev);
140697f5541fSBenjamin Tissoires 	if (error) {
140797f5541fSBenjamin Tissoires 		hid_err(wacom->hdev,
140897f5541fSBenjamin Tissoires 			"failed to register LED %s: %d\n",
140997f5541fSBenjamin Tissoires 			led->cdev.name, error);
141097f5541fSBenjamin Tissoires 		led->cdev.name = NULL;
141197f5541fSBenjamin Tissoires 		return error;
141297f5541fSBenjamin Tissoires 	}
141397f5541fSBenjamin Tissoires 
141497f5541fSBenjamin Tissoires 	return 0;
141597f5541fSBenjamin Tissoires }
141697f5541fSBenjamin Tissoires 
1417589e5060SBenjamin Tissoires static void wacom_led_groups_release_one(void *data)
1418589e5060SBenjamin Tissoires {
1419589e5060SBenjamin Tissoires 	struct wacom_group_leds *group = data;
1420589e5060SBenjamin Tissoires 
1421589e5060SBenjamin Tissoires 	devres_release_group(group->dev, group);
1422589e5060SBenjamin Tissoires }
1423589e5060SBenjamin Tissoires 
142497f5541fSBenjamin Tissoires static int wacom_led_groups_alloc_and_register_one(struct device *dev,
142597f5541fSBenjamin Tissoires 						   struct wacom *wacom,
142697f5541fSBenjamin Tissoires 						   int group_id, int count,
142797f5541fSBenjamin Tissoires 						   bool read_only)
142897f5541fSBenjamin Tissoires {
142997f5541fSBenjamin Tissoires 	struct wacom_led *leds;
143097f5541fSBenjamin Tissoires 	int i, error;
143197f5541fSBenjamin Tissoires 
143297f5541fSBenjamin Tissoires 	if (group_id >= wacom->led.count || count <= 0)
143397f5541fSBenjamin Tissoires 		return -EINVAL;
143497f5541fSBenjamin Tissoires 
143597f5541fSBenjamin Tissoires 	if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
143697f5541fSBenjamin Tissoires 		return -ENOMEM;
143797f5541fSBenjamin Tissoires 
1438a86854d0SKees Cook 	leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL);
143997f5541fSBenjamin Tissoires 	if (!leds) {
144097f5541fSBenjamin Tissoires 		error = -ENOMEM;
144197f5541fSBenjamin Tissoires 		goto err;
144297f5541fSBenjamin Tissoires 	}
144397f5541fSBenjamin Tissoires 
144497f5541fSBenjamin Tissoires 	wacom->led.groups[group_id].leds = leds;
144597f5541fSBenjamin Tissoires 	wacom->led.groups[group_id].count = count;
144697f5541fSBenjamin Tissoires 
144797f5541fSBenjamin Tissoires 	for (i = 0; i < count; i++) {
144897f5541fSBenjamin Tissoires 		error = wacom_led_register_one(dev, wacom, &leds[i],
144997f5541fSBenjamin Tissoires 					       group_id, i, read_only);
145097f5541fSBenjamin Tissoires 		if (error)
145197f5541fSBenjamin Tissoires 			goto err;
145297f5541fSBenjamin Tissoires 	}
145397f5541fSBenjamin Tissoires 
1454589e5060SBenjamin Tissoires 	wacom->led.groups[group_id].dev = dev;
1455589e5060SBenjamin Tissoires 
1456589e5060SBenjamin Tissoires 	devres_close_group(dev, &wacom->led.groups[group_id]);
1457589e5060SBenjamin Tissoires 
1458589e5060SBenjamin Tissoires 	/*
1459589e5060SBenjamin Tissoires 	 * There is a bug (?) in devm_led_classdev_register() in which its
1460589e5060SBenjamin Tissoires 	 * increments the refcount of the parent. If the parent is an input
1461589e5060SBenjamin Tissoires 	 * device, that means the ref count never reaches 0 when
1462589e5060SBenjamin Tissoires 	 * devm_input_device_release() gets called.
1463589e5060SBenjamin Tissoires 	 * This means that the LEDs are still there after disconnect.
1464589e5060SBenjamin Tissoires 	 * Manually force the release of the group so that the leds are released
1465589e5060SBenjamin Tissoires 	 * once we are done using them.
1466589e5060SBenjamin Tissoires 	 */
1467589e5060SBenjamin Tissoires 	error = devm_add_action_or_reset(&wacom->hdev->dev,
1468589e5060SBenjamin Tissoires 					 wacom_led_groups_release_one,
1469589e5060SBenjamin Tissoires 					 &wacom->led.groups[group_id]);
1470589e5060SBenjamin Tissoires 	if (error)
1471589e5060SBenjamin Tissoires 		return error;
1472589e5060SBenjamin Tissoires 
147397f5541fSBenjamin Tissoires 	return 0;
147497f5541fSBenjamin Tissoires 
147597f5541fSBenjamin Tissoires err:
147697f5541fSBenjamin Tissoires 	devres_release_group(dev, &wacom->led.groups[group_id]);
147797f5541fSBenjamin Tissoires 	return error;
147897f5541fSBenjamin Tissoires }
147997f5541fSBenjamin Tissoires 
148034736aa9SBenjamin Tissoires struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
148134736aa9SBenjamin Tissoires 				 unsigned int id)
148234736aa9SBenjamin Tissoires {
148334736aa9SBenjamin Tissoires 	struct wacom_group_leds *group;
148434736aa9SBenjamin Tissoires 
148534736aa9SBenjamin Tissoires 	if (group_id >= wacom->led.count)
148634736aa9SBenjamin Tissoires 		return NULL;
148734736aa9SBenjamin Tissoires 
148834736aa9SBenjamin Tissoires 	group = &wacom->led.groups[group_id];
148934736aa9SBenjamin Tissoires 
149034736aa9SBenjamin Tissoires 	if (!group->leds)
149134736aa9SBenjamin Tissoires 		return NULL;
149234736aa9SBenjamin Tissoires 
149334736aa9SBenjamin Tissoires 	id %= group->count;
149434736aa9SBenjamin Tissoires 
149534736aa9SBenjamin Tissoires 	return &group->leds[id];
149634736aa9SBenjamin Tissoires }
149734736aa9SBenjamin Tissoires 
149834736aa9SBenjamin Tissoires /**
149934736aa9SBenjamin Tissoires  * wacom_led_next: gives the next available led with a wacom trigger.
150034736aa9SBenjamin Tissoires  *
150134736aa9SBenjamin Tissoires  * returns the next available struct wacom_led which has its default trigger
150234736aa9SBenjamin Tissoires  * or the current one if none is available.
150334736aa9SBenjamin Tissoires  */
150434736aa9SBenjamin Tissoires struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
150534736aa9SBenjamin Tissoires {
150634736aa9SBenjamin Tissoires 	struct wacom_led *next_led;
150734736aa9SBenjamin Tissoires 	int group, next;
150834736aa9SBenjamin Tissoires 
150934736aa9SBenjamin Tissoires 	if (!wacom || !cur)
151034736aa9SBenjamin Tissoires 		return NULL;
151134736aa9SBenjamin Tissoires 
151234736aa9SBenjamin Tissoires 	group = cur->group;
151334736aa9SBenjamin Tissoires 	next = cur->id;
151434736aa9SBenjamin Tissoires 
151534736aa9SBenjamin Tissoires 	do {
151634736aa9SBenjamin Tissoires 		next_led = wacom_led_find(wacom, group, ++next);
151734736aa9SBenjamin Tissoires 		if (!next_led || next_led == cur)
151834736aa9SBenjamin Tissoires 			return next_led;
151934736aa9SBenjamin Tissoires 	} while (next_led->cdev.trigger != &next_led->trigger);
152034736aa9SBenjamin Tissoires 
152134736aa9SBenjamin Tissoires 	return next_led;
152234736aa9SBenjamin Tissoires }
152334736aa9SBenjamin Tissoires 
1524a50aac71SBenjamin Tissoires static void wacom_led_groups_release(void *data)
1525a50aac71SBenjamin Tissoires {
1526a50aac71SBenjamin Tissoires 	struct wacom *wacom = data;
1527a50aac71SBenjamin Tissoires 
1528a50aac71SBenjamin Tissoires 	wacom->led.groups = NULL;
152997f5541fSBenjamin Tissoires 	wacom->led.count = 0;
1530a50aac71SBenjamin Tissoires }
1531a50aac71SBenjamin Tissoires 
1532a50aac71SBenjamin Tissoires static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1533a50aac71SBenjamin Tissoires {
153497f5541fSBenjamin Tissoires 	struct device *dev = &wacom->hdev->dev;
1535a50aac71SBenjamin Tissoires 	struct wacom_group_leds *groups;
1536a50aac71SBenjamin Tissoires 	int error;
1537a50aac71SBenjamin Tissoires 
1538a86854d0SKees Cook 	groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds),
1539a50aac71SBenjamin Tissoires 			      GFP_KERNEL);
1540a50aac71SBenjamin Tissoires 	if (!groups)
1541a50aac71SBenjamin Tissoires 		return -ENOMEM;
1542a50aac71SBenjamin Tissoires 
154397f5541fSBenjamin Tissoires 	error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1544a50aac71SBenjamin Tissoires 	if (error)
1545a50aac71SBenjamin Tissoires 		return error;
1546a50aac71SBenjamin Tissoires 
1547a50aac71SBenjamin Tissoires 	wacom->led.groups = groups;
154897f5541fSBenjamin Tissoires 	wacom->led.count = count;
154997f5541fSBenjamin Tissoires 
155097f5541fSBenjamin Tissoires 	return 0;
155197f5541fSBenjamin Tissoires }
155297f5541fSBenjamin Tissoires 
155397f5541fSBenjamin Tissoires static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
155497f5541fSBenjamin Tissoires 					 int led_per_group, bool read_only)
155597f5541fSBenjamin Tissoires {
155697f5541fSBenjamin Tissoires 	struct device *dev;
155797f5541fSBenjamin Tissoires 	int i, error;
155897f5541fSBenjamin Tissoires 
155997f5541fSBenjamin Tissoires 	if (!wacom->wacom_wac.pad_input)
156097f5541fSBenjamin Tissoires 		return -EINVAL;
156197f5541fSBenjamin Tissoires 
156297f5541fSBenjamin Tissoires 	dev = &wacom->wacom_wac.pad_input->dev;
156397f5541fSBenjamin Tissoires 
156497f5541fSBenjamin Tissoires 	error = wacom_led_groups_allocate(wacom, group_count);
156597f5541fSBenjamin Tissoires 	if (error)
156697f5541fSBenjamin Tissoires 		return error;
156797f5541fSBenjamin Tissoires 
156897f5541fSBenjamin Tissoires 	for (i = 0; i < group_count; i++) {
156997f5541fSBenjamin Tissoires 		error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
157097f5541fSBenjamin Tissoires 								led_per_group,
157197f5541fSBenjamin Tissoires 								read_only);
157297f5541fSBenjamin Tissoires 		if (error)
157397f5541fSBenjamin Tissoires 			return error;
157497f5541fSBenjamin Tissoires 	}
1575a50aac71SBenjamin Tissoires 
1576a50aac71SBenjamin Tissoires 	return 0;
1577a50aac71SBenjamin Tissoires }
1578a50aac71SBenjamin Tissoires 
157910c55cacSAaron Armstrong Skomra int wacom_initialize_leds(struct wacom *wacom)
1580471d1714SBenjamin Tissoires {
1581471d1714SBenjamin Tissoires 	int error;
1582471d1714SBenjamin Tissoires 
1583862cf553SJason Gerecke 	if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1584862cf553SJason Gerecke 		return 0;
1585862cf553SJason Gerecke 
1586471d1714SBenjamin Tissoires 	/* Initialize default values */
1587471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
158810c55cacSAaron Armstrong Skomra 	case HID_GENERIC:
158910c55cacSAaron Armstrong Skomra 		if (!wacom->generic_has_leds)
159010c55cacSAaron Armstrong Skomra 			return 0;
159110c55cacSAaron Armstrong Skomra 		wacom->led.llv = 100;
159210c55cacSAaron Armstrong Skomra 		wacom->led.max_llv = 100;
159310c55cacSAaron Armstrong Skomra 
159410c55cacSAaron Armstrong Skomra 		error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
159510c55cacSAaron Armstrong Skomra 		if (error) {
159610c55cacSAaron Armstrong Skomra 			hid_err(wacom->hdev,
159710c55cacSAaron Armstrong Skomra 				"cannot create leds err: %d\n", error);
159810c55cacSAaron Armstrong Skomra 			return error;
159910c55cacSAaron Armstrong Skomra 		}
160010c55cacSAaron Armstrong Skomra 
160110c55cacSAaron Armstrong Skomra 		error = wacom_devm_sysfs_create_group(wacom,
160210c55cacSAaron Armstrong Skomra 						      &generic_led_attr_group);
160310c55cacSAaron Armstrong Skomra 		break;
160410c55cacSAaron Armstrong Skomra 
1605471d1714SBenjamin Tissoires 	case INTUOS4S:
1606471d1714SBenjamin Tissoires 	case INTUOS4:
160781af7e61SBenjamin Tissoires 	case INTUOS4WL:
1608471d1714SBenjamin Tissoires 	case INTUOS4L:
1609471d1714SBenjamin Tissoires 		wacom->led.llv = 10;
1610471d1714SBenjamin Tissoires 		wacom->led.hlv = 20;
161197f5541fSBenjamin Tissoires 		wacom->led.max_llv = 127;
161297f5541fSBenjamin Tissoires 		wacom->led.max_hlv = 127;
1613471d1714SBenjamin Tissoires 		wacom->led.img_lum = 10;
1614a50aac71SBenjamin Tissoires 
161597f5541fSBenjamin Tissoires 		error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1616a50aac71SBenjamin Tissoires 		if (error) {
1617a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1618a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1619a50aac71SBenjamin Tissoires 			return error;
1620a50aac71SBenjamin Tissoires 		}
1621a50aac71SBenjamin Tissoires 
16222df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
1623471d1714SBenjamin Tissoires 						      &intuos4_led_attr_group);
1624471d1714SBenjamin Tissoires 		break;
1625471d1714SBenjamin Tissoires 
1626471d1714SBenjamin Tissoires 	case WACOM_24HD:
1627471d1714SBenjamin Tissoires 	case WACOM_21UX2:
1628471d1714SBenjamin Tissoires 		wacom->led.llv = 0;
1629471d1714SBenjamin Tissoires 		wacom->led.hlv = 0;
1630471d1714SBenjamin Tissoires 		wacom->led.img_lum = 0;
1631471d1714SBenjamin Tissoires 
163297f5541fSBenjamin Tissoires 		error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1633a50aac71SBenjamin Tissoires 		if (error) {
1634a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1635a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1636a50aac71SBenjamin Tissoires 			return error;
1637a50aac71SBenjamin Tissoires 		}
1638a50aac71SBenjamin Tissoires 
16392df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
1640471d1714SBenjamin Tissoires 						      &cintiq_led_attr_group);
1641471d1714SBenjamin Tissoires 		break;
1642471d1714SBenjamin Tissoires 
1643471d1714SBenjamin Tissoires 	case INTUOS5S:
1644471d1714SBenjamin Tissoires 	case INTUOS5:
1645471d1714SBenjamin Tissoires 	case INTUOS5L:
1646471d1714SBenjamin Tissoires 	case INTUOSPS:
1647471d1714SBenjamin Tissoires 	case INTUOSPM:
1648471d1714SBenjamin Tissoires 	case INTUOSPL:
1649471d1714SBenjamin Tissoires 		wacom->led.llv = 32;
165097f5541fSBenjamin Tissoires 		wacom->led.max_llv = 96;
1651471d1714SBenjamin Tissoires 
165297f5541fSBenjamin Tissoires 		error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1653a50aac71SBenjamin Tissoires 		if (error) {
1654a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1655a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1656a50aac71SBenjamin Tissoires 			return error;
1657a50aac71SBenjamin Tissoires 		}
1658a50aac71SBenjamin Tissoires 
16592df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
1660471d1714SBenjamin Tissoires 						      &intuos5_led_attr_group);
1661471d1714SBenjamin Tissoires 		break;
1662471d1714SBenjamin Tissoires 
16634922cd26SJason Gerecke 	case INTUOSP2_BT:
16644922cd26SJason Gerecke 		wacom->led.llv = 50;
16654922cd26SJason Gerecke 		wacom->led.max_llv = 100;
16664922cd26SJason Gerecke 		error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
16674922cd26SJason Gerecke 		if (error) {
16684922cd26SJason Gerecke 			hid_err(wacom->hdev,
16694922cd26SJason Gerecke 				"cannot create leds err: %d\n", error);
16704922cd26SJason Gerecke 			return error;
16714922cd26SJason Gerecke 		}
16724922cd26SJason Gerecke 		return 0;
16734922cd26SJason Gerecke 
1674a50aac71SBenjamin Tissoires 	case REMOTE:
167597f5541fSBenjamin Tissoires 		wacom->led.llv = 255;
167697f5541fSBenjamin Tissoires 		wacom->led.max_llv = 255;
1677a50aac71SBenjamin Tissoires 		error = wacom_led_groups_allocate(wacom, 5);
1678a50aac71SBenjamin Tissoires 		if (error) {
1679a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1680a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1681a50aac71SBenjamin Tissoires 			return error;
1682a50aac71SBenjamin Tissoires 		}
1683a50aac71SBenjamin Tissoires 		return 0;
1684a50aac71SBenjamin Tissoires 
1685471d1714SBenjamin Tissoires 	default:
1686471d1714SBenjamin Tissoires 		return 0;
1687471d1714SBenjamin Tissoires 	}
1688471d1714SBenjamin Tissoires 
1689471d1714SBenjamin Tissoires 	if (error) {
1690471d1714SBenjamin Tissoires 		hid_err(wacom->hdev,
1691471d1714SBenjamin Tissoires 			"cannot create sysfs group err: %d\n", error);
1692471d1714SBenjamin Tissoires 		return error;
1693471d1714SBenjamin Tissoires 	}
1694471d1714SBenjamin Tissoires 
1695471d1714SBenjamin Tissoires 	return 0;
1696471d1714SBenjamin Tissoires }
1697471d1714SBenjamin Tissoires 
1698a544c619SBenjamin Tissoires static void wacom_init_work(struct work_struct *work)
1699a544c619SBenjamin Tissoires {
1700a544c619SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1701a544c619SBenjamin Tissoires 
1702a544c619SBenjamin Tissoires 	_wacom_query_tablet_data(wacom);
1703a544c619SBenjamin Tissoires 	wacom_led_control(wacom);
1704a544c619SBenjamin Tissoires }
1705a544c619SBenjamin Tissoires 
1706a544c619SBenjamin Tissoires static void wacom_query_tablet_data(struct wacom *wacom)
1707a544c619SBenjamin Tissoires {
1708a544c619SBenjamin Tissoires 	schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1709a544c619SBenjamin Tissoires }
1710a544c619SBenjamin Tissoires 
1711471d1714SBenjamin Tissoires static enum power_supply_property wacom_battery_props[] = {
17129956953eSBenjamin Tissoires 	POWER_SUPPLY_PROP_MODEL_NAME,
171371fa641eSJason Gerecke 	POWER_SUPPLY_PROP_PRESENT,
1714ac8d1010SBenjamin Tissoires 	POWER_SUPPLY_PROP_STATUS,
1715471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
1716471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_CAPACITY
1717471d1714SBenjamin Tissoires };
1718471d1714SBenjamin Tissoires 
1719471d1714SBenjamin Tissoires static int wacom_battery_get_property(struct power_supply *psy,
1720471d1714SBenjamin Tissoires 				      enum power_supply_property psp,
1721471d1714SBenjamin Tissoires 				      union power_supply_propval *val)
1722471d1714SBenjamin Tissoires {
172359d69bc8SBenjamin Tissoires 	struct wacom_battery *battery = power_supply_get_drvdata(psy);
1724471d1714SBenjamin Tissoires 	int ret = 0;
1725471d1714SBenjamin Tissoires 
1726471d1714SBenjamin Tissoires 	switch (psp) {
17279956953eSBenjamin Tissoires 		case POWER_SUPPLY_PROP_MODEL_NAME:
17289956953eSBenjamin Tissoires 			val->strval = battery->wacom->wacom_wac.name;
17299956953eSBenjamin Tissoires 			break;
173071fa641eSJason Gerecke 		case POWER_SUPPLY_PROP_PRESENT:
173159d69bc8SBenjamin Tissoires 			val->intval = battery->bat_connected;
173271fa641eSJason Gerecke 			break;
1733471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_SCOPE:
1734471d1714SBenjamin Tissoires 			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1735471d1714SBenjamin Tissoires 			break;
1736471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_CAPACITY:
173759d69bc8SBenjamin Tissoires 			val->intval = battery->battery_capacity;
1738ac8d1010SBenjamin Tissoires 			break;
1739ac8d1010SBenjamin Tissoires 		case POWER_SUPPLY_PROP_STATUS:
174016e45989SJason Gerecke 			if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
174116e45989SJason Gerecke 				val->intval = battery->bat_status;
174216e45989SJason Gerecke 			else if (battery->bat_charging)
1743ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_CHARGING;
174459d69bc8SBenjamin Tissoires 			else if (battery->battery_capacity == 100 &&
174559d69bc8SBenjamin Tissoires 				    battery->ps_connected)
1746ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_FULL;
174759d69bc8SBenjamin Tissoires 			else if (battery->ps_connected)
1748b0882cb7SJason Gerecke 				val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1749ac8d1010SBenjamin Tissoires 			else
1750ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1751471d1714SBenjamin Tissoires 			break;
1752471d1714SBenjamin Tissoires 		default:
1753471d1714SBenjamin Tissoires 			ret = -EINVAL;
1754471d1714SBenjamin Tissoires 			break;
1755471d1714SBenjamin Tissoires 	}
1756471d1714SBenjamin Tissoires 
1757471d1714SBenjamin Tissoires 	return ret;
1758471d1714SBenjamin Tissoires }
1759471d1714SBenjamin Tissoires 
176059d69bc8SBenjamin Tissoires static int __wacom_initialize_battery(struct wacom *wacom,
176159d69bc8SBenjamin Tissoires 				      struct wacom_battery *battery)
1762471d1714SBenjamin Tissoires {
1763d70420b9SBenjamin Tissoires 	static atomic_t battery_no = ATOMIC_INIT(0);
1764b189da90SBenjamin Tissoires 	struct device *dev = &wacom->hdev->dev;
176559d69bc8SBenjamin Tissoires 	struct power_supply_config psy_cfg = { .drv_data = battery, };
1766136ae5e9SBenjamin Tissoires 	struct power_supply *ps_bat;
176759d69bc8SBenjamin Tissoires 	struct power_supply_desc *bat_desc = &battery->bat_desc;
1768d70420b9SBenjamin Tissoires 	unsigned long n;
1769b189da90SBenjamin Tissoires 	int error;
1770b189da90SBenjamin Tissoires 
1771b189da90SBenjamin Tissoires 	if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1772b189da90SBenjamin Tissoires 		return -ENOMEM;
1773471d1714SBenjamin Tissoires 
17749956953eSBenjamin Tissoires 	battery->wacom = wacom;
17759956953eSBenjamin Tissoires 
1776d70420b9SBenjamin Tissoires 	n = atomic_inc_return(&battery_no) - 1;
17777dbd229eSBenjamin Tissoires 
1778297d716fSKrzysztof Kozlowski 	bat_desc->properties = wacom_battery_props;
1779297d716fSKrzysztof Kozlowski 	bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1780297d716fSKrzysztof Kozlowski 	bat_desc->get_property = wacom_battery_get_property;
178159d69bc8SBenjamin Tissoires 	sprintf(battery->bat_name, "wacom_battery_%ld", n);
178259d69bc8SBenjamin Tissoires 	bat_desc->name = battery->bat_name;
178396983296SBenjamin Tissoires 	bat_desc->type = POWER_SUPPLY_TYPE_USB;
1784297d716fSKrzysztof Kozlowski 	bat_desc->use_for_apm = 0;
1785471d1714SBenjamin Tissoires 
178659d69bc8SBenjamin Tissoires 	ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
178759d69bc8SBenjamin Tissoires 	if (IS_ERR(ps_bat)) {
178859d69bc8SBenjamin Tissoires 		error = PTR_ERR(ps_bat);
1789b189da90SBenjamin Tissoires 		goto err;
1790b189da90SBenjamin Tissoires 	}
1791471d1714SBenjamin Tissoires 
179259d69bc8SBenjamin Tissoires 	power_supply_powers(ps_bat, &wacom->hdev->dev);
179359d69bc8SBenjamin Tissoires 
179459d69bc8SBenjamin Tissoires 	battery->battery = ps_bat;
17957dbd229eSBenjamin Tissoires 
1796b189da90SBenjamin Tissoires 	devres_close_group(dev, bat_desc);
17977dbd229eSBenjamin Tissoires 	return 0;
1798b189da90SBenjamin Tissoires 
1799b189da90SBenjamin Tissoires err:
1800b189da90SBenjamin Tissoires 	devres_release_group(dev, bat_desc);
1801b189da90SBenjamin Tissoires 	return error;
1802471d1714SBenjamin Tissoires }
1803471d1714SBenjamin Tissoires 
180459d69bc8SBenjamin Tissoires static int wacom_initialize_battery(struct wacom *wacom)
180559d69bc8SBenjamin Tissoires {
180659d69bc8SBenjamin Tissoires 	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
180759d69bc8SBenjamin Tissoires 		return __wacom_initialize_battery(wacom, &wacom->battery);
180859d69bc8SBenjamin Tissoires 
180959d69bc8SBenjamin Tissoires 	return 0;
181059d69bc8SBenjamin Tissoires }
181159d69bc8SBenjamin Tissoires 
1812471d1714SBenjamin Tissoires static void wacom_destroy_battery(struct wacom *wacom)
1813471d1714SBenjamin Tissoires {
181459d69bc8SBenjamin Tissoires 	if (wacom->battery.battery) {
181559d69bc8SBenjamin Tissoires 		devres_release_group(&wacom->hdev->dev,
181659d69bc8SBenjamin Tissoires 				     &wacom->battery.bat_desc);
181759d69bc8SBenjamin Tissoires 		wacom->battery.battery = NULL;
1818471d1714SBenjamin Tissoires 	}
1819471d1714SBenjamin Tissoires }
1820471d1714SBenjamin Tissoires 
1821f81a1295SBenjamin Tissoires static ssize_t wacom_show_speed(struct device *dev,
1822f81a1295SBenjamin Tissoires 				struct device_attribute
1823f81a1295SBenjamin Tissoires 				*attr, char *buf)
1824f81a1295SBenjamin Tissoires {
1825ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1826f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1827f81a1295SBenjamin Tissoires 
1828*d8833124SJiapeng Chong 	return sysfs_emit(buf, "%i\n", wacom->wacom_wac.bt_high_speed);
1829f81a1295SBenjamin Tissoires }
1830f81a1295SBenjamin Tissoires 
1831f81a1295SBenjamin Tissoires static ssize_t wacom_store_speed(struct device *dev,
1832f81a1295SBenjamin Tissoires 				struct device_attribute *attr,
1833f81a1295SBenjamin Tissoires 				const char *buf, size_t count)
1834f81a1295SBenjamin Tissoires {
1835ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1836f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1837f81a1295SBenjamin Tissoires 	u8 new_speed;
1838f81a1295SBenjamin Tissoires 
1839f81a1295SBenjamin Tissoires 	if (kstrtou8(buf, 0, &new_speed))
1840f81a1295SBenjamin Tissoires 		return -EINVAL;
1841f81a1295SBenjamin Tissoires 
1842f81a1295SBenjamin Tissoires 	if (new_speed != 0 && new_speed != 1)
1843f81a1295SBenjamin Tissoires 		return -EINVAL;
1844f81a1295SBenjamin Tissoires 
1845f81a1295SBenjamin Tissoires 	wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1846f81a1295SBenjamin Tissoires 
1847f81a1295SBenjamin Tissoires 	return count;
1848f81a1295SBenjamin Tissoires }
1849f81a1295SBenjamin Tissoires 
1850e0984bc3SPing Cheng static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1851f81a1295SBenjamin Tissoires 		wacom_show_speed, wacom_store_speed);
1852f81a1295SBenjamin Tissoires 
185372b236d6SAaron Skomra 
185472b236d6SAaron Skomra static ssize_t wacom_show_remote_mode(struct kobject *kobj,
185572b236d6SAaron Skomra 				      struct kobj_attribute *kattr,
185672b236d6SAaron Skomra 				      char *buf, int index)
185772b236d6SAaron Skomra {
18582cf83833SGeliang Tang 	struct device *dev = kobj_to_dev(kobj->parent);
1859ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
186072b236d6SAaron Skomra 	struct wacom *wacom = hid_get_drvdata(hdev);
186172b236d6SAaron Skomra 	u8 mode;
186272b236d6SAaron Skomra 
1863a50aac71SBenjamin Tissoires 	mode = wacom->led.groups[index].select;
1864bc35f73aSChristos Gkekas 	return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
186572b236d6SAaron Skomra }
186672b236d6SAaron Skomra 
186772b236d6SAaron Skomra #define DEVICE_EKR_ATTR_GROUP(SET_ID)					\
186872b236d6SAaron Skomra static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj,	\
186972b236d6SAaron Skomra 			       struct kobj_attribute *kattr, char *buf)	\
187072b236d6SAaron Skomra {									\
187172b236d6SAaron Skomra 	return wacom_show_remote_mode(kobj, kattr, buf, SET_ID);	\
187272b236d6SAaron Skomra }									\
187372b236d6SAaron Skomra static struct kobj_attribute remote##SET_ID##_mode_attr = {		\
187472b236d6SAaron Skomra 	.attr = {.name = "remote_mode",					\
187572b236d6SAaron Skomra 		.mode = DEV_ATTR_RO_PERM},				\
187672b236d6SAaron Skomra 	.show = wacom_show_remote##SET_ID##_mode,			\
187772b236d6SAaron Skomra };									\
187872b236d6SAaron Skomra static struct attribute *remote##SET_ID##_serial_attrs[] = {		\
187972b236d6SAaron Skomra 	&remote##SET_ID##_mode_attr.attr,				\
188072b236d6SAaron Skomra 	NULL								\
188172b236d6SAaron Skomra };									\
18822a6a35a9SRikard Falkeborn static const struct attribute_group remote##SET_ID##_serial_group = {	\
188372b236d6SAaron Skomra 	.name = NULL,							\
188472b236d6SAaron Skomra 	.attrs = remote##SET_ID##_serial_attrs,				\
188572b236d6SAaron Skomra }
188672b236d6SAaron Skomra 
188772b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(0);
188872b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(1);
188972b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(2);
189072b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(3);
189172b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(4);
189272b236d6SAaron Skomra 
1893e6f2813aSBenjamin Tissoires static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1894e6f2813aSBenjamin Tissoires 					  int index)
189572b236d6SAaron Skomra {
189672b236d6SAaron Skomra 	int error = 0;
189783e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
189872b236d6SAaron Skomra 
1899e7749f6eSBenjamin Tissoires 	remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1900c1f5409bSBenjamin Tissoires 							  GFP_KERNEL,
1901c1f5409bSBenjamin Tissoires 							  "%d", serial);
1902e7749f6eSBenjamin Tissoires 	if (!remote->remotes[index].group.name)
190372b236d6SAaron Skomra 		return -ENOMEM;
190472b236d6SAaron Skomra 
1905f9036bd4SBenjamin Tissoires 	error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1906e7749f6eSBenjamin Tissoires 						&remote->remotes[index].group);
190772b236d6SAaron Skomra 	if (error) {
1908e7749f6eSBenjamin Tissoires 		remote->remotes[index].group.name = NULL;
190972b236d6SAaron Skomra 		hid_err(wacom->hdev,
191072b236d6SAaron Skomra 			"cannot create sysfs group err: %d\n", error);
191172b236d6SAaron Skomra 		return error;
191272b236d6SAaron Skomra 	}
191372b236d6SAaron Skomra 
191472b236d6SAaron Skomra 	return 0;
191572b236d6SAaron Skomra }
191672b236d6SAaron Skomra 
191772b236d6SAaron Skomra static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
191872b236d6SAaron Skomra {
191972b236d6SAaron Skomra 	const size_t buf_size = 2;
192072b236d6SAaron Skomra 	unsigned char *buf;
192172b236d6SAaron Skomra 	int retval;
192272b236d6SAaron Skomra 
192372b236d6SAaron Skomra 	buf = kzalloc(buf_size, GFP_KERNEL);
192472b236d6SAaron Skomra 	if (!buf)
192572b236d6SAaron Skomra 		return -ENOMEM;
192672b236d6SAaron Skomra 
192772b236d6SAaron Skomra 	buf[0] = WAC_CMD_DELETE_PAIRING;
192872b236d6SAaron Skomra 	buf[1] = selector;
192972b236d6SAaron Skomra 
193072b236d6SAaron Skomra 	retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
193172b236d6SAaron Skomra 				  buf_size, WAC_CMD_RETRIES);
193272b236d6SAaron Skomra 	kfree(buf);
193372b236d6SAaron Skomra 
193472b236d6SAaron Skomra 	return retval;
193572b236d6SAaron Skomra }
193672b236d6SAaron Skomra 
193772b236d6SAaron Skomra static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
193872b236d6SAaron Skomra 					 struct kobj_attribute *attr,
193972b236d6SAaron Skomra 					 const char *buf, size_t count)
194072b236d6SAaron Skomra {
194172b236d6SAaron Skomra 	unsigned char selector = 0;
19422cf83833SGeliang Tang 	struct device *dev = kobj_to_dev(kobj->parent);
1943ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
194472b236d6SAaron Skomra 	struct wacom *wacom = hid_get_drvdata(hdev);
194572b236d6SAaron Skomra 	int err;
194672b236d6SAaron Skomra 
194772b236d6SAaron Skomra 	if (!strncmp(buf, "*\n", 2)) {
194872b236d6SAaron Skomra 		selector = WAC_CMD_UNPAIR_ALL;
194972b236d6SAaron Skomra 	} else {
195072b236d6SAaron Skomra 		hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
195172b236d6SAaron Skomra 			 buf);
195272b236d6SAaron Skomra 		return -1;
195372b236d6SAaron Skomra 	}
195472b236d6SAaron Skomra 
195572b236d6SAaron Skomra 	mutex_lock(&wacom->lock);
195672b236d6SAaron Skomra 
195772b236d6SAaron Skomra 	err = wacom_cmd_unpair_remote(wacom, selector);
195872b236d6SAaron Skomra 	mutex_unlock(&wacom->lock);
195972b236d6SAaron Skomra 
196072b236d6SAaron Skomra 	return err < 0 ? err : count;
196172b236d6SAaron Skomra }
196272b236d6SAaron Skomra 
196372b236d6SAaron Skomra static struct kobj_attribute unpair_remote_attr = {
196472b236d6SAaron Skomra 	.attr = {.name = "unpair_remote", .mode = 0200},
196572b236d6SAaron Skomra 	.store = wacom_store_unpair_remote,
196672b236d6SAaron Skomra };
196772b236d6SAaron Skomra 
196872b236d6SAaron Skomra static const struct attribute *remote_unpair_attrs[] = {
196972b236d6SAaron Skomra 	&unpair_remote_attr.attr,
197072b236d6SAaron Skomra 	NULL
197172b236d6SAaron Skomra };
197272b236d6SAaron Skomra 
197383e6b40eSBenjamin Tissoires static void wacom_remotes_destroy(void *data)
197483e6b40eSBenjamin Tissoires {
197583e6b40eSBenjamin Tissoires 	struct wacom *wacom = data;
197683e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
197783e6b40eSBenjamin Tissoires 
197883e6b40eSBenjamin Tissoires 	if (!remote)
197983e6b40eSBenjamin Tissoires 		return;
198083e6b40eSBenjamin Tissoires 
198183e6b40eSBenjamin Tissoires 	kobject_put(remote->remote_dir);
198283e6b40eSBenjamin Tissoires 	kfifo_free(&remote->remote_fifo);
198383e6b40eSBenjamin Tissoires 	wacom->remote = NULL;
198483e6b40eSBenjamin Tissoires }
198583e6b40eSBenjamin Tissoires 
198683e6b40eSBenjamin Tissoires static int wacom_initialize_remotes(struct wacom *wacom)
198772b236d6SAaron Skomra {
198872b236d6SAaron Skomra 	int error = 0;
198983e6b40eSBenjamin Tissoires 	struct wacom_remote *remote;
199072b236d6SAaron Skomra 	int i;
199172b236d6SAaron Skomra 
199272b236d6SAaron Skomra 	if (wacom->wacom_wac.features.type != REMOTE)
199372b236d6SAaron Skomra 		return 0;
199472b236d6SAaron Skomra 
199583e6b40eSBenjamin Tissoires 	remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
199683e6b40eSBenjamin Tissoires 			      GFP_KERNEL);
199783e6b40eSBenjamin Tissoires 	if (!remote)
199872b236d6SAaron Skomra 		return -ENOMEM;
199972b236d6SAaron Skomra 
200083e6b40eSBenjamin Tissoires 	wacom->remote = remote;
200183e6b40eSBenjamin Tissoires 
200283e6b40eSBenjamin Tissoires 	spin_lock_init(&remote->remote_lock);
200383e6b40eSBenjamin Tissoires 
200483e6b40eSBenjamin Tissoires 	error = kfifo_alloc(&remote->remote_fifo,
200583e6b40eSBenjamin Tissoires 			5 * sizeof(struct wacom_remote_data),
200683e6b40eSBenjamin Tissoires 			GFP_KERNEL);
200783e6b40eSBenjamin Tissoires 	if (error) {
200883e6b40eSBenjamin Tissoires 		hid_err(wacom->hdev, "failed allocating remote_fifo\n");
200983e6b40eSBenjamin Tissoires 		return -ENOMEM;
201083e6b40eSBenjamin Tissoires 	}
201183e6b40eSBenjamin Tissoires 
2012e7749f6eSBenjamin Tissoires 	remote->remotes[0].group = remote0_serial_group;
2013e7749f6eSBenjamin Tissoires 	remote->remotes[1].group = remote1_serial_group;
2014e7749f6eSBenjamin Tissoires 	remote->remotes[2].group = remote2_serial_group;
2015e7749f6eSBenjamin Tissoires 	remote->remotes[3].group = remote3_serial_group;
2016e7749f6eSBenjamin Tissoires 	remote->remotes[4].group = remote4_serial_group;
201783e6b40eSBenjamin Tissoires 
201883e6b40eSBenjamin Tissoires 	remote->remote_dir = kobject_create_and_add("wacom_remote",
201983e6b40eSBenjamin Tissoires 						    &wacom->hdev->dev.kobj);
202083e6b40eSBenjamin Tissoires 	if (!remote->remote_dir)
202183e6b40eSBenjamin Tissoires 		return -ENOMEM;
202283e6b40eSBenjamin Tissoires 
202383e6b40eSBenjamin Tissoires 	error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
202472b236d6SAaron Skomra 
202572b236d6SAaron Skomra 	if (error) {
202672b236d6SAaron Skomra 		hid_err(wacom->hdev,
202772b236d6SAaron Skomra 			"cannot create sysfs group err: %d\n", error);
202872b236d6SAaron Skomra 		return error;
202972b236d6SAaron Skomra 	}
203072b236d6SAaron Skomra 
203172b236d6SAaron Skomra 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2032a50aac71SBenjamin Tissoires 		wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2033e7749f6eSBenjamin Tissoires 		remote->remotes[i].serial = 0;
203472b236d6SAaron Skomra 	}
203572b236d6SAaron Skomra 
203683e6b40eSBenjamin Tissoires 	error = devm_add_action_or_reset(&wacom->hdev->dev,
203783e6b40eSBenjamin Tissoires 					 wacom_remotes_destroy, wacom);
203883e6b40eSBenjamin Tissoires 	if (error)
203983e6b40eSBenjamin Tissoires 		return error;
204083e6b40eSBenjamin Tissoires 
204172b236d6SAaron Skomra 	return 0;
204272b236d6SAaron Skomra }
204372b236d6SAaron Skomra 
2044471d1714SBenjamin Tissoires static struct input_dev *wacom_allocate_input(struct wacom *wacom)
2045471d1714SBenjamin Tissoires {
2046471d1714SBenjamin Tissoires 	struct input_dev *input_dev;
2047471d1714SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
2048471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2049471d1714SBenjamin Tissoires 
20503dad188eSBenjamin Tissoires 	input_dev = devm_input_allocate_device(&hdev->dev);
2051471d1714SBenjamin Tissoires 	if (!input_dev)
2052471d1714SBenjamin Tissoires 		return NULL;
2053471d1714SBenjamin Tissoires 
20542bdd163cSJason Gerecke 	input_dev->name = wacom_wac->features.name;
2055471d1714SBenjamin Tissoires 	input_dev->phys = hdev->phys;
2056471d1714SBenjamin Tissoires 	input_dev->dev.parent = &hdev->dev;
2057471d1714SBenjamin Tissoires 	input_dev->open = wacom_open;
2058471d1714SBenjamin Tissoires 	input_dev->close = wacom_close;
2059471d1714SBenjamin Tissoires 	input_dev->uniq = hdev->uniq;
2060471d1714SBenjamin Tissoires 	input_dev->id.bustype = hdev->bus;
2061471d1714SBenjamin Tissoires 	input_dev->id.vendor  = hdev->vendor;
206212969e3bSBenjamin Tissoires 	input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
2063471d1714SBenjamin Tissoires 	input_dev->id.version = hdev->version;
2064471d1714SBenjamin Tissoires 	input_set_drvdata(input_dev, wacom);
2065471d1714SBenjamin Tissoires 
2066471d1714SBenjamin Tissoires 	return input_dev;
2067471d1714SBenjamin Tissoires }
2068471d1714SBenjamin Tissoires 
2069d9f2d203SJason Gerecke static int wacom_allocate_inputs(struct wacom *wacom)
2070d9f2d203SJason Gerecke {
2071d9f2d203SJason Gerecke 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2072d9f2d203SJason Gerecke 
2073d9f2d203SJason Gerecke 	wacom_wac->pen_input = wacom_allocate_input(wacom);
2074d9f2d203SJason Gerecke 	wacom_wac->touch_input = wacom_allocate_input(wacom);
2075d9f2d203SJason Gerecke 	wacom_wac->pad_input = wacom_allocate_input(wacom);
20763dad188eSBenjamin Tissoires 	if (!wacom_wac->pen_input ||
20773dad188eSBenjamin Tissoires 	    !wacom_wac->touch_input ||
20783dad188eSBenjamin Tissoires 	    !wacom_wac->pad_input)
2079d9f2d203SJason Gerecke 		return -ENOMEM;
2080d9f2d203SJason Gerecke 
20812bdd163cSJason Gerecke 	wacom_wac->pen_input->name = wacom_wac->pen_name;
2082d9f2d203SJason Gerecke 	wacom_wac->touch_input->name = wacom_wac->touch_name;
2083d9f2d203SJason Gerecke 	wacom_wac->pad_input->name = wacom_wac->pad_name;
2084d9f2d203SJason Gerecke 
2085d9f2d203SJason Gerecke 	return 0;
2086d9f2d203SJason Gerecke }
2087d9f2d203SJason Gerecke 
2088471d1714SBenjamin Tissoires static int wacom_register_inputs(struct wacom *wacom)
2089471d1714SBenjamin Tissoires {
20902a6cdbddSJason Gerecke 	struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2091471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
20922636a3f2SJason Gerecke 	int error = 0;
2093471d1714SBenjamin Tissoires 
20942a6cdbddSJason Gerecke 	pen_input_dev = wacom_wac->pen_input;
20952a6cdbddSJason Gerecke 	touch_input_dev = wacom_wac->touch_input;
20962546dacdSBenjamin Tissoires 	pad_input_dev = wacom_wac->pad_input;
2097471d1714SBenjamin Tissoires 
20982a6cdbddSJason Gerecke 	if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
20992546dacdSBenjamin Tissoires 		return -EINVAL;
2100471d1714SBenjamin Tissoires 
21012a6cdbddSJason Gerecke 	error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
21022a6cdbddSJason Gerecke 	if (error) {
21032a6cdbddSJason Gerecke 		/* no pen in use on this interface */
21042a6cdbddSJason Gerecke 		input_free_device(pen_input_dev);
21052a6cdbddSJason Gerecke 		wacom_wac->pen_input = NULL;
21062a6cdbddSJason Gerecke 		pen_input_dev = NULL;
21072a6cdbddSJason Gerecke 	} else {
21082a6cdbddSJason Gerecke 		error = input_register_device(pen_input_dev);
2109471d1714SBenjamin Tissoires 		if (error)
21103dad188eSBenjamin Tissoires 			goto fail;
21112a6cdbddSJason Gerecke 	}
21122a6cdbddSJason Gerecke 
21132a6cdbddSJason Gerecke 	error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
21142a6cdbddSJason Gerecke 	if (error) {
21152a6cdbddSJason Gerecke 		/* no touch in use on this interface */
21162a6cdbddSJason Gerecke 		input_free_device(touch_input_dev);
21172a6cdbddSJason Gerecke 		wacom_wac->touch_input = NULL;
21182a6cdbddSJason Gerecke 		touch_input_dev = NULL;
21192a6cdbddSJason Gerecke 	} else {
21202a6cdbddSJason Gerecke 		error = input_register_device(touch_input_dev);
21212a6cdbddSJason Gerecke 		if (error)
21223dad188eSBenjamin Tissoires 			goto fail;
212330ebc1aeSPing Cheng 	}
2124471d1714SBenjamin Tissoires 
2125471d1714SBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2126471d1714SBenjamin Tissoires 	if (error) {
2127471d1714SBenjamin Tissoires 		/* no pad in use on this interface */
2128471d1714SBenjamin Tissoires 		input_free_device(pad_input_dev);
2129471d1714SBenjamin Tissoires 		wacom_wac->pad_input = NULL;
2130471d1714SBenjamin Tissoires 		pad_input_dev = NULL;
2131471d1714SBenjamin Tissoires 	} else {
2132471d1714SBenjamin Tissoires 		error = input_register_device(pad_input_dev);
2133471d1714SBenjamin Tissoires 		if (error)
21343dad188eSBenjamin Tissoires 			goto fail;
2135471d1714SBenjamin Tissoires 	}
2136471d1714SBenjamin Tissoires 
2137471d1714SBenjamin Tissoires 	return 0;
2138471d1714SBenjamin Tissoires 
21393dad188eSBenjamin Tissoires fail:
21403dad188eSBenjamin Tissoires 	wacom_wac->pad_input = NULL;
21412a6cdbddSJason Gerecke 	wacom_wac->touch_input = NULL;
21422a6cdbddSJason Gerecke 	wacom_wac->pen_input = NULL;
2143471d1714SBenjamin Tissoires 	return error;
2144471d1714SBenjamin Tissoires }
2145471d1714SBenjamin Tissoires 
21460be01712SJason Gerecke /*
21470be01712SJason Gerecke  * Not all devices report physical dimensions from HID.
21480be01712SJason Gerecke  * Compute the default from hardcoded logical dimension
21490be01712SJason Gerecke  * and resolution before driver overwrites them.
21500be01712SJason Gerecke  */
21510be01712SJason Gerecke static void wacom_set_default_phy(struct wacom_features *features)
21520be01712SJason Gerecke {
21530be01712SJason Gerecke 	if (features->x_resolution) {
21540be01712SJason Gerecke 		features->x_phy = (features->x_max * 100) /
21550be01712SJason Gerecke 					features->x_resolution;
21560be01712SJason Gerecke 		features->y_phy = (features->y_max * 100) /
21570be01712SJason Gerecke 					features->y_resolution;
21580be01712SJason Gerecke 	}
21590be01712SJason Gerecke }
21600be01712SJason Gerecke 
21610be01712SJason Gerecke static void wacom_calculate_res(struct wacom_features *features)
21620be01712SJason Gerecke {
21630be01712SJason Gerecke 	/* set unit to "100th of a mm" for devices not reported by HID */
21640be01712SJason Gerecke 	if (!features->unit) {
21650be01712SJason Gerecke 		features->unit = 0x11;
21660be01712SJason Gerecke 		features->unitExpo = -3;
21670be01712SJason Gerecke 	}
21680be01712SJason Gerecke 
21690be01712SJason Gerecke 	features->x_resolution = wacom_calc_hid_res(features->x_max,
21700be01712SJason Gerecke 						    features->x_phy,
21710be01712SJason Gerecke 						    features->unit,
21720be01712SJason Gerecke 						    features->unitExpo);
21730be01712SJason Gerecke 	features->y_resolution = wacom_calc_hid_res(features->y_max,
21740be01712SJason Gerecke 						    features->y_phy,
21750be01712SJason Gerecke 						    features->unit,
21760be01712SJason Gerecke 						    features->unitExpo);
21770be01712SJason Gerecke }
21780be01712SJason Gerecke 
2179fce9957dSJason Gerecke void wacom_battery_work(struct work_struct *work)
2180fce9957dSJason Gerecke {
2181d17d1f17SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, battery_work);
2182fce9957dSJason Gerecke 
2183fce9957dSJason Gerecke 	if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
218459d69bc8SBenjamin Tissoires 	     !wacom->battery.battery) {
2185fce9957dSJason Gerecke 		wacom_initialize_battery(wacom);
2186fce9957dSJason Gerecke 	}
2187fce9957dSJason Gerecke 	else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
218859d69bc8SBenjamin Tissoires 		 wacom->battery.battery) {
2189fce9957dSJason Gerecke 		wacom_destroy_battery(wacom);
2190fce9957dSJason Gerecke 	}
2191fce9957dSJason Gerecke }
2192fce9957dSJason Gerecke 
2193471d1714SBenjamin Tissoires static size_t wacom_compute_pktlen(struct hid_device *hdev)
2194471d1714SBenjamin Tissoires {
2195471d1714SBenjamin Tissoires 	struct hid_report_enum *report_enum;
2196471d1714SBenjamin Tissoires 	struct hid_report *report;
2197471d1714SBenjamin Tissoires 	size_t size = 0;
2198471d1714SBenjamin Tissoires 
2199471d1714SBenjamin Tissoires 	report_enum = hdev->report_enum + HID_INPUT_REPORT;
2200471d1714SBenjamin Tissoires 
2201471d1714SBenjamin Tissoires 	list_for_each_entry(report, &report_enum->report_list, list) {
2202dabb05c6SMathieu Magnaudet 		size_t report_size = hid_report_len(report);
2203471d1714SBenjamin Tissoires 		if (report_size > size)
2204471d1714SBenjamin Tissoires 			size = report_size;
2205471d1714SBenjamin Tissoires 	}
2206471d1714SBenjamin Tissoires 
2207471d1714SBenjamin Tissoires 	return size;
2208471d1714SBenjamin Tissoires }
2209471d1714SBenjamin Tissoires 
2210fd5f92b6SBenjamin Tissoires static void wacom_update_name(struct wacom *wacom, const char *suffix)
2211c24eab4eSPing Cheng {
2212c24eab4eSPing Cheng 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2213c24eab4eSPing Cheng 	struct wacom_features *features = &wacom_wac->features;
2214073b50bcSJason Gerecke 	char name[WACOM_NAME_MAX - 20]; /* Leave some room for suffixes */
2215c24eab4eSPing Cheng 
2216c24eab4eSPing Cheng 	/* Generic devices name unspecified */
2217c24eab4eSPing Cheng 	if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
221809dc28acSJason Gerecke 		char *product_name = wacom->hdev->name;
221909dc28acSJason Gerecke 
222009dc28acSJason Gerecke 		if (hid_is_using_ll_driver(wacom->hdev, &usb_hid_driver)) {
222109dc28acSJason Gerecke 			struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
222209dc28acSJason Gerecke 			struct usb_device *dev = interface_to_usbdev(intf);
222309dc28acSJason Gerecke 			product_name = dev->product;
222409dc28acSJason Gerecke 		}
222509dc28acSJason Gerecke 
222609dc28acSJason Gerecke 		if (wacom->hdev->bus == BUS_I2C) {
222709dc28acSJason Gerecke 			snprintf(name, sizeof(name), "%s %X",
222809dc28acSJason Gerecke 				 features->name, wacom->hdev->product);
222909dc28acSJason Gerecke 		} else if (strstr(product_name, "Wacom") ||
223009dc28acSJason Gerecke 			   strstr(product_name, "wacom") ||
223109dc28acSJason Gerecke 			   strstr(product_name, "WACOM")) {
223209dc28acSJason Gerecke 			strlcpy(name, product_name, sizeof(name));
223309dc28acSJason Gerecke 		} else {
223409dc28acSJason Gerecke 			snprintf(name, sizeof(name), "Wacom %s", product_name);
223509dc28acSJason Gerecke 		}
2236c24eab4eSPing Cheng 
2237c24eab4eSPing Cheng 		/* strip out excess whitespaces */
2238c24eab4eSPing Cheng 		while (1) {
223944b5250bSJason Gerecke 			char *gap = strstr(name, "  ");
2240c24eab4eSPing Cheng 			if (gap == NULL)
2241c24eab4eSPing Cheng 				break;
2242c24eab4eSPing Cheng 			/* shift everything including the terminator */
2243c24eab4eSPing Cheng 			memmove(gap, gap+1, strlen(gap));
2244c24eab4eSPing Cheng 		}
2245f2209d4aSJason Gerecke 
2246c24eab4eSPing Cheng 		/* get rid of trailing whitespace */
224744b5250bSJason Gerecke 		if (name[strlen(name)-1] == ' ')
224844b5250bSJason Gerecke 			name[strlen(name)-1] = '\0';
2249c24eab4eSPing Cheng 	} else {
225044b5250bSJason Gerecke 		strlcpy(name, features->name, sizeof(name));
2251c24eab4eSPing Cheng 	}
2252c24eab4eSPing Cheng 
22539956953eSBenjamin Tissoires 	snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
22549956953eSBenjamin Tissoires 		 name, suffix);
22559956953eSBenjamin Tissoires 
2256c24eab4eSPing Cheng 	/* Append the device type to the name */
22572a6cdbddSJason Gerecke 	snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
2258fd5f92b6SBenjamin Tissoires 		"%s%s Pen", name, suffix);
22592a6cdbddSJason Gerecke 	snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
2260fd5f92b6SBenjamin Tissoires 		"%s%s Finger", name, suffix);
2261c24eab4eSPing Cheng 	snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
2262fd5f92b6SBenjamin Tissoires 		"%s%s Pad", name, suffix);
22638d80f790SJason Gerecke }
2264c24eab4eSPing Cheng 
226584dfbd7fSBenjamin Tissoires static void wacom_release_resources(struct wacom *wacom)
226684dfbd7fSBenjamin Tissoires {
226784dfbd7fSBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
226884dfbd7fSBenjamin Tissoires 
226984dfbd7fSBenjamin Tissoires 	if (!wacom->resources)
227084dfbd7fSBenjamin Tissoires 		return;
227184dfbd7fSBenjamin Tissoires 
227284dfbd7fSBenjamin Tissoires 	devres_release_group(&hdev->dev, wacom);
227384dfbd7fSBenjamin Tissoires 
227484dfbd7fSBenjamin Tissoires 	wacom->resources = false;
227584dfbd7fSBenjamin Tissoires 
227684dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.pen_input = NULL;
227784dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.touch_input = NULL;
227884dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.pad_input = NULL;
227984dfbd7fSBenjamin Tissoires }
228084dfbd7fSBenjamin Tissoires 
2281d2ec58aeSAaron Armstrong Skomra static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2282d2ec58aeSAaron Armstrong Skomra {
2283d2ec58aeSAaron Armstrong Skomra 	if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2284d2ec58aeSAaron Armstrong Skomra 		wacom_wac->shared->type = wacom_wac->features.type;
2285d2ec58aeSAaron Armstrong Skomra 		wacom_wac->shared->touch_input = wacom_wac->touch_input;
2286d2ec58aeSAaron Armstrong Skomra 	}
2287d2ec58aeSAaron Armstrong Skomra 
2288d793ff81SPing Cheng 	if (wacom_wac->has_mute_touch_switch) {
2289d2ec58aeSAaron Armstrong Skomra 		wacom_wac->shared->has_mute_touch_switch = true;
2290d793ff81SPing Cheng 		wacom_wac->shared->is_touch_on = true;
2291d793ff81SPing Cheng 	}
2292d2ec58aeSAaron Armstrong Skomra 
2293d2ec58aeSAaron Armstrong Skomra 	if (wacom_wac->shared->has_mute_touch_switch &&
2294d2ec58aeSAaron Armstrong Skomra 	    wacom_wac->shared->touch_input) {
2295d2ec58aeSAaron Armstrong Skomra 		set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2296d2ec58aeSAaron Armstrong Skomra 		input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2297d2ec58aeSAaron Armstrong Skomra 				     SW_MUTE_DEVICE);
2298d2ec58aeSAaron Armstrong Skomra 	}
2299d2ec58aeSAaron Armstrong Skomra }
2300d2ec58aeSAaron Armstrong Skomra 
2301fd5f92b6SBenjamin Tissoires static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
2302471d1714SBenjamin Tissoires {
2303c58ac3a8SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2304c58ac3a8SBenjamin Tissoires 	struct wacom_features *features = &wacom_wac->features;
2305c58ac3a8SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
2306471d1714SBenjamin Tissoires 	int error;
23077704ac93SBenjamin Tissoires 	unsigned int connect_mask = HID_CONNECT_HIDRAW;
2308471d1714SBenjamin Tissoires 
2309471d1714SBenjamin Tissoires 	features->pktlen = wacom_compute_pktlen(hdev);
2310c58ac3a8SBenjamin Tissoires 	if (features->pktlen > WACOM_PKGLEN_MAX)
2311c58ac3a8SBenjamin Tissoires 		return -EINVAL;
2312471d1714SBenjamin Tissoires 
231384dfbd7fSBenjamin Tissoires 	if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
231484dfbd7fSBenjamin Tissoires 		return -ENOMEM;
231584dfbd7fSBenjamin Tissoires 
231684dfbd7fSBenjamin Tissoires 	wacom->resources = true;
231784dfbd7fSBenjamin Tissoires 
2318494078b0SBenjamin Tissoires 	error = wacom_allocate_inputs(wacom);
2319494078b0SBenjamin Tissoires 	if (error)
23203888b0d5SBenjamin Tissoires 		goto fail;
2321494078b0SBenjamin Tissoires 
23228c97a765SBenjamin Tissoires 	/*
23238c97a765SBenjamin Tissoires 	 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
23248c97a765SBenjamin Tissoires 	 * into debug mode for the touch part.
23258c97a765SBenjamin Tissoires 	 * We ignore the other interfaces.
23268c97a765SBenjamin Tissoires 	 */
23278c97a765SBenjamin Tissoires 	if (features->type == BAMBOO_PAD) {
23288c97a765SBenjamin Tissoires 		if (features->pktlen == WACOM_PKGLEN_PENABLED) {
23298c97a765SBenjamin Tissoires 			features->type = HID_GENERIC;
23308c97a765SBenjamin Tissoires 		} else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
23318c97a765SBenjamin Tissoires 			   (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
23328c97a765SBenjamin Tissoires 			error = -ENODEV;
23333888b0d5SBenjamin Tissoires 			goto fail;
23348c97a765SBenjamin Tissoires 		}
23358c97a765SBenjamin Tissoires 	}
23368c97a765SBenjamin Tissoires 
2337471d1714SBenjamin Tissoires 	/* set the default size in case we do not get them from hid */
2338471d1714SBenjamin Tissoires 	wacom_set_default_phy(features);
2339471d1714SBenjamin Tissoires 
2340471d1714SBenjamin Tissoires 	/* Retrieve the physical and logical size for touch devices */
2341471d1714SBenjamin Tissoires 	wacom_retrieve_hid_descriptor(hdev, features);
234242f4f272SPing Cheng 	wacom_setup_device_quirks(wacom);
2343042628abSJason Gerecke 
2344aa86b18cSJason Gerecke 	if (features->device_type == WACOM_DEVICETYPE_NONE &&
2345aa86b18cSJason Gerecke 	    features->type != WIRELESS) {
23468e116d31SJason Gerecke 		error = features->type == HID_GENERIC ? -ENODEV : 0;
23478e116d31SJason Gerecke 
2348042628abSJason Gerecke 		dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
23498e116d31SJason Gerecke 			 hdev->name,
23508e116d31SJason Gerecke 			 error ? "Ignoring" : "Assuming pen");
23518e116d31SJason Gerecke 
23528e116d31SJason Gerecke 		if (error)
23533888b0d5SBenjamin Tissoires 			goto fail;
2354042628abSJason Gerecke 
2355aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_PEN;
2356042628abSJason Gerecke 	}
2357042628abSJason Gerecke 
2358471d1714SBenjamin Tissoires 	wacom_calculate_res(features);
2359471d1714SBenjamin Tissoires 
2360fd5f92b6SBenjamin Tissoires 	wacom_update_name(wacom, wireless ? " (WL)" : "");
2361471d1714SBenjamin Tissoires 
23628b407359SAaron Armstrong Skomra 	/* pen only Bamboo neither support touch nor pad */
23638b407359SAaron Armstrong Skomra 	if ((features->type == BAMBOO_PEN) &&
23648b407359SAaron Armstrong Skomra 	    ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
23658b407359SAaron Armstrong Skomra 	    (features->device_type & WACOM_DEVICETYPE_PAD))) {
23668b407359SAaron Armstrong Skomra 		error = -ENODEV;
23678b407359SAaron Armstrong Skomra 		goto fail;
23688b407359SAaron Armstrong Skomra 	}
23698b407359SAaron Armstrong Skomra 
2370a9ce7856SJason Gerecke 	error = wacom_add_shared_data(hdev);
2371a9ce7856SJason Gerecke 	if (error)
2372a9ce7856SJason Gerecke 		goto fail;
2373471d1714SBenjamin Tissoires 
2374ccad85ccSJason Gerecke 	if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2375f81a1295SBenjamin Tissoires 	     (features->quirks & WACOM_QUIRK_BATTERY)) {
2376f81a1295SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
2377f81a1295SBenjamin Tissoires 		if (error)
23783888b0d5SBenjamin Tissoires 			goto fail;
2379f81a1295SBenjamin Tissoires 	}
2380f81a1295SBenjamin Tissoires 
2381494078b0SBenjamin Tissoires 	error = wacom_register_inputs(wacom);
2382471d1714SBenjamin Tissoires 	if (error)
23833888b0d5SBenjamin Tissoires 		goto fail;
2384f81a1295SBenjamin Tissoires 
2385b62f6465SBenjamin Tissoires 	if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
238685d2c77bSBenjamin Tissoires 		error = wacom_initialize_leds(wacom);
238785d2c77bSBenjamin Tissoires 		if (error)
23883888b0d5SBenjamin Tissoires 			goto fail;
238985d2c77bSBenjamin Tissoires 
239083e6b40eSBenjamin Tissoires 		error = wacom_initialize_remotes(wacom);
2391b62f6465SBenjamin Tissoires 		if (error)
23923888b0d5SBenjamin Tissoires 			goto fail;
2393b62f6465SBenjamin Tissoires 	}
2394b62f6465SBenjamin Tissoires 
23957704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
23967704ac93SBenjamin Tissoires 		connect_mask |= HID_CONNECT_DRIVER;
23977704ac93SBenjamin Tissoires 
2398471d1714SBenjamin Tissoires 	/* Regular HID work starts now */
23997704ac93SBenjamin Tissoires 	error = hid_hw_start(hdev, connect_mask);
2400471d1714SBenjamin Tissoires 	if (error) {
2401471d1714SBenjamin Tissoires 		hid_err(hdev, "hw start failed\n");
24023888b0d5SBenjamin Tissoires 		goto fail;
2403471d1714SBenjamin Tissoires 	}
2404471d1714SBenjamin Tissoires 
2405fd5f92b6SBenjamin Tissoires 	if (!wireless) {
240686e88f0eSJason Gerecke 		/* Note that if query fails it is not a hard failure */
2407a544c619SBenjamin Tissoires 		wacom_query_tablet_data(wacom);
2408fd5f92b6SBenjamin Tissoires 	}
240986e88f0eSJason Gerecke 
241086e88f0eSJason Gerecke 	/* touch only Bamboo doesn't support pen */
241186e88f0eSJason Gerecke 	if ((features->type == BAMBOO_TOUCH) &&
241286e88f0eSJason Gerecke 	    (features->device_type & WACOM_DEVICETYPE_PEN)) {
24134d20c332SAaron Armstrong Skomra 		cancel_delayed_work_sync(&wacom->init_work);
24144d20c332SAaron Armstrong Skomra 		_wacom_query_tablet_data(wacom);
241586e88f0eSJason Gerecke 		error = -ENODEV;
2416b62f6465SBenjamin Tissoires 		goto fail_quirks;
241786e88f0eSJason Gerecke 	}
241886e88f0eSJason Gerecke 
2419ccad85ccSJason Gerecke 	if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2420471d1714SBenjamin Tissoires 		error = hid_hw_open(hdev);
2421471d1714SBenjamin Tissoires 
2422d2ec58aeSAaron Armstrong Skomra 	wacom_set_shared_values(wacom_wac);
242384dfbd7fSBenjamin Tissoires 	devres_close_group(&hdev->dev, wacom);
242484dfbd7fSBenjamin Tissoires 
2425471d1714SBenjamin Tissoires 	return 0;
2426471d1714SBenjamin Tissoires 
2427b62f6465SBenjamin Tissoires fail_quirks:
2428c58ac3a8SBenjamin Tissoires 	hid_hw_stop(hdev);
24293888b0d5SBenjamin Tissoires fail:
243084dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom);
2431c58ac3a8SBenjamin Tissoires 	return error;
2432c58ac3a8SBenjamin Tissoires }
2433c58ac3a8SBenjamin Tissoires 
2434a2f091afSBenjamin Tissoires static void wacom_wireless_work(struct work_struct *work)
2435a2f091afSBenjamin Tissoires {
2436d17d1f17SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2437a2f091afSBenjamin Tissoires 	struct usb_device *usbdev = wacom->usbdev;
2438a2f091afSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2439a2f091afSBenjamin Tissoires 	struct hid_device *hdev1, *hdev2;
2440a2f091afSBenjamin Tissoires 	struct wacom *wacom1, *wacom2;
2441a2f091afSBenjamin Tissoires 	struct wacom_wac *wacom_wac1, *wacom_wac2;
2442a2f091afSBenjamin Tissoires 	int error;
2443a2f091afSBenjamin Tissoires 
2444a2f091afSBenjamin Tissoires 	/*
2445a2f091afSBenjamin Tissoires 	 * Regardless if this is a disconnect or a new tablet,
2446a2f091afSBenjamin Tissoires 	 * remove any existing input and battery devices.
2447a2f091afSBenjamin Tissoires 	 */
2448a2f091afSBenjamin Tissoires 
2449a2f091afSBenjamin Tissoires 	wacom_destroy_battery(wacom);
2450a2f091afSBenjamin Tissoires 
2451a2f091afSBenjamin Tissoires 	/* Stylus interface */
2452a2f091afSBenjamin Tissoires 	hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2453a2f091afSBenjamin Tissoires 	wacom1 = hid_get_drvdata(hdev1);
2454a2f091afSBenjamin Tissoires 	wacom_wac1 = &(wacom1->wacom_wac);
245584dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom1);
2456a2f091afSBenjamin Tissoires 
2457a2f091afSBenjamin Tissoires 	/* Touch interface */
2458a2f091afSBenjamin Tissoires 	hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2459a2f091afSBenjamin Tissoires 	wacom2 = hid_get_drvdata(hdev2);
2460a2f091afSBenjamin Tissoires 	wacom_wac2 = &(wacom2->wacom_wac);
246184dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom2);
2462a2f091afSBenjamin Tissoires 
2463a2f091afSBenjamin Tissoires 	if (wacom_wac->pid == 0) {
2464a2f091afSBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet disconnected\n");
2465a2f091afSBenjamin Tissoires 	} else {
2466a2f091afSBenjamin Tissoires 		const struct hid_device_id *id = wacom_ids;
2467a2f091afSBenjamin Tissoires 
2468a2f091afSBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2469a2f091afSBenjamin Tissoires 			 wacom_wac->pid);
2470a2f091afSBenjamin Tissoires 
2471a2f091afSBenjamin Tissoires 		while (id->bus) {
2472a2f091afSBenjamin Tissoires 			if (id->vendor == USB_VENDOR_ID_WACOM &&
2473a2f091afSBenjamin Tissoires 			    id->product == wacom_wac->pid)
2474a2f091afSBenjamin Tissoires 				break;
2475a2f091afSBenjamin Tissoires 			id++;
2476a2f091afSBenjamin Tissoires 		}
2477a2f091afSBenjamin Tissoires 
2478a2f091afSBenjamin Tissoires 		if (!id->bus) {
2479a2f091afSBenjamin Tissoires 			hid_info(wacom->hdev, "ignoring unknown PID.\n");
2480a2f091afSBenjamin Tissoires 			return;
2481a2f091afSBenjamin Tissoires 		}
2482a2f091afSBenjamin Tissoires 
2483a2f091afSBenjamin Tissoires 		/* Stylus interface */
2484a2f091afSBenjamin Tissoires 		wacom_wac1->features =
2485a2f091afSBenjamin Tissoires 			*((struct wacom_features *)id->driver_data);
2486fd5f92b6SBenjamin Tissoires 
2487a2f091afSBenjamin Tissoires 		wacom_wac1->pid = wacom_wac->pid;
2488fd5f92b6SBenjamin Tissoires 		hid_hw_stop(hdev1);
2489fd5f92b6SBenjamin Tissoires 		error = wacom_parse_and_register(wacom1, true);
2490a2f091afSBenjamin Tissoires 		if (error)
2491a2f091afSBenjamin Tissoires 			goto fail;
2492a2f091afSBenjamin Tissoires 
2493a2f091afSBenjamin Tissoires 		/* Touch interface */
2494a2f091afSBenjamin Tissoires 		if (wacom_wac1->features.touch_max ||
2495a2f091afSBenjamin Tissoires 		    (wacom_wac1->features.type >= INTUOSHT &&
2496a2f091afSBenjamin Tissoires 		    wacom_wac1->features.type <= BAMBOO_PT)) {
2497a2f091afSBenjamin Tissoires 			wacom_wac2->features =
2498a2f091afSBenjamin Tissoires 				*((struct wacom_features *)id->driver_data);
2499a2f091afSBenjamin Tissoires 			wacom_wac2->pid = wacom_wac->pid;
2500fd5f92b6SBenjamin Tissoires 			hid_hw_stop(hdev2);
2501fd5f92b6SBenjamin Tissoires 			error = wacom_parse_and_register(wacom2, true);
2502a2f091afSBenjamin Tissoires 			if (error)
2503a2f091afSBenjamin Tissoires 				goto fail;
2504a2f091afSBenjamin Tissoires 		}
2505a2f091afSBenjamin Tissoires 
25069956953eSBenjamin Tissoires 		strlcpy(wacom_wac->name, wacom_wac1->name,
25079956953eSBenjamin Tissoires 			sizeof(wacom_wac->name));
2508a2f091afSBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
2509a2f091afSBenjamin Tissoires 		if (error)
2510a2f091afSBenjamin Tissoires 			goto fail;
2511a2f091afSBenjamin Tissoires 	}
2512a2f091afSBenjamin Tissoires 
2513a2f091afSBenjamin Tissoires 	return;
2514a2f091afSBenjamin Tissoires 
2515a2f091afSBenjamin Tissoires fail:
251684dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom1);
251784dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom2);
2518a2f091afSBenjamin Tissoires 	return;
2519a2f091afSBenjamin Tissoires }
2520a2f091afSBenjamin Tissoires 
252104bfa27bSBenjamin Tissoires static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
252204bfa27bSBenjamin Tissoires {
252304bfa27bSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
2524e7749f6eSBenjamin Tissoires 	u32 serial = remote->remotes[index].serial;
252504bfa27bSBenjamin Tissoires 	int i;
25267c35dc3cSBenjamin Tissoires 	unsigned long flags;
25277c35dc3cSBenjamin Tissoires 
252804bfa27bSBenjamin Tissoires 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2529e7749f6eSBenjamin Tissoires 		if (remote->remotes[i].serial == serial) {
2530791ae273SAaron Armstrong Skomra 
2531791ae273SAaron Armstrong Skomra 			spin_lock_irqsave(&remote->remote_lock, flags);
2532791ae273SAaron Armstrong Skomra 			remote->remotes[i].registered = false;
2533791ae273SAaron Armstrong Skomra 			spin_unlock_irqrestore(&remote->remote_lock, flags);
2534791ae273SAaron Armstrong Skomra 
2535791ae273SAaron Armstrong Skomra 			if (remote->remotes[i].battery.battery)
2536791ae273SAaron Armstrong Skomra 				devres_release_group(&wacom->hdev->dev,
2537791ae273SAaron Armstrong Skomra 						     &remote->remotes[i].battery.bat_desc);
2538791ae273SAaron Armstrong Skomra 
2539791ae273SAaron Armstrong Skomra 			if (remote->remotes[i].group.name)
2540791ae273SAaron Armstrong Skomra 				devres_release_group(&wacom->hdev->dev,
2541791ae273SAaron Armstrong Skomra 						     &remote->remotes[i]);
2542791ae273SAaron Armstrong Skomra 
2543e7749f6eSBenjamin Tissoires 			remote->remotes[i].serial = 0;
2544e7749f6eSBenjamin Tissoires 			remote->remotes[i].group.name = NULL;
25459f1015d4SBenjamin Tissoires 			remote->remotes[i].battery.battery = NULL;
254604bfa27bSBenjamin Tissoires 			wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
254704bfa27bSBenjamin Tissoires 		}
254804bfa27bSBenjamin Tissoires 	}
254904bfa27bSBenjamin Tissoires }
255004bfa27bSBenjamin Tissoires 
255104bfa27bSBenjamin Tissoires static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
255204bfa27bSBenjamin Tissoires 				   unsigned int index)
255304bfa27bSBenjamin Tissoires {
255404bfa27bSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
2555f9036bd4SBenjamin Tissoires 	struct device *dev = &wacom->hdev->dev;
255604bfa27bSBenjamin Tissoires 	int error, k;
255704bfa27bSBenjamin Tissoires 
255804bfa27bSBenjamin Tissoires 	/* A remote can pair more than once with an EKR,
255904bfa27bSBenjamin Tissoires 	 * check to make sure this serial isn't already paired.
256004bfa27bSBenjamin Tissoires 	 */
256104bfa27bSBenjamin Tissoires 	for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2562e7749f6eSBenjamin Tissoires 		if (remote->remotes[k].serial == serial)
256304bfa27bSBenjamin Tissoires 			break;
256404bfa27bSBenjamin Tissoires 	}
256504bfa27bSBenjamin Tissoires 
256604bfa27bSBenjamin Tissoires 	if (k < WACOM_MAX_REMOTES) {
2567e7749f6eSBenjamin Tissoires 		remote->remotes[index].serial = serial;
256804bfa27bSBenjamin Tissoires 		return 0;
256904bfa27bSBenjamin Tissoires 	}
257004bfa27bSBenjamin Tissoires 
2571e7749f6eSBenjamin Tissoires 	if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2572f9036bd4SBenjamin Tissoires 		return -ENOMEM;
2573f9036bd4SBenjamin Tissoires 
257404bfa27bSBenjamin Tissoires 	error = wacom_remote_create_attr_group(wacom, serial, index);
257504bfa27bSBenjamin Tissoires 	if (error)
2576f9036bd4SBenjamin Tissoires 		goto fail;
257704bfa27bSBenjamin Tissoires 
25787c35dc3cSBenjamin Tissoires 	remote->remotes[index].input = wacom_allocate_input(wacom);
25797c35dc3cSBenjamin Tissoires 	if (!remote->remotes[index].input) {
25807c35dc3cSBenjamin Tissoires 		error = -ENOMEM;
25817c35dc3cSBenjamin Tissoires 		goto fail;
25827c35dc3cSBenjamin Tissoires 	}
25837c35dc3cSBenjamin Tissoires 	remote->remotes[index].input->uniq = remote->remotes[index].group.name;
25847c35dc3cSBenjamin Tissoires 	remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
25857c35dc3cSBenjamin Tissoires 
25867c35dc3cSBenjamin Tissoires 	if (!remote->remotes[index].input->name) {
25877c35dc3cSBenjamin Tissoires 		error = -EINVAL;
25887c35dc3cSBenjamin Tissoires 		goto fail;
25897c35dc3cSBenjamin Tissoires 	}
25907c35dc3cSBenjamin Tissoires 
25917c35dc3cSBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
25927c35dc3cSBenjamin Tissoires 						   &wacom->wacom_wac);
25937c35dc3cSBenjamin Tissoires 	if (error)
25947c35dc3cSBenjamin Tissoires 		goto fail;
25957c35dc3cSBenjamin Tissoires 
2596e7749f6eSBenjamin Tissoires 	remote->remotes[index].serial = serial;
259704bfa27bSBenjamin Tissoires 
25987c35dc3cSBenjamin Tissoires 	error = input_register_device(remote->remotes[index].input);
25997c35dc3cSBenjamin Tissoires 	if (error)
26007c35dc3cSBenjamin Tissoires 		goto fail;
26017c35dc3cSBenjamin Tissoires 
260297f5541fSBenjamin Tissoires 	error = wacom_led_groups_alloc_and_register_one(
260397f5541fSBenjamin Tissoires 					&remote->remotes[index].input->dev,
260497f5541fSBenjamin Tissoires 					wacom, index, 3, true);
260597f5541fSBenjamin Tissoires 	if (error)
260697f5541fSBenjamin Tissoires 		goto fail;
260797f5541fSBenjamin Tissoires 
26087c35dc3cSBenjamin Tissoires 	remote->remotes[index].registered = true;
26097c35dc3cSBenjamin Tissoires 
2610e7749f6eSBenjamin Tissoires 	devres_close_group(dev, &remote->remotes[index]);
261104bfa27bSBenjamin Tissoires 	return 0;
2612f9036bd4SBenjamin Tissoires 
2613f9036bd4SBenjamin Tissoires fail:
2614e7749f6eSBenjamin Tissoires 	devres_release_group(dev, &remote->remotes[index]);
2615e7749f6eSBenjamin Tissoires 	remote->remotes[index].serial = 0;
2616f9036bd4SBenjamin Tissoires 	return error;
261704bfa27bSBenjamin Tissoires }
261804bfa27bSBenjamin Tissoires 
26199f1015d4SBenjamin Tissoires static int wacom_remote_attach_battery(struct wacom *wacom, int index)
26209f1015d4SBenjamin Tissoires {
26219f1015d4SBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
26229f1015d4SBenjamin Tissoires 	int error;
26239f1015d4SBenjamin Tissoires 
26249f1015d4SBenjamin Tissoires 	if (!remote->remotes[index].registered)
26259f1015d4SBenjamin Tissoires 		return 0;
26269f1015d4SBenjamin Tissoires 
26279f1015d4SBenjamin Tissoires 	if (remote->remotes[index].battery.battery)
26289f1015d4SBenjamin Tissoires 		return 0;
26299f1015d4SBenjamin Tissoires 
26309f1015d4SBenjamin Tissoires 	if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
26319f1015d4SBenjamin Tissoires 		return 0;
26329f1015d4SBenjamin Tissoires 
26339f1015d4SBenjamin Tissoires 	error = __wacom_initialize_battery(wacom,
26349f1015d4SBenjamin Tissoires 					&wacom->remote->remotes[index].battery);
26359f1015d4SBenjamin Tissoires 	if (error)
26369f1015d4SBenjamin Tissoires 		return error;
26379f1015d4SBenjamin Tissoires 
26389f1015d4SBenjamin Tissoires 	return 0;
26399f1015d4SBenjamin Tissoires }
26409f1015d4SBenjamin Tissoires 
2641e6f2813aSBenjamin Tissoires static void wacom_remote_work(struct work_struct *work)
2642e6f2813aSBenjamin Tissoires {
2643e6f2813aSBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, remote_work);
264483e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
2645e6f2813aSBenjamin Tissoires 	struct wacom_remote_data data;
2646e6f2813aSBenjamin Tissoires 	unsigned long flags;
2647e6f2813aSBenjamin Tissoires 	unsigned int count;
2648e6f2813aSBenjamin Tissoires 	u32 serial;
264904bfa27bSBenjamin Tissoires 	int i;
2650e6f2813aSBenjamin Tissoires 
265183e6b40eSBenjamin Tissoires 	spin_lock_irqsave(&remote->remote_lock, flags);
2652e6f2813aSBenjamin Tissoires 
265383e6b40eSBenjamin Tissoires 	count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2654e6f2813aSBenjamin Tissoires 
2655e6f2813aSBenjamin Tissoires 	if (count != sizeof(data)) {
2656e6f2813aSBenjamin Tissoires 		hid_err(wacom->hdev,
2657e6f2813aSBenjamin Tissoires 			"workitem triggered without status available\n");
265883e6b40eSBenjamin Tissoires 		spin_unlock_irqrestore(&remote->remote_lock, flags);
2659e6f2813aSBenjamin Tissoires 		return;
2660e6f2813aSBenjamin Tissoires 	}
2661e6f2813aSBenjamin Tissoires 
266283e6b40eSBenjamin Tissoires 	if (!kfifo_is_empty(&remote->remote_fifo))
2663e6f2813aSBenjamin Tissoires 		wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2664e6f2813aSBenjamin Tissoires 
266583e6b40eSBenjamin Tissoires 	spin_unlock_irqrestore(&remote->remote_lock, flags);
2666e6f2813aSBenjamin Tissoires 
2667e6f2813aSBenjamin Tissoires 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2668e6f2813aSBenjamin Tissoires 		serial = data.remote[i].serial;
2669e6f2813aSBenjamin Tissoires 		if (data.remote[i].connected) {
2670e6f2813aSBenjamin Tissoires 
26719f1015d4SBenjamin Tissoires 			if (remote->remotes[i].serial == serial) {
26729f1015d4SBenjamin Tissoires 				wacom_remote_attach_battery(wacom, i);
2673e6f2813aSBenjamin Tissoires 				continue;
26749f1015d4SBenjamin Tissoires 			}
2675e6f2813aSBenjamin Tissoires 
2676e7749f6eSBenjamin Tissoires 			if (remote->remotes[i].serial)
267704bfa27bSBenjamin Tissoires 				wacom_remote_destroy_one(wacom, i);
2678e6f2813aSBenjamin Tissoires 
267904bfa27bSBenjamin Tissoires 			wacom_remote_create_one(wacom, serial, i);
2680e6f2813aSBenjamin Tissoires 
2681e7749f6eSBenjamin Tissoires 		} else if (remote->remotes[i].serial) {
268204bfa27bSBenjamin Tissoires 			wacom_remote_destroy_one(wacom, i);
2683e6f2813aSBenjamin Tissoires 		}
2684e6f2813aSBenjamin Tissoires 	}
2685e6f2813aSBenjamin Tissoires }
2686e6f2813aSBenjamin Tissoires 
26874082da80SBenjamin Tissoires static void wacom_mode_change_work(struct work_struct *work)
26884082da80SBenjamin Tissoires {
26894082da80SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
26904082da80SBenjamin Tissoires 	struct wacom_shared *shared = wacom->wacom_wac.shared;
26914082da80SBenjamin Tissoires 	struct wacom *wacom1 = NULL;
26924082da80SBenjamin Tissoires 	struct wacom *wacom2 = NULL;
26934082da80SBenjamin Tissoires 	bool is_direct = wacom->wacom_wac.is_direct_mode;
26944082da80SBenjamin Tissoires 	int error = 0;
26954082da80SBenjamin Tissoires 
26964082da80SBenjamin Tissoires 	if (shared->pen) {
26974082da80SBenjamin Tissoires 		wacom1 = hid_get_drvdata(shared->pen);
26984082da80SBenjamin Tissoires 		wacom_release_resources(wacom1);
26994082da80SBenjamin Tissoires 		hid_hw_stop(wacom1->hdev);
27004082da80SBenjamin Tissoires 		wacom1->wacom_wac.has_mode_change = true;
27014082da80SBenjamin Tissoires 		wacom1->wacom_wac.is_direct_mode = is_direct;
27024082da80SBenjamin Tissoires 	}
27034082da80SBenjamin Tissoires 
27044082da80SBenjamin Tissoires 	if (shared->touch) {
27054082da80SBenjamin Tissoires 		wacom2 = hid_get_drvdata(shared->touch);
27064082da80SBenjamin Tissoires 		wacom_release_resources(wacom2);
27074082da80SBenjamin Tissoires 		hid_hw_stop(wacom2->hdev);
27084082da80SBenjamin Tissoires 		wacom2->wacom_wac.has_mode_change = true;
27094082da80SBenjamin Tissoires 		wacom2->wacom_wac.is_direct_mode = is_direct;
27104082da80SBenjamin Tissoires 	}
27114082da80SBenjamin Tissoires 
27124082da80SBenjamin Tissoires 	if (wacom1) {
27134082da80SBenjamin Tissoires 		error = wacom_parse_and_register(wacom1, false);
27144082da80SBenjamin Tissoires 		if (error)
27154082da80SBenjamin Tissoires 			return;
27164082da80SBenjamin Tissoires 	}
27174082da80SBenjamin Tissoires 
27184082da80SBenjamin Tissoires 	if (wacom2) {
27194082da80SBenjamin Tissoires 		error = wacom_parse_and_register(wacom2, false);
27204082da80SBenjamin Tissoires 		if (error)
27214082da80SBenjamin Tissoires 			return;
27224082da80SBenjamin Tissoires 	}
27234082da80SBenjamin Tissoires 
2724c58ac3a8SBenjamin Tissoires 	return;
2725c58ac3a8SBenjamin Tissoires }
2726c58ac3a8SBenjamin Tissoires 
2727c58ac3a8SBenjamin Tissoires static int wacom_probe(struct hid_device *hdev,
2728c58ac3a8SBenjamin Tissoires 		const struct hid_device_id *id)
2729c58ac3a8SBenjamin Tissoires {
2730c58ac3a8SBenjamin Tissoires 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2731c58ac3a8SBenjamin Tissoires 	struct usb_device *dev = interface_to_usbdev(intf);
2732c58ac3a8SBenjamin Tissoires 	struct wacom *wacom;
2733c58ac3a8SBenjamin Tissoires 	struct wacom_wac *wacom_wac;
2734c58ac3a8SBenjamin Tissoires 	struct wacom_features *features;
2735c58ac3a8SBenjamin Tissoires 	int error;
2736c58ac3a8SBenjamin Tissoires 
2737c58ac3a8SBenjamin Tissoires 	if (!id->driver_data)
2738c58ac3a8SBenjamin Tissoires 		return -EINVAL;
2739c58ac3a8SBenjamin Tissoires 
2740c58ac3a8SBenjamin Tissoires 	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2741c58ac3a8SBenjamin Tissoires 
2742c58ac3a8SBenjamin Tissoires 	/* hid-core sets this quirk for the boot interface */
2743c58ac3a8SBenjamin Tissoires 	hdev->quirks &= ~HID_QUIRK_NOGET;
2744c58ac3a8SBenjamin Tissoires 
274519b64330SBenjamin Tissoires 	wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2746c58ac3a8SBenjamin Tissoires 	if (!wacom)
2747c58ac3a8SBenjamin Tissoires 		return -ENOMEM;
2748c58ac3a8SBenjamin Tissoires 
2749c58ac3a8SBenjamin Tissoires 	hid_set_drvdata(hdev, wacom);
2750c58ac3a8SBenjamin Tissoires 	wacom->hdev = hdev;
2751c58ac3a8SBenjamin Tissoires 
2752c58ac3a8SBenjamin Tissoires 	wacom_wac = &wacom->wacom_wac;
2753c58ac3a8SBenjamin Tissoires 	wacom_wac->features = *((struct wacom_features *)id->driver_data);
2754c58ac3a8SBenjamin Tissoires 	features = &wacom_wac->features;
2755c58ac3a8SBenjamin Tissoires 
2756362c571bSBenjamin Tissoires 	if (features->check_for_hid_type && features->hid_type != hdev->type)
2757362c571bSBenjamin Tissoires 		return -ENODEV;
2758c58ac3a8SBenjamin Tissoires 
275937309f47SPing Cheng 	error = wacom_devm_kfifo_alloc(wacom);
276083417206SJason Gerecke 	if (error)
2761362c571bSBenjamin Tissoires 		return error;
276283417206SJason Gerecke 
2763c6fa1aebSJason Gerecke 	wacom_wac->hid_data.inputmode = -1;
2764326ea2a9SJason Gerecke 	wacom_wac->mode_report = -1;
2765c6fa1aebSJason Gerecke 
2766c58ac3a8SBenjamin Tissoires 	wacom->usbdev = dev;
2767c58ac3a8SBenjamin Tissoires 	wacom->intf = intf;
2768c58ac3a8SBenjamin Tissoires 	mutex_init(&wacom->lock);
2769a544c619SBenjamin Tissoires 	INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
2770d17d1f17SBenjamin Tissoires 	INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2771d17d1f17SBenjamin Tissoires 	INIT_WORK(&wacom->battery_work, wacom_battery_work);
2772e6f2813aSBenjamin Tissoires 	INIT_WORK(&wacom->remote_work, wacom_remote_work);
27734082da80SBenjamin Tissoires 	INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
2774c58ac3a8SBenjamin Tissoires 
2775c58ac3a8SBenjamin Tissoires 	/* ask for the report descriptor to be loaded by HID */
2776c58ac3a8SBenjamin Tissoires 	error = hid_parse(hdev);
2777c58ac3a8SBenjamin Tissoires 	if (error) {
2778c58ac3a8SBenjamin Tissoires 		hid_err(hdev, "parse failed\n");
2779362c571bSBenjamin Tissoires 		return error;
2780c58ac3a8SBenjamin Tissoires 	}
2781c58ac3a8SBenjamin Tissoires 
2782fd5f92b6SBenjamin Tissoires 	error = wacom_parse_and_register(wacom, false);
2783c58ac3a8SBenjamin Tissoires 	if (error)
2784362c571bSBenjamin Tissoires 		return error;
2785c58ac3a8SBenjamin Tissoires 
2786c58ac3a8SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
2787c58ac3a8SBenjamin Tissoires 		error = device_create_file(&hdev->dev, &dev_attr_speed);
2788c58ac3a8SBenjamin Tissoires 		if (error)
2789c58ac3a8SBenjamin Tissoires 			hid_warn(hdev,
2790c58ac3a8SBenjamin Tissoires 				 "can't create sysfs speed attribute err: %d\n",
2791c58ac3a8SBenjamin Tissoires 				 error);
2792c58ac3a8SBenjamin Tissoires 	}
2793c58ac3a8SBenjamin Tissoires 
2794c58ac3a8SBenjamin Tissoires 	return 0;
2795471d1714SBenjamin Tissoires }
2796471d1714SBenjamin Tissoires 
2797471d1714SBenjamin Tissoires static void wacom_remove(struct hid_device *hdev)
2798471d1714SBenjamin Tissoires {
2799471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
2800f6205161SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2801f6205161SBenjamin Tissoires 	struct wacom_features *features = &wacom_wac->features;
2802f6205161SBenjamin Tissoires 
2803f6205161SBenjamin Tissoires 	if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2804f6205161SBenjamin Tissoires 		hid_hw_close(hdev);
2805471d1714SBenjamin Tissoires 
2806471d1714SBenjamin Tissoires 	hid_hw_stop(hdev);
2807471d1714SBenjamin Tissoires 
2808a544c619SBenjamin Tissoires 	cancel_delayed_work_sync(&wacom->init_work);
2809d17d1f17SBenjamin Tissoires 	cancel_work_sync(&wacom->wireless_work);
2810d17d1f17SBenjamin Tissoires 	cancel_work_sync(&wacom->battery_work);
2811e6f2813aSBenjamin Tissoires 	cancel_work_sync(&wacom->remote_work);
28124082da80SBenjamin Tissoires 	cancel_work_sync(&wacom->mode_change_work);
2813f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
2814f81a1295SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
2815471d1714SBenjamin Tissoires 
2816c0265a94SBenjamin Tissoires 	/* make sure we don't trigger the LEDs */
2817c0265a94SBenjamin Tissoires 	wacom_led_groups_release(wacom);
2818b6b1f19bSAaron Armstrong Skomra 
2819b6b1f19bSAaron Armstrong Skomra 	if (wacom->wacom_wac.features.type != REMOTE)
28205b779fc5SBenjamin Tissoires 		wacom_release_resources(wacom);
2821471d1714SBenjamin Tissoires }
2822471d1714SBenjamin Tissoires 
282341a74581SGeert Uytterhoeven #ifdef CONFIG_PM
2824471d1714SBenjamin Tissoires static int wacom_resume(struct hid_device *hdev)
2825471d1714SBenjamin Tissoires {
2826471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
2827471d1714SBenjamin Tissoires 
2828471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
2829471d1714SBenjamin Tissoires 
2830471d1714SBenjamin Tissoires 	/* switch to wacom mode first */
2831a544c619SBenjamin Tissoires 	_wacom_query_tablet_data(wacom);
2832471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
2833471d1714SBenjamin Tissoires 
2834471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
2835471d1714SBenjamin Tissoires 
2836471d1714SBenjamin Tissoires 	return 0;
2837471d1714SBenjamin Tissoires }
2838471d1714SBenjamin Tissoires 
2839471d1714SBenjamin Tissoires static int wacom_reset_resume(struct hid_device *hdev)
2840471d1714SBenjamin Tissoires {
2841471d1714SBenjamin Tissoires 	return wacom_resume(hdev);
2842471d1714SBenjamin Tissoires }
284341a74581SGeert Uytterhoeven #endif /* CONFIG_PM */
2844471d1714SBenjamin Tissoires 
2845471d1714SBenjamin Tissoires static struct hid_driver wacom_driver = {
2846471d1714SBenjamin Tissoires 	.name =		"wacom",
2847471d1714SBenjamin Tissoires 	.id_table =	wacom_ids,
2848471d1714SBenjamin Tissoires 	.probe =	wacom_probe,
2849471d1714SBenjamin Tissoires 	.remove =	wacom_remove,
28507704ac93SBenjamin Tissoires 	.report =	wacom_wac_report,
2851471d1714SBenjamin Tissoires #ifdef CONFIG_PM
2852471d1714SBenjamin Tissoires 	.resume =	wacom_resume,
2853471d1714SBenjamin Tissoires 	.reset_resume =	wacom_reset_resume,
2854471d1714SBenjamin Tissoires #endif
2855471d1714SBenjamin Tissoires 	.raw_event =	wacom_raw_event,
2856471d1714SBenjamin Tissoires };
2857471d1714SBenjamin Tissoires module_hid_driver(wacom_driver);
2858f2e0a7d4SBenjamin Tissoires 
2859f2e0a7d4SBenjamin Tissoires MODULE_VERSION(DRIVER_VERSION);
2860f2e0a7d4SBenjamin Tissoires MODULE_AUTHOR(DRIVER_AUTHOR);
2861f2e0a7d4SBenjamin Tissoires MODULE_DESCRIPTION(DRIVER_DESC);
28627021b600SGrant Grundler MODULE_LICENSE("GPL");
2863