xref: /openbmc/linux/drivers/hid/wacom_sys.c (revision 278002edb19bce2c628fafb0af936e77000f3a5b)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2471d1714SBenjamin Tissoires /*
3471d1714SBenjamin Tissoires  *  USB Wacom tablet support - system specific code
4471d1714SBenjamin Tissoires  */
5471d1714SBenjamin Tissoires 
6471d1714SBenjamin Tissoires #include "wacom_wac.h"
7471d1714SBenjamin Tissoires #include "wacom.h"
8b58ba1baSJason Gerecke #include <linux/input/mt.h>
9471d1714SBenjamin Tissoires 
10471d1714SBenjamin Tissoires #define WAC_MSG_RETRIES		5
11471d1714SBenjamin Tissoires #define WAC_CMD_RETRIES		10
12471d1714SBenjamin Tissoires 
13e0984bc3SPing Cheng #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
14e0984bc3SPing Cheng #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
1572b236d6SAaron Skomra #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
16e0984bc3SPing Cheng 
wacom_get_report(struct hid_device * hdev,u8 type,u8 * buf,size_t size,unsigned int retries)17c64d8834SPing Cheng static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
18c64d8834SPing Cheng 			    size_t size, unsigned int retries)
19471d1714SBenjamin Tissoires {
20471d1714SBenjamin Tissoires 	int retval;
21471d1714SBenjamin Tissoires 
22471d1714SBenjamin Tissoires 	do {
23c64d8834SPing Cheng 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
24471d1714SBenjamin Tissoires 				HID_REQ_GET_REPORT);
25aef3156dSJason Gerecke 	} while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
26aef3156dSJason Gerecke 
27aef3156dSJason Gerecke 	if (retval < 0)
28aef3156dSJason Gerecke 		hid_err(hdev, "wacom_get_report: ran out of retries "
29aef3156dSJason Gerecke 			"(last error = %d)\n", retval);
30471d1714SBenjamin Tissoires 
31471d1714SBenjamin Tissoires 	return retval;
32471d1714SBenjamin Tissoires }
33471d1714SBenjamin Tissoires 
wacom_set_report(struct hid_device * hdev,u8 type,u8 * buf,size_t size,unsigned int retries)34296b7378SPrzemo Firszt static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
35296b7378SPrzemo Firszt 			    size_t size, unsigned int retries)
36471d1714SBenjamin Tissoires {
37471d1714SBenjamin Tissoires 	int retval;
38471d1714SBenjamin Tissoires 
39471d1714SBenjamin Tissoires 	do {
40296b7378SPrzemo Firszt 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
41471d1714SBenjamin Tissoires 				HID_REQ_SET_REPORT);
42aef3156dSJason Gerecke 	} while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
43aef3156dSJason Gerecke 
44aef3156dSJason Gerecke 	if (retval < 0)
45aef3156dSJason Gerecke 		hid_err(hdev, "wacom_set_report: ran out of retries "
46aef3156dSJason Gerecke 			"(last error = %d)\n", retval);
47471d1714SBenjamin Tissoires 
48471d1714SBenjamin Tissoires 	return retval;
49471d1714SBenjamin Tissoires }
50471d1714SBenjamin Tissoires 
wacom_wac_queue_insert(struct hid_device * hdev,struct kfifo_rec_ptr_2 * fifo,u8 * raw_data,int size)5183417206SJason Gerecke static void wacom_wac_queue_insert(struct hid_device *hdev,
5283417206SJason Gerecke 				   struct kfifo_rec_ptr_2 *fifo,
5383417206SJason Gerecke 				   u8 *raw_data, int size)
5483417206SJason Gerecke {
5583417206SJason Gerecke 	bool warned = false;
5683417206SJason Gerecke 
5783417206SJason Gerecke 	while (kfifo_avail(fifo) < size) {
5883417206SJason Gerecke 		if (!warned)
5983417206SJason Gerecke 			hid_warn(hdev, "%s: kfifo has filled, starting to drop events\n", __func__);
6083417206SJason Gerecke 		warned = true;
6183417206SJason Gerecke 
6283417206SJason Gerecke 		kfifo_skip(fifo);
6383417206SJason Gerecke 	}
6483417206SJason Gerecke 
6583417206SJason Gerecke 	kfifo_in(fifo, raw_data, size);
6683417206SJason Gerecke }
6783417206SJason Gerecke 
wacom_wac_queue_flush(struct hid_device * hdev,struct kfifo_rec_ptr_2 * fifo)6883417206SJason Gerecke static void wacom_wac_queue_flush(struct hid_device *hdev,
6983417206SJason Gerecke 				  struct kfifo_rec_ptr_2 *fifo)
7083417206SJason Gerecke {
7183417206SJason Gerecke 	while (!kfifo_is_empty(fifo)) {
7283417206SJason Gerecke 		u8 buf[WACOM_PKGLEN_MAX];
7383417206SJason Gerecke 		int size;
7483417206SJason Gerecke 		int err;
7583417206SJason Gerecke 
7683417206SJason Gerecke 		size = kfifo_out(fifo, buf, sizeof(buf));
7783417206SJason Gerecke 		err = hid_report_raw_event(hdev, HID_INPUT_REPORT, buf, size, false);
7883417206SJason Gerecke 		if (err) {
7983417206SJason Gerecke 			hid_warn(hdev, "%s: unable to flush event due to error %d\n",
8083417206SJason Gerecke 				 __func__, err);
8183417206SJason Gerecke 		}
8283417206SJason Gerecke 	}
8383417206SJason Gerecke }
8483417206SJason Gerecke 
wacom_wac_pen_serial_enforce(struct hid_device * hdev,struct hid_report * report,u8 * raw_data,int report_size)8583417206SJason Gerecke static int wacom_wac_pen_serial_enforce(struct hid_device *hdev,
86073b50bcSJason Gerecke 		struct hid_report *report, u8 *raw_data, int report_size)
8783417206SJason Gerecke {
8883417206SJason Gerecke 	struct wacom *wacom = hid_get_drvdata(hdev);
8983417206SJason Gerecke 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
9083417206SJason Gerecke 	struct wacom_features *features = &wacom_wac->features;
9183417206SJason Gerecke 	bool flush = false;
9283417206SJason Gerecke 	bool insert = false;
9383417206SJason Gerecke 	int i, j;
9483417206SJason Gerecke 
9583417206SJason Gerecke 	if (wacom_wac->serial[0] || !(features->quirks & WACOM_QUIRK_TOOLSERIAL))
9683417206SJason Gerecke 		return 0;
9783417206SJason Gerecke 
9883417206SJason Gerecke 	/* Queue events which have invalid tool type or serial number */
9983417206SJason Gerecke 	for (i = 0; i < report->maxfield; i++) {
10083417206SJason Gerecke 		for (j = 0; j < report->field[i]->maxusage; j++) {
10183417206SJason Gerecke 			struct hid_field *field = report->field[i];
10283417206SJason Gerecke 			struct hid_usage *usage = &field->usage[j];
10383417206SJason Gerecke 			unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
10483417206SJason Gerecke 			unsigned int offset;
10583417206SJason Gerecke 			unsigned int size;
10683417206SJason Gerecke 			unsigned int value;
10783417206SJason Gerecke 
10883417206SJason Gerecke 			if (equivalent_usage != HID_DG_INRANGE &&
10983417206SJason Gerecke 			    equivalent_usage != HID_DG_TOOLSERIALNUMBER &&
11083417206SJason Gerecke 			    equivalent_usage != WACOM_HID_WD_SERIALHI &&
11183417206SJason Gerecke 			    equivalent_usage != WACOM_HID_WD_TOOLTYPE)
11283417206SJason Gerecke 				continue;
11383417206SJason Gerecke 
11483417206SJason Gerecke 			offset = field->report_offset;
11583417206SJason Gerecke 			size = field->report_size;
11683417206SJason Gerecke 			value = hid_field_extract(hdev, raw_data+1, offset + j * size, size);
11783417206SJason Gerecke 
11883417206SJason Gerecke 			/* If we go out of range, we need to flush the queue ASAP */
11983417206SJason Gerecke 			if (equivalent_usage == HID_DG_INRANGE)
12083417206SJason Gerecke 				value = !value;
12183417206SJason Gerecke 
12283417206SJason Gerecke 			if (value) {
12383417206SJason Gerecke 				flush = true;
12483417206SJason Gerecke 				switch (equivalent_usage) {
12583417206SJason Gerecke 				case HID_DG_TOOLSERIALNUMBER:
12683417206SJason Gerecke 					wacom_wac->serial[0] = value;
12783417206SJason Gerecke 					break;
12883417206SJason Gerecke 
12983417206SJason Gerecke 				case WACOM_HID_WD_SERIALHI:
13083417206SJason Gerecke 					wacom_wac->serial[0] |= ((__u64)value) << 32;
13183417206SJason Gerecke 					break;
13283417206SJason Gerecke 
13383417206SJason Gerecke 				case WACOM_HID_WD_TOOLTYPE:
13483417206SJason Gerecke 					wacom_wac->id[0] = value;
13583417206SJason Gerecke 					break;
13683417206SJason Gerecke 				}
13783417206SJason Gerecke 			}
13883417206SJason Gerecke 			else {
13983417206SJason Gerecke 				insert = true;
14083417206SJason Gerecke 			}
14183417206SJason Gerecke 		}
14283417206SJason Gerecke 	}
14383417206SJason Gerecke 
14483417206SJason Gerecke 	if (flush)
145179e8e47SJason Gerecke 		wacom_wac_queue_flush(hdev, wacom_wac->pen_fifo);
14683417206SJason Gerecke 	else if (insert)
147179e8e47SJason Gerecke 		wacom_wac_queue_insert(hdev, wacom_wac->pen_fifo,
148073b50bcSJason Gerecke 				       raw_data, report_size);
14983417206SJason Gerecke 
15083417206SJason Gerecke 	return insert && !flush;
15183417206SJason Gerecke }
15283417206SJason Gerecke 
wacom_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * raw_data,int size)153471d1714SBenjamin Tissoires static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
154471d1714SBenjamin Tissoires 		u8 *raw_data, int size)
155471d1714SBenjamin Tissoires {
156471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
157471d1714SBenjamin Tissoires 
1581db1f392SJason Gerecke 	if (wacom->wacom_wac.features.type == BOOTLOADER)
1591db1f392SJason Gerecke 		return 0;
1601db1f392SJason Gerecke 
161471d1714SBenjamin Tissoires 	if (size > WACOM_PKGLEN_MAX)
162471d1714SBenjamin Tissoires 		return 1;
163471d1714SBenjamin Tissoires 
16483417206SJason Gerecke 	if (wacom_wac_pen_serial_enforce(hdev, report, raw_data, size))
16583417206SJason Gerecke 		return -1;
16683417206SJason Gerecke 
167471d1714SBenjamin Tissoires 	memcpy(wacom->wacom_wac.data, raw_data, size);
168471d1714SBenjamin Tissoires 
169471d1714SBenjamin Tissoires 	wacom_wac_irq(&wacom->wacom_wac, size);
170471d1714SBenjamin Tissoires 
171471d1714SBenjamin Tissoires 	return 0;
172471d1714SBenjamin Tissoires }
173471d1714SBenjamin Tissoires 
wacom_open(struct input_dev * dev)174471d1714SBenjamin Tissoires static int wacom_open(struct input_dev *dev)
175471d1714SBenjamin Tissoires {
176471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
177471d1714SBenjamin Tissoires 
178dff67416SBenjamin Tissoires 	return hid_hw_open(wacom->hdev);
179471d1714SBenjamin Tissoires }
180471d1714SBenjamin Tissoires 
wacom_close(struct input_dev * dev)181471d1714SBenjamin Tissoires static void wacom_close(struct input_dev *dev)
182471d1714SBenjamin Tissoires {
183471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
184471d1714SBenjamin Tissoires 
1853dad188eSBenjamin Tissoires 	/*
1863dad188eSBenjamin Tissoires 	 * wacom->hdev should never be null, but surprisingly, I had the case
1873dad188eSBenjamin Tissoires 	 * once while unplugging the Wacom Wireless Receiver.
1883dad188eSBenjamin Tissoires 	 */
1893dad188eSBenjamin Tissoires 	if (wacom->hdev)
190471d1714SBenjamin Tissoires 		hid_hw_close(wacom->hdev);
191471d1714SBenjamin Tissoires }
192471d1714SBenjamin Tissoires 
193471d1714SBenjamin Tissoires /*
194471d1714SBenjamin Tissoires  * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
195471d1714SBenjamin Tissoires  */
wacom_calc_hid_res(int logical_extents,int physical_extents,unsigned unit,int exponent)196471d1714SBenjamin Tissoires static int wacom_calc_hid_res(int logical_extents, int physical_extents,
197471d1714SBenjamin Tissoires 			       unsigned unit, int exponent)
198471d1714SBenjamin Tissoires {
199471d1714SBenjamin Tissoires 	struct hid_field field = {
200471d1714SBenjamin Tissoires 		.logical_maximum = logical_extents,
201471d1714SBenjamin Tissoires 		.physical_maximum = physical_extents,
202471d1714SBenjamin Tissoires 		.unit = unit,
203471d1714SBenjamin Tissoires 		.unit_exponent = exponent,
204471d1714SBenjamin Tissoires 	};
205471d1714SBenjamin Tissoires 
206471d1714SBenjamin Tissoires 	return hidinput_calc_abs_res(&field, ABS_X);
207471d1714SBenjamin Tissoires }
208471d1714SBenjamin Tissoires 
wacom_hid_usage_quirk(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage)20957832512SJason Gerecke static void wacom_hid_usage_quirk(struct hid_device *hdev,
21057832512SJason Gerecke 		struct hid_field *field, struct hid_usage *usage)
21157832512SJason Gerecke {
21257832512SJason Gerecke 	struct wacom *wacom = hid_get_drvdata(hdev);
21357832512SJason Gerecke 	struct wacom_features *features = &wacom->wacom_wac.features;
21457832512SJason Gerecke 	unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
21557832512SJason Gerecke 
21657832512SJason Gerecke 	/*
21757832512SJason Gerecke 	 * The Dell Canvas 27 needs to be switched to its vendor-defined
21857832512SJason Gerecke 	 * report to provide the best resolution.
21957832512SJason Gerecke 	 */
22057832512SJason Gerecke 	if (hdev->vendor == USB_VENDOR_ID_WACOM &&
22157832512SJason Gerecke 	    hdev->product == 0x4200 &&
22257832512SJason Gerecke 	    field->application == HID_UP_MSVENDOR) {
22357832512SJason Gerecke 		wacom->wacom_wac.mode_report = field->report->id;
22457832512SJason Gerecke 		wacom->wacom_wac.mode_value = 2;
22557832512SJason Gerecke 	}
22657832512SJason Gerecke 
22757832512SJason Gerecke 	/*
22857832512SJason Gerecke 	 * ISDv4 devices which predate HID's adoption of the
22957832512SJason Gerecke 	 * HID_DG_BARELSWITCH2 usage use 0x000D0000 in its
23057832512SJason Gerecke 	 * position instead. We can accurately detect if a
23157832512SJason Gerecke 	 * usage with that value should be HID_DG_BARRELSWITCH2
23257832512SJason Gerecke 	 * based on the surrounding usages, which have remained
23357832512SJason Gerecke 	 * constant across generations.
23457832512SJason Gerecke 	 */
23557832512SJason Gerecke 	if (features->type == HID_GENERIC &&
23657832512SJason Gerecke 	    usage->hid == 0x000D0000 &&
23757832512SJason Gerecke 	    field->application == HID_DG_PEN &&
23857832512SJason Gerecke 	    field->physical == HID_DG_STYLUS) {
23957832512SJason Gerecke 		int i = usage->usage_index;
24057832512SJason Gerecke 
24157832512SJason Gerecke 		if (i-4 >= 0 && i+1 < field->maxusage &&
24257832512SJason Gerecke 		    field->usage[i-4].hid == HID_DG_TIPSWITCH &&
24357832512SJason Gerecke 		    field->usage[i-3].hid == HID_DG_BARRELSWITCH &&
24457832512SJason Gerecke 		    field->usage[i-2].hid == HID_DG_ERASER &&
24557832512SJason Gerecke 		    field->usage[i-1].hid == HID_DG_INVERT &&
24657832512SJason Gerecke 		    field->usage[i+1].hid == HID_DG_INRANGE) {
24757832512SJason Gerecke 			usage->hid = HID_DG_BARRELSWITCH2;
24857832512SJason Gerecke 		}
24957832512SJason Gerecke 	}
25057832512SJason Gerecke 
251e9fe0d49SJason Gerecke 	/*
252e9fe0d49SJason Gerecke 	 * Wacom's AES devices use different vendor-defined usages to
253e9fe0d49SJason Gerecke 	 * report serial number information compared to their branded
254e9fe0d49SJason Gerecke 	 * hardware. The usages are also sometimes ill-defined and do
255e9fe0d49SJason Gerecke 	 * not have the correct logical min/max values set. Lets patch
256e9fe0d49SJason Gerecke 	 * the descriptor to use the branded usage convention and fix
257e9fe0d49SJason Gerecke 	 * the errors.
258e9fe0d49SJason Gerecke 	 */
259e9fe0d49SJason Gerecke 	if (usage->hid == WACOM_HID_WT_SERIALNUMBER &&
260e9fe0d49SJason Gerecke 	    field->report_size == 16 &&
261e9fe0d49SJason Gerecke 	    field->index + 2 < field->report->maxfield) {
262e9fe0d49SJason Gerecke 		struct hid_field *a = field->report->field[field->index + 1];
263e9fe0d49SJason Gerecke 		struct hid_field *b = field->report->field[field->index + 2];
264e9fe0d49SJason Gerecke 
265e9fe0d49SJason Gerecke 		if (a->maxusage > 0 &&
266e9fe0d49SJason Gerecke 		    a->usage[0].hid == HID_DG_TOOLSERIALNUMBER &&
267e9fe0d49SJason Gerecke 		    a->report_size == 32 &&
268e9fe0d49SJason Gerecke 		    b->maxusage > 0 &&
269e9fe0d49SJason Gerecke 		    b->usage[0].hid == 0xFF000000 &&
270e9fe0d49SJason Gerecke 		    b->report_size == 8) {
271e9fe0d49SJason Gerecke 			features->quirks |= WACOM_QUIRK_AESPEN;
272e9fe0d49SJason Gerecke 			usage->hid = WACOM_HID_WD_TOOLTYPE;
273e9fe0d49SJason Gerecke 			field->logical_minimum = S16_MIN;
274e9fe0d49SJason Gerecke 			field->logical_maximum = S16_MAX;
275e9fe0d49SJason Gerecke 			a->logical_minimum = S32_MIN;
276e9fe0d49SJason Gerecke 			a->logical_maximum = S32_MAX;
277e9fe0d49SJason Gerecke 			b->usage[0].hid = WACOM_HID_WD_SERIALHI;
278e9fe0d49SJason Gerecke 			b->logical_minimum = 0;
279e9fe0d49SJason Gerecke 			b->logical_maximum = U8_MAX;
280e9fe0d49SJason Gerecke 		}
281e9fe0d49SJason Gerecke 	}
282e9fe0d49SJason Gerecke 
28357832512SJason Gerecke 	/* 2nd-generation Intuos Pro Large has incorrect Y maximum */
28457832512SJason Gerecke 	if (hdev->vendor == USB_VENDOR_ID_WACOM &&
28557832512SJason Gerecke 	    hdev->product == 0x0358 &&
28657832512SJason Gerecke 	    WACOM_PEN_FIELD(field) &&
28757832512SJason Gerecke 	    equivalent_usage == HID_GD_Y) {
28857832512SJason Gerecke 		field->logical_maximum = 43200;
28957832512SJason Gerecke 	}
29057832512SJason Gerecke }
29157832512SJason Gerecke 
wacom_feature_mapping(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage)292471d1714SBenjamin Tissoires static void wacom_feature_mapping(struct hid_device *hdev,
293471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
294471d1714SBenjamin Tissoires {
295471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
296471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
2975ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
298ac2423c9SAaron Armstrong Skomra 	unsigned int equivalent_usage = wacom_equivalent_usage(usage->hid);
2998ffffd52SBenjamin Tissoires 	u8 *data;
3008ffffd52SBenjamin Tissoires 	int ret;
3013064a03bSAaron Ma 	u32 n;
302471d1714SBenjamin Tissoires 
30357832512SJason Gerecke 	wacom_hid_usage_quirk(hdev, field, usage);
30457832512SJason Gerecke 
305ac2423c9SAaron Armstrong Skomra 	switch (equivalent_usage) {
306d8e98060SAaron Armstrong Skomra 	case WACOM_HID_WD_TOUCH_RING_SETTING:
307d8e98060SAaron Armstrong Skomra 		wacom->generic_has_leds = true;
308d8e98060SAaron Armstrong Skomra 		break;
309471d1714SBenjamin Tissoires 	case HID_DG_CONTACTMAX:
310471d1714SBenjamin Tissoires 		/* leave touch_max as is if predefined */
3118ffffd52SBenjamin Tissoires 		if (!features->touch_max) {
3128ffffd52SBenjamin Tissoires 			/* read manually */
313184eccd4SAaron Armstrong Skomra 			n = hid_report_len(field->report);
314184eccd4SAaron Armstrong Skomra 			data = hid_alloc_report_buf(field->report, GFP_KERNEL);
3158ffffd52SBenjamin Tissoires 			if (!data)
3168ffffd52SBenjamin Tissoires 				break;
3178ffffd52SBenjamin Tissoires 			data[0] = field->report->id;
3188ffffd52SBenjamin Tissoires 			ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
319184eccd4SAaron Armstrong Skomra 					       data, n, WAC_CMD_RETRIES);
320778fbf41SJason Gerecke 			if (ret == n && features->type == HID_GENERIC) {
321184eccd4SAaron Armstrong Skomra 				ret = hid_report_raw_event(hdev,
322184eccd4SAaron Armstrong Skomra 					HID_FEATURE_REPORT, data, n, 0);
323778fbf41SJason Gerecke 			} else if (ret == 2 && features->type != HID_GENERIC) {
324778fbf41SJason Gerecke 				features->touch_max = data[1];
32505e8fd92SJason Gerecke 			} else {
32605e8fd92SJason Gerecke 				features->touch_max = 16;
32705e8fd92SJason Gerecke 				hid_warn(hdev, "wacom_feature_mapping: "
32805e8fd92SJason Gerecke 					 "could not get HID_DG_CONTACTMAX, "
32905e8fd92SJason Gerecke 					 "defaulting to %d\n",
33005e8fd92SJason Gerecke 					  features->touch_max);
33105e8fd92SJason Gerecke 			}
3328ffffd52SBenjamin Tissoires 			kfree(data);
3338ffffd52SBenjamin Tissoires 		}
334471d1714SBenjamin Tissoires 		break;
3355ae6e89fSBenjamin Tissoires 	case HID_DG_INPUTMODE:
3365ae6e89fSBenjamin Tissoires 		/* Ignore if value index is out of bounds. */
3375ae6e89fSBenjamin Tissoires 		if (usage->usage_index >= field->report_count) {
3385ae6e89fSBenjamin Tissoires 			dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
3395ae6e89fSBenjamin Tissoires 			break;
3405ae6e89fSBenjamin Tissoires 		}
3415ae6e89fSBenjamin Tissoires 
3425ae6e89fSBenjamin Tissoires 		hid_data->inputmode = field->report->id;
3435ae6e89fSBenjamin Tissoires 		hid_data->inputmode_index = usage->usage_index;
3445ae6e89fSBenjamin Tissoires 		break;
345326ea2a9SJason Gerecke 
346326ea2a9SJason Gerecke 	case HID_UP_DIGITIZER:
347326ea2a9SJason Gerecke 		if (field->report->id == 0x0B &&
3488de82280SJason Gerecke 		    (field->application == WACOM_HID_G9_PEN ||
3498de82280SJason Gerecke 		     field->application == WACOM_HID_G11_PEN)) {
350326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_report = field->report->id;
351326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_value = 0;
352326ea2a9SJason Gerecke 		}
353326ea2a9SJason Gerecke 		break;
354326ea2a9SJason Gerecke 
355c9c09587SJason Gerecke 	case WACOM_HID_WD_DATAMODE:
356c9c09587SJason Gerecke 		wacom->wacom_wac.mode_report = field->report->id;
357c9c09587SJason Gerecke 		wacom->wacom_wac.mode_value = 2;
358c9c09587SJason Gerecke 		break;
359c9c09587SJason Gerecke 
3608de82280SJason Gerecke 	case WACOM_HID_UP_G9:
3618de82280SJason Gerecke 	case WACOM_HID_UP_G11:
362326ea2a9SJason Gerecke 		if (field->report->id == 0x03 &&
3638de82280SJason Gerecke 		    (field->application == WACOM_HID_G9_TOUCHSCREEN ||
3648de82280SJason Gerecke 		     field->application == WACOM_HID_G11_TOUCHSCREEN)) {
365326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_report = field->report->id;
366326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_value = 0;
367326ea2a9SJason Gerecke 		}
368326ea2a9SJason Gerecke 		break;
369345857bbSJason Gerecke 	case WACOM_HID_WD_OFFSETLEFT:
370345857bbSJason Gerecke 	case WACOM_HID_WD_OFFSETTOP:
371345857bbSJason Gerecke 	case WACOM_HID_WD_OFFSETRIGHT:
372345857bbSJason Gerecke 	case WACOM_HID_WD_OFFSETBOTTOM:
373345857bbSJason Gerecke 		/* read manually */
374345857bbSJason Gerecke 		n = hid_report_len(field->report);
375345857bbSJason Gerecke 		data = hid_alloc_report_buf(field->report, GFP_KERNEL);
376345857bbSJason Gerecke 		if (!data)
377345857bbSJason Gerecke 			break;
378345857bbSJason Gerecke 		data[0] = field->report->id;
379345857bbSJason Gerecke 		ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
380345857bbSJason Gerecke 					data, n, WAC_CMD_RETRIES);
381345857bbSJason Gerecke 		if (ret == n) {
382345857bbSJason Gerecke 			ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT,
383345857bbSJason Gerecke 						   data, n, 0);
384345857bbSJason Gerecke 		} else {
385345857bbSJason Gerecke 			hid_warn(hdev, "%s: could not retrieve sensor offsets\n",
386345857bbSJason Gerecke 				 __func__);
387345857bbSJason Gerecke 		}
388345857bbSJason Gerecke 		kfree(data);
389345857bbSJason Gerecke 		break;
390471d1714SBenjamin Tissoires 	}
391471d1714SBenjamin Tissoires }
392471d1714SBenjamin Tissoires 
393471d1714SBenjamin Tissoires /*
394471d1714SBenjamin Tissoires  * Interface Descriptor of wacom devices can be incomplete and
395471d1714SBenjamin Tissoires  * inconsistent so wacom_features table is used to store stylus
396471d1714SBenjamin Tissoires  * device's packet lengths, various maximum values, and tablet
397471d1714SBenjamin Tissoires  * resolution based on product ID's.
398471d1714SBenjamin Tissoires  *
399471d1714SBenjamin Tissoires  * For devices that contain 2 interfaces, wacom_features table is
400471d1714SBenjamin Tissoires  * inaccurate for the touch interface.  Since the Interface Descriptor
401471d1714SBenjamin Tissoires  * for touch interfaces has pretty complete data, this function exists
402471d1714SBenjamin Tissoires  * to query tablet for this missing information instead of hard coding in
403471d1714SBenjamin Tissoires  * an additional table.
404471d1714SBenjamin Tissoires  *
405471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a stylus will contain a
406471d1714SBenjamin Tissoires  * boot mouse application collection that is not of interest and this
407471d1714SBenjamin Tissoires  * function will ignore it.
408471d1714SBenjamin Tissoires  *
409471d1714SBenjamin Tissoires  * It also contains a digitizer application collection that also is not
410471d1714SBenjamin Tissoires  * of interest since any information it contains would be duplicate
411471d1714SBenjamin Tissoires  * of what is in wacom_features. Usually it defines a report of an array
412471d1714SBenjamin Tissoires  * of bytes that could be used as max length of the stylus packet returned.
413471d1714SBenjamin Tissoires  * If it happens to define a Digitizer-Stylus Physical Collection then
414471d1714SBenjamin Tissoires  * the X and Y logical values contain valid data but it is ignored.
415471d1714SBenjamin Tissoires  *
416471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a touch interface will contain a
417471d1714SBenjamin Tissoires  * Digitizer-Finger Physical Collection which will define both logical
418471d1714SBenjamin Tissoires  * X/Y maximum as well as the physical size of tablet. Since touch
419471d1714SBenjamin Tissoires  * interfaces haven't supported pressure or distance, this is enough
420471d1714SBenjamin Tissoires  * information to override invalid values in the wacom_features table.
421471d1714SBenjamin Tissoires  *
422471d1714SBenjamin Tissoires  * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
423471d1714SBenjamin Tissoires  * data. We deal with them after returning from this function.
424471d1714SBenjamin Tissoires  */
wacom_usage_mapping(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage)425471d1714SBenjamin Tissoires static void wacom_usage_mapping(struct hid_device *hdev,
426471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
427471d1714SBenjamin Tissoires {
428471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
429471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
430d97a5522SBenjamin Tissoires 	bool finger = WACOM_FINGER_FIELD(field);
431d97a5522SBenjamin Tissoires 	bool pen = WACOM_PEN_FIELD(field);
432418b573bSPing Cheng 	unsigned equivalent_usage = wacom_equivalent_usage(usage->hid);
433471d1714SBenjamin Tissoires 
434471d1714SBenjamin Tissoires 	/*
435471d1714SBenjamin Tissoires 	* Requiring Stylus Usage will ignore boot mouse
436471d1714SBenjamin Tissoires 	* X/Y values and some cases of invalid Digitizer X/Y
437471d1714SBenjamin Tissoires 	* values commonly reported.
438471d1714SBenjamin Tissoires 	*/
439042628abSJason Gerecke 	if (pen)
440aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_PEN;
441042628abSJason Gerecke 	else if (finger)
442aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_TOUCH;
443042628abSJason Gerecke 	else
444471d1714SBenjamin Tissoires 		return;
445471d1714SBenjamin Tissoires 
44657832512SJason Gerecke 	wacom_hid_usage_quirk(hdev, field, usage);
447471d1714SBenjamin Tissoires 
448418b573bSPing Cheng 	switch (equivalent_usage) {
449471d1714SBenjamin Tissoires 	case HID_GD_X:
450471d1714SBenjamin Tissoires 		features->x_max = field->logical_maximum;
451471d1714SBenjamin Tissoires 		if (finger) {
452471d1714SBenjamin Tissoires 			features->x_phy = field->physical_maximum;
4533b164a00SPing Cheng 			if ((features->type != BAMBOO_PT) &&
4543b164a00SPing Cheng 			    (features->type != BAMBOO_TOUCH)) {
455471d1714SBenjamin Tissoires 				features->unit = field->unit;
456471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
457471d1714SBenjamin Tissoires 			}
458471d1714SBenjamin Tissoires 		}
459471d1714SBenjamin Tissoires 		break;
460471d1714SBenjamin Tissoires 	case HID_GD_Y:
461471d1714SBenjamin Tissoires 		features->y_max = field->logical_maximum;
462471d1714SBenjamin Tissoires 		if (finger) {
463471d1714SBenjamin Tissoires 			features->y_phy = field->physical_maximum;
4643b164a00SPing Cheng 			if ((features->type != BAMBOO_PT) &&
4653b164a00SPing Cheng 			    (features->type != BAMBOO_TOUCH)) {
466471d1714SBenjamin Tissoires 				features->unit = field->unit;
467471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
468471d1714SBenjamin Tissoires 			}
469471d1714SBenjamin Tissoires 		}
470471d1714SBenjamin Tissoires 		break;
471471d1714SBenjamin Tissoires 	case HID_DG_TIPPRESSURE:
472471d1714SBenjamin Tissoires 		if (pen)
473471d1714SBenjamin Tissoires 			features->pressure_max = field->logical_maximum;
474471d1714SBenjamin Tissoires 		break;
475471d1714SBenjamin Tissoires 	}
4767704ac93SBenjamin Tissoires 
4777704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
4787704ac93SBenjamin Tissoires 		wacom_wac_usage_mapping(hdev, field, usage);
479471d1714SBenjamin Tissoires }
480471d1714SBenjamin Tissoires 
wacom_post_parse_hid(struct hid_device * hdev,struct wacom_features * features)481b58ba1baSJason Gerecke static void wacom_post_parse_hid(struct hid_device *hdev,
482b58ba1baSJason Gerecke 				 struct wacom_features *features)
483b58ba1baSJason Gerecke {
484b58ba1baSJason Gerecke 	struct wacom *wacom = hid_get_drvdata(hdev);
485b58ba1baSJason Gerecke 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
486b58ba1baSJason Gerecke 
487b58ba1baSJason Gerecke 	if (features->type == HID_GENERIC) {
488b58ba1baSJason Gerecke 		/* Any last-minute generic device setup */
4894082da80SBenjamin Tissoires 		if (wacom_wac->has_mode_change) {
4904082da80SBenjamin Tissoires 			if (wacom_wac->is_direct_mode)
4914082da80SBenjamin Tissoires 				features->device_type |= WACOM_DEVICETYPE_DIRECT;
4924082da80SBenjamin Tissoires 			else
4934082da80SBenjamin Tissoires 				features->device_type &= ~WACOM_DEVICETYPE_DIRECT;
4944082da80SBenjamin Tissoires 		}
4954082da80SBenjamin Tissoires 
496b58ba1baSJason Gerecke 		if (features->touch_max > 1) {
497ac2423c9SAaron Armstrong Skomra 			if (features->device_type & WACOM_DEVICETYPE_DIRECT)
498ac2423c9SAaron Armstrong Skomra 				input_mt_init_slots(wacom_wac->touch_input,
499ac2423c9SAaron Armstrong Skomra 						    wacom_wac->features.touch_max,
500b58ba1baSJason Gerecke 						    INPUT_MT_DIRECT);
501ac2423c9SAaron Armstrong Skomra 			else
502ac2423c9SAaron Armstrong Skomra 				input_mt_init_slots(wacom_wac->touch_input,
503ac2423c9SAaron Armstrong Skomra 						    wacom_wac->features.touch_max,
504ac2423c9SAaron Armstrong Skomra 						    INPUT_MT_POINTER);
505b58ba1baSJason Gerecke 		}
506b58ba1baSJason Gerecke 	}
507b58ba1baSJason Gerecke }
508b58ba1baSJason Gerecke 
wacom_parse_hid(struct hid_device * hdev,struct wacom_features * features)509471d1714SBenjamin Tissoires static void wacom_parse_hid(struct hid_device *hdev,
510471d1714SBenjamin Tissoires 			   struct wacom_features *features)
511471d1714SBenjamin Tissoires {
512471d1714SBenjamin Tissoires 	struct hid_report_enum *rep_enum;
513471d1714SBenjamin Tissoires 	struct hid_report *hreport;
514471d1714SBenjamin Tissoires 	int i, j;
515471d1714SBenjamin Tissoires 
516471d1714SBenjamin Tissoires 	/* check features first */
517471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
518471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
519471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++) {
520471d1714SBenjamin Tissoires 			/* Ignore if report count is out of bounds. */
521471d1714SBenjamin Tissoires 			if (hreport->field[i]->report_count < 1)
522471d1714SBenjamin Tissoires 				continue;
523471d1714SBenjamin Tissoires 
524471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++) {
525471d1714SBenjamin Tissoires 				wacom_feature_mapping(hdev, hreport->field[i],
526471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
527471d1714SBenjamin Tissoires 			}
528471d1714SBenjamin Tissoires 		}
529471d1714SBenjamin Tissoires 	}
530471d1714SBenjamin Tissoires 
531471d1714SBenjamin Tissoires 	/* now check the input usages */
532471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
533471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
534471d1714SBenjamin Tissoires 
535471d1714SBenjamin Tissoires 		if (!hreport->maxfield)
536471d1714SBenjamin Tissoires 			continue;
537471d1714SBenjamin Tissoires 
538471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++)
539471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++)
540471d1714SBenjamin Tissoires 				wacom_usage_mapping(hdev, hreport->field[i],
541471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
542471d1714SBenjamin Tissoires 	}
543b58ba1baSJason Gerecke 
544b58ba1baSJason Gerecke 	wacom_post_parse_hid(hdev, features);
545471d1714SBenjamin Tissoires }
546471d1714SBenjamin Tissoires 
wacom_hid_set_device_mode(struct hid_device * hdev)5475ae6e89fSBenjamin Tissoires static int wacom_hid_set_device_mode(struct hid_device *hdev)
5485ae6e89fSBenjamin Tissoires {
5495ae6e89fSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
5505ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
5515ae6e89fSBenjamin Tissoires 	struct hid_report *r;
5525ae6e89fSBenjamin Tissoires 	struct hid_report_enum *re;
5535ae6e89fSBenjamin Tissoires 
5545ae6e89fSBenjamin Tissoires 	if (hid_data->inputmode < 0)
5555ae6e89fSBenjamin Tissoires 		return 0;
5565ae6e89fSBenjamin Tissoires 
5575ae6e89fSBenjamin Tissoires 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
5585ae6e89fSBenjamin Tissoires 	r = re->report_id_hash[hid_data->inputmode];
5595ae6e89fSBenjamin Tissoires 	if (r) {
5605ae6e89fSBenjamin Tissoires 		r->field[0]->value[hid_data->inputmode_index] = 2;
5615ae6e89fSBenjamin Tissoires 		hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
5625ae6e89fSBenjamin Tissoires 	}
5635ae6e89fSBenjamin Tissoires 	return 0;
5645ae6e89fSBenjamin Tissoires }
5655ae6e89fSBenjamin Tissoires 
wacom_set_device_mode(struct hid_device * hdev,struct wacom_wac * wacom_wac)566326ea2a9SJason Gerecke static int wacom_set_device_mode(struct hid_device *hdev,
567326ea2a9SJason Gerecke 				 struct wacom_wac *wacom_wac)
568471d1714SBenjamin Tissoires {
569326ea2a9SJason Gerecke 	u8 *rep_data;
570326ea2a9SJason Gerecke 	struct hid_report *r;
571326ea2a9SJason Gerecke 	struct hid_report_enum *re;
5723064a03bSAaron Ma 	u32 length;
573471d1714SBenjamin Tissoires 	int error = -ENOMEM, limit = 0;
574471d1714SBenjamin Tissoires 
575326ea2a9SJason Gerecke 	if (wacom_wac->mode_report < 0)
576326ea2a9SJason Gerecke 		return 0;
577326ea2a9SJason Gerecke 
578326ea2a9SJason Gerecke 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
579326ea2a9SJason Gerecke 	r = re->report_id_hash[wacom_wac->mode_report];
580326ea2a9SJason Gerecke 	if (!r)
581326ea2a9SJason Gerecke 		return -EINVAL;
582326ea2a9SJason Gerecke 
583326ea2a9SJason Gerecke 	rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
584471d1714SBenjamin Tissoires 	if (!rep_data)
585326ea2a9SJason Gerecke 		return -ENOMEM;
586326ea2a9SJason Gerecke 
587326ea2a9SJason Gerecke 	length = hid_report_len(r);
588471d1714SBenjamin Tissoires 
589471d1714SBenjamin Tissoires 	do {
590326ea2a9SJason Gerecke 		rep_data[0] = wacom_wac->mode_report;
591326ea2a9SJason Gerecke 		rep_data[1] = wacom_wac->mode_value;
592471d1714SBenjamin Tissoires 
593296b7378SPrzemo Firszt 		error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
594296b7378SPrzemo Firszt 					 length, 1);
595471d1714SBenjamin Tissoires 		if (error >= 0)
596471d1714SBenjamin Tissoires 			error = wacom_get_report(hdev, HID_FEATURE_REPORT,
597c64d8834SPing Cheng 			                         rep_data, length, 1);
598326ea2a9SJason Gerecke 	} while (error >= 0 &&
599326ea2a9SJason Gerecke 		 rep_data[1] != wacom_wac->mode_report &&
600326ea2a9SJason Gerecke 		 limit++ < WAC_MSG_RETRIES);
601471d1714SBenjamin Tissoires 
602471d1714SBenjamin Tissoires 	kfree(rep_data);
603471d1714SBenjamin Tissoires 
604471d1714SBenjamin Tissoires 	return error < 0 ? error : 0;
605471d1714SBenjamin Tissoires }
606471d1714SBenjamin Tissoires 
wacom_bt_query_tablet_data(struct hid_device * hdev,u8 speed,struct wacom_features * features)607f81a1295SBenjamin Tissoires static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
608f81a1295SBenjamin Tissoires 		struct wacom_features *features)
609f81a1295SBenjamin Tissoires {
610387142bbSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
611387142bbSBenjamin Tissoires 	int ret;
612387142bbSBenjamin Tissoires 	u8 rep_data[2];
613387142bbSBenjamin Tissoires 
614387142bbSBenjamin Tissoires 	switch (features->type) {
615387142bbSBenjamin Tissoires 	case GRAPHIRE_BT:
616387142bbSBenjamin Tissoires 		rep_data[0] = 0x03;
617387142bbSBenjamin Tissoires 		rep_data[1] = 0x00;
618296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
619296b7378SPrzemo Firszt 					3);
620387142bbSBenjamin Tissoires 
621387142bbSBenjamin Tissoires 		if (ret >= 0) {
622387142bbSBenjamin Tissoires 			rep_data[0] = speed == 0 ? 0x05 : 0x06;
623387142bbSBenjamin Tissoires 			rep_data[1] = 0x00;
624387142bbSBenjamin Tissoires 
625387142bbSBenjamin Tissoires 			ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
626296b7378SPrzemo Firszt 						rep_data, 2, 3);
627387142bbSBenjamin Tissoires 
628387142bbSBenjamin Tissoires 			if (ret >= 0) {
629387142bbSBenjamin Tissoires 				wacom->wacom_wac.bt_high_speed = speed;
630387142bbSBenjamin Tissoires 				return 0;
631387142bbSBenjamin Tissoires 			}
632387142bbSBenjamin Tissoires 		}
633387142bbSBenjamin Tissoires 
634387142bbSBenjamin Tissoires 		/*
635387142bbSBenjamin Tissoires 		 * Note that if the raw queries fail, it's not a hard failure
636387142bbSBenjamin Tissoires 		 * and it is safe to continue
637387142bbSBenjamin Tissoires 		 */
638387142bbSBenjamin Tissoires 		hid_warn(hdev, "failed to poke device, command %d, err %d\n",
639387142bbSBenjamin Tissoires 			 rep_data[0], ret);
640387142bbSBenjamin Tissoires 		break;
64181af7e61SBenjamin Tissoires 	case INTUOS4WL:
64281af7e61SBenjamin Tissoires 		if (speed == 1)
64381af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features &= ~0x20;
64481af7e61SBenjamin Tissoires 		else
64581af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features |= 0x20;
64681af7e61SBenjamin Tissoires 
64781af7e61SBenjamin Tissoires 		rep_data[0] = 0x03;
64881af7e61SBenjamin Tissoires 		rep_data[1] = wacom->wacom_wac.bt_features;
64981af7e61SBenjamin Tissoires 
650296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
651296b7378SPrzemo Firszt 					1);
65281af7e61SBenjamin Tissoires 		if (ret >= 0)
65381af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_high_speed = speed;
65481af7e61SBenjamin Tissoires 		break;
655387142bbSBenjamin Tissoires 	}
656387142bbSBenjamin Tissoires 
657f81a1295SBenjamin Tissoires 	return 0;
658f81a1295SBenjamin Tissoires }
659f81a1295SBenjamin Tissoires 
660471d1714SBenjamin Tissoires /*
661471d1714SBenjamin Tissoires  * Switch the tablet into its most-capable mode. Wacom tablets are
662471d1714SBenjamin Tissoires  * typically configured to power-up in a mode which sends mouse-like
663471d1714SBenjamin Tissoires  * reports to the OS. To get absolute position, pressure data, etc.
664471d1714SBenjamin Tissoires  * from the tablet, it is necessary to switch the tablet out of this
665471d1714SBenjamin Tissoires  * mode and into one which sends the full range of tablet data.
666471d1714SBenjamin Tissoires  */
_wacom_query_tablet_data(struct wacom * wacom)667a544c619SBenjamin Tissoires static int _wacom_query_tablet_data(struct wacom *wacom)
668471d1714SBenjamin Tissoires {
669a544c619SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
670326ea2a9SJason Gerecke 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
671a544c619SBenjamin Tissoires 	struct wacom_features *features = &wacom_wac->features;
672326ea2a9SJason Gerecke 
673f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
674f81a1295SBenjamin Tissoires 		return wacom_bt_query_tablet_data(hdev, 1, features);
675f81a1295SBenjamin Tissoires 
676326ea2a9SJason Gerecke 	if (features->type != HID_GENERIC) {
677aa86b18cSJason Gerecke 		if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
678471d1714SBenjamin Tissoires 			if (features->type > TABLETPC) {
679471d1714SBenjamin Tissoires 				/* MT Tablet PC touch */
680326ea2a9SJason Gerecke 				wacom_wac->mode_report = 3;
681326ea2a9SJason Gerecke 				wacom_wac->mode_value = 4;
682326ea2a9SJason Gerecke 			} else if (features->type == WACOM_24HDT) {
683326ea2a9SJason Gerecke 				wacom_wac->mode_report = 18;
684326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
685326ea2a9SJason Gerecke 			} else if (features->type == WACOM_27QHDT) {
686326ea2a9SJason Gerecke 				wacom_wac->mode_report = 131;
687326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
688326ea2a9SJason Gerecke 			} else if (features->type == BAMBOO_PAD) {
689326ea2a9SJason Gerecke 				wacom_wac->mode_report = 2;
690326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
6918c97a765SBenjamin Tissoires 			}
692aa86b18cSJason Gerecke 		} else if (features->device_type & WACOM_DEVICETYPE_PEN) {
6937adb91bdSPing Cheng 			if (features->type <= BAMBOO_PT) {
694326ea2a9SJason Gerecke 				wacom_wac->mode_report = 2;
695326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
696471d1714SBenjamin Tissoires 			}
697471d1714SBenjamin Tissoires 		}
698326ea2a9SJason Gerecke 	}
699326ea2a9SJason Gerecke 
700326ea2a9SJason Gerecke 	wacom_set_device_mode(hdev, wacom_wac);
701326ea2a9SJason Gerecke 
702326ea2a9SJason Gerecke 	if (features->type == HID_GENERIC)
703326ea2a9SJason Gerecke 		return wacom_hid_set_device_mode(hdev);
704471d1714SBenjamin Tissoires 
705471d1714SBenjamin Tissoires 	return 0;
706471d1714SBenjamin Tissoires }
707471d1714SBenjamin Tissoires 
wacom_retrieve_hid_descriptor(struct hid_device * hdev,struct wacom_features * features)708471d1714SBenjamin Tissoires static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
709471d1714SBenjamin Tissoires 					 struct wacom_features *features)
710471d1714SBenjamin Tissoires {
711471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
712471d1714SBenjamin Tissoires 	struct usb_interface *intf = wacom->intf;
713471d1714SBenjamin Tissoires 
714471d1714SBenjamin Tissoires 	/* default features */
715471d1714SBenjamin Tissoires 	features->x_fuzz = 4;
716471d1714SBenjamin Tissoires 	features->y_fuzz = 4;
717471d1714SBenjamin Tissoires 	features->pressure_fuzz = 0;
718bef7e200SJason Gerecke 	features->distance_fuzz = 1;
719bef7e200SJason Gerecke 	features->tilt_fuzz = 1;
720471d1714SBenjamin Tissoires 
721471d1714SBenjamin Tissoires 	/*
722471d1714SBenjamin Tissoires 	 * The wireless device HID is basic and layout conflicts with
723471d1714SBenjamin Tissoires 	 * other tablets (monitor and touch interface can look like pen).
724471d1714SBenjamin Tissoires 	 * Skip the query for this type and modify defaults based on
725471d1714SBenjamin Tissoires 	 * interface number.
726471d1714SBenjamin Tissoires 	 */
727720ac467SGreg Kroah-Hartman 	if (features->type == WIRELESS && intf) {
7283f14a63aSJason Gerecke 		if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
729ccad85ccSJason Gerecke 			features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
7303f14a63aSJason Gerecke 		else
731aa86b18cSJason Gerecke 			features->device_type = WACOM_DEVICETYPE_NONE;
7323f14a63aSJason Gerecke 		return;
733471d1714SBenjamin Tissoires 	}
734471d1714SBenjamin Tissoires 
735471d1714SBenjamin Tissoires 	wacom_parse_hid(hdev, features);
736471d1714SBenjamin Tissoires }
737471d1714SBenjamin Tissoires 
738471d1714SBenjamin Tissoires struct wacom_hdev_data {
739471d1714SBenjamin Tissoires 	struct list_head list;
740471d1714SBenjamin Tissoires 	struct kref kref;
741471d1714SBenjamin Tissoires 	struct hid_device *dev;
742471d1714SBenjamin Tissoires 	struct wacom_shared shared;
743471d1714SBenjamin Tissoires };
744471d1714SBenjamin Tissoires 
745471d1714SBenjamin Tissoires static LIST_HEAD(wacom_udev_list);
746471d1714SBenjamin Tissoires static DEFINE_MUTEX(wacom_udev_list_lock);
747471d1714SBenjamin Tissoires 
wacom_are_sibling(struct hid_device * hdev,struct hid_device * sibling)748471d1714SBenjamin Tissoires static bool wacom_are_sibling(struct hid_device *hdev,
749471d1714SBenjamin Tissoires 		struct hid_device *sibling)
750471d1714SBenjamin Tissoires {
751471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
752471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
75341372d5dSJason Gerecke 	struct wacom *sibling_wacom = hid_get_drvdata(sibling);
75441372d5dSJason Gerecke 	struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
75541372d5dSJason Gerecke 	__u32 oVid = features->oVid ? features->oVid : hdev->vendor;
75641372d5dSJason Gerecke 	__u32 oPid = features->oPid ? features->oPid : hdev->product;
757471d1714SBenjamin Tissoires 
75841372d5dSJason Gerecke 	/* The defined oVid/oPid must match that of the sibling */
75941372d5dSJason Gerecke 	if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
76041372d5dSJason Gerecke 		return false;
76141372d5dSJason Gerecke 	if (features->oPid != HID_ANY_ID && sibling->product != oPid)
76241372d5dSJason Gerecke 		return false;
76341372d5dSJason Gerecke 
76441372d5dSJason Gerecke 	/*
76541372d5dSJason Gerecke 	 * Devices with the same VID/PID must share the same physical
76641372d5dSJason Gerecke 	 * device path, while those with different VID/PID must share
76741372d5dSJason Gerecke 	 * the same physical parent device path.
76841372d5dSJason Gerecke 	 */
76941372d5dSJason Gerecke 	if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
7701a8861f1SDaniel M. Lambea 		if (!hid_compare_device_paths(hdev, sibling, '/'))
77141372d5dSJason Gerecke 			return false;
77241372d5dSJason Gerecke 	} else {
7731a8861f1SDaniel M. Lambea 		if (!hid_compare_device_paths(hdev, sibling, '.'))
77441372d5dSJason Gerecke 			return false;
775471d1714SBenjamin Tissoires 	}
776471d1714SBenjamin Tissoires 
77741372d5dSJason Gerecke 	/* Skip the remaining heuristics unless you are a HID_GENERIC device */
77841372d5dSJason Gerecke 	if (features->type != HID_GENERIC)
77941372d5dSJason Gerecke 		return true;
78041372d5dSJason Gerecke 
78141372d5dSJason Gerecke 	/*
78241372d5dSJason Gerecke 	 * Direct-input devices may not be siblings of indirect-input
78341372d5dSJason Gerecke 	 * devices.
78441372d5dSJason Gerecke 	 */
78541372d5dSJason Gerecke 	if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
78641372d5dSJason Gerecke 	    !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
787471d1714SBenjamin Tissoires 		return false;
788471d1714SBenjamin Tissoires 
78941372d5dSJason Gerecke 	/*
79041372d5dSJason Gerecke 	 * Indirect-input devices may not be siblings of direct-input
79141372d5dSJason Gerecke 	 * devices.
79241372d5dSJason Gerecke 	 */
79341372d5dSJason Gerecke 	if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
79441372d5dSJason Gerecke 	    (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
795471d1714SBenjamin Tissoires 		return false;
796471d1714SBenjamin Tissoires 
79741372d5dSJason Gerecke 	/* Pen devices may only be siblings of touch devices */
79841372d5dSJason Gerecke 	if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
79941372d5dSJason Gerecke 	    !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
80041372d5dSJason Gerecke 		return false;
80141372d5dSJason Gerecke 
80241372d5dSJason Gerecke 	/* Touch devices may only be siblings of pen devices */
80341372d5dSJason Gerecke 	if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
80441372d5dSJason Gerecke 	    !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
80541372d5dSJason Gerecke 		return false;
80641372d5dSJason Gerecke 
80741372d5dSJason Gerecke 	/*
80841372d5dSJason Gerecke 	 * No reason could be found for these two devices to NOT be
80941372d5dSJason Gerecke 	 * siblings, so there's a good chance they ARE siblings
81041372d5dSJason Gerecke 	 */
81141372d5dSJason Gerecke 	return true;
812471d1714SBenjamin Tissoires }
813471d1714SBenjamin Tissoires 
wacom_get_hdev_data(struct hid_device * hdev)814471d1714SBenjamin Tissoires static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
815471d1714SBenjamin Tissoires {
816471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
817471d1714SBenjamin Tissoires 
81841372d5dSJason Gerecke 	/* Try to find an already-probed interface from the same device */
81941372d5dSJason Gerecke 	list_for_each_entry(data, &wacom_udev_list, list) {
8201a8861f1SDaniel M. Lambea 		if (hid_compare_device_paths(hdev, data->dev, '/')) {
8212a5e597cSJason Gerecke 			kref_get(&data->kref);
82241372d5dSJason Gerecke 			return data;
82341372d5dSJason Gerecke 		}
8242a5e597cSJason Gerecke 	}
82541372d5dSJason Gerecke 
82641372d5dSJason Gerecke 	/* Fallback to finding devices that appear to be "siblings" */
827471d1714SBenjamin Tissoires 	list_for_each_entry(data, &wacom_udev_list, list) {
828471d1714SBenjamin Tissoires 		if (wacom_are_sibling(hdev, data->dev)) {
829471d1714SBenjamin Tissoires 			kref_get(&data->kref);
830471d1714SBenjamin Tissoires 			return data;
831471d1714SBenjamin Tissoires 		}
832471d1714SBenjamin Tissoires 	}
833471d1714SBenjamin Tissoires 
834471d1714SBenjamin Tissoires 	return NULL;
835471d1714SBenjamin Tissoires }
836471d1714SBenjamin Tissoires 
wacom_release_shared_data(struct kref * kref)8371c817c83SBenjamin Tissoires static void wacom_release_shared_data(struct kref *kref)
8381c817c83SBenjamin Tissoires {
8391c817c83SBenjamin Tissoires 	struct wacom_hdev_data *data =
8401c817c83SBenjamin Tissoires 		container_of(kref, struct wacom_hdev_data, kref);
8411c817c83SBenjamin Tissoires 
8421c817c83SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
8431c817c83SBenjamin Tissoires 	list_del(&data->list);
8441c817c83SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
8451c817c83SBenjamin Tissoires 
8461c817c83SBenjamin Tissoires 	kfree(data);
8471c817c83SBenjamin Tissoires }
8481c817c83SBenjamin Tissoires 
wacom_remove_shared_data(void * res)8491c817c83SBenjamin Tissoires static void wacom_remove_shared_data(void *res)
8501c817c83SBenjamin Tissoires {
8511c817c83SBenjamin Tissoires 	struct wacom *wacom = res;
8521c817c83SBenjamin Tissoires 	struct wacom_hdev_data *data;
8531c817c83SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
8541c817c83SBenjamin Tissoires 
8551c817c83SBenjamin Tissoires 	if (wacom_wac->shared) {
8561c817c83SBenjamin Tissoires 		data = container_of(wacom_wac->shared, struct wacom_hdev_data,
8571c817c83SBenjamin Tissoires 				    shared);
8581c817c83SBenjamin Tissoires 
8591c817c83SBenjamin Tissoires 		if (wacom_wac->shared->touch == wacom->hdev)
8601c817c83SBenjamin Tissoires 			wacom_wac->shared->touch = NULL;
8611c817c83SBenjamin Tissoires 		else if (wacom_wac->shared->pen == wacom->hdev)
8621c817c83SBenjamin Tissoires 			wacom_wac->shared->pen = NULL;
8631c817c83SBenjamin Tissoires 
8641c817c83SBenjamin Tissoires 		kref_put(&data->kref, wacom_release_shared_data);
8651c817c83SBenjamin Tissoires 		wacom_wac->shared = NULL;
8661c817c83SBenjamin Tissoires 	}
8671c817c83SBenjamin Tissoires }
8681c817c83SBenjamin Tissoires 
wacom_add_shared_data(struct hid_device * hdev)869471d1714SBenjamin Tissoires static int wacom_add_shared_data(struct hid_device *hdev)
870471d1714SBenjamin Tissoires {
871471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
872471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
873471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
874471d1714SBenjamin Tissoires 	int retval = 0;
875471d1714SBenjamin Tissoires 
876471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
877471d1714SBenjamin Tissoires 
878471d1714SBenjamin Tissoires 	data = wacom_get_hdev_data(hdev);
879471d1714SBenjamin Tissoires 	if (!data) {
880471d1714SBenjamin Tissoires 		data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
881471d1714SBenjamin Tissoires 		if (!data) {
882b7644592SJason Gerecke 			mutex_unlock(&wacom_udev_list_lock);
883b7644592SJason Gerecke 			return -ENOMEM;
884471d1714SBenjamin Tissoires 		}
885471d1714SBenjamin Tissoires 
886471d1714SBenjamin Tissoires 		kref_init(&data->kref);
887471d1714SBenjamin Tissoires 		data->dev = hdev;
888471d1714SBenjamin Tissoires 		list_add_tail(&data->list, &wacom_udev_list);
889471d1714SBenjamin Tissoires 	}
890471d1714SBenjamin Tissoires 
891b7644592SJason Gerecke 	mutex_unlock(&wacom_udev_list_lock);
892b7644592SJason Gerecke 
893471d1714SBenjamin Tissoires 	wacom_wac->shared = &data->shared;
894471d1714SBenjamin Tissoires 
8953d422a46SCai Huoqing 	retval = devm_add_action_or_reset(&hdev->dev, wacom_remove_shared_data, wacom);
8963d422a46SCai Huoqing 	if (retval)
8971c817c83SBenjamin Tissoires 		return retval;
8981c817c83SBenjamin Tissoires 
899a9ce7856SJason Gerecke 	if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
900a9ce7856SJason Gerecke 		wacom_wac->shared->touch = hdev;
901a9ce7856SJason Gerecke 	else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
902a9ce7856SJason Gerecke 		wacom_wac->shared->pen = hdev;
903a9ce7856SJason Gerecke 
904471d1714SBenjamin Tissoires 	return retval;
905471d1714SBenjamin Tissoires }
906471d1714SBenjamin Tissoires 
wacom_led_control(struct wacom * wacom)907471d1714SBenjamin Tissoires static int wacom_led_control(struct wacom *wacom)
908471d1714SBenjamin Tissoires {
909471d1714SBenjamin Tissoires 	unsigned char *buf;
910471d1714SBenjamin Tissoires 	int retval;
911912ca216SPing Cheng 	unsigned char report_id = WAC_CMD_LED_CONTROL;
912912ca216SPing Cheng 	int buf_size = 9;
913471d1714SBenjamin Tissoires 
914a50aac71SBenjamin Tissoires 	if (!wacom->led.groups)
915a50aac71SBenjamin Tissoires 		return -ENOTSUPP;
916a50aac71SBenjamin Tissoires 
91774aebed6SAaron Armstrong Skomra 	if (wacom->wacom_wac.features.type == REMOTE)
91874aebed6SAaron Armstrong Skomra 		return -ENOTSUPP;
91974aebed6SAaron Armstrong Skomra 
920912ca216SPing Cheng 	if (wacom->wacom_wac.pid) { /* wireless connected */
921912ca216SPing Cheng 		report_id = WAC_CMD_WL_LED_CONTROL;
922912ca216SPing Cheng 		buf_size = 13;
923912ca216SPing Cheng 	}
9244922cd26SJason Gerecke 	else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
9254922cd26SJason Gerecke 		report_id = WAC_CMD_WL_INTUOSP2;
9264922cd26SJason Gerecke 		buf_size = 51;
9274922cd26SJason Gerecke 	}
928912ca216SPing Cheng 	buf = kzalloc(buf_size, GFP_KERNEL);
929471d1714SBenjamin Tissoires 	if (!buf)
930471d1714SBenjamin Tissoires 		return -ENOMEM;
931471d1714SBenjamin Tissoires 
93210c55cacSAaron Armstrong Skomra 	if (wacom->wacom_wac.features.type == HID_GENERIC) {
93310c55cacSAaron Armstrong Skomra 		buf[0] = WAC_CMD_LED_CONTROL_GENERIC;
93410c55cacSAaron Armstrong Skomra 		buf[1] = wacom->led.llv;
93510c55cacSAaron Armstrong Skomra 		buf[2] = wacom->led.groups[0].select & 0x03;
93610c55cacSAaron Armstrong Skomra 
93710c55cacSAaron Armstrong Skomra 	} else if ((wacom->wacom_wac.features.type >= INTUOS5S &&
93810c55cacSAaron Armstrong Skomra 	    wacom->wacom_wac.features.type <= INTUOSPL)) {
939471d1714SBenjamin Tissoires 		/*
940471d1714SBenjamin Tissoires 		 * Touch Ring and crop mark LED luminance may take on
941471d1714SBenjamin Tissoires 		 * one of four values:
942471d1714SBenjamin Tissoires 		 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
943471d1714SBenjamin Tissoires 		 */
944a50aac71SBenjamin Tissoires 		int ring_led = wacom->led.groups[0].select & 0x03;
945471d1714SBenjamin Tissoires 		int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
946471d1714SBenjamin Tissoires 		int crop_lum = 0;
947912ca216SPing Cheng 		unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
948471d1714SBenjamin Tissoires 
949912ca216SPing Cheng 		buf[0] = report_id;
950912ca216SPing Cheng 		if (wacom->wacom_wac.pid) {
951912ca216SPing Cheng 			wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
952912ca216SPing Cheng 					 buf, buf_size, WAC_CMD_RETRIES);
953912ca216SPing Cheng 			buf[0] = report_id;
954912ca216SPing Cheng 			buf[4] = led_bits;
955912ca216SPing Cheng 		} else
956912ca216SPing Cheng 			buf[1] = led_bits;
957471d1714SBenjamin Tissoires 	}
9584922cd26SJason Gerecke 	else if (wacom->wacom_wac.features.type == INTUOSP2_BT) {
9594922cd26SJason Gerecke 		buf[0] = report_id;
9604922cd26SJason Gerecke 		buf[4] = 100; // Power Connection LED (ORANGE)
9614922cd26SJason Gerecke 		buf[5] = 100; // BT Connection LED (BLUE)
9624922cd26SJason Gerecke 		buf[6] = 100; // Paper Mode (RED?)
9634922cd26SJason Gerecke 		buf[7] = 100; // Paper Mode (GREEN?)
9644922cd26SJason Gerecke 		buf[8] = 100; // Paper Mode (BLUE?)
9654922cd26SJason Gerecke 		buf[9] = wacom->led.llv;
9664922cd26SJason Gerecke 		buf[10] = wacom->led.groups[0].select & 0x03;
9674922cd26SJason Gerecke 	}
968471d1714SBenjamin Tissoires 	else {
969a50aac71SBenjamin Tissoires 		int led = wacom->led.groups[0].select | 0x4;
970471d1714SBenjamin Tissoires 
971471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
972471d1714SBenjamin Tissoires 		    wacom->wacom_wac.features.type == WACOM_24HD)
973a50aac71SBenjamin Tissoires 			led |= (wacom->led.groups[1].select << 4) | 0x40;
974471d1714SBenjamin Tissoires 
975912ca216SPing Cheng 		buf[0] = report_id;
976471d1714SBenjamin Tissoires 		buf[1] = led;
977471d1714SBenjamin Tissoires 		buf[2] = wacom->led.llv;
978471d1714SBenjamin Tissoires 		buf[3] = wacom->led.hlv;
979471d1714SBenjamin Tissoires 		buf[4] = wacom->led.img_lum;
980471d1714SBenjamin Tissoires 	}
981471d1714SBenjamin Tissoires 
982912ca216SPing Cheng 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
983296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
984471d1714SBenjamin Tissoires 	kfree(buf);
985471d1714SBenjamin Tissoires 
986471d1714SBenjamin Tissoires 	return retval;
987471d1714SBenjamin Tissoires }
988471d1714SBenjamin Tissoires 
wacom_led_putimage(struct wacom * wacom,int button_id,u8 xfer_id,const unsigned len,const void * img)989849e2f06SBenjamin Tissoires static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
990849e2f06SBenjamin Tissoires 		const unsigned len, const void *img)
991471d1714SBenjamin Tissoires {
992471d1714SBenjamin Tissoires 	unsigned char *buf;
993471d1714SBenjamin Tissoires 	int i, retval;
994849e2f06SBenjamin Tissoires 	const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
995471d1714SBenjamin Tissoires 
996849e2f06SBenjamin Tissoires 	buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
997471d1714SBenjamin Tissoires 	if (!buf)
998471d1714SBenjamin Tissoires 		return -ENOMEM;
999471d1714SBenjamin Tissoires 
1000471d1714SBenjamin Tissoires 	/* Send 'start' command */
1001471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
1002471d1714SBenjamin Tissoires 	buf[1] = 1;
1003296b7378SPrzemo Firszt 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1004296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
1005471d1714SBenjamin Tissoires 	if (retval < 0)
1006471d1714SBenjamin Tissoires 		goto out;
1007471d1714SBenjamin Tissoires 
1008849e2f06SBenjamin Tissoires 	buf[0] = xfer_id;
1009471d1714SBenjamin Tissoires 	buf[1] = button_id & 0x07;
1010471d1714SBenjamin Tissoires 	for (i = 0; i < 4; i++) {
1011471d1714SBenjamin Tissoires 		buf[2] = i;
1012849e2f06SBenjamin Tissoires 		memcpy(buf + 3, img + i * chunk_len, chunk_len);
1013471d1714SBenjamin Tissoires 
1014471d1714SBenjamin Tissoires 		retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
1015296b7378SPrzemo Firszt 					  buf, chunk_len + 3, WAC_CMD_RETRIES);
1016471d1714SBenjamin Tissoires 		if (retval < 0)
1017471d1714SBenjamin Tissoires 			break;
1018471d1714SBenjamin Tissoires 	}
1019471d1714SBenjamin Tissoires 
1020471d1714SBenjamin Tissoires 	/* Send 'stop' */
1021471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
1022471d1714SBenjamin Tissoires 	buf[1] = 0;
1023296b7378SPrzemo Firszt 	wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
1024296b7378SPrzemo Firszt 			 WAC_CMD_RETRIES);
1025471d1714SBenjamin Tissoires 
1026471d1714SBenjamin Tissoires out:
1027471d1714SBenjamin Tissoires 	kfree(buf);
1028471d1714SBenjamin Tissoires 	return retval;
1029471d1714SBenjamin Tissoires }
1030471d1714SBenjamin Tissoires 
wacom_led_select_store(struct device * dev,int set_id,const char * buf,size_t count)1031471d1714SBenjamin Tissoires static ssize_t wacom_led_select_store(struct device *dev, int set_id,
1032471d1714SBenjamin Tissoires 				      const char *buf, size_t count)
1033471d1714SBenjamin Tissoires {
1034ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1035471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1036471d1714SBenjamin Tissoires 	unsigned int id;
1037471d1714SBenjamin Tissoires 	int err;
1038471d1714SBenjamin Tissoires 
1039471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &id);
1040471d1714SBenjamin Tissoires 	if (err)
1041471d1714SBenjamin Tissoires 		return err;
1042471d1714SBenjamin Tissoires 
1043471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
1044471d1714SBenjamin Tissoires 
1045a50aac71SBenjamin Tissoires 	wacom->led.groups[set_id].select = id & 0x3;
1046471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
1047471d1714SBenjamin Tissoires 
1048471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
1049471d1714SBenjamin Tissoires 
1050471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
1051471d1714SBenjamin Tissoires }
1052471d1714SBenjamin Tissoires 
1053471d1714SBenjamin Tissoires #define DEVICE_LED_SELECT_ATTR(SET_ID)					\
1054471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
1055471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
1056471d1714SBenjamin Tissoires {									\
1057471d1714SBenjamin Tissoires 	return wacom_led_select_store(dev, SET_ID, buf, count);		\
1058471d1714SBenjamin Tissoires }									\
1059471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
1060471d1714SBenjamin Tissoires 	struct device_attribute *attr, char *buf)			\
1061471d1714SBenjamin Tissoires {									\
1062ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);\
1063471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
106437449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n",			\
1065a50aac71SBenjamin Tissoires 			 wacom->led.groups[SET_ID].select);		\
1066471d1714SBenjamin Tissoires }									\
1067e0984bc3SPing Cheng static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,	\
1068471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_show,			\
1069471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_store)
1070471d1714SBenjamin Tissoires 
1071471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(0);
1072471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(1);
1073471d1714SBenjamin Tissoires 
wacom_luminance_store(struct wacom * wacom,u8 * dest,const char * buf,size_t count)1074471d1714SBenjamin Tissoires static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
1075471d1714SBenjamin Tissoires 				     const char *buf, size_t count)
1076471d1714SBenjamin Tissoires {
1077471d1714SBenjamin Tissoires 	unsigned int value;
1078471d1714SBenjamin Tissoires 	int err;
1079471d1714SBenjamin Tissoires 
1080471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &value);
1081471d1714SBenjamin Tissoires 	if (err)
1082471d1714SBenjamin Tissoires 		return err;
1083471d1714SBenjamin Tissoires 
1084471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
1085471d1714SBenjamin Tissoires 
1086471d1714SBenjamin Tissoires 	*dest = value & 0x7f;
1087471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
1088471d1714SBenjamin Tissoires 
1089471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
1090471d1714SBenjamin Tissoires 
1091471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
1092471d1714SBenjamin Tissoires }
1093471d1714SBenjamin Tissoires 
1094471d1714SBenjamin Tissoires #define DEVICE_LUMINANCE_ATTR(name, field)				\
1095471d1714SBenjamin Tissoires static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
1096471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
1097471d1714SBenjamin Tissoires {									\
1098ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);\
1099471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
1100471d1714SBenjamin Tissoires 									\
1101471d1714SBenjamin Tissoires 	return wacom_luminance_store(wacom, &wacom->led.field,		\
1102471d1714SBenjamin Tissoires 				     buf, count);			\
1103471d1714SBenjamin Tissoires }									\
110437449adcSPing Cheng static ssize_t wacom_##name##_luminance_show(struct device *dev,	\
110537449adcSPing Cheng 	struct device_attribute *attr, char *buf)			\
110637449adcSPing Cheng {									\
110737449adcSPing Cheng 	struct wacom *wacom = dev_get_drvdata(dev);			\
110837449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);	\
110937449adcSPing Cheng }									\
1110e0984bc3SPing Cheng static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,			\
111137449adcSPing Cheng 		   wacom_##name##_luminance_show,			\
111237449adcSPing Cheng 		   wacom_##name##_luminance_store)
1113471d1714SBenjamin Tissoires 
1114471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status0, llv);
1115471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status1, hlv);
1116471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(buttons, img_lum);
1117471d1714SBenjamin Tissoires 
wacom_button_image_store(struct device * dev,int button_id,const char * buf,size_t count)1118471d1714SBenjamin Tissoires static ssize_t wacom_button_image_store(struct device *dev, int button_id,
1119471d1714SBenjamin Tissoires 					const char *buf, size_t count)
1120471d1714SBenjamin Tissoires {
1121ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1122471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1123471d1714SBenjamin Tissoires 	int err;
1124849e2f06SBenjamin Tissoires 	unsigned len;
1125849e2f06SBenjamin Tissoires 	u8 xfer_id;
1126471d1714SBenjamin Tissoires 
1127849e2f06SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
1128849e2f06SBenjamin Tissoires 		len = 256;
1129849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_BT_XFER;
1130849e2f06SBenjamin Tissoires 	} else {
1131849e2f06SBenjamin Tissoires 		len = 1024;
1132849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_XFER;
1133849e2f06SBenjamin Tissoires 	}
1134849e2f06SBenjamin Tissoires 
1135849e2f06SBenjamin Tissoires 	if (count != len)
1136471d1714SBenjamin Tissoires 		return -EINVAL;
1137471d1714SBenjamin Tissoires 
1138471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
1139471d1714SBenjamin Tissoires 
1140849e2f06SBenjamin Tissoires 	err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
1141471d1714SBenjamin Tissoires 
1142471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
1143471d1714SBenjamin Tissoires 
1144471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
1145471d1714SBenjamin Tissoires }
1146471d1714SBenjamin Tissoires 
1147471d1714SBenjamin Tissoires #define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
1148471d1714SBenjamin Tissoires static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
1149471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
1150471d1714SBenjamin Tissoires {									\
1151471d1714SBenjamin Tissoires 	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
1152471d1714SBenjamin Tissoires }									\
1153e0984bc3SPing Cheng static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,	\
1154471d1714SBenjamin Tissoires 		   NULL, wacom_btnimg##BUTTON_ID##_store)
1155471d1714SBenjamin Tissoires 
1156471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(0);
1157471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(1);
1158471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(2);
1159471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(3);
1160471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(4);
1161471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(5);
1162471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(6);
1163471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(7);
1164471d1714SBenjamin Tissoires 
1165471d1714SBenjamin Tissoires static struct attribute *cintiq_led_attrs[] = {
1166471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
1167471d1714SBenjamin Tissoires 	&dev_attr_status_led1_select.attr,
1168471d1714SBenjamin Tissoires 	NULL
1169471d1714SBenjamin Tissoires };
1170471d1714SBenjamin Tissoires 
11712a6a35a9SRikard Falkeborn static const struct attribute_group cintiq_led_attr_group = {
1172471d1714SBenjamin Tissoires 	.name = "wacom_led",
1173471d1714SBenjamin Tissoires 	.attrs = cintiq_led_attrs,
1174471d1714SBenjamin Tissoires };
1175471d1714SBenjamin Tissoires 
1176471d1714SBenjamin Tissoires static struct attribute *intuos4_led_attrs[] = {
1177471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
1178471d1714SBenjamin Tissoires 	&dev_attr_status1_luminance.attr,
1179471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
1180471d1714SBenjamin Tissoires 	&dev_attr_buttons_luminance.attr,
1181471d1714SBenjamin Tissoires 	&dev_attr_button0_rawimg.attr,
1182471d1714SBenjamin Tissoires 	&dev_attr_button1_rawimg.attr,
1183471d1714SBenjamin Tissoires 	&dev_attr_button2_rawimg.attr,
1184471d1714SBenjamin Tissoires 	&dev_attr_button3_rawimg.attr,
1185471d1714SBenjamin Tissoires 	&dev_attr_button4_rawimg.attr,
1186471d1714SBenjamin Tissoires 	&dev_attr_button5_rawimg.attr,
1187471d1714SBenjamin Tissoires 	&dev_attr_button6_rawimg.attr,
1188471d1714SBenjamin Tissoires 	&dev_attr_button7_rawimg.attr,
1189471d1714SBenjamin Tissoires 	NULL
1190471d1714SBenjamin Tissoires };
1191471d1714SBenjamin Tissoires 
11922a6a35a9SRikard Falkeborn static const struct attribute_group intuos4_led_attr_group = {
1193471d1714SBenjamin Tissoires 	.name = "wacom_led",
1194471d1714SBenjamin Tissoires 	.attrs = intuos4_led_attrs,
1195471d1714SBenjamin Tissoires };
1196471d1714SBenjamin Tissoires 
1197471d1714SBenjamin Tissoires static struct attribute *intuos5_led_attrs[] = {
1198471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
1199471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
1200471d1714SBenjamin Tissoires 	NULL
1201471d1714SBenjamin Tissoires };
1202471d1714SBenjamin Tissoires 
12032a6a35a9SRikard Falkeborn static const struct attribute_group intuos5_led_attr_group = {
1204471d1714SBenjamin Tissoires 	.name = "wacom_led",
1205471d1714SBenjamin Tissoires 	.attrs = intuos5_led_attrs,
1206471d1714SBenjamin Tissoires };
1207471d1714SBenjamin Tissoires 
120810c55cacSAaron Armstrong Skomra static struct attribute *generic_led_attrs[] = {
120910c55cacSAaron Armstrong Skomra 	&dev_attr_status0_luminance.attr,
121010c55cacSAaron Armstrong Skomra 	&dev_attr_status_led0_select.attr,
121110c55cacSAaron Armstrong Skomra 	NULL
121210c55cacSAaron Armstrong Skomra };
121310c55cacSAaron Armstrong Skomra 
12142a6a35a9SRikard Falkeborn static const struct attribute_group generic_led_attr_group = {
121510c55cacSAaron Armstrong Skomra 	.name = "wacom_led",
121610c55cacSAaron Armstrong Skomra 	.attrs = generic_led_attrs,
121710c55cacSAaron Armstrong Skomra };
121810c55cacSAaron Armstrong Skomra 
12192df68a88SBenjamin Tissoires struct wacom_sysfs_group_devres {
12202a6a35a9SRikard Falkeborn 	const struct attribute_group *group;
12212df68a88SBenjamin Tissoires 	struct kobject *root;
12222df68a88SBenjamin Tissoires };
12232df68a88SBenjamin Tissoires 
wacom_devm_sysfs_group_release(struct device * dev,void * res)12242df68a88SBenjamin Tissoires static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
12252df68a88SBenjamin Tissoires {
12262df68a88SBenjamin Tissoires 	struct wacom_sysfs_group_devres *devres = res;
12272df68a88SBenjamin Tissoires 	struct kobject *kobj = devres->root;
12282df68a88SBenjamin Tissoires 
12292df68a88SBenjamin Tissoires 	dev_dbg(dev, "%s: dropping reference to %s\n",
12302df68a88SBenjamin Tissoires 		__func__, devres->group->name);
12312df68a88SBenjamin Tissoires 	sysfs_remove_group(kobj, devres->group);
12322df68a88SBenjamin Tissoires }
12332df68a88SBenjamin Tissoires 
__wacom_devm_sysfs_create_group(struct wacom * wacom,struct kobject * root,const struct attribute_group * group)1234f9036bd4SBenjamin Tissoires static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1235f9036bd4SBenjamin Tissoires 					   struct kobject *root,
12362a6a35a9SRikard Falkeborn 					   const struct attribute_group *group)
12372df68a88SBenjamin Tissoires {
12382df68a88SBenjamin Tissoires 	struct wacom_sysfs_group_devres *devres;
12392df68a88SBenjamin Tissoires 	int error;
12402df68a88SBenjamin Tissoires 
12412df68a88SBenjamin Tissoires 	devres = devres_alloc(wacom_devm_sysfs_group_release,
12422df68a88SBenjamin Tissoires 			      sizeof(struct wacom_sysfs_group_devres),
12432df68a88SBenjamin Tissoires 			      GFP_KERNEL);
12442df68a88SBenjamin Tissoires 	if (!devres)
12452df68a88SBenjamin Tissoires 		return -ENOMEM;
12462df68a88SBenjamin Tissoires 
12472df68a88SBenjamin Tissoires 	devres->group = group;
1248f9036bd4SBenjamin Tissoires 	devres->root = root;
12492df68a88SBenjamin Tissoires 
12502df68a88SBenjamin Tissoires 	error = sysfs_create_group(devres->root, group);
1251097b8f62SArvind Yadav 	if (error) {
1252097b8f62SArvind Yadav 		devres_free(devres);
12532df68a88SBenjamin Tissoires 		return error;
1254097b8f62SArvind Yadav 	}
12552df68a88SBenjamin Tissoires 
12562df68a88SBenjamin Tissoires 	devres_add(&wacom->hdev->dev, devres);
12572df68a88SBenjamin Tissoires 
12582df68a88SBenjamin Tissoires 	return 0;
12592df68a88SBenjamin Tissoires }
12602df68a88SBenjamin Tissoires 
wacom_devm_sysfs_create_group(struct wacom * wacom,const struct attribute_group * group)1261f9036bd4SBenjamin Tissoires static int wacom_devm_sysfs_create_group(struct wacom *wacom,
12622a6a35a9SRikard Falkeborn 					 const struct attribute_group *group)
1263f9036bd4SBenjamin Tissoires {
1264f9036bd4SBenjamin Tissoires 	return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1265f9036bd4SBenjamin Tissoires 					       group);
1266f9036bd4SBenjamin Tissoires }
1267f9036bd4SBenjamin Tissoires 
wacom_devm_kfifo_release(struct device * dev,void * res)126837309f47SPing Cheng static void wacom_devm_kfifo_release(struct device *dev, void *res)
126937309f47SPing Cheng {
127037309f47SPing Cheng 	struct kfifo_rec_ptr_2 *devres = res;
127137309f47SPing Cheng 
127237309f47SPing Cheng 	kfifo_free(devres);
127337309f47SPing Cheng }
127437309f47SPing Cheng 
wacom_devm_kfifo_alloc(struct wacom * wacom)127537309f47SPing Cheng static int wacom_devm_kfifo_alloc(struct wacom *wacom)
127637309f47SPing Cheng {
127737309f47SPing Cheng 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1278179e8e47SJason Gerecke 	struct kfifo_rec_ptr_2 *pen_fifo;
127937309f47SPing Cheng 	int error;
128037309f47SPing Cheng 
128137309f47SPing Cheng 	pen_fifo = devres_alloc(wacom_devm_kfifo_release,
128237309f47SPing Cheng 			      sizeof(struct kfifo_rec_ptr_2),
128337309f47SPing Cheng 			      GFP_KERNEL);
128437309f47SPing Cheng 
128537309f47SPing Cheng 	if (!pen_fifo)
128637309f47SPing Cheng 		return -ENOMEM;
128737309f47SPing Cheng 
128837309f47SPing Cheng 	error = kfifo_alloc(pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
128937309f47SPing Cheng 	if (error) {
129037309f47SPing Cheng 		devres_free(pen_fifo);
129137309f47SPing Cheng 		return error;
129237309f47SPing Cheng 	}
129337309f47SPing Cheng 
129437309f47SPing Cheng 	devres_add(&wacom->hdev->dev, pen_fifo);
1295179e8e47SJason Gerecke 	wacom_wac->pen_fifo = pen_fifo;
129637309f47SPing Cheng 
129737309f47SPing Cheng 	return 0;
129837309f47SPing Cheng }
129937309f47SPing Cheng 
wacom_leds_brightness_get(struct wacom_led * led)130034736aa9SBenjamin Tissoires enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
130197f5541fSBenjamin Tissoires {
130297f5541fSBenjamin Tissoires 	struct wacom *wacom = led->wacom;
130397f5541fSBenjamin Tissoires 
130497f5541fSBenjamin Tissoires 	if (wacom->led.max_hlv)
130597f5541fSBenjamin Tissoires 		return led->hlv * LED_FULL / wacom->led.max_hlv;
130697f5541fSBenjamin Tissoires 
130797f5541fSBenjamin Tissoires 	if (wacom->led.max_llv)
130897f5541fSBenjamin Tissoires 		return led->llv * LED_FULL / wacom->led.max_llv;
130997f5541fSBenjamin Tissoires 
131097f5541fSBenjamin Tissoires 	/* device doesn't support brightness tuning */
131197f5541fSBenjamin Tissoires 	return LED_FULL;
131297f5541fSBenjamin Tissoires }
131397f5541fSBenjamin Tissoires 
__wacom_led_brightness_get(struct led_classdev * cdev)131497f5541fSBenjamin Tissoires static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
131597f5541fSBenjamin Tissoires {
131697f5541fSBenjamin Tissoires 	struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
131797f5541fSBenjamin Tissoires 	struct wacom *wacom = led->wacom;
131897f5541fSBenjamin Tissoires 
131997f5541fSBenjamin Tissoires 	if (wacom->led.groups[led->group].select != led->id)
132097f5541fSBenjamin Tissoires 		return LED_OFF;
132197f5541fSBenjamin Tissoires 
132297f5541fSBenjamin Tissoires 	return wacom_leds_brightness_get(led);
132397f5541fSBenjamin Tissoires }
132497f5541fSBenjamin Tissoires 
wacom_led_brightness_set(struct led_classdev * cdev,enum led_brightness brightness)132597f5541fSBenjamin Tissoires static int wacom_led_brightness_set(struct led_classdev *cdev,
132697f5541fSBenjamin Tissoires 				    enum led_brightness brightness)
132797f5541fSBenjamin Tissoires {
132897f5541fSBenjamin Tissoires 	struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
132997f5541fSBenjamin Tissoires 	struct wacom *wacom = led->wacom;
133097f5541fSBenjamin Tissoires 	int error;
133197f5541fSBenjamin Tissoires 
133297f5541fSBenjamin Tissoires 	mutex_lock(&wacom->lock);
133397f5541fSBenjamin Tissoires 
133497f5541fSBenjamin Tissoires 	if (!wacom->led.groups || (brightness == LED_OFF &&
133597f5541fSBenjamin Tissoires 	    wacom->led.groups[led->group].select != led->id)) {
133697f5541fSBenjamin Tissoires 		error = 0;
133797f5541fSBenjamin Tissoires 		goto out;
133897f5541fSBenjamin Tissoires 	}
133997f5541fSBenjamin Tissoires 
134097f5541fSBenjamin Tissoires 	led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
134197f5541fSBenjamin Tissoires 	led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
134297f5541fSBenjamin Tissoires 
134397f5541fSBenjamin Tissoires 	wacom->led.groups[led->group].select = led->id;
134497f5541fSBenjamin Tissoires 
134597f5541fSBenjamin Tissoires 	error = wacom_led_control(wacom);
134697f5541fSBenjamin Tissoires 
134797f5541fSBenjamin Tissoires out:
134897f5541fSBenjamin Tissoires 	mutex_unlock(&wacom->lock);
134997f5541fSBenjamin Tissoires 
135097f5541fSBenjamin Tissoires 	return error;
135197f5541fSBenjamin Tissoires }
135297f5541fSBenjamin Tissoires 
wacom_led_readonly_brightness_set(struct led_classdev * cdev,enum led_brightness brightness)135397f5541fSBenjamin Tissoires static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
135497f5541fSBenjamin Tissoires 					       enum led_brightness brightness)
135597f5541fSBenjamin Tissoires {
135697f5541fSBenjamin Tissoires }
135797f5541fSBenjamin Tissoires 
wacom_led_register_one(struct device * dev,struct wacom * wacom,struct wacom_led * led,unsigned int group,unsigned int id,bool read_only)135897f5541fSBenjamin Tissoires static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
135997f5541fSBenjamin Tissoires 				  struct wacom_led *led, unsigned int group,
136097f5541fSBenjamin Tissoires 				  unsigned int id, bool read_only)
136197f5541fSBenjamin Tissoires {
136297f5541fSBenjamin Tissoires 	int error;
136397f5541fSBenjamin Tissoires 	char *name;
136497f5541fSBenjamin Tissoires 
136597f5541fSBenjamin Tissoires 	name = devm_kasprintf(dev, GFP_KERNEL,
136697f5541fSBenjamin Tissoires 			      "%s::wacom-%d.%d",
136797f5541fSBenjamin Tissoires 			      dev_name(dev),
136897f5541fSBenjamin Tissoires 			      group,
136997f5541fSBenjamin Tissoires 			      id);
137097f5541fSBenjamin Tissoires 	if (!name)
137197f5541fSBenjamin Tissoires 		return -ENOMEM;
137297f5541fSBenjamin Tissoires 
137334736aa9SBenjamin Tissoires 	if (!read_only) {
137434736aa9SBenjamin Tissoires 		led->trigger.name = name;
137534736aa9SBenjamin Tissoires 		error = devm_led_trigger_register(dev, &led->trigger);
137634736aa9SBenjamin Tissoires 		if (error) {
137734736aa9SBenjamin Tissoires 			hid_err(wacom->hdev,
137834736aa9SBenjamin Tissoires 				"failed to register LED trigger %s: %d\n",
137934736aa9SBenjamin Tissoires 				led->cdev.name, error);
138034736aa9SBenjamin Tissoires 			return error;
138134736aa9SBenjamin Tissoires 		}
138234736aa9SBenjamin Tissoires 	}
138334736aa9SBenjamin Tissoires 
138497f5541fSBenjamin Tissoires 	led->group = group;
138597f5541fSBenjamin Tissoires 	led->id = id;
138697f5541fSBenjamin Tissoires 	led->wacom = wacom;
138797f5541fSBenjamin Tissoires 	led->llv = wacom->led.llv;
138897f5541fSBenjamin Tissoires 	led->hlv = wacom->led.hlv;
138997f5541fSBenjamin Tissoires 	led->cdev.name = name;
139097f5541fSBenjamin Tissoires 	led->cdev.max_brightness = LED_FULL;
139197f5541fSBenjamin Tissoires 	led->cdev.flags = LED_HW_PLUGGABLE;
139297f5541fSBenjamin Tissoires 	led->cdev.brightness_get = __wacom_led_brightness_get;
139334736aa9SBenjamin Tissoires 	if (!read_only) {
139497f5541fSBenjamin Tissoires 		led->cdev.brightness_set_blocking = wacom_led_brightness_set;
139534736aa9SBenjamin Tissoires 		led->cdev.default_trigger = led->cdev.name;
139634736aa9SBenjamin Tissoires 	} else {
139797f5541fSBenjamin Tissoires 		led->cdev.brightness_set = wacom_led_readonly_brightness_set;
139834736aa9SBenjamin Tissoires 	}
139997f5541fSBenjamin Tissoires 
140097f5541fSBenjamin Tissoires 	error = devm_led_classdev_register(dev, &led->cdev);
140197f5541fSBenjamin Tissoires 	if (error) {
140297f5541fSBenjamin Tissoires 		hid_err(wacom->hdev,
140397f5541fSBenjamin Tissoires 			"failed to register LED %s: %d\n",
140497f5541fSBenjamin Tissoires 			led->cdev.name, error);
140597f5541fSBenjamin Tissoires 		led->cdev.name = NULL;
140697f5541fSBenjamin Tissoires 		return error;
140797f5541fSBenjamin Tissoires 	}
140897f5541fSBenjamin Tissoires 
140997f5541fSBenjamin Tissoires 	return 0;
141097f5541fSBenjamin Tissoires }
141197f5541fSBenjamin Tissoires 
wacom_led_groups_release_one(void * data)1412589e5060SBenjamin Tissoires static void wacom_led_groups_release_one(void *data)
1413589e5060SBenjamin Tissoires {
1414589e5060SBenjamin Tissoires 	struct wacom_group_leds *group = data;
1415589e5060SBenjamin Tissoires 
1416589e5060SBenjamin Tissoires 	devres_release_group(group->dev, group);
1417589e5060SBenjamin Tissoires }
1418589e5060SBenjamin Tissoires 
wacom_led_groups_alloc_and_register_one(struct device * dev,struct wacom * wacom,int group_id,int count,bool read_only)141997f5541fSBenjamin Tissoires static int wacom_led_groups_alloc_and_register_one(struct device *dev,
142097f5541fSBenjamin Tissoires 						   struct wacom *wacom,
142197f5541fSBenjamin Tissoires 						   int group_id, int count,
142297f5541fSBenjamin Tissoires 						   bool read_only)
142397f5541fSBenjamin Tissoires {
142497f5541fSBenjamin Tissoires 	struct wacom_led *leds;
142597f5541fSBenjamin Tissoires 	int i, error;
142697f5541fSBenjamin Tissoires 
142797f5541fSBenjamin Tissoires 	if (group_id >= wacom->led.count || count <= 0)
142897f5541fSBenjamin Tissoires 		return -EINVAL;
142997f5541fSBenjamin Tissoires 
143097f5541fSBenjamin Tissoires 	if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
143197f5541fSBenjamin Tissoires 		return -ENOMEM;
143297f5541fSBenjamin Tissoires 
1433a86854d0SKees Cook 	leds = devm_kcalloc(dev, count, sizeof(struct wacom_led), GFP_KERNEL);
143497f5541fSBenjamin Tissoires 	if (!leds) {
143597f5541fSBenjamin Tissoires 		error = -ENOMEM;
143697f5541fSBenjamin Tissoires 		goto err;
143797f5541fSBenjamin Tissoires 	}
143897f5541fSBenjamin Tissoires 
143997f5541fSBenjamin Tissoires 	wacom->led.groups[group_id].leds = leds;
144097f5541fSBenjamin Tissoires 	wacom->led.groups[group_id].count = count;
144197f5541fSBenjamin Tissoires 
144297f5541fSBenjamin Tissoires 	for (i = 0; i < count; i++) {
144397f5541fSBenjamin Tissoires 		error = wacom_led_register_one(dev, wacom, &leds[i],
144497f5541fSBenjamin Tissoires 					       group_id, i, read_only);
144597f5541fSBenjamin Tissoires 		if (error)
144697f5541fSBenjamin Tissoires 			goto err;
144797f5541fSBenjamin Tissoires 	}
144897f5541fSBenjamin Tissoires 
1449589e5060SBenjamin Tissoires 	wacom->led.groups[group_id].dev = dev;
1450589e5060SBenjamin Tissoires 
1451589e5060SBenjamin Tissoires 	devres_close_group(dev, &wacom->led.groups[group_id]);
1452589e5060SBenjamin Tissoires 
1453589e5060SBenjamin Tissoires 	/*
1454589e5060SBenjamin Tissoires 	 * There is a bug (?) in devm_led_classdev_register() in which its
1455589e5060SBenjamin Tissoires 	 * increments the refcount of the parent. If the parent is an input
1456589e5060SBenjamin Tissoires 	 * device, that means the ref count never reaches 0 when
1457589e5060SBenjamin Tissoires 	 * devm_input_device_release() gets called.
1458589e5060SBenjamin Tissoires 	 * This means that the LEDs are still there after disconnect.
1459589e5060SBenjamin Tissoires 	 * Manually force the release of the group so that the leds are released
1460589e5060SBenjamin Tissoires 	 * once we are done using them.
1461589e5060SBenjamin Tissoires 	 */
1462589e5060SBenjamin Tissoires 	error = devm_add_action_or_reset(&wacom->hdev->dev,
1463589e5060SBenjamin Tissoires 					 wacom_led_groups_release_one,
1464589e5060SBenjamin Tissoires 					 &wacom->led.groups[group_id]);
1465589e5060SBenjamin Tissoires 	if (error)
1466589e5060SBenjamin Tissoires 		return error;
1467589e5060SBenjamin Tissoires 
146897f5541fSBenjamin Tissoires 	return 0;
146997f5541fSBenjamin Tissoires 
147097f5541fSBenjamin Tissoires err:
147197f5541fSBenjamin Tissoires 	devres_release_group(dev, &wacom->led.groups[group_id]);
147297f5541fSBenjamin Tissoires 	return error;
147397f5541fSBenjamin Tissoires }
147497f5541fSBenjamin Tissoires 
wacom_led_find(struct wacom * wacom,unsigned int group_id,unsigned int id)147534736aa9SBenjamin Tissoires struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
147634736aa9SBenjamin Tissoires 				 unsigned int id)
147734736aa9SBenjamin Tissoires {
147834736aa9SBenjamin Tissoires 	struct wacom_group_leds *group;
147934736aa9SBenjamin Tissoires 
148034736aa9SBenjamin Tissoires 	if (group_id >= wacom->led.count)
148134736aa9SBenjamin Tissoires 		return NULL;
148234736aa9SBenjamin Tissoires 
148334736aa9SBenjamin Tissoires 	group = &wacom->led.groups[group_id];
148434736aa9SBenjamin Tissoires 
148534736aa9SBenjamin Tissoires 	if (!group->leds)
148634736aa9SBenjamin Tissoires 		return NULL;
148734736aa9SBenjamin Tissoires 
148834736aa9SBenjamin Tissoires 	id %= group->count;
148934736aa9SBenjamin Tissoires 
149034736aa9SBenjamin Tissoires 	return &group->leds[id];
149134736aa9SBenjamin Tissoires }
149234736aa9SBenjamin Tissoires 
14931784cd57SLee Jones /*
149434736aa9SBenjamin Tissoires  * wacom_led_next: gives the next available led with a wacom trigger.
149534736aa9SBenjamin Tissoires  *
149634736aa9SBenjamin Tissoires  * returns the next available struct wacom_led which has its default trigger
149734736aa9SBenjamin Tissoires  * or the current one if none is available.
149834736aa9SBenjamin Tissoires  */
wacom_led_next(struct wacom * wacom,struct wacom_led * cur)149934736aa9SBenjamin Tissoires struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
150034736aa9SBenjamin Tissoires {
150134736aa9SBenjamin Tissoires 	struct wacom_led *next_led;
150234736aa9SBenjamin Tissoires 	int group, next;
150334736aa9SBenjamin Tissoires 
150434736aa9SBenjamin Tissoires 	if (!wacom || !cur)
150534736aa9SBenjamin Tissoires 		return NULL;
150634736aa9SBenjamin Tissoires 
150734736aa9SBenjamin Tissoires 	group = cur->group;
150834736aa9SBenjamin Tissoires 	next = cur->id;
150934736aa9SBenjamin Tissoires 
151034736aa9SBenjamin Tissoires 	do {
151134736aa9SBenjamin Tissoires 		next_led = wacom_led_find(wacom, group, ++next);
151234736aa9SBenjamin Tissoires 		if (!next_led || next_led == cur)
151334736aa9SBenjamin Tissoires 			return next_led;
151434736aa9SBenjamin Tissoires 	} while (next_led->cdev.trigger != &next_led->trigger);
151534736aa9SBenjamin Tissoires 
151634736aa9SBenjamin Tissoires 	return next_led;
151734736aa9SBenjamin Tissoires }
151834736aa9SBenjamin Tissoires 
wacom_led_groups_release(void * data)1519a50aac71SBenjamin Tissoires static void wacom_led_groups_release(void *data)
1520a50aac71SBenjamin Tissoires {
1521a50aac71SBenjamin Tissoires 	struct wacom *wacom = data;
1522a50aac71SBenjamin Tissoires 
1523a50aac71SBenjamin Tissoires 	wacom->led.groups = NULL;
152497f5541fSBenjamin Tissoires 	wacom->led.count = 0;
1525a50aac71SBenjamin Tissoires }
1526a50aac71SBenjamin Tissoires 
wacom_led_groups_allocate(struct wacom * wacom,int count)1527a50aac71SBenjamin Tissoires static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1528a50aac71SBenjamin Tissoires {
152997f5541fSBenjamin Tissoires 	struct device *dev = &wacom->hdev->dev;
1530a50aac71SBenjamin Tissoires 	struct wacom_group_leds *groups;
1531a50aac71SBenjamin Tissoires 	int error;
1532a50aac71SBenjamin Tissoires 
1533a86854d0SKees Cook 	groups = devm_kcalloc(dev, count, sizeof(struct wacom_group_leds),
1534a50aac71SBenjamin Tissoires 			      GFP_KERNEL);
1535a50aac71SBenjamin Tissoires 	if (!groups)
1536a50aac71SBenjamin Tissoires 		return -ENOMEM;
1537a50aac71SBenjamin Tissoires 
153897f5541fSBenjamin Tissoires 	error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1539a50aac71SBenjamin Tissoires 	if (error)
1540a50aac71SBenjamin Tissoires 		return error;
1541a50aac71SBenjamin Tissoires 
1542a50aac71SBenjamin Tissoires 	wacom->led.groups = groups;
154397f5541fSBenjamin Tissoires 	wacom->led.count = count;
154497f5541fSBenjamin Tissoires 
154597f5541fSBenjamin Tissoires 	return 0;
154697f5541fSBenjamin Tissoires }
154797f5541fSBenjamin Tissoires 
wacom_leds_alloc_and_register(struct wacom * wacom,int group_count,int led_per_group,bool read_only)154897f5541fSBenjamin Tissoires static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
154997f5541fSBenjamin Tissoires 					 int led_per_group, bool read_only)
155097f5541fSBenjamin Tissoires {
155197f5541fSBenjamin Tissoires 	struct device *dev;
155297f5541fSBenjamin Tissoires 	int i, error;
155397f5541fSBenjamin Tissoires 
155497f5541fSBenjamin Tissoires 	if (!wacom->wacom_wac.pad_input)
155597f5541fSBenjamin Tissoires 		return -EINVAL;
155697f5541fSBenjamin Tissoires 
155797f5541fSBenjamin Tissoires 	dev = &wacom->wacom_wac.pad_input->dev;
155897f5541fSBenjamin Tissoires 
155997f5541fSBenjamin Tissoires 	error = wacom_led_groups_allocate(wacom, group_count);
156097f5541fSBenjamin Tissoires 	if (error)
156197f5541fSBenjamin Tissoires 		return error;
156297f5541fSBenjamin Tissoires 
156397f5541fSBenjamin Tissoires 	for (i = 0; i < group_count; i++) {
156497f5541fSBenjamin Tissoires 		error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
156597f5541fSBenjamin Tissoires 								led_per_group,
156697f5541fSBenjamin Tissoires 								read_only);
156797f5541fSBenjamin Tissoires 		if (error)
156897f5541fSBenjamin Tissoires 			return error;
156997f5541fSBenjamin Tissoires 	}
1570a50aac71SBenjamin Tissoires 
1571a50aac71SBenjamin Tissoires 	return 0;
1572a50aac71SBenjamin Tissoires }
1573a50aac71SBenjamin Tissoires 
wacom_initialize_leds(struct wacom * wacom)157410c55cacSAaron Armstrong Skomra int wacom_initialize_leds(struct wacom *wacom)
1575471d1714SBenjamin Tissoires {
1576471d1714SBenjamin Tissoires 	int error;
1577471d1714SBenjamin Tissoires 
1578862cf553SJason Gerecke 	if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1579862cf553SJason Gerecke 		return 0;
1580862cf553SJason Gerecke 
1581471d1714SBenjamin Tissoires 	/* Initialize default values */
1582471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
158310c55cacSAaron Armstrong Skomra 	case HID_GENERIC:
158410c55cacSAaron Armstrong Skomra 		if (!wacom->generic_has_leds)
158510c55cacSAaron Armstrong Skomra 			return 0;
158610c55cacSAaron Armstrong Skomra 		wacom->led.llv = 100;
158710c55cacSAaron Armstrong Skomra 		wacom->led.max_llv = 100;
158810c55cacSAaron Armstrong Skomra 
158910c55cacSAaron Armstrong Skomra 		error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
159010c55cacSAaron Armstrong Skomra 		if (error) {
159110c55cacSAaron Armstrong Skomra 			hid_err(wacom->hdev,
159210c55cacSAaron Armstrong Skomra 				"cannot create leds err: %d\n", error);
159310c55cacSAaron Armstrong Skomra 			return error;
159410c55cacSAaron Armstrong Skomra 		}
159510c55cacSAaron Armstrong Skomra 
159610c55cacSAaron Armstrong Skomra 		error = wacom_devm_sysfs_create_group(wacom,
159710c55cacSAaron Armstrong Skomra 						      &generic_led_attr_group);
159810c55cacSAaron Armstrong Skomra 		break;
159910c55cacSAaron Armstrong Skomra 
1600471d1714SBenjamin Tissoires 	case INTUOS4S:
1601471d1714SBenjamin Tissoires 	case INTUOS4:
160281af7e61SBenjamin Tissoires 	case INTUOS4WL:
1603471d1714SBenjamin Tissoires 	case INTUOS4L:
1604471d1714SBenjamin Tissoires 		wacom->led.llv = 10;
1605471d1714SBenjamin Tissoires 		wacom->led.hlv = 20;
160697f5541fSBenjamin Tissoires 		wacom->led.max_llv = 127;
160797f5541fSBenjamin Tissoires 		wacom->led.max_hlv = 127;
1608471d1714SBenjamin Tissoires 		wacom->led.img_lum = 10;
1609a50aac71SBenjamin Tissoires 
161097f5541fSBenjamin Tissoires 		error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1611a50aac71SBenjamin Tissoires 		if (error) {
1612a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1613a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1614a50aac71SBenjamin Tissoires 			return error;
1615a50aac71SBenjamin Tissoires 		}
1616a50aac71SBenjamin Tissoires 
16172df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
1618471d1714SBenjamin Tissoires 						      &intuos4_led_attr_group);
1619471d1714SBenjamin Tissoires 		break;
1620471d1714SBenjamin Tissoires 
1621471d1714SBenjamin Tissoires 	case WACOM_24HD:
1622471d1714SBenjamin Tissoires 	case WACOM_21UX2:
1623471d1714SBenjamin Tissoires 		wacom->led.llv = 0;
1624471d1714SBenjamin Tissoires 		wacom->led.hlv = 0;
1625471d1714SBenjamin Tissoires 		wacom->led.img_lum = 0;
1626471d1714SBenjamin Tissoires 
162797f5541fSBenjamin Tissoires 		error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1628a50aac71SBenjamin Tissoires 		if (error) {
1629a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1630a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1631a50aac71SBenjamin Tissoires 			return error;
1632a50aac71SBenjamin Tissoires 		}
1633a50aac71SBenjamin Tissoires 
16342df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
1635471d1714SBenjamin Tissoires 						      &cintiq_led_attr_group);
1636471d1714SBenjamin Tissoires 		break;
1637471d1714SBenjamin Tissoires 
1638471d1714SBenjamin Tissoires 	case INTUOS5S:
1639471d1714SBenjamin Tissoires 	case INTUOS5:
1640471d1714SBenjamin Tissoires 	case INTUOS5L:
1641471d1714SBenjamin Tissoires 	case INTUOSPS:
1642471d1714SBenjamin Tissoires 	case INTUOSPM:
1643471d1714SBenjamin Tissoires 	case INTUOSPL:
1644471d1714SBenjamin Tissoires 		wacom->led.llv = 32;
164597f5541fSBenjamin Tissoires 		wacom->led.max_llv = 96;
1646471d1714SBenjamin Tissoires 
164797f5541fSBenjamin Tissoires 		error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1648a50aac71SBenjamin Tissoires 		if (error) {
1649a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1650a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1651a50aac71SBenjamin Tissoires 			return error;
1652a50aac71SBenjamin Tissoires 		}
1653a50aac71SBenjamin Tissoires 
16542df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
1655471d1714SBenjamin Tissoires 						      &intuos5_led_attr_group);
1656471d1714SBenjamin Tissoires 		break;
1657471d1714SBenjamin Tissoires 
16584922cd26SJason Gerecke 	case INTUOSP2_BT:
16594922cd26SJason Gerecke 		wacom->led.llv = 50;
16604922cd26SJason Gerecke 		wacom->led.max_llv = 100;
16614922cd26SJason Gerecke 		error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
16624922cd26SJason Gerecke 		if (error) {
16634922cd26SJason Gerecke 			hid_err(wacom->hdev,
16644922cd26SJason Gerecke 				"cannot create leds err: %d\n", error);
16654922cd26SJason Gerecke 			return error;
16664922cd26SJason Gerecke 		}
16674922cd26SJason Gerecke 		return 0;
16684922cd26SJason Gerecke 
1669a50aac71SBenjamin Tissoires 	case REMOTE:
167097f5541fSBenjamin Tissoires 		wacom->led.llv = 255;
167197f5541fSBenjamin Tissoires 		wacom->led.max_llv = 255;
1672a50aac71SBenjamin Tissoires 		error = wacom_led_groups_allocate(wacom, 5);
1673a50aac71SBenjamin Tissoires 		if (error) {
1674a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1675a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1676a50aac71SBenjamin Tissoires 			return error;
1677a50aac71SBenjamin Tissoires 		}
1678a50aac71SBenjamin Tissoires 		return 0;
1679a50aac71SBenjamin Tissoires 
1680471d1714SBenjamin Tissoires 	default:
1681471d1714SBenjamin Tissoires 		return 0;
1682471d1714SBenjamin Tissoires 	}
1683471d1714SBenjamin Tissoires 
1684471d1714SBenjamin Tissoires 	if (error) {
1685471d1714SBenjamin Tissoires 		hid_err(wacom->hdev,
1686471d1714SBenjamin Tissoires 			"cannot create sysfs group err: %d\n", error);
1687471d1714SBenjamin Tissoires 		return error;
1688471d1714SBenjamin Tissoires 	}
1689471d1714SBenjamin Tissoires 
1690471d1714SBenjamin Tissoires 	return 0;
1691471d1714SBenjamin Tissoires }
1692471d1714SBenjamin Tissoires 
wacom_init_work(struct work_struct * work)1693a544c619SBenjamin Tissoires static void wacom_init_work(struct work_struct *work)
1694a544c619SBenjamin Tissoires {
1695a544c619SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, init_work.work);
1696a544c619SBenjamin Tissoires 
1697a544c619SBenjamin Tissoires 	_wacom_query_tablet_data(wacom);
1698a544c619SBenjamin Tissoires 	wacom_led_control(wacom);
1699a544c619SBenjamin Tissoires }
1700a544c619SBenjamin Tissoires 
wacom_query_tablet_data(struct wacom * wacom)1701a544c619SBenjamin Tissoires static void wacom_query_tablet_data(struct wacom *wacom)
1702a544c619SBenjamin Tissoires {
1703a544c619SBenjamin Tissoires 	schedule_delayed_work(&wacom->init_work, msecs_to_jiffies(1000));
1704a544c619SBenjamin Tissoires }
1705a544c619SBenjamin Tissoires 
1706471d1714SBenjamin Tissoires static enum power_supply_property wacom_battery_props[] = {
17079956953eSBenjamin Tissoires 	POWER_SUPPLY_PROP_MODEL_NAME,
170871fa641eSJason Gerecke 	POWER_SUPPLY_PROP_PRESENT,
1709ac8d1010SBenjamin Tissoires 	POWER_SUPPLY_PROP_STATUS,
1710471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
1711471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_CAPACITY
1712471d1714SBenjamin Tissoires };
1713471d1714SBenjamin Tissoires 
wacom_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)1714471d1714SBenjamin Tissoires static int wacom_battery_get_property(struct power_supply *psy,
1715471d1714SBenjamin Tissoires 				      enum power_supply_property psp,
1716471d1714SBenjamin Tissoires 				      union power_supply_propval *val)
1717471d1714SBenjamin Tissoires {
171859d69bc8SBenjamin Tissoires 	struct wacom_battery *battery = power_supply_get_drvdata(psy);
1719471d1714SBenjamin Tissoires 	int ret = 0;
1720471d1714SBenjamin Tissoires 
1721471d1714SBenjamin Tissoires 	switch (psp) {
17229956953eSBenjamin Tissoires 		case POWER_SUPPLY_PROP_MODEL_NAME:
17239956953eSBenjamin Tissoires 			val->strval = battery->wacom->wacom_wac.name;
17249956953eSBenjamin Tissoires 			break;
172571fa641eSJason Gerecke 		case POWER_SUPPLY_PROP_PRESENT:
172659d69bc8SBenjamin Tissoires 			val->intval = battery->bat_connected;
172771fa641eSJason Gerecke 			break;
1728471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_SCOPE:
1729471d1714SBenjamin Tissoires 			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1730471d1714SBenjamin Tissoires 			break;
1731471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_CAPACITY:
173259d69bc8SBenjamin Tissoires 			val->intval = battery->battery_capacity;
1733ac8d1010SBenjamin Tissoires 			break;
1734ac8d1010SBenjamin Tissoires 		case POWER_SUPPLY_PROP_STATUS:
173516e45989SJason Gerecke 			if (battery->bat_status != WACOM_POWER_SUPPLY_STATUS_AUTO)
173616e45989SJason Gerecke 				val->intval = battery->bat_status;
173716e45989SJason Gerecke 			else if (battery->bat_charging)
1738ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_CHARGING;
173959d69bc8SBenjamin Tissoires 			else if (battery->battery_capacity == 100 &&
174059d69bc8SBenjamin Tissoires 				    battery->ps_connected)
1741ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_FULL;
174259d69bc8SBenjamin Tissoires 			else if (battery->ps_connected)
1743b0882cb7SJason Gerecke 				val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1744ac8d1010SBenjamin Tissoires 			else
1745ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1746471d1714SBenjamin Tissoires 			break;
1747471d1714SBenjamin Tissoires 		default:
1748471d1714SBenjamin Tissoires 			ret = -EINVAL;
1749471d1714SBenjamin Tissoires 			break;
1750471d1714SBenjamin Tissoires 	}
1751471d1714SBenjamin Tissoires 
1752471d1714SBenjamin Tissoires 	return ret;
1753471d1714SBenjamin Tissoires }
1754471d1714SBenjamin Tissoires 
__wacom_initialize_battery(struct wacom * wacom,struct wacom_battery * battery)175559d69bc8SBenjamin Tissoires static int __wacom_initialize_battery(struct wacom *wacom,
175659d69bc8SBenjamin Tissoires 				      struct wacom_battery *battery)
1757471d1714SBenjamin Tissoires {
1758d70420b9SBenjamin Tissoires 	static atomic_t battery_no = ATOMIC_INIT(0);
1759b189da90SBenjamin Tissoires 	struct device *dev = &wacom->hdev->dev;
176059d69bc8SBenjamin Tissoires 	struct power_supply_config psy_cfg = { .drv_data = battery, };
1761136ae5e9SBenjamin Tissoires 	struct power_supply *ps_bat;
176259d69bc8SBenjamin Tissoires 	struct power_supply_desc *bat_desc = &battery->bat_desc;
1763d70420b9SBenjamin Tissoires 	unsigned long n;
1764b189da90SBenjamin Tissoires 	int error;
1765b189da90SBenjamin Tissoires 
1766b189da90SBenjamin Tissoires 	if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1767b189da90SBenjamin Tissoires 		return -ENOMEM;
1768471d1714SBenjamin Tissoires 
17699956953eSBenjamin Tissoires 	battery->wacom = wacom;
17709956953eSBenjamin Tissoires 
1771d70420b9SBenjamin Tissoires 	n = atomic_inc_return(&battery_no) - 1;
17727dbd229eSBenjamin Tissoires 
1773297d716fSKrzysztof Kozlowski 	bat_desc->properties = wacom_battery_props;
1774297d716fSKrzysztof Kozlowski 	bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1775297d716fSKrzysztof Kozlowski 	bat_desc->get_property = wacom_battery_get_property;
177659d69bc8SBenjamin Tissoires 	sprintf(battery->bat_name, "wacom_battery_%ld", n);
177759d69bc8SBenjamin Tissoires 	bat_desc->name = battery->bat_name;
17786ff1cae2SBastien Nocera 	bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1779297d716fSKrzysztof Kozlowski 	bat_desc->use_for_apm = 0;
1780471d1714SBenjamin Tissoires 
178159d69bc8SBenjamin Tissoires 	ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
178259d69bc8SBenjamin Tissoires 	if (IS_ERR(ps_bat)) {
178359d69bc8SBenjamin Tissoires 		error = PTR_ERR(ps_bat);
1784b189da90SBenjamin Tissoires 		goto err;
1785b189da90SBenjamin Tissoires 	}
1786471d1714SBenjamin Tissoires 
178759d69bc8SBenjamin Tissoires 	power_supply_powers(ps_bat, &wacom->hdev->dev);
178859d69bc8SBenjamin Tissoires 
178959d69bc8SBenjamin Tissoires 	battery->battery = ps_bat;
17907dbd229eSBenjamin Tissoires 
1791b189da90SBenjamin Tissoires 	devres_close_group(dev, bat_desc);
17927dbd229eSBenjamin Tissoires 	return 0;
1793b189da90SBenjamin Tissoires 
1794b189da90SBenjamin Tissoires err:
1795b189da90SBenjamin Tissoires 	devres_release_group(dev, bat_desc);
1796b189da90SBenjamin Tissoires 	return error;
1797471d1714SBenjamin Tissoires }
1798471d1714SBenjamin Tissoires 
wacom_initialize_battery(struct wacom * wacom)179959d69bc8SBenjamin Tissoires static int wacom_initialize_battery(struct wacom *wacom)
180059d69bc8SBenjamin Tissoires {
180159d69bc8SBenjamin Tissoires 	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
180259d69bc8SBenjamin Tissoires 		return __wacom_initialize_battery(wacom, &wacom->battery);
180359d69bc8SBenjamin Tissoires 
180459d69bc8SBenjamin Tissoires 	return 0;
180559d69bc8SBenjamin Tissoires }
180659d69bc8SBenjamin Tissoires 
wacom_destroy_battery(struct wacom * wacom)1807471d1714SBenjamin Tissoires static void wacom_destroy_battery(struct wacom *wacom)
1808471d1714SBenjamin Tissoires {
180959d69bc8SBenjamin Tissoires 	if (wacom->battery.battery) {
181059d69bc8SBenjamin Tissoires 		devres_release_group(&wacom->hdev->dev,
181159d69bc8SBenjamin Tissoires 				     &wacom->battery.bat_desc);
181259d69bc8SBenjamin Tissoires 		wacom->battery.battery = NULL;
1813471d1714SBenjamin Tissoires 	}
1814471d1714SBenjamin Tissoires }
1815471d1714SBenjamin Tissoires 
wacom_show_speed(struct device * dev,struct device_attribute * attr,char * buf)1816f81a1295SBenjamin Tissoires static ssize_t wacom_show_speed(struct device *dev,
1817f81a1295SBenjamin Tissoires 				struct device_attribute
1818f81a1295SBenjamin Tissoires 				*attr, char *buf)
1819f81a1295SBenjamin Tissoires {
1820ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1821f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1822f81a1295SBenjamin Tissoires 
1823d8833124SJiapeng Chong 	return sysfs_emit(buf, "%i\n", wacom->wacom_wac.bt_high_speed);
1824f81a1295SBenjamin Tissoires }
1825f81a1295SBenjamin Tissoires 
wacom_store_speed(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1826f81a1295SBenjamin Tissoires static ssize_t wacom_store_speed(struct device *dev,
1827f81a1295SBenjamin Tissoires 				struct device_attribute *attr,
1828f81a1295SBenjamin Tissoires 				const char *buf, size_t count)
1829f81a1295SBenjamin Tissoires {
1830ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1831f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1832f81a1295SBenjamin Tissoires 	u8 new_speed;
1833f81a1295SBenjamin Tissoires 
1834f81a1295SBenjamin Tissoires 	if (kstrtou8(buf, 0, &new_speed))
1835f81a1295SBenjamin Tissoires 		return -EINVAL;
1836f81a1295SBenjamin Tissoires 
1837f81a1295SBenjamin Tissoires 	if (new_speed != 0 && new_speed != 1)
1838f81a1295SBenjamin Tissoires 		return -EINVAL;
1839f81a1295SBenjamin Tissoires 
1840f81a1295SBenjamin Tissoires 	wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1841f81a1295SBenjamin Tissoires 
1842f81a1295SBenjamin Tissoires 	return count;
1843f81a1295SBenjamin Tissoires }
1844f81a1295SBenjamin Tissoires 
1845e0984bc3SPing Cheng static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1846f81a1295SBenjamin Tissoires 		wacom_show_speed, wacom_store_speed);
1847f81a1295SBenjamin Tissoires 
184872b236d6SAaron Skomra 
wacom_show_remote_mode(struct kobject * kobj,struct kobj_attribute * kattr,char * buf,int index)184972b236d6SAaron Skomra static ssize_t wacom_show_remote_mode(struct kobject *kobj,
185072b236d6SAaron Skomra 				      struct kobj_attribute *kattr,
185172b236d6SAaron Skomra 				      char *buf, int index)
185272b236d6SAaron Skomra {
18532cf83833SGeliang Tang 	struct device *dev = kobj_to_dev(kobj->parent);
1854ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
185572b236d6SAaron Skomra 	struct wacom *wacom = hid_get_drvdata(hdev);
185672b236d6SAaron Skomra 	u8 mode;
185772b236d6SAaron Skomra 
1858a50aac71SBenjamin Tissoires 	mode = wacom->led.groups[index].select;
1859bc35f73aSChristos Gkekas 	return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
186072b236d6SAaron Skomra }
186172b236d6SAaron Skomra 
186272b236d6SAaron Skomra #define DEVICE_EKR_ATTR_GROUP(SET_ID)					\
186372b236d6SAaron Skomra static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj,	\
186472b236d6SAaron Skomra 			       struct kobj_attribute *kattr, char *buf)	\
186572b236d6SAaron Skomra {									\
186672b236d6SAaron Skomra 	return wacom_show_remote_mode(kobj, kattr, buf, SET_ID);	\
186772b236d6SAaron Skomra }									\
186872b236d6SAaron Skomra static struct kobj_attribute remote##SET_ID##_mode_attr = {		\
186972b236d6SAaron Skomra 	.attr = {.name = "remote_mode",					\
187072b236d6SAaron Skomra 		.mode = DEV_ATTR_RO_PERM},				\
187172b236d6SAaron Skomra 	.show = wacom_show_remote##SET_ID##_mode,			\
187272b236d6SAaron Skomra };									\
187372b236d6SAaron Skomra static struct attribute *remote##SET_ID##_serial_attrs[] = {		\
187472b236d6SAaron Skomra 	&remote##SET_ID##_mode_attr.attr,				\
187572b236d6SAaron Skomra 	NULL								\
187672b236d6SAaron Skomra };									\
18772a6a35a9SRikard Falkeborn static const struct attribute_group remote##SET_ID##_serial_group = {	\
187872b236d6SAaron Skomra 	.name = NULL,							\
187972b236d6SAaron Skomra 	.attrs = remote##SET_ID##_serial_attrs,				\
188072b236d6SAaron Skomra }
188172b236d6SAaron Skomra 
188272b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(0);
188372b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(1);
188472b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(2);
188572b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(3);
188672b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(4);
188772b236d6SAaron Skomra 
wacom_remote_create_attr_group(struct wacom * wacom,__u32 serial,int index)1888e6f2813aSBenjamin Tissoires static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1889e6f2813aSBenjamin Tissoires 					  int index)
189072b236d6SAaron Skomra {
189172b236d6SAaron Skomra 	int error = 0;
189283e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
189372b236d6SAaron Skomra 
1894e7749f6eSBenjamin Tissoires 	remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1895c1f5409bSBenjamin Tissoires 							  GFP_KERNEL,
1896c1f5409bSBenjamin Tissoires 							  "%d", serial);
1897e7749f6eSBenjamin Tissoires 	if (!remote->remotes[index].group.name)
189872b236d6SAaron Skomra 		return -ENOMEM;
189972b236d6SAaron Skomra 
1900f9036bd4SBenjamin Tissoires 	error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1901e7749f6eSBenjamin Tissoires 						&remote->remotes[index].group);
190272b236d6SAaron Skomra 	if (error) {
1903e7749f6eSBenjamin Tissoires 		remote->remotes[index].group.name = NULL;
190472b236d6SAaron Skomra 		hid_err(wacom->hdev,
190572b236d6SAaron Skomra 			"cannot create sysfs group err: %d\n", error);
190672b236d6SAaron Skomra 		return error;
190772b236d6SAaron Skomra 	}
190872b236d6SAaron Skomra 
190972b236d6SAaron Skomra 	return 0;
191072b236d6SAaron Skomra }
191172b236d6SAaron Skomra 
wacom_cmd_unpair_remote(struct wacom * wacom,unsigned char selector)191272b236d6SAaron Skomra static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
191372b236d6SAaron Skomra {
191472b236d6SAaron Skomra 	const size_t buf_size = 2;
191572b236d6SAaron Skomra 	unsigned char *buf;
191672b236d6SAaron Skomra 	int retval;
191772b236d6SAaron Skomra 
191872b236d6SAaron Skomra 	buf = kzalloc(buf_size, GFP_KERNEL);
191972b236d6SAaron Skomra 	if (!buf)
192072b236d6SAaron Skomra 		return -ENOMEM;
192172b236d6SAaron Skomra 
192272b236d6SAaron Skomra 	buf[0] = WAC_CMD_DELETE_PAIRING;
192372b236d6SAaron Skomra 	buf[1] = selector;
192472b236d6SAaron Skomra 
192572b236d6SAaron Skomra 	retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
192672b236d6SAaron Skomra 				  buf_size, WAC_CMD_RETRIES);
192772b236d6SAaron Skomra 	kfree(buf);
192872b236d6SAaron Skomra 
192972b236d6SAaron Skomra 	return retval;
193072b236d6SAaron Skomra }
193172b236d6SAaron Skomra 
wacom_store_unpair_remote(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)193272b236d6SAaron Skomra static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
193372b236d6SAaron Skomra 					 struct kobj_attribute *attr,
193472b236d6SAaron Skomra 					 const char *buf, size_t count)
193572b236d6SAaron Skomra {
193672b236d6SAaron Skomra 	unsigned char selector = 0;
19372cf83833SGeliang Tang 	struct device *dev = kobj_to_dev(kobj->parent);
1938ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
193972b236d6SAaron Skomra 	struct wacom *wacom = hid_get_drvdata(hdev);
194072b236d6SAaron Skomra 	int err;
194172b236d6SAaron Skomra 
194272b236d6SAaron Skomra 	if (!strncmp(buf, "*\n", 2)) {
194372b236d6SAaron Skomra 		selector = WAC_CMD_UNPAIR_ALL;
194472b236d6SAaron Skomra 	} else {
194572b236d6SAaron Skomra 		hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
194672b236d6SAaron Skomra 			 buf);
194772b236d6SAaron Skomra 		return -1;
194872b236d6SAaron Skomra 	}
194972b236d6SAaron Skomra 
195072b236d6SAaron Skomra 	mutex_lock(&wacom->lock);
195172b236d6SAaron Skomra 
195272b236d6SAaron Skomra 	err = wacom_cmd_unpair_remote(wacom, selector);
195372b236d6SAaron Skomra 	mutex_unlock(&wacom->lock);
195472b236d6SAaron Skomra 
195572b236d6SAaron Skomra 	return err < 0 ? err : count;
195672b236d6SAaron Skomra }
195772b236d6SAaron Skomra 
195872b236d6SAaron Skomra static struct kobj_attribute unpair_remote_attr = {
195972b236d6SAaron Skomra 	.attr = {.name = "unpair_remote", .mode = 0200},
196072b236d6SAaron Skomra 	.store = wacom_store_unpair_remote,
196172b236d6SAaron Skomra };
196272b236d6SAaron Skomra 
196372b236d6SAaron Skomra static const struct attribute *remote_unpair_attrs[] = {
196472b236d6SAaron Skomra 	&unpair_remote_attr.attr,
196572b236d6SAaron Skomra 	NULL
196672b236d6SAaron Skomra };
196772b236d6SAaron Skomra 
wacom_remotes_destroy(void * data)196883e6b40eSBenjamin Tissoires static void wacom_remotes_destroy(void *data)
196983e6b40eSBenjamin Tissoires {
197083e6b40eSBenjamin Tissoires 	struct wacom *wacom = data;
197183e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
197283e6b40eSBenjamin Tissoires 
197383e6b40eSBenjamin Tissoires 	if (!remote)
197483e6b40eSBenjamin Tissoires 		return;
197583e6b40eSBenjamin Tissoires 
197683e6b40eSBenjamin Tissoires 	kobject_put(remote->remote_dir);
197783e6b40eSBenjamin Tissoires 	kfifo_free(&remote->remote_fifo);
197883e6b40eSBenjamin Tissoires 	wacom->remote = NULL;
197983e6b40eSBenjamin Tissoires }
198083e6b40eSBenjamin Tissoires 
wacom_initialize_remotes(struct wacom * wacom)198183e6b40eSBenjamin Tissoires static int wacom_initialize_remotes(struct wacom *wacom)
198272b236d6SAaron Skomra {
198372b236d6SAaron Skomra 	int error = 0;
198483e6b40eSBenjamin Tissoires 	struct wacom_remote *remote;
198572b236d6SAaron Skomra 	int i;
198672b236d6SAaron Skomra 
198772b236d6SAaron Skomra 	if (wacom->wacom_wac.features.type != REMOTE)
198872b236d6SAaron Skomra 		return 0;
198972b236d6SAaron Skomra 
199083e6b40eSBenjamin Tissoires 	remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
199183e6b40eSBenjamin Tissoires 			      GFP_KERNEL);
199283e6b40eSBenjamin Tissoires 	if (!remote)
199372b236d6SAaron Skomra 		return -ENOMEM;
199472b236d6SAaron Skomra 
199583e6b40eSBenjamin Tissoires 	wacom->remote = remote;
199683e6b40eSBenjamin Tissoires 
199783e6b40eSBenjamin Tissoires 	spin_lock_init(&remote->remote_lock);
199883e6b40eSBenjamin Tissoires 
199983e6b40eSBenjamin Tissoires 	error = kfifo_alloc(&remote->remote_fifo,
200055ab9b2cSAaron Armstrong Skomra 			5 * sizeof(struct wacom_remote_work_data),
200183e6b40eSBenjamin Tissoires 			GFP_KERNEL);
200283e6b40eSBenjamin Tissoires 	if (error) {
200383e6b40eSBenjamin Tissoires 		hid_err(wacom->hdev, "failed allocating remote_fifo\n");
200483e6b40eSBenjamin Tissoires 		return -ENOMEM;
200583e6b40eSBenjamin Tissoires 	}
200683e6b40eSBenjamin Tissoires 
2007e7749f6eSBenjamin Tissoires 	remote->remotes[0].group = remote0_serial_group;
2008e7749f6eSBenjamin Tissoires 	remote->remotes[1].group = remote1_serial_group;
2009e7749f6eSBenjamin Tissoires 	remote->remotes[2].group = remote2_serial_group;
2010e7749f6eSBenjamin Tissoires 	remote->remotes[3].group = remote3_serial_group;
2011e7749f6eSBenjamin Tissoires 	remote->remotes[4].group = remote4_serial_group;
201283e6b40eSBenjamin Tissoires 
201383e6b40eSBenjamin Tissoires 	remote->remote_dir = kobject_create_and_add("wacom_remote",
201483e6b40eSBenjamin Tissoires 						    &wacom->hdev->dev.kobj);
201583e6b40eSBenjamin Tissoires 	if (!remote->remote_dir)
201683e6b40eSBenjamin Tissoires 		return -ENOMEM;
201783e6b40eSBenjamin Tissoires 
201883e6b40eSBenjamin Tissoires 	error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
201972b236d6SAaron Skomra 
202072b236d6SAaron Skomra 	if (error) {
202172b236d6SAaron Skomra 		hid_err(wacom->hdev,
202272b236d6SAaron Skomra 			"cannot create sysfs group err: %d\n", error);
202372b236d6SAaron Skomra 		return error;
202472b236d6SAaron Skomra 	}
202572b236d6SAaron Skomra 
202672b236d6SAaron Skomra 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2027a50aac71SBenjamin Tissoires 		wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
2028e7749f6eSBenjamin Tissoires 		remote->remotes[i].serial = 0;
202972b236d6SAaron Skomra 	}
203072b236d6SAaron Skomra 
203183e6b40eSBenjamin Tissoires 	error = devm_add_action_or_reset(&wacom->hdev->dev,
203283e6b40eSBenjamin Tissoires 					 wacom_remotes_destroy, wacom);
203383e6b40eSBenjamin Tissoires 	if (error)
203483e6b40eSBenjamin Tissoires 		return error;
203583e6b40eSBenjamin Tissoires 
203672b236d6SAaron Skomra 	return 0;
203772b236d6SAaron Skomra }
203872b236d6SAaron Skomra 
wacom_allocate_input(struct wacom * wacom)2039471d1714SBenjamin Tissoires static struct input_dev *wacom_allocate_input(struct wacom *wacom)
2040471d1714SBenjamin Tissoires {
2041471d1714SBenjamin Tissoires 	struct input_dev *input_dev;
2042471d1714SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
2043471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2044471d1714SBenjamin Tissoires 
20453dad188eSBenjamin Tissoires 	input_dev = devm_input_allocate_device(&hdev->dev);
2046471d1714SBenjamin Tissoires 	if (!input_dev)
2047471d1714SBenjamin Tissoires 		return NULL;
2048471d1714SBenjamin Tissoires 
20492bdd163cSJason Gerecke 	input_dev->name = wacom_wac->features.name;
2050471d1714SBenjamin Tissoires 	input_dev->phys = hdev->phys;
2051471d1714SBenjamin Tissoires 	input_dev->dev.parent = &hdev->dev;
2052471d1714SBenjamin Tissoires 	input_dev->open = wacom_open;
2053471d1714SBenjamin Tissoires 	input_dev->close = wacom_close;
2054471d1714SBenjamin Tissoires 	input_dev->uniq = hdev->uniq;
2055471d1714SBenjamin Tissoires 	input_dev->id.bustype = hdev->bus;
2056471d1714SBenjamin Tissoires 	input_dev->id.vendor  = hdev->vendor;
205712969e3bSBenjamin Tissoires 	input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
2058471d1714SBenjamin Tissoires 	input_dev->id.version = hdev->version;
2059471d1714SBenjamin Tissoires 	input_set_drvdata(input_dev, wacom);
2060471d1714SBenjamin Tissoires 
2061471d1714SBenjamin Tissoires 	return input_dev;
2062471d1714SBenjamin Tissoires }
2063471d1714SBenjamin Tissoires 
wacom_allocate_inputs(struct wacom * wacom)2064d9f2d203SJason Gerecke static int wacom_allocate_inputs(struct wacom *wacom)
2065d9f2d203SJason Gerecke {
2066d9f2d203SJason Gerecke 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2067d9f2d203SJason Gerecke 
2068d9f2d203SJason Gerecke 	wacom_wac->pen_input = wacom_allocate_input(wacom);
2069d9f2d203SJason Gerecke 	wacom_wac->touch_input = wacom_allocate_input(wacom);
2070d9f2d203SJason Gerecke 	wacom_wac->pad_input = wacom_allocate_input(wacom);
20713dad188eSBenjamin Tissoires 	if (!wacom_wac->pen_input ||
20723dad188eSBenjamin Tissoires 	    !wacom_wac->touch_input ||
20733dad188eSBenjamin Tissoires 	    !wacom_wac->pad_input)
2074d9f2d203SJason Gerecke 		return -ENOMEM;
2075d9f2d203SJason Gerecke 
20762bdd163cSJason Gerecke 	wacom_wac->pen_input->name = wacom_wac->pen_name;
2077d9f2d203SJason Gerecke 	wacom_wac->touch_input->name = wacom_wac->touch_name;
2078d9f2d203SJason Gerecke 	wacom_wac->pad_input->name = wacom_wac->pad_name;
2079d9f2d203SJason Gerecke 
2080d9f2d203SJason Gerecke 	return 0;
2081d9f2d203SJason Gerecke }
2082d9f2d203SJason Gerecke 
wacom_setup_inputs(struct wacom * wacom)2083fc41101aSJason Gerecke static int wacom_setup_inputs(struct wacom *wacom)
2084471d1714SBenjamin Tissoires {
20852a6cdbddSJason Gerecke 	struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2086471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
20872636a3f2SJason Gerecke 	int error = 0;
2088471d1714SBenjamin Tissoires 
20892a6cdbddSJason Gerecke 	pen_input_dev = wacom_wac->pen_input;
20902a6cdbddSJason Gerecke 	touch_input_dev = wacom_wac->touch_input;
20912546dacdSBenjamin Tissoires 	pad_input_dev = wacom_wac->pad_input;
2092471d1714SBenjamin Tissoires 
20932a6cdbddSJason Gerecke 	if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
20942546dacdSBenjamin Tissoires 		return -EINVAL;
2095471d1714SBenjamin Tissoires 
20962a6cdbddSJason Gerecke 	error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
20972a6cdbddSJason Gerecke 	if (error) {
20982a6cdbddSJason Gerecke 		/* no pen in use on this interface */
20992a6cdbddSJason Gerecke 		input_free_device(pen_input_dev);
21002a6cdbddSJason Gerecke 		wacom_wac->pen_input = NULL;
21012a6cdbddSJason Gerecke 		pen_input_dev = NULL;
21022a6cdbddSJason Gerecke 	}
21032a6cdbddSJason Gerecke 
21042a6cdbddSJason Gerecke 	error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
21052a6cdbddSJason Gerecke 	if (error) {
21062a6cdbddSJason Gerecke 		/* no touch in use on this interface */
21072a6cdbddSJason Gerecke 		input_free_device(touch_input_dev);
21082a6cdbddSJason Gerecke 		wacom_wac->touch_input = NULL;
21092a6cdbddSJason Gerecke 		touch_input_dev = NULL;
211030ebc1aeSPing Cheng 	}
2111471d1714SBenjamin Tissoires 
2112471d1714SBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
2113471d1714SBenjamin Tissoires 	if (error) {
2114d6b67568SPing Cheng 		/* no pad events using this interface */
2115471d1714SBenjamin Tissoires 		input_free_device(pad_input_dev);
2116471d1714SBenjamin Tissoires 		wacom_wac->pad_input = NULL;
2117471d1714SBenjamin Tissoires 		pad_input_dev = NULL;
2118fc41101aSJason Gerecke 	}
2119fc41101aSJason Gerecke 
2120fc41101aSJason Gerecke 	return 0;
2121fc41101aSJason Gerecke }
2122fc41101aSJason Gerecke 
wacom_register_inputs(struct wacom * wacom)2123fc41101aSJason Gerecke static int wacom_register_inputs(struct wacom *wacom)
2124fc41101aSJason Gerecke {
2125fc41101aSJason Gerecke 	struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
2126fc41101aSJason Gerecke 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
2127fc41101aSJason Gerecke 	int error = 0;
2128fc41101aSJason Gerecke 
2129fc41101aSJason Gerecke 	pen_input_dev = wacom_wac->pen_input;
2130fc41101aSJason Gerecke 	touch_input_dev = wacom_wac->touch_input;
2131fc41101aSJason Gerecke 	pad_input_dev = wacom_wac->pad_input;
2132fc41101aSJason Gerecke 
2133fc41101aSJason Gerecke 	if (pen_input_dev) {
2134fc41101aSJason Gerecke 		error = input_register_device(pen_input_dev);
2135fc41101aSJason Gerecke 		if (error)
2136fc41101aSJason Gerecke 			goto fail;
2137fc41101aSJason Gerecke 	}
2138fc41101aSJason Gerecke 
2139fc41101aSJason Gerecke 	if (touch_input_dev) {
2140fc41101aSJason Gerecke 		error = input_register_device(touch_input_dev);
2141fc41101aSJason Gerecke 		if (error)
2142fc41101aSJason Gerecke 			goto fail;
2143fc41101aSJason Gerecke 	}
2144fc41101aSJason Gerecke 
2145fc41101aSJason Gerecke 	if (pad_input_dev) {
2146471d1714SBenjamin Tissoires 		error = input_register_device(pad_input_dev);
2147471d1714SBenjamin Tissoires 		if (error)
21483dad188eSBenjamin Tissoires 			goto fail;
2149471d1714SBenjamin Tissoires 	}
2150471d1714SBenjamin Tissoires 
2151471d1714SBenjamin Tissoires 	return 0;
2152471d1714SBenjamin Tissoires 
21533dad188eSBenjamin Tissoires fail:
21543dad188eSBenjamin Tissoires 	wacom_wac->pad_input = NULL;
21552a6cdbddSJason Gerecke 	wacom_wac->touch_input = NULL;
21562a6cdbddSJason Gerecke 	wacom_wac->pen_input = NULL;
2157471d1714SBenjamin Tissoires 	return error;
2158471d1714SBenjamin Tissoires }
2159471d1714SBenjamin Tissoires 
21600be01712SJason Gerecke /*
21610be01712SJason Gerecke  * Not all devices report physical dimensions from HID.
21620be01712SJason Gerecke  * Compute the default from hardcoded logical dimension
21630be01712SJason Gerecke  * and resolution before driver overwrites them.
21640be01712SJason Gerecke  */
wacom_set_default_phy(struct wacom_features * features)21650be01712SJason Gerecke static void wacom_set_default_phy(struct wacom_features *features)
21660be01712SJason Gerecke {
21670be01712SJason Gerecke 	if (features->x_resolution) {
21680be01712SJason Gerecke 		features->x_phy = (features->x_max * 100) /
21690be01712SJason Gerecke 					features->x_resolution;
21700be01712SJason Gerecke 		features->y_phy = (features->y_max * 100) /
21710be01712SJason Gerecke 					features->y_resolution;
21720be01712SJason Gerecke 	}
21730be01712SJason Gerecke }
21740be01712SJason Gerecke 
wacom_calculate_res(struct wacom_features * features)21750be01712SJason Gerecke static void wacom_calculate_res(struct wacom_features *features)
21760be01712SJason Gerecke {
21770be01712SJason Gerecke 	/* set unit to "100th of a mm" for devices not reported by HID */
21780be01712SJason Gerecke 	if (!features->unit) {
21790be01712SJason Gerecke 		features->unit = 0x11;
21800be01712SJason Gerecke 		features->unitExpo = -3;
21810be01712SJason Gerecke 	}
21820be01712SJason Gerecke 
21830be01712SJason Gerecke 	features->x_resolution = wacom_calc_hid_res(features->x_max,
21840be01712SJason Gerecke 						    features->x_phy,
21850be01712SJason Gerecke 						    features->unit,
21860be01712SJason Gerecke 						    features->unitExpo);
21870be01712SJason Gerecke 	features->y_resolution = wacom_calc_hid_res(features->y_max,
21880be01712SJason Gerecke 						    features->y_phy,
21890be01712SJason Gerecke 						    features->unit,
21900be01712SJason Gerecke 						    features->unitExpo);
21910be01712SJason Gerecke }
21920be01712SJason Gerecke 
wacom_battery_work(struct work_struct * work)2193fce9957dSJason Gerecke void wacom_battery_work(struct work_struct *work)
2194fce9957dSJason Gerecke {
2195d17d1f17SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, battery_work);
2196fce9957dSJason Gerecke 
2197fce9957dSJason Gerecke 	if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
219859d69bc8SBenjamin Tissoires 	     !wacom->battery.battery) {
2199fce9957dSJason Gerecke 		wacom_initialize_battery(wacom);
2200fce9957dSJason Gerecke 	}
2201fce9957dSJason Gerecke 	else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
220259d69bc8SBenjamin Tissoires 		 wacom->battery.battery) {
2203fce9957dSJason Gerecke 		wacom_destroy_battery(wacom);
2204fce9957dSJason Gerecke 	}
2205fce9957dSJason Gerecke }
2206fce9957dSJason Gerecke 
wacom_compute_pktlen(struct hid_device * hdev)2207471d1714SBenjamin Tissoires static size_t wacom_compute_pktlen(struct hid_device *hdev)
2208471d1714SBenjamin Tissoires {
2209471d1714SBenjamin Tissoires 	struct hid_report_enum *report_enum;
2210471d1714SBenjamin Tissoires 	struct hid_report *report;
2211471d1714SBenjamin Tissoires 	size_t size = 0;
2212471d1714SBenjamin Tissoires 
2213471d1714SBenjamin Tissoires 	report_enum = hdev->report_enum + HID_INPUT_REPORT;
2214471d1714SBenjamin Tissoires 
2215471d1714SBenjamin Tissoires 	list_for_each_entry(report, &report_enum->report_list, list) {
2216dabb05c6SMathieu Magnaudet 		size_t report_size = hid_report_len(report);
2217471d1714SBenjamin Tissoires 		if (report_size > size)
2218471d1714SBenjamin Tissoires 			size = report_size;
2219471d1714SBenjamin Tissoires 	}
2220471d1714SBenjamin Tissoires 
2221471d1714SBenjamin Tissoires 	return size;
2222471d1714SBenjamin Tissoires }
2223471d1714SBenjamin Tissoires 
wacom_update_name(struct wacom * wacom,const char * suffix)2224fd5f92b6SBenjamin Tissoires static void wacom_update_name(struct wacom *wacom, const char *suffix)
2225c24eab4eSPing Cheng {
2226c24eab4eSPing Cheng 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2227c24eab4eSPing Cheng 	struct wacom_features *features = &wacom_wac->features;
2228073b50bcSJason Gerecke 	char name[WACOM_NAME_MAX - 20]; /* Leave some room for suffixes */
2229c24eab4eSPing Cheng 
2230c24eab4eSPing Cheng 	/* Generic devices name unspecified */
2231c24eab4eSPing Cheng 	if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
223209dc28acSJason Gerecke 		char *product_name = wacom->hdev->name;
223309dc28acSJason Gerecke 
2234f83baa0cSGreg Kroah-Hartman 		if (hid_is_usb(wacom->hdev)) {
223509dc28acSJason Gerecke 			struct usb_interface *intf = to_usb_interface(wacom->hdev->dev.parent);
223609dc28acSJason Gerecke 			struct usb_device *dev = interface_to_usbdev(intf);
2237*a7f05095SWangYuli 			if (dev->product != NULL)
223809dc28acSJason Gerecke 				product_name = dev->product;
223909dc28acSJason Gerecke 		}
224009dc28acSJason Gerecke 
224109dc28acSJason Gerecke 		if (wacom->hdev->bus == BUS_I2C) {
224209dc28acSJason Gerecke 			snprintf(name, sizeof(name), "%s %X",
224309dc28acSJason Gerecke 				 features->name, wacom->hdev->product);
224409dc28acSJason Gerecke 		} else if (strstr(product_name, "Wacom") ||
224509dc28acSJason Gerecke 			   strstr(product_name, "wacom") ||
224609dc28acSJason Gerecke 			   strstr(product_name, "WACOM")) {
2247d9eef346SJason Gerecke 			if (strscpy(name, product_name, sizeof(name)) < 0) {
2248d9eef346SJason Gerecke 				hid_warn(wacom->hdev, "String overflow while assembling device name");
2249d9eef346SJason Gerecke 			}
225009dc28acSJason Gerecke 		} else {
225109dc28acSJason Gerecke 			snprintf(name, sizeof(name), "Wacom %s", product_name);
225209dc28acSJason Gerecke 		}
2253c24eab4eSPing Cheng 
2254c24eab4eSPing Cheng 		/* strip out excess whitespaces */
2255c24eab4eSPing Cheng 		while (1) {
225644b5250bSJason Gerecke 			char *gap = strstr(name, "  ");
2257c24eab4eSPing Cheng 			if (gap == NULL)
2258c24eab4eSPing Cheng 				break;
2259c24eab4eSPing Cheng 			/* shift everything including the terminator */
2260c24eab4eSPing Cheng 			memmove(gap, gap+1, strlen(gap));
2261c24eab4eSPing Cheng 		}
2262f2209d4aSJason Gerecke 
2263c24eab4eSPing Cheng 		/* get rid of trailing whitespace */
226444b5250bSJason Gerecke 		if (name[strlen(name)-1] == ' ')
226544b5250bSJason Gerecke 			name[strlen(name)-1] = '\0';
2266c24eab4eSPing Cheng 	} else {
2267d9eef346SJason Gerecke 		if (strscpy(name, features->name, sizeof(name)) < 0) {
2268d9eef346SJason Gerecke 			hid_warn(wacom->hdev, "String overflow while assembling device name");
2269d9eef346SJason Gerecke 		}
2270c24eab4eSPing Cheng 	}
2271c24eab4eSPing Cheng 
22729956953eSBenjamin Tissoires 	snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
22739956953eSBenjamin Tissoires 		 name, suffix);
22749956953eSBenjamin Tissoires 
2275c24eab4eSPing Cheng 	/* Append the device type to the name */
22762a6cdbddSJason Gerecke 	snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
2277fd5f92b6SBenjamin Tissoires 		"%s%s Pen", name, suffix);
22782a6cdbddSJason Gerecke 	snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
2279fd5f92b6SBenjamin Tissoires 		"%s%s Finger", name, suffix);
2280c24eab4eSPing Cheng 	snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
2281fd5f92b6SBenjamin Tissoires 		"%s%s Pad", name, suffix);
22828d80f790SJason Gerecke }
2283c24eab4eSPing Cheng 
wacom_release_resources(struct wacom * wacom)228484dfbd7fSBenjamin Tissoires static void wacom_release_resources(struct wacom *wacom)
228584dfbd7fSBenjamin Tissoires {
228684dfbd7fSBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
228784dfbd7fSBenjamin Tissoires 
228884dfbd7fSBenjamin Tissoires 	if (!wacom->resources)
228984dfbd7fSBenjamin Tissoires 		return;
229084dfbd7fSBenjamin Tissoires 
229184dfbd7fSBenjamin Tissoires 	devres_release_group(&hdev->dev, wacom);
229284dfbd7fSBenjamin Tissoires 
229384dfbd7fSBenjamin Tissoires 	wacom->resources = false;
229484dfbd7fSBenjamin Tissoires 
229584dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.pen_input = NULL;
229684dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.touch_input = NULL;
229784dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.pad_input = NULL;
229884dfbd7fSBenjamin Tissoires }
229984dfbd7fSBenjamin Tissoires 
wacom_set_shared_values(struct wacom_wac * wacom_wac)2300d2ec58aeSAaron Armstrong Skomra static void wacom_set_shared_values(struct wacom_wac *wacom_wac)
2301d2ec58aeSAaron Armstrong Skomra {
2302d2ec58aeSAaron Armstrong Skomra 	if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH) {
2303d2ec58aeSAaron Armstrong Skomra 		wacom_wac->shared->type = wacom_wac->features.type;
2304d2ec58aeSAaron Armstrong Skomra 		wacom_wac->shared->touch_input = wacom_wac->touch_input;
2305d2ec58aeSAaron Armstrong Skomra 	}
2306d2ec58aeSAaron Armstrong Skomra 
2307d793ff81SPing Cheng 	if (wacom_wac->has_mute_touch_switch) {
2308d2ec58aeSAaron Armstrong Skomra 		wacom_wac->shared->has_mute_touch_switch = true;
2309dc9dc864SPing Cheng 		/* Hardware touch switch may be off. Wait until
2310dc9dc864SPing Cheng 		 * we know the switch state to decide is_touch_on.
2311dc9dc864SPing Cheng 		 * Softkey state should be initialized to "on" to
2312dc9dc864SPing Cheng 		 * match historic default.
2313dc9dc864SPing Cheng 		 */
2314dc9dc864SPing Cheng 		if (wacom_wac->is_soft_touch_switch)
2315d793ff81SPing Cheng 			wacom_wac->shared->is_touch_on = true;
2316d793ff81SPing Cheng 	}
2317d2ec58aeSAaron Armstrong Skomra 
2318d2ec58aeSAaron Armstrong Skomra 	if (wacom_wac->shared->has_mute_touch_switch &&
2319d2ec58aeSAaron Armstrong Skomra 	    wacom_wac->shared->touch_input) {
2320d2ec58aeSAaron Armstrong Skomra 		set_bit(EV_SW, wacom_wac->shared->touch_input->evbit);
2321d2ec58aeSAaron Armstrong Skomra 		input_set_capability(wacom_wac->shared->touch_input, EV_SW,
2322d2ec58aeSAaron Armstrong Skomra 				     SW_MUTE_DEVICE);
2323d2ec58aeSAaron Armstrong Skomra 	}
2324d2ec58aeSAaron Armstrong Skomra }
2325d2ec58aeSAaron Armstrong Skomra 
wacom_parse_and_register(struct wacom * wacom,bool wireless)2326fd5f92b6SBenjamin Tissoires static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
2327471d1714SBenjamin Tissoires {
2328c58ac3a8SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2329c58ac3a8SBenjamin Tissoires 	struct wacom_features *features = &wacom_wac->features;
2330c58ac3a8SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
2331471d1714SBenjamin Tissoires 	int error;
23327704ac93SBenjamin Tissoires 	unsigned int connect_mask = HID_CONNECT_HIDRAW;
2333471d1714SBenjamin Tissoires 
2334471d1714SBenjamin Tissoires 	features->pktlen = wacom_compute_pktlen(hdev);
2335c58ac3a8SBenjamin Tissoires 	if (features->pktlen > WACOM_PKGLEN_MAX)
2336c58ac3a8SBenjamin Tissoires 		return -EINVAL;
2337471d1714SBenjamin Tissoires 
233884dfbd7fSBenjamin Tissoires 	if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
233984dfbd7fSBenjamin Tissoires 		return -ENOMEM;
234084dfbd7fSBenjamin Tissoires 
234184dfbd7fSBenjamin Tissoires 	wacom->resources = true;
234284dfbd7fSBenjamin Tissoires 
2343494078b0SBenjamin Tissoires 	error = wacom_allocate_inputs(wacom);
2344494078b0SBenjamin Tissoires 	if (error)
23453888b0d5SBenjamin Tissoires 		goto fail;
2346494078b0SBenjamin Tissoires 
23478c97a765SBenjamin Tissoires 	/*
23488c97a765SBenjamin Tissoires 	 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
23498c97a765SBenjamin Tissoires 	 * into debug mode for the touch part.
23508c97a765SBenjamin Tissoires 	 * We ignore the other interfaces.
23518c97a765SBenjamin Tissoires 	 */
23528c97a765SBenjamin Tissoires 	if (features->type == BAMBOO_PAD) {
23538c97a765SBenjamin Tissoires 		if (features->pktlen == WACOM_PKGLEN_PENABLED) {
23548c97a765SBenjamin Tissoires 			features->type = HID_GENERIC;
23558c97a765SBenjamin Tissoires 		} else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
23568c97a765SBenjamin Tissoires 			   (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
23578c97a765SBenjamin Tissoires 			error = -ENODEV;
23583888b0d5SBenjamin Tissoires 			goto fail;
23598c97a765SBenjamin Tissoires 		}
23608c97a765SBenjamin Tissoires 	}
23618c97a765SBenjamin Tissoires 
2362471d1714SBenjamin Tissoires 	/* set the default size in case we do not get them from hid */
2363471d1714SBenjamin Tissoires 	wacom_set_default_phy(features);
2364471d1714SBenjamin Tissoires 
2365471d1714SBenjamin Tissoires 	/* Retrieve the physical and logical size for touch devices */
2366471d1714SBenjamin Tissoires 	wacom_retrieve_hid_descriptor(hdev, features);
236742f4f272SPing Cheng 	wacom_setup_device_quirks(wacom);
2368042628abSJason Gerecke 
2369aa86b18cSJason Gerecke 	if (features->device_type == WACOM_DEVICETYPE_NONE &&
2370aa86b18cSJason Gerecke 	    features->type != WIRELESS) {
23718e116d31SJason Gerecke 		error = features->type == HID_GENERIC ? -ENODEV : 0;
23728e116d31SJason Gerecke 
2373042628abSJason Gerecke 		dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
23748e116d31SJason Gerecke 			 hdev->name,
23758e116d31SJason Gerecke 			 error ? "Ignoring" : "Assuming pen");
23768e116d31SJason Gerecke 
23778e116d31SJason Gerecke 		if (error)
23783888b0d5SBenjamin Tissoires 			goto fail;
2379042628abSJason Gerecke 
2380aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_PEN;
2381042628abSJason Gerecke 	}
2382042628abSJason Gerecke 
2383471d1714SBenjamin Tissoires 	wacom_calculate_res(features);
2384471d1714SBenjamin Tissoires 
2385fd5f92b6SBenjamin Tissoires 	wacom_update_name(wacom, wireless ? " (WL)" : "");
2386471d1714SBenjamin Tissoires 
23878b407359SAaron Armstrong Skomra 	/* pen only Bamboo neither support touch nor pad */
23888b407359SAaron Armstrong Skomra 	if ((features->type == BAMBOO_PEN) &&
23898b407359SAaron Armstrong Skomra 	    ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
23908b407359SAaron Armstrong Skomra 	    (features->device_type & WACOM_DEVICETYPE_PAD))) {
23918b407359SAaron Armstrong Skomra 		error = -ENODEV;
23928b407359SAaron Armstrong Skomra 		goto fail;
23938b407359SAaron Armstrong Skomra 	}
23948b407359SAaron Armstrong Skomra 
2395a9ce7856SJason Gerecke 	error = wacom_add_shared_data(hdev);
2396a9ce7856SJason Gerecke 	if (error)
2397a9ce7856SJason Gerecke 		goto fail;
2398471d1714SBenjamin Tissoires 
2399fc41101aSJason Gerecke 	error = wacom_setup_inputs(wacom);
2400fc41101aSJason Gerecke 	if (error)
2401fc41101aSJason Gerecke 		goto fail;
2402fc41101aSJason Gerecke 
2403fc41101aSJason Gerecke 	if (features->type == HID_GENERIC)
2404fc41101aSJason Gerecke 		connect_mask |= HID_CONNECT_DRIVER;
2405fc41101aSJason Gerecke 
2406fc41101aSJason Gerecke 	/* Regular HID work starts now */
2407fc41101aSJason Gerecke 	error = hid_hw_start(hdev, connect_mask);
2408fc41101aSJason Gerecke 	if (error) {
2409fc41101aSJason Gerecke 		hid_err(hdev, "hw start failed\n");
2410fc41101aSJason Gerecke 		goto fail;
2411fc41101aSJason Gerecke 	}
2412fc41101aSJason Gerecke 
2413494078b0SBenjamin Tissoires 	error = wacom_register_inputs(wacom);
2414471d1714SBenjamin Tissoires 	if (error)
24153888b0d5SBenjamin Tissoires 		goto fail;
2416f81a1295SBenjamin Tissoires 
2417b62f6465SBenjamin Tissoires 	if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
241885d2c77bSBenjamin Tissoires 		error = wacom_initialize_leds(wacom);
241985d2c77bSBenjamin Tissoires 		if (error)
24203888b0d5SBenjamin Tissoires 			goto fail;
242185d2c77bSBenjamin Tissoires 
242283e6b40eSBenjamin Tissoires 		error = wacom_initialize_remotes(wacom);
2423b62f6465SBenjamin Tissoires 		if (error)
24243888b0d5SBenjamin Tissoires 			goto fail;
2425b62f6465SBenjamin Tissoires 	}
2426b62f6465SBenjamin Tissoires 
2427fd5f92b6SBenjamin Tissoires 	if (!wireless) {
242886e88f0eSJason Gerecke 		/* Note that if query fails it is not a hard failure */
2429a544c619SBenjamin Tissoires 		wacom_query_tablet_data(wacom);
2430fd5f92b6SBenjamin Tissoires 	}
243186e88f0eSJason Gerecke 
243286e88f0eSJason Gerecke 	/* touch only Bamboo doesn't support pen */
243386e88f0eSJason Gerecke 	if ((features->type == BAMBOO_TOUCH) &&
243486e88f0eSJason Gerecke 	    (features->device_type & WACOM_DEVICETYPE_PEN)) {
24354d20c332SAaron Armstrong Skomra 		cancel_delayed_work_sync(&wacom->init_work);
24364d20c332SAaron Armstrong Skomra 		_wacom_query_tablet_data(wacom);
243786e88f0eSJason Gerecke 		error = -ENODEV;
2438b62f6465SBenjamin Tissoires 		goto fail_quirks;
243986e88f0eSJason Gerecke 	}
244086e88f0eSJason Gerecke 
244116a9c24fSDenis Arefev 	if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR) {
2442471d1714SBenjamin Tissoires 		error = hid_hw_open(hdev);
244316a9c24fSDenis Arefev 		if (error) {
244416a9c24fSDenis Arefev 			hid_err(hdev, "hw open failed\n");
244516a9c24fSDenis Arefev 			goto fail_quirks;
244616a9c24fSDenis Arefev 		}
244716a9c24fSDenis Arefev 	}
2448471d1714SBenjamin Tissoires 
2449d2ec58aeSAaron Armstrong Skomra 	wacom_set_shared_values(wacom_wac);
245084dfbd7fSBenjamin Tissoires 	devres_close_group(&hdev->dev, wacom);
245184dfbd7fSBenjamin Tissoires 
2452471d1714SBenjamin Tissoires 	return 0;
2453471d1714SBenjamin Tissoires 
2454b62f6465SBenjamin Tissoires fail_quirks:
2455c58ac3a8SBenjamin Tissoires 	hid_hw_stop(hdev);
24563888b0d5SBenjamin Tissoires fail:
245784dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom);
2458c58ac3a8SBenjamin Tissoires 	return error;
2459c58ac3a8SBenjamin Tissoires }
2460c58ac3a8SBenjamin Tissoires 
wacom_wireless_work(struct work_struct * work)2461a2f091afSBenjamin Tissoires static void wacom_wireless_work(struct work_struct *work)
2462a2f091afSBenjamin Tissoires {
2463d17d1f17SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2464a2f091afSBenjamin Tissoires 	struct usb_device *usbdev = wacom->usbdev;
2465a2f091afSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2466a2f091afSBenjamin Tissoires 	struct hid_device *hdev1, *hdev2;
2467a2f091afSBenjamin Tissoires 	struct wacom *wacom1, *wacom2;
2468a2f091afSBenjamin Tissoires 	struct wacom_wac *wacom_wac1, *wacom_wac2;
2469a2f091afSBenjamin Tissoires 	int error;
2470a2f091afSBenjamin Tissoires 
2471a2f091afSBenjamin Tissoires 	/*
2472a2f091afSBenjamin Tissoires 	 * Regardless if this is a disconnect or a new tablet,
2473a2f091afSBenjamin Tissoires 	 * remove any existing input and battery devices.
2474a2f091afSBenjamin Tissoires 	 */
2475a2f091afSBenjamin Tissoires 
2476a2f091afSBenjamin Tissoires 	wacom_destroy_battery(wacom);
2477a2f091afSBenjamin Tissoires 
2478720ac467SGreg Kroah-Hartman 	if (!usbdev)
2479720ac467SGreg Kroah-Hartman 		return;
2480720ac467SGreg Kroah-Hartman 
2481a2f091afSBenjamin Tissoires 	/* Stylus interface */
2482a2f091afSBenjamin Tissoires 	hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2483a2f091afSBenjamin Tissoires 	wacom1 = hid_get_drvdata(hdev1);
2484a2f091afSBenjamin Tissoires 	wacom_wac1 = &(wacom1->wacom_wac);
248584dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom1);
2486a2f091afSBenjamin Tissoires 
2487a2f091afSBenjamin Tissoires 	/* Touch interface */
2488a2f091afSBenjamin Tissoires 	hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2489a2f091afSBenjamin Tissoires 	wacom2 = hid_get_drvdata(hdev2);
2490a2f091afSBenjamin Tissoires 	wacom_wac2 = &(wacom2->wacom_wac);
249184dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom2);
2492a2f091afSBenjamin Tissoires 
2493a2f091afSBenjamin Tissoires 	if (wacom_wac->pid == 0) {
2494a2f091afSBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet disconnected\n");
2495a2f091afSBenjamin Tissoires 	} else {
2496a2f091afSBenjamin Tissoires 		const struct hid_device_id *id = wacom_ids;
2497a2f091afSBenjamin Tissoires 
2498a2f091afSBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2499a2f091afSBenjamin Tissoires 			 wacom_wac->pid);
2500a2f091afSBenjamin Tissoires 
2501a2f091afSBenjamin Tissoires 		while (id->bus) {
2502a2f091afSBenjamin Tissoires 			if (id->vendor == USB_VENDOR_ID_WACOM &&
2503a2f091afSBenjamin Tissoires 			    id->product == wacom_wac->pid)
2504a2f091afSBenjamin Tissoires 				break;
2505a2f091afSBenjamin Tissoires 			id++;
2506a2f091afSBenjamin Tissoires 		}
2507a2f091afSBenjamin Tissoires 
2508a2f091afSBenjamin Tissoires 		if (!id->bus) {
2509a2f091afSBenjamin Tissoires 			hid_info(wacom->hdev, "ignoring unknown PID.\n");
2510a2f091afSBenjamin Tissoires 			return;
2511a2f091afSBenjamin Tissoires 		}
2512a2f091afSBenjamin Tissoires 
2513a2f091afSBenjamin Tissoires 		/* Stylus interface */
2514a2f091afSBenjamin Tissoires 		wacom_wac1->features =
2515a2f091afSBenjamin Tissoires 			*((struct wacom_features *)id->driver_data);
2516fd5f92b6SBenjamin Tissoires 
2517a2f091afSBenjamin Tissoires 		wacom_wac1->pid = wacom_wac->pid;
2518fd5f92b6SBenjamin Tissoires 		hid_hw_stop(hdev1);
2519fd5f92b6SBenjamin Tissoires 		error = wacom_parse_and_register(wacom1, true);
2520a2f091afSBenjamin Tissoires 		if (error)
2521a2f091afSBenjamin Tissoires 			goto fail;
2522a2f091afSBenjamin Tissoires 
2523a2f091afSBenjamin Tissoires 		/* Touch interface */
2524a2f091afSBenjamin Tissoires 		if (wacom_wac1->features.touch_max ||
2525a2f091afSBenjamin Tissoires 		    (wacom_wac1->features.type >= INTUOSHT &&
2526a2f091afSBenjamin Tissoires 		    wacom_wac1->features.type <= BAMBOO_PT)) {
2527a2f091afSBenjamin Tissoires 			wacom_wac2->features =
2528a2f091afSBenjamin Tissoires 				*((struct wacom_features *)id->driver_data);
2529a2f091afSBenjamin Tissoires 			wacom_wac2->pid = wacom_wac->pid;
2530fd5f92b6SBenjamin Tissoires 			hid_hw_stop(hdev2);
2531fd5f92b6SBenjamin Tissoires 			error = wacom_parse_and_register(wacom2, true);
2532a2f091afSBenjamin Tissoires 			if (error)
2533a2f091afSBenjamin Tissoires 				goto fail;
2534a2f091afSBenjamin Tissoires 		}
2535a2f091afSBenjamin Tissoires 
2536d9eef346SJason Gerecke 		if (strscpy(wacom_wac->name, wacom_wac1->name,
2537d9eef346SJason Gerecke 			sizeof(wacom_wac->name)) < 0) {
2538d9eef346SJason Gerecke 			hid_warn(wacom->hdev, "String overflow while assembling device name");
2539d9eef346SJason Gerecke 		}
2540a2f091afSBenjamin Tissoires 	}
2541a2f091afSBenjamin Tissoires 
2542a2f091afSBenjamin Tissoires 	return;
2543a2f091afSBenjamin Tissoires 
2544a2f091afSBenjamin Tissoires fail:
254584dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom1);
254684dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom2);
2547a2f091afSBenjamin Tissoires 	return;
2548a2f091afSBenjamin Tissoires }
2549a2f091afSBenjamin Tissoires 
wacom_remote_destroy_battery(struct wacom * wacom,int index)25509ac6678bSAaron Armstrong Skomra static void wacom_remote_destroy_battery(struct wacom *wacom, int index)
25519ac6678bSAaron Armstrong Skomra {
25529ac6678bSAaron Armstrong Skomra 	struct wacom_remote *remote = wacom->remote;
25539ac6678bSAaron Armstrong Skomra 
25549ac6678bSAaron Armstrong Skomra 	if (remote->remotes[index].battery.battery) {
25559ac6678bSAaron Armstrong Skomra 		devres_release_group(&wacom->hdev->dev,
25569ac6678bSAaron Armstrong Skomra 				     &remote->remotes[index].battery.bat_desc);
25579ac6678bSAaron Armstrong Skomra 		remote->remotes[index].battery.battery = NULL;
25589ac6678bSAaron Armstrong Skomra 		remote->remotes[index].active_time = 0;
25599ac6678bSAaron Armstrong Skomra 	}
25609ac6678bSAaron Armstrong Skomra }
25619ac6678bSAaron Armstrong Skomra 
wacom_remote_destroy_one(struct wacom * wacom,unsigned int index)256204bfa27bSBenjamin Tissoires static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
256304bfa27bSBenjamin Tissoires {
256404bfa27bSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
2565e7749f6eSBenjamin Tissoires 	u32 serial = remote->remotes[index].serial;
256604bfa27bSBenjamin Tissoires 	int i;
25677c35dc3cSBenjamin Tissoires 	unsigned long flags;
25687c35dc3cSBenjamin Tissoires 
256904bfa27bSBenjamin Tissoires 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2570e7749f6eSBenjamin Tissoires 		if (remote->remotes[i].serial == serial) {
2571791ae273SAaron Armstrong Skomra 
2572791ae273SAaron Armstrong Skomra 			spin_lock_irqsave(&remote->remote_lock, flags);
2573791ae273SAaron Armstrong Skomra 			remote->remotes[i].registered = false;
2574791ae273SAaron Armstrong Skomra 			spin_unlock_irqrestore(&remote->remote_lock, flags);
2575791ae273SAaron Armstrong Skomra 
25769ac6678bSAaron Armstrong Skomra 			wacom_remote_destroy_battery(wacom, i);
2577791ae273SAaron Armstrong Skomra 
2578791ae273SAaron Armstrong Skomra 			if (remote->remotes[i].group.name)
2579791ae273SAaron Armstrong Skomra 				devres_release_group(&wacom->hdev->dev,
2580791ae273SAaron Armstrong Skomra 						     &remote->remotes[i]);
2581791ae273SAaron Armstrong Skomra 
2582e7749f6eSBenjamin Tissoires 			remote->remotes[i].serial = 0;
2583e7749f6eSBenjamin Tissoires 			remote->remotes[i].group.name = NULL;
258404bfa27bSBenjamin Tissoires 			wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
258504bfa27bSBenjamin Tissoires 		}
258604bfa27bSBenjamin Tissoires 	}
258704bfa27bSBenjamin Tissoires }
258804bfa27bSBenjamin Tissoires 
wacom_remote_create_one(struct wacom * wacom,u32 serial,unsigned int index)258904bfa27bSBenjamin Tissoires static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
259004bfa27bSBenjamin Tissoires 				   unsigned int index)
259104bfa27bSBenjamin Tissoires {
259204bfa27bSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
2593f9036bd4SBenjamin Tissoires 	struct device *dev = &wacom->hdev->dev;
259404bfa27bSBenjamin Tissoires 	int error, k;
259504bfa27bSBenjamin Tissoires 
259604bfa27bSBenjamin Tissoires 	/* A remote can pair more than once with an EKR,
259704bfa27bSBenjamin Tissoires 	 * check to make sure this serial isn't already paired.
259804bfa27bSBenjamin Tissoires 	 */
259904bfa27bSBenjamin Tissoires 	for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2600e7749f6eSBenjamin Tissoires 		if (remote->remotes[k].serial == serial)
260104bfa27bSBenjamin Tissoires 			break;
260204bfa27bSBenjamin Tissoires 	}
260304bfa27bSBenjamin Tissoires 
260404bfa27bSBenjamin Tissoires 	if (k < WACOM_MAX_REMOTES) {
2605e7749f6eSBenjamin Tissoires 		remote->remotes[index].serial = serial;
260604bfa27bSBenjamin Tissoires 		return 0;
260704bfa27bSBenjamin Tissoires 	}
260804bfa27bSBenjamin Tissoires 
2609e7749f6eSBenjamin Tissoires 	if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2610f9036bd4SBenjamin Tissoires 		return -ENOMEM;
2611f9036bd4SBenjamin Tissoires 
261204bfa27bSBenjamin Tissoires 	error = wacom_remote_create_attr_group(wacom, serial, index);
261304bfa27bSBenjamin Tissoires 	if (error)
2614f9036bd4SBenjamin Tissoires 		goto fail;
261504bfa27bSBenjamin Tissoires 
26167c35dc3cSBenjamin Tissoires 	remote->remotes[index].input = wacom_allocate_input(wacom);
26177c35dc3cSBenjamin Tissoires 	if (!remote->remotes[index].input) {
26187c35dc3cSBenjamin Tissoires 		error = -ENOMEM;
26197c35dc3cSBenjamin Tissoires 		goto fail;
26207c35dc3cSBenjamin Tissoires 	}
26217c35dc3cSBenjamin Tissoires 	remote->remotes[index].input->uniq = remote->remotes[index].group.name;
26227c35dc3cSBenjamin Tissoires 	remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
26237c35dc3cSBenjamin Tissoires 
26247c35dc3cSBenjamin Tissoires 	if (!remote->remotes[index].input->name) {
26257c35dc3cSBenjamin Tissoires 		error = -EINVAL;
26267c35dc3cSBenjamin Tissoires 		goto fail;
26277c35dc3cSBenjamin Tissoires 	}
26287c35dc3cSBenjamin Tissoires 
26297c35dc3cSBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
26307c35dc3cSBenjamin Tissoires 						   &wacom->wacom_wac);
26317c35dc3cSBenjamin Tissoires 	if (error)
26327c35dc3cSBenjamin Tissoires 		goto fail;
26337c35dc3cSBenjamin Tissoires 
2634e7749f6eSBenjamin Tissoires 	remote->remotes[index].serial = serial;
263504bfa27bSBenjamin Tissoires 
26367c35dc3cSBenjamin Tissoires 	error = input_register_device(remote->remotes[index].input);
26377c35dc3cSBenjamin Tissoires 	if (error)
26387c35dc3cSBenjamin Tissoires 		goto fail;
26397c35dc3cSBenjamin Tissoires 
264097f5541fSBenjamin Tissoires 	error = wacom_led_groups_alloc_and_register_one(
264197f5541fSBenjamin Tissoires 					&remote->remotes[index].input->dev,
264297f5541fSBenjamin Tissoires 					wacom, index, 3, true);
264397f5541fSBenjamin Tissoires 	if (error)
264497f5541fSBenjamin Tissoires 		goto fail;
264597f5541fSBenjamin Tissoires 
26467c35dc3cSBenjamin Tissoires 	remote->remotes[index].registered = true;
26477c35dc3cSBenjamin Tissoires 
2648e7749f6eSBenjamin Tissoires 	devres_close_group(dev, &remote->remotes[index]);
264904bfa27bSBenjamin Tissoires 	return 0;
2650f9036bd4SBenjamin Tissoires 
2651f9036bd4SBenjamin Tissoires fail:
2652e7749f6eSBenjamin Tissoires 	devres_release_group(dev, &remote->remotes[index]);
2653e7749f6eSBenjamin Tissoires 	remote->remotes[index].serial = 0;
2654f9036bd4SBenjamin Tissoires 	return error;
265504bfa27bSBenjamin Tissoires }
265604bfa27bSBenjamin Tissoires 
wacom_remote_attach_battery(struct wacom * wacom,int index)26579f1015d4SBenjamin Tissoires static int wacom_remote_attach_battery(struct wacom *wacom, int index)
26589f1015d4SBenjamin Tissoires {
26599f1015d4SBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
26609f1015d4SBenjamin Tissoires 	int error;
26619f1015d4SBenjamin Tissoires 
26629f1015d4SBenjamin Tissoires 	if (!remote->remotes[index].registered)
26639f1015d4SBenjamin Tissoires 		return 0;
26649f1015d4SBenjamin Tissoires 
26659f1015d4SBenjamin Tissoires 	if (remote->remotes[index].battery.battery)
26669f1015d4SBenjamin Tissoires 		return 0;
26679f1015d4SBenjamin Tissoires 
26689ac6678bSAaron Armstrong Skomra 	if (!remote->remotes[index].active_time)
26699ac6678bSAaron Armstrong Skomra 		return 0;
26709ac6678bSAaron Armstrong Skomra 
26719f1015d4SBenjamin Tissoires 	if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
26729f1015d4SBenjamin Tissoires 		return 0;
26739f1015d4SBenjamin Tissoires 
26749f1015d4SBenjamin Tissoires 	error = __wacom_initialize_battery(wacom,
26759f1015d4SBenjamin Tissoires 					&wacom->remote->remotes[index].battery);
26769f1015d4SBenjamin Tissoires 	if (error)
26779f1015d4SBenjamin Tissoires 		return error;
26789f1015d4SBenjamin Tissoires 
26799f1015d4SBenjamin Tissoires 	return 0;
26809f1015d4SBenjamin Tissoires }
26819f1015d4SBenjamin Tissoires 
wacom_remote_work(struct work_struct * work)2682e6f2813aSBenjamin Tissoires static void wacom_remote_work(struct work_struct *work)
2683e6f2813aSBenjamin Tissoires {
2684e6f2813aSBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, remote_work);
268583e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
26869ac6678bSAaron Armstrong Skomra 	ktime_t kt = ktime_get();
268755ab9b2cSAaron Armstrong Skomra 	struct wacom_remote_work_data remote_work_data;
2688e6f2813aSBenjamin Tissoires 	unsigned long flags;
2689e6f2813aSBenjamin Tissoires 	unsigned int count;
269055ab9b2cSAaron Armstrong Skomra 	u32 work_serial;
269104bfa27bSBenjamin Tissoires 	int i;
2692e6f2813aSBenjamin Tissoires 
269383e6b40eSBenjamin Tissoires 	spin_lock_irqsave(&remote->remote_lock, flags);
2694e6f2813aSBenjamin Tissoires 
269555ab9b2cSAaron Armstrong Skomra 	count = kfifo_out(&remote->remote_fifo, &remote_work_data,
269655ab9b2cSAaron Armstrong Skomra 			  sizeof(remote_work_data));
2697e6f2813aSBenjamin Tissoires 
269855ab9b2cSAaron Armstrong Skomra 	if (count != sizeof(remote_work_data)) {
2699e6f2813aSBenjamin Tissoires 		hid_err(wacom->hdev,
2700e6f2813aSBenjamin Tissoires 			"workitem triggered without status available\n");
270183e6b40eSBenjamin Tissoires 		spin_unlock_irqrestore(&remote->remote_lock, flags);
2702e6f2813aSBenjamin Tissoires 		return;
2703e6f2813aSBenjamin Tissoires 	}
2704e6f2813aSBenjamin Tissoires 
270583e6b40eSBenjamin Tissoires 	if (!kfifo_is_empty(&remote->remote_fifo))
2706e6f2813aSBenjamin Tissoires 		wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2707e6f2813aSBenjamin Tissoires 
270883e6b40eSBenjamin Tissoires 	spin_unlock_irqrestore(&remote->remote_lock, flags);
2709e6f2813aSBenjamin Tissoires 
2710e6f2813aSBenjamin Tissoires 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
271155ab9b2cSAaron Armstrong Skomra 		work_serial = remote_work_data.remote[i].serial;
271255ab9b2cSAaron Armstrong Skomra 		if (work_serial) {
2713e6f2813aSBenjamin Tissoires 
27149ac6678bSAaron Armstrong Skomra 			if (kt - remote->remotes[i].active_time > WACOM_REMOTE_BATTERY_TIMEOUT
27159ac6678bSAaron Armstrong Skomra 			    && remote->remotes[i].active_time != 0)
27169ac6678bSAaron Armstrong Skomra 				wacom_remote_destroy_battery(wacom, i);
27179ac6678bSAaron Armstrong Skomra 
271855ab9b2cSAaron Armstrong Skomra 			if (remote->remotes[i].serial == work_serial) {
27199f1015d4SBenjamin Tissoires 				wacom_remote_attach_battery(wacom, i);
2720e6f2813aSBenjamin Tissoires 				continue;
27219f1015d4SBenjamin Tissoires 			}
2722e6f2813aSBenjamin Tissoires 
2723e7749f6eSBenjamin Tissoires 			if (remote->remotes[i].serial)
272404bfa27bSBenjamin Tissoires 				wacom_remote_destroy_one(wacom, i);
2725e6f2813aSBenjamin Tissoires 
272655ab9b2cSAaron Armstrong Skomra 			wacom_remote_create_one(wacom, work_serial, i);
2727e6f2813aSBenjamin Tissoires 
2728e7749f6eSBenjamin Tissoires 		} else if (remote->remotes[i].serial) {
272904bfa27bSBenjamin Tissoires 			wacom_remote_destroy_one(wacom, i);
2730e6f2813aSBenjamin Tissoires 		}
2731e6f2813aSBenjamin Tissoires 	}
2732e6f2813aSBenjamin Tissoires }
2733e6f2813aSBenjamin Tissoires 
wacom_mode_change_work(struct work_struct * work)27344082da80SBenjamin Tissoires static void wacom_mode_change_work(struct work_struct *work)
27354082da80SBenjamin Tissoires {
27364082da80SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, mode_change_work);
27374082da80SBenjamin Tissoires 	struct wacom_shared *shared = wacom->wacom_wac.shared;
27384082da80SBenjamin Tissoires 	struct wacom *wacom1 = NULL;
27394082da80SBenjamin Tissoires 	struct wacom *wacom2 = NULL;
27404082da80SBenjamin Tissoires 	bool is_direct = wacom->wacom_wac.is_direct_mode;
27414082da80SBenjamin Tissoires 	int error = 0;
27424082da80SBenjamin Tissoires 
27434082da80SBenjamin Tissoires 	if (shared->pen) {
27444082da80SBenjamin Tissoires 		wacom1 = hid_get_drvdata(shared->pen);
27454082da80SBenjamin Tissoires 		wacom_release_resources(wacom1);
27464082da80SBenjamin Tissoires 		hid_hw_stop(wacom1->hdev);
27474082da80SBenjamin Tissoires 		wacom1->wacom_wac.has_mode_change = true;
27484082da80SBenjamin Tissoires 		wacom1->wacom_wac.is_direct_mode = is_direct;
27494082da80SBenjamin Tissoires 	}
27504082da80SBenjamin Tissoires 
27514082da80SBenjamin Tissoires 	if (shared->touch) {
27524082da80SBenjamin Tissoires 		wacom2 = hid_get_drvdata(shared->touch);
27534082da80SBenjamin Tissoires 		wacom_release_resources(wacom2);
27544082da80SBenjamin Tissoires 		hid_hw_stop(wacom2->hdev);
27554082da80SBenjamin Tissoires 		wacom2->wacom_wac.has_mode_change = true;
27564082da80SBenjamin Tissoires 		wacom2->wacom_wac.is_direct_mode = is_direct;
27574082da80SBenjamin Tissoires 	}
27584082da80SBenjamin Tissoires 
27594082da80SBenjamin Tissoires 	if (wacom1) {
27604082da80SBenjamin Tissoires 		error = wacom_parse_and_register(wacom1, false);
27614082da80SBenjamin Tissoires 		if (error)
27624082da80SBenjamin Tissoires 			return;
27634082da80SBenjamin Tissoires 	}
27644082da80SBenjamin Tissoires 
27654082da80SBenjamin Tissoires 	if (wacom2) {
27664082da80SBenjamin Tissoires 		error = wacom_parse_and_register(wacom2, false);
27674082da80SBenjamin Tissoires 		if (error)
27684082da80SBenjamin Tissoires 			return;
27694082da80SBenjamin Tissoires 	}
27704082da80SBenjamin Tissoires 
2771c58ac3a8SBenjamin Tissoires 	return;
2772c58ac3a8SBenjamin Tissoires }
2773c58ac3a8SBenjamin Tissoires 
wacom_probe(struct hid_device * hdev,const struct hid_device_id * id)2774c58ac3a8SBenjamin Tissoires static int wacom_probe(struct hid_device *hdev,
2775c58ac3a8SBenjamin Tissoires 		const struct hid_device_id *id)
2776c58ac3a8SBenjamin Tissoires {
2777c58ac3a8SBenjamin Tissoires 	struct wacom *wacom;
2778c58ac3a8SBenjamin Tissoires 	struct wacom_wac *wacom_wac;
2779c58ac3a8SBenjamin Tissoires 	struct wacom_features *features;
2780c58ac3a8SBenjamin Tissoires 	int error;
2781c58ac3a8SBenjamin Tissoires 
2782c58ac3a8SBenjamin Tissoires 	if (!id->driver_data)
2783c58ac3a8SBenjamin Tissoires 		return -EINVAL;
2784c58ac3a8SBenjamin Tissoires 
2785c58ac3a8SBenjamin Tissoires 	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2786c58ac3a8SBenjamin Tissoires 
2787c58ac3a8SBenjamin Tissoires 	/* hid-core sets this quirk for the boot interface */
2788c58ac3a8SBenjamin Tissoires 	hdev->quirks &= ~HID_QUIRK_NOGET;
2789c58ac3a8SBenjamin Tissoires 
279019b64330SBenjamin Tissoires 	wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2791c58ac3a8SBenjamin Tissoires 	if (!wacom)
2792c58ac3a8SBenjamin Tissoires 		return -ENOMEM;
2793c58ac3a8SBenjamin Tissoires 
2794c58ac3a8SBenjamin Tissoires 	hid_set_drvdata(hdev, wacom);
2795c58ac3a8SBenjamin Tissoires 	wacom->hdev = hdev;
2796c58ac3a8SBenjamin Tissoires 
2797c58ac3a8SBenjamin Tissoires 	wacom_wac = &wacom->wacom_wac;
2798c58ac3a8SBenjamin Tissoires 	wacom_wac->features = *((struct wacom_features *)id->driver_data);
2799c58ac3a8SBenjamin Tissoires 	features = &wacom_wac->features;
2800c58ac3a8SBenjamin Tissoires 
2801362c571bSBenjamin Tissoires 	if (features->check_for_hid_type && features->hid_type != hdev->type)
2802362c571bSBenjamin Tissoires 		return -ENODEV;
2803c58ac3a8SBenjamin Tissoires 
280437309f47SPing Cheng 	error = wacom_devm_kfifo_alloc(wacom);
280583417206SJason Gerecke 	if (error)
2806362c571bSBenjamin Tissoires 		return error;
280783417206SJason Gerecke 
2808c6fa1aebSJason Gerecke 	wacom_wac->hid_data.inputmode = -1;
2809326ea2a9SJason Gerecke 	wacom_wac->mode_report = -1;
2810c6fa1aebSJason Gerecke 
2811720ac467SGreg Kroah-Hartman 	if (hid_is_usb(hdev)) {
2812720ac467SGreg Kroah-Hartman 		struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2813720ac467SGreg Kroah-Hartman 		struct usb_device *dev = interface_to_usbdev(intf);
2814720ac467SGreg Kroah-Hartman 
2815c58ac3a8SBenjamin Tissoires 		wacom->usbdev = dev;
2816c58ac3a8SBenjamin Tissoires 		wacom->intf = intf;
2817720ac467SGreg Kroah-Hartman 	}
2818720ac467SGreg Kroah-Hartman 
2819c58ac3a8SBenjamin Tissoires 	mutex_init(&wacom->lock);
2820a544c619SBenjamin Tissoires 	INIT_DELAYED_WORK(&wacom->init_work, wacom_init_work);
2821d17d1f17SBenjamin Tissoires 	INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2822d17d1f17SBenjamin Tissoires 	INIT_WORK(&wacom->battery_work, wacom_battery_work);
2823e6f2813aSBenjamin Tissoires 	INIT_WORK(&wacom->remote_work, wacom_remote_work);
28244082da80SBenjamin Tissoires 	INIT_WORK(&wacom->mode_change_work, wacom_mode_change_work);
282594b17905SJason Gerecke 	timer_setup(&wacom->idleprox_timer, &wacom_idleprox_timeout, TIMER_DEFERRABLE);
2826c58ac3a8SBenjamin Tissoires 
2827c58ac3a8SBenjamin Tissoires 	/* ask for the report descriptor to be loaded by HID */
2828c58ac3a8SBenjamin Tissoires 	error = hid_parse(hdev);
2829c58ac3a8SBenjamin Tissoires 	if (error) {
2830c58ac3a8SBenjamin Tissoires 		hid_err(hdev, "parse failed\n");
2831362c571bSBenjamin Tissoires 		return error;
2832c58ac3a8SBenjamin Tissoires 	}
2833c58ac3a8SBenjamin Tissoires 
28341db1f392SJason Gerecke 	if (features->type == BOOTLOADER) {
28351db1f392SJason Gerecke 		hid_warn(hdev, "Using device in hidraw-only mode");
28361db1f392SJason Gerecke 		return hid_hw_start(hdev, HID_CONNECT_HIDRAW);
28371db1f392SJason Gerecke 	}
28381db1f392SJason Gerecke 
2839fd5f92b6SBenjamin Tissoires 	error = wacom_parse_and_register(wacom, false);
2840c58ac3a8SBenjamin Tissoires 	if (error)
2841362c571bSBenjamin Tissoires 		return error;
2842c58ac3a8SBenjamin Tissoires 
2843c58ac3a8SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
2844c58ac3a8SBenjamin Tissoires 		error = device_create_file(&hdev->dev, &dev_attr_speed);
2845c58ac3a8SBenjamin Tissoires 		if (error)
2846c58ac3a8SBenjamin Tissoires 			hid_warn(hdev,
2847c58ac3a8SBenjamin Tissoires 				 "can't create sysfs speed attribute err: %d\n",
2848c58ac3a8SBenjamin Tissoires 				 error);
2849c58ac3a8SBenjamin Tissoires 	}
2850c58ac3a8SBenjamin Tissoires 
28515bed0128SJason Gerecke 	wacom_wac->probe_complete = true;
2852c58ac3a8SBenjamin Tissoires 	return 0;
2853471d1714SBenjamin Tissoires }
2854471d1714SBenjamin Tissoires 
wacom_remove(struct hid_device * hdev)2855471d1714SBenjamin Tissoires static void wacom_remove(struct hid_device *hdev)
2856471d1714SBenjamin Tissoires {
2857471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
2858f6205161SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2859f6205161SBenjamin Tissoires 	struct wacom_features *features = &wacom_wac->features;
2860f6205161SBenjamin Tissoires 
2861f6205161SBenjamin Tissoires 	if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2862f6205161SBenjamin Tissoires 		hid_hw_close(hdev);
2863471d1714SBenjamin Tissoires 
2864471d1714SBenjamin Tissoires 	hid_hw_stop(hdev);
2865471d1714SBenjamin Tissoires 
2866a544c619SBenjamin Tissoires 	cancel_delayed_work_sync(&wacom->init_work);
2867d17d1f17SBenjamin Tissoires 	cancel_work_sync(&wacom->wireless_work);
2868d17d1f17SBenjamin Tissoires 	cancel_work_sync(&wacom->battery_work);
2869e6f2813aSBenjamin Tissoires 	cancel_work_sync(&wacom->remote_work);
28704082da80SBenjamin Tissoires 	cancel_work_sync(&wacom->mode_change_work);
287194b17905SJason Gerecke 	del_timer_sync(&wacom->idleprox_timer);
2872f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
2873f81a1295SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
2874471d1714SBenjamin Tissoires 
2875c0265a94SBenjamin Tissoires 	/* make sure we don't trigger the LEDs */
2876c0265a94SBenjamin Tissoires 	wacom_led_groups_release(wacom);
2877b6b1f19bSAaron Armstrong Skomra 
2878b6b1f19bSAaron Armstrong Skomra 	if (wacom->wacom_wac.features.type != REMOTE)
28795b779fc5SBenjamin Tissoires 		wacom_release_resources(wacom);
2880471d1714SBenjamin Tissoires }
2881471d1714SBenjamin Tissoires 
288241a74581SGeert Uytterhoeven #ifdef CONFIG_PM
wacom_resume(struct hid_device * hdev)2883471d1714SBenjamin Tissoires static int wacom_resume(struct hid_device *hdev)
2884471d1714SBenjamin Tissoires {
2885471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
2886471d1714SBenjamin Tissoires 
2887471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
2888471d1714SBenjamin Tissoires 
2889471d1714SBenjamin Tissoires 	/* switch to wacom mode first */
2890a544c619SBenjamin Tissoires 	_wacom_query_tablet_data(wacom);
2891471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
2892471d1714SBenjamin Tissoires 
2893471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
2894471d1714SBenjamin Tissoires 
2895471d1714SBenjamin Tissoires 	return 0;
2896471d1714SBenjamin Tissoires }
2897471d1714SBenjamin Tissoires 
wacom_reset_resume(struct hid_device * hdev)2898471d1714SBenjamin Tissoires static int wacom_reset_resume(struct hid_device *hdev)
2899471d1714SBenjamin Tissoires {
2900471d1714SBenjamin Tissoires 	return wacom_resume(hdev);
2901471d1714SBenjamin Tissoires }
290241a74581SGeert Uytterhoeven #endif /* CONFIG_PM */
2903471d1714SBenjamin Tissoires 
2904471d1714SBenjamin Tissoires static struct hid_driver wacom_driver = {
2905471d1714SBenjamin Tissoires 	.name =		"wacom",
2906471d1714SBenjamin Tissoires 	.id_table =	wacom_ids,
2907471d1714SBenjamin Tissoires 	.probe =	wacom_probe,
2908471d1714SBenjamin Tissoires 	.remove =	wacom_remove,
29097704ac93SBenjamin Tissoires 	.report =	wacom_wac_report,
2910471d1714SBenjamin Tissoires #ifdef CONFIG_PM
2911471d1714SBenjamin Tissoires 	.resume =	wacom_resume,
2912471d1714SBenjamin Tissoires 	.reset_resume =	wacom_reset_resume,
2913471d1714SBenjamin Tissoires #endif
2914471d1714SBenjamin Tissoires 	.raw_event =	wacom_raw_event,
2915471d1714SBenjamin Tissoires };
2916471d1714SBenjamin Tissoires module_hid_driver(wacom_driver);
2917f2e0a7d4SBenjamin Tissoires 
2918f2e0a7d4SBenjamin Tissoires MODULE_VERSION(DRIVER_VERSION);
2919f2e0a7d4SBenjamin Tissoires MODULE_AUTHOR(DRIVER_AUTHOR);
2920f2e0a7d4SBenjamin Tissoires MODULE_DESCRIPTION(DRIVER_DESC);
29217021b600SGrant Grundler MODULE_LICENSE("GPL");
2922