xref: /openbmc/linux/drivers/hid/wacom_sys.c (revision 136ae5e9bacebbd8f2a94e8dea69f0dc22fbc8c7)
1471d1714SBenjamin Tissoires /*
2471d1714SBenjamin Tissoires  * drivers/input/tablet/wacom_sys.c
3471d1714SBenjamin Tissoires  *
4471d1714SBenjamin Tissoires  *  USB Wacom tablet support - system specific code
5471d1714SBenjamin Tissoires  */
6471d1714SBenjamin Tissoires 
7471d1714SBenjamin Tissoires /*
8471d1714SBenjamin Tissoires  * This program is free software; you can redistribute it and/or modify
9471d1714SBenjamin Tissoires  * it under the terms of the GNU General Public License as published by
10471d1714SBenjamin Tissoires  * the Free Software Foundation; either version 2 of the License, or
11471d1714SBenjamin Tissoires  * (at your option) any later version.
12471d1714SBenjamin Tissoires  */
13471d1714SBenjamin Tissoires 
14471d1714SBenjamin Tissoires #include "wacom_wac.h"
15471d1714SBenjamin Tissoires #include "wacom.h"
16b58ba1baSJason Gerecke #include <linux/input/mt.h>
17471d1714SBenjamin Tissoires 
18471d1714SBenjamin Tissoires #define WAC_MSG_RETRIES		5
19471d1714SBenjamin Tissoires 
20912ca216SPing Cheng #define WAC_CMD_WL_LED_CONTROL	0x03
21471d1714SBenjamin Tissoires #define WAC_CMD_LED_CONTROL	0x20
22471d1714SBenjamin Tissoires #define WAC_CMD_ICON_START	0x21
23471d1714SBenjamin Tissoires #define WAC_CMD_ICON_XFER	0x23
24849e2f06SBenjamin Tissoires #define WAC_CMD_ICON_BT_XFER	0x26
25471d1714SBenjamin Tissoires #define WAC_CMD_RETRIES		10
2672b236d6SAaron Skomra #define WAC_CMD_DELETE_PAIRING	0x20
2772b236d6SAaron Skomra #define WAC_CMD_UNPAIR_ALL	0xFF
28471d1714SBenjamin Tissoires 
29e0984bc3SPing Cheng #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
30e0984bc3SPing Cheng #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
3172b236d6SAaron Skomra #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
32e0984bc3SPing Cheng 
33c64d8834SPing Cheng static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
34c64d8834SPing Cheng 			    size_t size, unsigned int retries)
35471d1714SBenjamin Tissoires {
36471d1714SBenjamin Tissoires 	int retval;
37471d1714SBenjamin Tissoires 
38471d1714SBenjamin Tissoires 	do {
39c64d8834SPing Cheng 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
40471d1714SBenjamin Tissoires 				HID_REQ_GET_REPORT);
41aef3156dSJason Gerecke 	} while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
42aef3156dSJason Gerecke 
43aef3156dSJason Gerecke 	if (retval < 0)
44aef3156dSJason Gerecke 		hid_err(hdev, "wacom_get_report: ran out of retries "
45aef3156dSJason Gerecke 			"(last error = %d)\n", retval);
46471d1714SBenjamin Tissoires 
47471d1714SBenjamin Tissoires 	return retval;
48471d1714SBenjamin Tissoires }
49471d1714SBenjamin Tissoires 
50296b7378SPrzemo Firszt static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
51296b7378SPrzemo Firszt 			    size_t size, unsigned int retries)
52471d1714SBenjamin Tissoires {
53471d1714SBenjamin Tissoires 	int retval;
54471d1714SBenjamin Tissoires 
55471d1714SBenjamin Tissoires 	do {
56296b7378SPrzemo Firszt 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
57471d1714SBenjamin Tissoires 				HID_REQ_SET_REPORT);
58aef3156dSJason Gerecke 	} while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
59aef3156dSJason Gerecke 
60aef3156dSJason Gerecke 	if (retval < 0)
61aef3156dSJason Gerecke 		hid_err(hdev, "wacom_set_report: ran out of retries "
62aef3156dSJason Gerecke 			"(last error = %d)\n", retval);
63471d1714SBenjamin Tissoires 
64471d1714SBenjamin Tissoires 	return retval;
65471d1714SBenjamin Tissoires }
66471d1714SBenjamin Tissoires 
67471d1714SBenjamin Tissoires static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
68471d1714SBenjamin Tissoires 		u8 *raw_data, int size)
69471d1714SBenjamin Tissoires {
70471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
71471d1714SBenjamin Tissoires 
72471d1714SBenjamin Tissoires 	if (size > WACOM_PKGLEN_MAX)
73471d1714SBenjamin Tissoires 		return 1;
74471d1714SBenjamin Tissoires 
75471d1714SBenjamin Tissoires 	memcpy(wacom->wacom_wac.data, raw_data, size);
76471d1714SBenjamin Tissoires 
77471d1714SBenjamin Tissoires 	wacom_wac_irq(&wacom->wacom_wac, size);
78471d1714SBenjamin Tissoires 
79471d1714SBenjamin Tissoires 	return 0;
80471d1714SBenjamin Tissoires }
81471d1714SBenjamin Tissoires 
82471d1714SBenjamin Tissoires static int wacom_open(struct input_dev *dev)
83471d1714SBenjamin Tissoires {
84471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
85471d1714SBenjamin Tissoires 
86dff67416SBenjamin Tissoires 	return hid_hw_open(wacom->hdev);
87471d1714SBenjamin Tissoires }
88471d1714SBenjamin Tissoires 
89471d1714SBenjamin Tissoires static void wacom_close(struct input_dev *dev)
90471d1714SBenjamin Tissoires {
91471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
92471d1714SBenjamin Tissoires 
933dad188eSBenjamin Tissoires 	/*
943dad188eSBenjamin Tissoires 	 * wacom->hdev should never be null, but surprisingly, I had the case
953dad188eSBenjamin Tissoires 	 * once while unplugging the Wacom Wireless Receiver.
963dad188eSBenjamin Tissoires 	 */
973dad188eSBenjamin Tissoires 	if (wacom->hdev)
98471d1714SBenjamin Tissoires 		hid_hw_close(wacom->hdev);
99471d1714SBenjamin Tissoires }
100471d1714SBenjamin Tissoires 
101471d1714SBenjamin Tissoires /*
102471d1714SBenjamin Tissoires  * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
103471d1714SBenjamin Tissoires  */
104471d1714SBenjamin Tissoires static int wacom_calc_hid_res(int logical_extents, int physical_extents,
105471d1714SBenjamin Tissoires 			       unsigned unit, int exponent)
106471d1714SBenjamin Tissoires {
107471d1714SBenjamin Tissoires 	struct hid_field field = {
108471d1714SBenjamin Tissoires 		.logical_maximum = logical_extents,
109471d1714SBenjamin Tissoires 		.physical_maximum = physical_extents,
110471d1714SBenjamin Tissoires 		.unit = unit,
111471d1714SBenjamin Tissoires 		.unit_exponent = exponent,
112471d1714SBenjamin Tissoires 	};
113471d1714SBenjamin Tissoires 
114471d1714SBenjamin Tissoires 	return hidinput_calc_abs_res(&field, ABS_X);
115471d1714SBenjamin Tissoires }
116471d1714SBenjamin Tissoires 
117471d1714SBenjamin Tissoires static void wacom_feature_mapping(struct hid_device *hdev,
118471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
119471d1714SBenjamin Tissoires {
120471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
121471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
1225ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
1238ffffd52SBenjamin Tissoires 	u8 *data;
1248ffffd52SBenjamin Tissoires 	int ret;
125471d1714SBenjamin Tissoires 
126471d1714SBenjamin Tissoires 	switch (usage->hid) {
127471d1714SBenjamin Tissoires 	case HID_DG_CONTACTMAX:
128471d1714SBenjamin Tissoires 		/* leave touch_max as is if predefined */
1298ffffd52SBenjamin Tissoires 		if (!features->touch_max) {
1308ffffd52SBenjamin Tissoires 			/* read manually */
1318ffffd52SBenjamin Tissoires 			data = kzalloc(2, GFP_KERNEL);
1328ffffd52SBenjamin Tissoires 			if (!data)
1338ffffd52SBenjamin Tissoires 				break;
1348ffffd52SBenjamin Tissoires 			data[0] = field->report->id;
1358ffffd52SBenjamin Tissoires 			ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
13605e8fd92SJason Gerecke 						data, 2, WAC_CMD_RETRIES);
13705e8fd92SJason Gerecke 			if (ret == 2) {
1388ffffd52SBenjamin Tissoires 				features->touch_max = data[1];
13905e8fd92SJason Gerecke 			} else {
14005e8fd92SJason Gerecke 				features->touch_max = 16;
14105e8fd92SJason Gerecke 				hid_warn(hdev, "wacom_feature_mapping: "
14205e8fd92SJason Gerecke 					 "could not get HID_DG_CONTACTMAX, "
14305e8fd92SJason Gerecke 					 "defaulting to %d\n",
14405e8fd92SJason Gerecke 					  features->touch_max);
14505e8fd92SJason Gerecke 			}
1468ffffd52SBenjamin Tissoires 			kfree(data);
1478ffffd52SBenjamin Tissoires 		}
148471d1714SBenjamin Tissoires 		break;
1495ae6e89fSBenjamin Tissoires 	case HID_DG_INPUTMODE:
1505ae6e89fSBenjamin Tissoires 		/* Ignore if value index is out of bounds. */
1515ae6e89fSBenjamin Tissoires 		if (usage->usage_index >= field->report_count) {
1525ae6e89fSBenjamin Tissoires 			dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
1535ae6e89fSBenjamin Tissoires 			break;
1545ae6e89fSBenjamin Tissoires 		}
1555ae6e89fSBenjamin Tissoires 
1565ae6e89fSBenjamin Tissoires 		hid_data->inputmode = field->report->id;
1575ae6e89fSBenjamin Tissoires 		hid_data->inputmode_index = usage->usage_index;
1585ae6e89fSBenjamin Tissoires 		break;
159326ea2a9SJason Gerecke 
160326ea2a9SJason Gerecke 	case HID_UP_DIGITIZER:
161326ea2a9SJason Gerecke 		if (field->report->id == 0x0B &&
162326ea2a9SJason Gerecke 		    (field->application == WACOM_G9_DIGITIZER ||
163326ea2a9SJason Gerecke 		     field->application == WACOM_G11_DIGITIZER)) {
164326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_report = field->report->id;
165326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_value = 0;
166326ea2a9SJason Gerecke 		}
167326ea2a9SJason Gerecke 		break;
168326ea2a9SJason Gerecke 
169326ea2a9SJason Gerecke 	case WACOM_G9_PAGE:
170326ea2a9SJason Gerecke 	case WACOM_G11_PAGE:
171326ea2a9SJason Gerecke 		if (field->report->id == 0x03 &&
172326ea2a9SJason Gerecke 		    (field->application == WACOM_G9_TOUCHSCREEN ||
173326ea2a9SJason Gerecke 		     field->application == WACOM_G11_TOUCHSCREEN)) {
174326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_report = field->report->id;
175326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_value = 0;
176326ea2a9SJason Gerecke 		}
177326ea2a9SJason Gerecke 		break;
178471d1714SBenjamin Tissoires 	}
179471d1714SBenjamin Tissoires }
180471d1714SBenjamin Tissoires 
181471d1714SBenjamin Tissoires /*
182471d1714SBenjamin Tissoires  * Interface Descriptor of wacom devices can be incomplete and
183471d1714SBenjamin Tissoires  * inconsistent so wacom_features table is used to store stylus
184471d1714SBenjamin Tissoires  * device's packet lengths, various maximum values, and tablet
185471d1714SBenjamin Tissoires  * resolution based on product ID's.
186471d1714SBenjamin Tissoires  *
187471d1714SBenjamin Tissoires  * For devices that contain 2 interfaces, wacom_features table is
188471d1714SBenjamin Tissoires  * inaccurate for the touch interface.  Since the Interface Descriptor
189471d1714SBenjamin Tissoires  * for touch interfaces has pretty complete data, this function exists
190471d1714SBenjamin Tissoires  * to query tablet for this missing information instead of hard coding in
191471d1714SBenjamin Tissoires  * an additional table.
192471d1714SBenjamin Tissoires  *
193471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a stylus will contain a
194471d1714SBenjamin Tissoires  * boot mouse application collection that is not of interest and this
195471d1714SBenjamin Tissoires  * function will ignore it.
196471d1714SBenjamin Tissoires  *
197471d1714SBenjamin Tissoires  * It also contains a digitizer application collection that also is not
198471d1714SBenjamin Tissoires  * of interest since any information it contains would be duplicate
199471d1714SBenjamin Tissoires  * of what is in wacom_features. Usually it defines a report of an array
200471d1714SBenjamin Tissoires  * of bytes that could be used as max length of the stylus packet returned.
201471d1714SBenjamin Tissoires  * If it happens to define a Digitizer-Stylus Physical Collection then
202471d1714SBenjamin Tissoires  * the X and Y logical values contain valid data but it is ignored.
203471d1714SBenjamin Tissoires  *
204471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a touch interface will contain a
205471d1714SBenjamin Tissoires  * Digitizer-Finger Physical Collection which will define both logical
206471d1714SBenjamin Tissoires  * X/Y maximum as well as the physical size of tablet. Since touch
207471d1714SBenjamin Tissoires  * interfaces haven't supported pressure or distance, this is enough
208471d1714SBenjamin Tissoires  * information to override invalid values in the wacom_features table.
209471d1714SBenjamin Tissoires  *
210471d1714SBenjamin Tissoires  * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
211471d1714SBenjamin Tissoires  * data. We deal with them after returning from this function.
212471d1714SBenjamin Tissoires  */
213471d1714SBenjamin Tissoires static void wacom_usage_mapping(struct hid_device *hdev,
214471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
215471d1714SBenjamin Tissoires {
216471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
217471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
218d97a5522SBenjamin Tissoires 	bool finger = WACOM_FINGER_FIELD(field);
219d97a5522SBenjamin Tissoires 	bool pen = WACOM_PEN_FIELD(field);
220471d1714SBenjamin Tissoires 
221471d1714SBenjamin Tissoires 	/*
222471d1714SBenjamin Tissoires 	* Requiring Stylus Usage will ignore boot mouse
223471d1714SBenjamin Tissoires 	* X/Y values and some cases of invalid Digitizer X/Y
224471d1714SBenjamin Tissoires 	* values commonly reported.
225471d1714SBenjamin Tissoires 	*/
226042628abSJason Gerecke 	if (pen)
227aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_PEN;
228042628abSJason Gerecke 	else if (finger)
229aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_TOUCH;
230042628abSJason Gerecke 	else
231471d1714SBenjamin Tissoires 		return;
232471d1714SBenjamin Tissoires 
23330ebc1aeSPing Cheng 	/*
23430ebc1aeSPing Cheng 	 * Bamboo models do not support HID_DG_CONTACTMAX.
23530ebc1aeSPing Cheng 	 * And, Bamboo Pen only descriptor contains touch.
23630ebc1aeSPing Cheng 	 */
2373b164a00SPing Cheng 	if (features->type > BAMBOO_PT) {
23830ebc1aeSPing Cheng 		/* ISDv4 touch devices at least supports one touch point */
239471d1714SBenjamin Tissoires 		if (finger && !features->touch_max)
240471d1714SBenjamin Tissoires 			features->touch_max = 1;
24130ebc1aeSPing Cheng 	}
242471d1714SBenjamin Tissoires 
243471d1714SBenjamin Tissoires 	switch (usage->hid) {
244471d1714SBenjamin Tissoires 	case HID_GD_X:
245471d1714SBenjamin Tissoires 		features->x_max = field->logical_maximum;
246471d1714SBenjamin Tissoires 		if (finger) {
247471d1714SBenjamin Tissoires 			features->x_phy = field->physical_maximum;
2483b164a00SPing Cheng 			if ((features->type != BAMBOO_PT) &&
2493b164a00SPing Cheng 			    (features->type != BAMBOO_TOUCH)) {
250471d1714SBenjamin Tissoires 				features->unit = field->unit;
251471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
252471d1714SBenjamin Tissoires 			}
253471d1714SBenjamin Tissoires 		}
254471d1714SBenjamin Tissoires 		break;
255471d1714SBenjamin Tissoires 	case HID_GD_Y:
256471d1714SBenjamin Tissoires 		features->y_max = field->logical_maximum;
257471d1714SBenjamin Tissoires 		if (finger) {
258471d1714SBenjamin Tissoires 			features->y_phy = field->physical_maximum;
2593b164a00SPing Cheng 			if ((features->type != BAMBOO_PT) &&
2603b164a00SPing Cheng 			    (features->type != BAMBOO_TOUCH)) {
261471d1714SBenjamin Tissoires 				features->unit = field->unit;
262471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
263471d1714SBenjamin Tissoires 			}
264471d1714SBenjamin Tissoires 		}
265471d1714SBenjamin Tissoires 		break;
266471d1714SBenjamin Tissoires 	case HID_DG_TIPPRESSURE:
267471d1714SBenjamin Tissoires 		if (pen)
268471d1714SBenjamin Tissoires 			features->pressure_max = field->logical_maximum;
269471d1714SBenjamin Tissoires 		break;
270471d1714SBenjamin Tissoires 	}
2717704ac93SBenjamin Tissoires 
2727704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
2737704ac93SBenjamin Tissoires 		wacom_wac_usage_mapping(hdev, field, usage);
274471d1714SBenjamin Tissoires }
275471d1714SBenjamin Tissoires 
276b58ba1baSJason Gerecke static void wacom_post_parse_hid(struct hid_device *hdev,
277b58ba1baSJason Gerecke 				 struct wacom_features *features)
278b58ba1baSJason Gerecke {
279b58ba1baSJason Gerecke 	struct wacom *wacom = hid_get_drvdata(hdev);
280b58ba1baSJason Gerecke 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
281b58ba1baSJason Gerecke 
282b58ba1baSJason Gerecke 	if (features->type == HID_GENERIC) {
283b58ba1baSJason Gerecke 		/* Any last-minute generic device setup */
284b58ba1baSJason Gerecke 		if (features->touch_max > 1) {
2852a6cdbddSJason Gerecke 			input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
286b58ba1baSJason Gerecke 				    INPUT_MT_DIRECT);
287b58ba1baSJason Gerecke 		}
288b58ba1baSJason Gerecke 	}
289b58ba1baSJason Gerecke }
290b58ba1baSJason Gerecke 
291471d1714SBenjamin Tissoires static void wacom_parse_hid(struct hid_device *hdev,
292471d1714SBenjamin Tissoires 			   struct wacom_features *features)
293471d1714SBenjamin Tissoires {
294471d1714SBenjamin Tissoires 	struct hid_report_enum *rep_enum;
295471d1714SBenjamin Tissoires 	struct hid_report *hreport;
296471d1714SBenjamin Tissoires 	int i, j;
297471d1714SBenjamin Tissoires 
298471d1714SBenjamin Tissoires 	/* check features first */
299471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
300471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
301471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++) {
302471d1714SBenjamin Tissoires 			/* Ignore if report count is out of bounds. */
303471d1714SBenjamin Tissoires 			if (hreport->field[i]->report_count < 1)
304471d1714SBenjamin Tissoires 				continue;
305471d1714SBenjamin Tissoires 
306471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++) {
307471d1714SBenjamin Tissoires 				wacom_feature_mapping(hdev, hreport->field[i],
308471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
309471d1714SBenjamin Tissoires 			}
310471d1714SBenjamin Tissoires 		}
311471d1714SBenjamin Tissoires 	}
312471d1714SBenjamin Tissoires 
313471d1714SBenjamin Tissoires 	/* now check the input usages */
314471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
315471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
316471d1714SBenjamin Tissoires 
317471d1714SBenjamin Tissoires 		if (!hreport->maxfield)
318471d1714SBenjamin Tissoires 			continue;
319471d1714SBenjamin Tissoires 
320471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++)
321471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++)
322471d1714SBenjamin Tissoires 				wacom_usage_mapping(hdev, hreport->field[i],
323471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
324471d1714SBenjamin Tissoires 	}
325b58ba1baSJason Gerecke 
326b58ba1baSJason Gerecke 	wacom_post_parse_hid(hdev, features);
327471d1714SBenjamin Tissoires }
328471d1714SBenjamin Tissoires 
3295ae6e89fSBenjamin Tissoires static int wacom_hid_set_device_mode(struct hid_device *hdev)
3305ae6e89fSBenjamin Tissoires {
3315ae6e89fSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
3325ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
3335ae6e89fSBenjamin Tissoires 	struct hid_report *r;
3345ae6e89fSBenjamin Tissoires 	struct hid_report_enum *re;
3355ae6e89fSBenjamin Tissoires 
3365ae6e89fSBenjamin Tissoires 	if (hid_data->inputmode < 0)
3375ae6e89fSBenjamin Tissoires 		return 0;
3385ae6e89fSBenjamin Tissoires 
3395ae6e89fSBenjamin Tissoires 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
3405ae6e89fSBenjamin Tissoires 	r = re->report_id_hash[hid_data->inputmode];
3415ae6e89fSBenjamin Tissoires 	if (r) {
3425ae6e89fSBenjamin Tissoires 		r->field[0]->value[hid_data->inputmode_index] = 2;
3435ae6e89fSBenjamin Tissoires 		hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
3445ae6e89fSBenjamin Tissoires 	}
3455ae6e89fSBenjamin Tissoires 	return 0;
3465ae6e89fSBenjamin Tissoires }
3475ae6e89fSBenjamin Tissoires 
348326ea2a9SJason Gerecke static int wacom_set_device_mode(struct hid_device *hdev,
349326ea2a9SJason Gerecke 				 struct wacom_wac *wacom_wac)
350471d1714SBenjamin Tissoires {
351326ea2a9SJason Gerecke 	u8 *rep_data;
352326ea2a9SJason Gerecke 	struct hid_report *r;
353326ea2a9SJason Gerecke 	struct hid_report_enum *re;
354326ea2a9SJason Gerecke 	int length;
355471d1714SBenjamin Tissoires 	int error = -ENOMEM, limit = 0;
356471d1714SBenjamin Tissoires 
357326ea2a9SJason Gerecke 	if (wacom_wac->mode_report < 0)
358326ea2a9SJason Gerecke 		return 0;
359326ea2a9SJason Gerecke 
360326ea2a9SJason Gerecke 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
361326ea2a9SJason Gerecke 	r = re->report_id_hash[wacom_wac->mode_report];
362326ea2a9SJason Gerecke 	if (!r)
363326ea2a9SJason Gerecke 		return -EINVAL;
364326ea2a9SJason Gerecke 
365326ea2a9SJason Gerecke 	rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
366471d1714SBenjamin Tissoires 	if (!rep_data)
367326ea2a9SJason Gerecke 		return -ENOMEM;
368326ea2a9SJason Gerecke 
369326ea2a9SJason Gerecke 	length = hid_report_len(r);
370471d1714SBenjamin Tissoires 
371471d1714SBenjamin Tissoires 	do {
372326ea2a9SJason Gerecke 		rep_data[0] = wacom_wac->mode_report;
373326ea2a9SJason Gerecke 		rep_data[1] = wacom_wac->mode_value;
374471d1714SBenjamin Tissoires 
375296b7378SPrzemo Firszt 		error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
376296b7378SPrzemo Firszt 					 length, 1);
377471d1714SBenjamin Tissoires 		if (error >= 0)
378471d1714SBenjamin Tissoires 			error = wacom_get_report(hdev, HID_FEATURE_REPORT,
379c64d8834SPing Cheng 			                         rep_data, length, 1);
380326ea2a9SJason Gerecke 	} while (error >= 0 &&
381326ea2a9SJason Gerecke 		 rep_data[1] != wacom_wac->mode_report &&
382326ea2a9SJason Gerecke 		 limit++ < WAC_MSG_RETRIES);
383471d1714SBenjamin Tissoires 
384471d1714SBenjamin Tissoires 	kfree(rep_data);
385471d1714SBenjamin Tissoires 
386471d1714SBenjamin Tissoires 	return error < 0 ? error : 0;
387471d1714SBenjamin Tissoires }
388471d1714SBenjamin Tissoires 
389f81a1295SBenjamin Tissoires static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
390f81a1295SBenjamin Tissoires 		struct wacom_features *features)
391f81a1295SBenjamin Tissoires {
392387142bbSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
393387142bbSBenjamin Tissoires 	int ret;
394387142bbSBenjamin Tissoires 	u8 rep_data[2];
395387142bbSBenjamin Tissoires 
396387142bbSBenjamin Tissoires 	switch (features->type) {
397387142bbSBenjamin Tissoires 	case GRAPHIRE_BT:
398387142bbSBenjamin Tissoires 		rep_data[0] = 0x03;
399387142bbSBenjamin Tissoires 		rep_data[1] = 0x00;
400296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
401296b7378SPrzemo Firszt 					3);
402387142bbSBenjamin Tissoires 
403387142bbSBenjamin Tissoires 		if (ret >= 0) {
404387142bbSBenjamin Tissoires 			rep_data[0] = speed == 0 ? 0x05 : 0x06;
405387142bbSBenjamin Tissoires 			rep_data[1] = 0x00;
406387142bbSBenjamin Tissoires 
407387142bbSBenjamin Tissoires 			ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
408296b7378SPrzemo Firszt 						rep_data, 2, 3);
409387142bbSBenjamin Tissoires 
410387142bbSBenjamin Tissoires 			if (ret >= 0) {
411387142bbSBenjamin Tissoires 				wacom->wacom_wac.bt_high_speed = speed;
412387142bbSBenjamin Tissoires 				return 0;
413387142bbSBenjamin Tissoires 			}
414387142bbSBenjamin Tissoires 		}
415387142bbSBenjamin Tissoires 
416387142bbSBenjamin Tissoires 		/*
417387142bbSBenjamin Tissoires 		 * Note that if the raw queries fail, it's not a hard failure
418387142bbSBenjamin Tissoires 		 * and it is safe to continue
419387142bbSBenjamin Tissoires 		 */
420387142bbSBenjamin Tissoires 		hid_warn(hdev, "failed to poke device, command %d, err %d\n",
421387142bbSBenjamin Tissoires 			 rep_data[0], ret);
422387142bbSBenjamin Tissoires 		break;
42381af7e61SBenjamin Tissoires 	case INTUOS4WL:
42481af7e61SBenjamin Tissoires 		if (speed == 1)
42581af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features &= ~0x20;
42681af7e61SBenjamin Tissoires 		else
42781af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features |= 0x20;
42881af7e61SBenjamin Tissoires 
42981af7e61SBenjamin Tissoires 		rep_data[0] = 0x03;
43081af7e61SBenjamin Tissoires 		rep_data[1] = wacom->wacom_wac.bt_features;
43181af7e61SBenjamin Tissoires 
432296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
433296b7378SPrzemo Firszt 					1);
43481af7e61SBenjamin Tissoires 		if (ret >= 0)
43581af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_high_speed = speed;
43681af7e61SBenjamin Tissoires 		break;
437387142bbSBenjamin Tissoires 	}
438387142bbSBenjamin Tissoires 
439f81a1295SBenjamin Tissoires 	return 0;
440f81a1295SBenjamin Tissoires }
441f81a1295SBenjamin Tissoires 
442471d1714SBenjamin Tissoires /*
443471d1714SBenjamin Tissoires  * Switch the tablet into its most-capable mode. Wacom tablets are
444471d1714SBenjamin Tissoires  * typically configured to power-up in a mode which sends mouse-like
445471d1714SBenjamin Tissoires  * reports to the OS. To get absolute position, pressure data, etc.
446471d1714SBenjamin Tissoires  * from the tablet, it is necessary to switch the tablet out of this
447471d1714SBenjamin Tissoires  * mode and into one which sends the full range of tablet data.
448471d1714SBenjamin Tissoires  */
449471d1714SBenjamin Tissoires static int wacom_query_tablet_data(struct hid_device *hdev,
450471d1714SBenjamin Tissoires 		struct wacom_features *features)
451471d1714SBenjamin Tissoires {
452326ea2a9SJason Gerecke 	struct wacom *wacom = hid_get_drvdata(hdev);
453326ea2a9SJason Gerecke 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
454326ea2a9SJason Gerecke 
455f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
456f81a1295SBenjamin Tissoires 		return wacom_bt_query_tablet_data(hdev, 1, features);
457f81a1295SBenjamin Tissoires 
458326ea2a9SJason Gerecke 	if (features->type != HID_GENERIC) {
459aa86b18cSJason Gerecke 		if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
460471d1714SBenjamin Tissoires 			if (features->type > TABLETPC) {
461471d1714SBenjamin Tissoires 				/* MT Tablet PC touch */
462326ea2a9SJason Gerecke 				wacom_wac->mode_report = 3;
463326ea2a9SJason Gerecke 				wacom_wac->mode_value = 4;
464326ea2a9SJason Gerecke 			} else if (features->type == WACOM_24HDT) {
465326ea2a9SJason Gerecke 				wacom_wac->mode_report = 18;
466326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
467326ea2a9SJason Gerecke 			} else if (features->type == WACOM_27QHDT) {
468326ea2a9SJason Gerecke 				wacom_wac->mode_report = 131;
469326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
470326ea2a9SJason Gerecke 			} else if (features->type == BAMBOO_PAD) {
471326ea2a9SJason Gerecke 				wacom_wac->mode_report = 2;
472326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
4738c97a765SBenjamin Tissoires 			}
474aa86b18cSJason Gerecke 		} else if (features->device_type & WACOM_DEVICETYPE_PEN) {
4757adb91bdSPing Cheng 			if (features->type <= BAMBOO_PT) {
476326ea2a9SJason Gerecke 				wacom_wac->mode_report = 2;
477326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
478471d1714SBenjamin Tissoires 			}
479471d1714SBenjamin Tissoires 		}
480326ea2a9SJason Gerecke 	}
481326ea2a9SJason Gerecke 
482326ea2a9SJason Gerecke 	wacom_set_device_mode(hdev, wacom_wac);
483326ea2a9SJason Gerecke 
484326ea2a9SJason Gerecke 	if (features->type == HID_GENERIC)
485326ea2a9SJason Gerecke 		return wacom_hid_set_device_mode(hdev);
486471d1714SBenjamin Tissoires 
487471d1714SBenjamin Tissoires 	return 0;
488471d1714SBenjamin Tissoires }
489471d1714SBenjamin Tissoires 
490471d1714SBenjamin Tissoires static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
491471d1714SBenjamin Tissoires 					 struct wacom_features *features)
492471d1714SBenjamin Tissoires {
493471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
494471d1714SBenjamin Tissoires 	struct usb_interface *intf = wacom->intf;
495471d1714SBenjamin Tissoires 
496471d1714SBenjamin Tissoires 	/* default features */
497471d1714SBenjamin Tissoires 	features->x_fuzz = 4;
498471d1714SBenjamin Tissoires 	features->y_fuzz = 4;
499471d1714SBenjamin Tissoires 	features->pressure_fuzz = 0;
500bef7e200SJason Gerecke 	features->distance_fuzz = 1;
501bef7e200SJason Gerecke 	features->tilt_fuzz = 1;
502471d1714SBenjamin Tissoires 
503471d1714SBenjamin Tissoires 	/*
504471d1714SBenjamin Tissoires 	 * The wireless device HID is basic and layout conflicts with
505471d1714SBenjamin Tissoires 	 * other tablets (monitor and touch interface can look like pen).
506471d1714SBenjamin Tissoires 	 * Skip the query for this type and modify defaults based on
507471d1714SBenjamin Tissoires 	 * interface number.
508471d1714SBenjamin Tissoires 	 */
509471d1714SBenjamin Tissoires 	if (features->type == WIRELESS) {
5103f14a63aSJason Gerecke 		if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
511ccad85ccSJason Gerecke 			features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
5123f14a63aSJason Gerecke 		else
513aa86b18cSJason Gerecke 			features->device_type = WACOM_DEVICETYPE_NONE;
5143f14a63aSJason Gerecke 		return;
515471d1714SBenjamin Tissoires 	}
516471d1714SBenjamin Tissoires 
517471d1714SBenjamin Tissoires 	wacom_parse_hid(hdev, features);
518471d1714SBenjamin Tissoires }
519471d1714SBenjamin Tissoires 
520471d1714SBenjamin Tissoires struct wacom_hdev_data {
521471d1714SBenjamin Tissoires 	struct list_head list;
522471d1714SBenjamin Tissoires 	struct kref kref;
523471d1714SBenjamin Tissoires 	struct hid_device *dev;
524471d1714SBenjamin Tissoires 	struct wacom_shared shared;
525471d1714SBenjamin Tissoires };
526471d1714SBenjamin Tissoires 
527471d1714SBenjamin Tissoires static LIST_HEAD(wacom_udev_list);
528471d1714SBenjamin Tissoires static DEFINE_MUTEX(wacom_udev_list_lock);
529471d1714SBenjamin Tissoires 
530471d1714SBenjamin Tissoires static bool wacom_are_sibling(struct hid_device *hdev,
531471d1714SBenjamin Tissoires 		struct hid_device *sibling)
532471d1714SBenjamin Tissoires {
533471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
534471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
535471d1714SBenjamin Tissoires 	int vid = features->oVid;
536471d1714SBenjamin Tissoires 	int pid = features->oPid;
537471d1714SBenjamin Tissoires 	int n1,n2;
538471d1714SBenjamin Tissoires 
539471d1714SBenjamin Tissoires 	if (vid == 0 && pid == 0) {
540471d1714SBenjamin Tissoires 		vid = hdev->vendor;
541471d1714SBenjamin Tissoires 		pid = hdev->product;
542471d1714SBenjamin Tissoires 	}
543471d1714SBenjamin Tissoires 
544471d1714SBenjamin Tissoires 	if (vid != sibling->vendor || pid != sibling->product)
545471d1714SBenjamin Tissoires 		return false;
546471d1714SBenjamin Tissoires 
547471d1714SBenjamin Tissoires 	/* Compare the physical path. */
548471d1714SBenjamin Tissoires 	n1 = strrchr(hdev->phys, '.') - hdev->phys;
549471d1714SBenjamin Tissoires 	n2 = strrchr(sibling->phys, '.') - sibling->phys;
550471d1714SBenjamin Tissoires 	if (n1 != n2 || n1 <= 0 || n2 <= 0)
551471d1714SBenjamin Tissoires 		return false;
552471d1714SBenjamin Tissoires 
553471d1714SBenjamin Tissoires 	return !strncmp(hdev->phys, sibling->phys, n1);
554471d1714SBenjamin Tissoires }
555471d1714SBenjamin Tissoires 
556471d1714SBenjamin Tissoires static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
557471d1714SBenjamin Tissoires {
558471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
559471d1714SBenjamin Tissoires 
560471d1714SBenjamin Tissoires 	list_for_each_entry(data, &wacom_udev_list, list) {
561471d1714SBenjamin Tissoires 		if (wacom_are_sibling(hdev, data->dev)) {
562471d1714SBenjamin Tissoires 			kref_get(&data->kref);
563471d1714SBenjamin Tissoires 			return data;
564471d1714SBenjamin Tissoires 		}
565471d1714SBenjamin Tissoires 	}
566471d1714SBenjamin Tissoires 
567471d1714SBenjamin Tissoires 	return NULL;
568471d1714SBenjamin Tissoires }
569471d1714SBenjamin Tissoires 
5701c817c83SBenjamin Tissoires static void wacom_release_shared_data(struct kref *kref)
5711c817c83SBenjamin Tissoires {
5721c817c83SBenjamin Tissoires 	struct wacom_hdev_data *data =
5731c817c83SBenjamin Tissoires 		container_of(kref, struct wacom_hdev_data, kref);
5741c817c83SBenjamin Tissoires 
5751c817c83SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
5761c817c83SBenjamin Tissoires 	list_del(&data->list);
5771c817c83SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
5781c817c83SBenjamin Tissoires 
5791c817c83SBenjamin Tissoires 	kfree(data);
5801c817c83SBenjamin Tissoires }
5811c817c83SBenjamin Tissoires 
5821c817c83SBenjamin Tissoires static void wacom_remove_shared_data(void *res)
5831c817c83SBenjamin Tissoires {
5841c817c83SBenjamin Tissoires 	struct wacom *wacom = res;
5851c817c83SBenjamin Tissoires 	struct wacom_hdev_data *data;
5861c817c83SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
5871c817c83SBenjamin Tissoires 
5881c817c83SBenjamin Tissoires 	if (wacom_wac->shared) {
5891c817c83SBenjamin Tissoires 		data = container_of(wacom_wac->shared, struct wacom_hdev_data,
5901c817c83SBenjamin Tissoires 				    shared);
5911c817c83SBenjamin Tissoires 
5921c817c83SBenjamin Tissoires 		if (wacom_wac->shared->touch == wacom->hdev)
5931c817c83SBenjamin Tissoires 			wacom_wac->shared->touch = NULL;
5941c817c83SBenjamin Tissoires 		else if (wacom_wac->shared->pen == wacom->hdev)
5951c817c83SBenjamin Tissoires 			wacom_wac->shared->pen = NULL;
5961c817c83SBenjamin Tissoires 
5971c817c83SBenjamin Tissoires 		kref_put(&data->kref, wacom_release_shared_data);
5981c817c83SBenjamin Tissoires 		wacom_wac->shared = NULL;
5991c817c83SBenjamin Tissoires 	}
6001c817c83SBenjamin Tissoires }
6011c817c83SBenjamin Tissoires 
602471d1714SBenjamin Tissoires static int wacom_add_shared_data(struct hid_device *hdev)
603471d1714SBenjamin Tissoires {
604471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
605471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
606471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
607471d1714SBenjamin Tissoires 	int retval = 0;
608471d1714SBenjamin Tissoires 
609471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
610471d1714SBenjamin Tissoires 
611471d1714SBenjamin Tissoires 	data = wacom_get_hdev_data(hdev);
612471d1714SBenjamin Tissoires 	if (!data) {
613471d1714SBenjamin Tissoires 		data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
614471d1714SBenjamin Tissoires 		if (!data) {
615471d1714SBenjamin Tissoires 			retval = -ENOMEM;
616471d1714SBenjamin Tissoires 			goto out;
617471d1714SBenjamin Tissoires 		}
618471d1714SBenjamin Tissoires 
619471d1714SBenjamin Tissoires 		kref_init(&data->kref);
620471d1714SBenjamin Tissoires 		data->dev = hdev;
621471d1714SBenjamin Tissoires 		list_add_tail(&data->list, &wacom_udev_list);
622471d1714SBenjamin Tissoires 	}
623471d1714SBenjamin Tissoires 
624471d1714SBenjamin Tissoires 	wacom_wac->shared = &data->shared;
625471d1714SBenjamin Tissoires 
6261c817c83SBenjamin Tissoires 	retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
6271c817c83SBenjamin Tissoires 	if (retval) {
6281c817c83SBenjamin Tissoires 		mutex_unlock(&wacom_udev_list_lock);
6291c817c83SBenjamin Tissoires 		wacom_remove_shared_data(wacom);
6301c817c83SBenjamin Tissoires 		return retval;
6311c817c83SBenjamin Tissoires 	}
6321c817c83SBenjamin Tissoires 
633aa86b18cSJason Gerecke 	if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
634a97ac104SBenjamin Tissoires 		wacom_wac->shared->touch = hdev;
635aa86b18cSJason Gerecke 	else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
636a97ac104SBenjamin Tissoires 		wacom_wac->shared->pen = hdev;
637a97ac104SBenjamin Tissoires 
638471d1714SBenjamin Tissoires out:
639471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
640471d1714SBenjamin Tissoires 	return retval;
641471d1714SBenjamin Tissoires }
642471d1714SBenjamin Tissoires 
643471d1714SBenjamin Tissoires static int wacom_led_control(struct wacom *wacom)
644471d1714SBenjamin Tissoires {
645471d1714SBenjamin Tissoires 	unsigned char *buf;
646471d1714SBenjamin Tissoires 	int retval;
647912ca216SPing Cheng 	unsigned char report_id = WAC_CMD_LED_CONTROL;
648912ca216SPing Cheng 	int buf_size = 9;
649471d1714SBenjamin Tissoires 
65097f5541fSBenjamin Tissoires 	if (!hid_get_drvdata(wacom->hdev))
65197f5541fSBenjamin Tissoires 		return -ENODEV;
65297f5541fSBenjamin Tissoires 
653a50aac71SBenjamin Tissoires 	if (!wacom->led.groups)
654a50aac71SBenjamin Tissoires 		return -ENOTSUPP;
655a50aac71SBenjamin Tissoires 
656912ca216SPing Cheng 	if (wacom->wacom_wac.pid) { /* wireless connected */
657912ca216SPing Cheng 		report_id = WAC_CMD_WL_LED_CONTROL;
658912ca216SPing Cheng 		buf_size = 13;
659912ca216SPing Cheng 	}
660912ca216SPing Cheng 	buf = kzalloc(buf_size, GFP_KERNEL);
661471d1714SBenjamin Tissoires 	if (!buf)
662471d1714SBenjamin Tissoires 		return -ENOMEM;
663471d1714SBenjamin Tissoires 
664471d1714SBenjamin Tissoires 	if (wacom->wacom_wac.features.type >= INTUOS5S &&
665471d1714SBenjamin Tissoires 	    wacom->wacom_wac.features.type <= INTUOSPL) {
666471d1714SBenjamin Tissoires 		/*
667471d1714SBenjamin Tissoires 		 * Touch Ring and crop mark LED luminance may take on
668471d1714SBenjamin Tissoires 		 * one of four values:
669471d1714SBenjamin Tissoires 		 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
670471d1714SBenjamin Tissoires 		 */
671a50aac71SBenjamin Tissoires 		int ring_led = wacom->led.groups[0].select & 0x03;
672471d1714SBenjamin Tissoires 		int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
673471d1714SBenjamin Tissoires 		int crop_lum = 0;
674912ca216SPing Cheng 		unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
675471d1714SBenjamin Tissoires 
676912ca216SPing Cheng 		buf[0] = report_id;
677912ca216SPing Cheng 		if (wacom->wacom_wac.pid) {
678912ca216SPing Cheng 			wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
679912ca216SPing Cheng 					 buf, buf_size, WAC_CMD_RETRIES);
680912ca216SPing Cheng 			buf[0] = report_id;
681912ca216SPing Cheng 			buf[4] = led_bits;
682912ca216SPing Cheng 		} else
683912ca216SPing Cheng 			buf[1] = led_bits;
684471d1714SBenjamin Tissoires 	}
685471d1714SBenjamin Tissoires 	else {
686a50aac71SBenjamin Tissoires 		int led = wacom->led.groups[0].select | 0x4;
687471d1714SBenjamin Tissoires 
688471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
689471d1714SBenjamin Tissoires 		    wacom->wacom_wac.features.type == WACOM_24HD)
690a50aac71SBenjamin Tissoires 			led |= (wacom->led.groups[1].select << 4) | 0x40;
691471d1714SBenjamin Tissoires 
692912ca216SPing Cheng 		buf[0] = report_id;
693471d1714SBenjamin Tissoires 		buf[1] = led;
694471d1714SBenjamin Tissoires 		buf[2] = wacom->led.llv;
695471d1714SBenjamin Tissoires 		buf[3] = wacom->led.hlv;
696471d1714SBenjamin Tissoires 		buf[4] = wacom->led.img_lum;
697471d1714SBenjamin Tissoires 	}
698471d1714SBenjamin Tissoires 
699912ca216SPing Cheng 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
700296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
701471d1714SBenjamin Tissoires 	kfree(buf);
702471d1714SBenjamin Tissoires 
703471d1714SBenjamin Tissoires 	return retval;
704471d1714SBenjamin Tissoires }
705471d1714SBenjamin Tissoires 
706849e2f06SBenjamin Tissoires static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
707849e2f06SBenjamin Tissoires 		const unsigned len, const void *img)
708471d1714SBenjamin Tissoires {
709471d1714SBenjamin Tissoires 	unsigned char *buf;
710471d1714SBenjamin Tissoires 	int i, retval;
711849e2f06SBenjamin Tissoires 	const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
712471d1714SBenjamin Tissoires 
713849e2f06SBenjamin Tissoires 	buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
714471d1714SBenjamin Tissoires 	if (!buf)
715471d1714SBenjamin Tissoires 		return -ENOMEM;
716471d1714SBenjamin Tissoires 
717471d1714SBenjamin Tissoires 	/* Send 'start' command */
718471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
719471d1714SBenjamin Tissoires 	buf[1] = 1;
720296b7378SPrzemo Firszt 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
721296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
722471d1714SBenjamin Tissoires 	if (retval < 0)
723471d1714SBenjamin Tissoires 		goto out;
724471d1714SBenjamin Tissoires 
725849e2f06SBenjamin Tissoires 	buf[0] = xfer_id;
726471d1714SBenjamin Tissoires 	buf[1] = button_id & 0x07;
727471d1714SBenjamin Tissoires 	for (i = 0; i < 4; i++) {
728471d1714SBenjamin Tissoires 		buf[2] = i;
729849e2f06SBenjamin Tissoires 		memcpy(buf + 3, img + i * chunk_len, chunk_len);
730471d1714SBenjamin Tissoires 
731471d1714SBenjamin Tissoires 		retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
732296b7378SPrzemo Firszt 					  buf, chunk_len + 3, WAC_CMD_RETRIES);
733471d1714SBenjamin Tissoires 		if (retval < 0)
734471d1714SBenjamin Tissoires 			break;
735471d1714SBenjamin Tissoires 	}
736471d1714SBenjamin Tissoires 
737471d1714SBenjamin Tissoires 	/* Send 'stop' */
738471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
739471d1714SBenjamin Tissoires 	buf[1] = 0;
740296b7378SPrzemo Firszt 	wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
741296b7378SPrzemo Firszt 			 WAC_CMD_RETRIES);
742471d1714SBenjamin Tissoires 
743471d1714SBenjamin Tissoires out:
744471d1714SBenjamin Tissoires 	kfree(buf);
745471d1714SBenjamin Tissoires 	return retval;
746471d1714SBenjamin Tissoires }
747471d1714SBenjamin Tissoires 
748471d1714SBenjamin Tissoires static ssize_t wacom_led_select_store(struct device *dev, int set_id,
749471d1714SBenjamin Tissoires 				      const char *buf, size_t count)
750471d1714SBenjamin Tissoires {
751ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
752471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
753471d1714SBenjamin Tissoires 	unsigned int id;
754471d1714SBenjamin Tissoires 	int err;
755471d1714SBenjamin Tissoires 
756471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &id);
757471d1714SBenjamin Tissoires 	if (err)
758471d1714SBenjamin Tissoires 		return err;
759471d1714SBenjamin Tissoires 
760471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
761471d1714SBenjamin Tissoires 
762a50aac71SBenjamin Tissoires 	wacom->led.groups[set_id].select = id & 0x3;
763471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
764471d1714SBenjamin Tissoires 
765471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
766471d1714SBenjamin Tissoires 
767471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
768471d1714SBenjamin Tissoires }
769471d1714SBenjamin Tissoires 
770471d1714SBenjamin Tissoires #define DEVICE_LED_SELECT_ATTR(SET_ID)					\
771471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
772471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
773471d1714SBenjamin Tissoires {									\
774471d1714SBenjamin Tissoires 	return wacom_led_select_store(dev, SET_ID, buf, count);		\
775471d1714SBenjamin Tissoires }									\
776471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
777471d1714SBenjamin Tissoires 	struct device_attribute *attr, char *buf)			\
778471d1714SBenjamin Tissoires {									\
779ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);\
780471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
78137449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n",			\
782a50aac71SBenjamin Tissoires 			 wacom->led.groups[SET_ID].select);		\
783471d1714SBenjamin Tissoires }									\
784e0984bc3SPing Cheng static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,	\
785471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_show,			\
786471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_store)
787471d1714SBenjamin Tissoires 
788471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(0);
789471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(1);
790471d1714SBenjamin Tissoires 
791471d1714SBenjamin Tissoires static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
792471d1714SBenjamin Tissoires 				     const char *buf, size_t count)
793471d1714SBenjamin Tissoires {
794471d1714SBenjamin Tissoires 	unsigned int value;
795471d1714SBenjamin Tissoires 	int err;
796471d1714SBenjamin Tissoires 
797471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &value);
798471d1714SBenjamin Tissoires 	if (err)
799471d1714SBenjamin Tissoires 		return err;
800471d1714SBenjamin Tissoires 
801471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
802471d1714SBenjamin Tissoires 
803471d1714SBenjamin Tissoires 	*dest = value & 0x7f;
804471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
805471d1714SBenjamin Tissoires 
806471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
807471d1714SBenjamin Tissoires 
808471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
809471d1714SBenjamin Tissoires }
810471d1714SBenjamin Tissoires 
811471d1714SBenjamin Tissoires #define DEVICE_LUMINANCE_ATTR(name, field)				\
812471d1714SBenjamin Tissoires static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
813471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
814471d1714SBenjamin Tissoires {									\
815ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);\
816471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
817471d1714SBenjamin Tissoires 									\
818471d1714SBenjamin Tissoires 	return wacom_luminance_store(wacom, &wacom->led.field,		\
819471d1714SBenjamin Tissoires 				     buf, count);			\
820471d1714SBenjamin Tissoires }									\
82137449adcSPing Cheng static ssize_t wacom_##name##_luminance_show(struct device *dev,	\
82237449adcSPing Cheng 	struct device_attribute *attr, char *buf)			\
82337449adcSPing Cheng {									\
82437449adcSPing Cheng 	struct wacom *wacom = dev_get_drvdata(dev);			\
82537449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);	\
82637449adcSPing Cheng }									\
827e0984bc3SPing Cheng static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,			\
82837449adcSPing Cheng 		   wacom_##name##_luminance_show,			\
82937449adcSPing Cheng 		   wacom_##name##_luminance_store)
830471d1714SBenjamin Tissoires 
831471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status0, llv);
832471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status1, hlv);
833471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(buttons, img_lum);
834471d1714SBenjamin Tissoires 
835471d1714SBenjamin Tissoires static ssize_t wacom_button_image_store(struct device *dev, int button_id,
836471d1714SBenjamin Tissoires 					const char *buf, size_t count)
837471d1714SBenjamin Tissoires {
838ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
839471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
840471d1714SBenjamin Tissoires 	int err;
841849e2f06SBenjamin Tissoires 	unsigned len;
842849e2f06SBenjamin Tissoires 	u8 xfer_id;
843471d1714SBenjamin Tissoires 
844849e2f06SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
845849e2f06SBenjamin Tissoires 		len = 256;
846849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_BT_XFER;
847849e2f06SBenjamin Tissoires 	} else {
848849e2f06SBenjamin Tissoires 		len = 1024;
849849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_XFER;
850849e2f06SBenjamin Tissoires 	}
851849e2f06SBenjamin Tissoires 
852849e2f06SBenjamin Tissoires 	if (count != len)
853471d1714SBenjamin Tissoires 		return -EINVAL;
854471d1714SBenjamin Tissoires 
855471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
856471d1714SBenjamin Tissoires 
857849e2f06SBenjamin Tissoires 	err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
858471d1714SBenjamin Tissoires 
859471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
860471d1714SBenjamin Tissoires 
861471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
862471d1714SBenjamin Tissoires }
863471d1714SBenjamin Tissoires 
864471d1714SBenjamin Tissoires #define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
865471d1714SBenjamin Tissoires static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
866471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
867471d1714SBenjamin Tissoires {									\
868471d1714SBenjamin Tissoires 	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
869471d1714SBenjamin Tissoires }									\
870e0984bc3SPing Cheng static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,	\
871471d1714SBenjamin Tissoires 		   NULL, wacom_btnimg##BUTTON_ID##_store)
872471d1714SBenjamin Tissoires 
873471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(0);
874471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(1);
875471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(2);
876471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(3);
877471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(4);
878471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(5);
879471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(6);
880471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(7);
881471d1714SBenjamin Tissoires 
882471d1714SBenjamin Tissoires static struct attribute *cintiq_led_attrs[] = {
883471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
884471d1714SBenjamin Tissoires 	&dev_attr_status_led1_select.attr,
885471d1714SBenjamin Tissoires 	NULL
886471d1714SBenjamin Tissoires };
887471d1714SBenjamin Tissoires 
888471d1714SBenjamin Tissoires static struct attribute_group cintiq_led_attr_group = {
889471d1714SBenjamin Tissoires 	.name = "wacom_led",
890471d1714SBenjamin Tissoires 	.attrs = cintiq_led_attrs,
891471d1714SBenjamin Tissoires };
892471d1714SBenjamin Tissoires 
893471d1714SBenjamin Tissoires static struct attribute *intuos4_led_attrs[] = {
894471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
895471d1714SBenjamin Tissoires 	&dev_attr_status1_luminance.attr,
896471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
897471d1714SBenjamin Tissoires 	&dev_attr_buttons_luminance.attr,
898471d1714SBenjamin Tissoires 	&dev_attr_button0_rawimg.attr,
899471d1714SBenjamin Tissoires 	&dev_attr_button1_rawimg.attr,
900471d1714SBenjamin Tissoires 	&dev_attr_button2_rawimg.attr,
901471d1714SBenjamin Tissoires 	&dev_attr_button3_rawimg.attr,
902471d1714SBenjamin Tissoires 	&dev_attr_button4_rawimg.attr,
903471d1714SBenjamin Tissoires 	&dev_attr_button5_rawimg.attr,
904471d1714SBenjamin Tissoires 	&dev_attr_button6_rawimg.attr,
905471d1714SBenjamin Tissoires 	&dev_attr_button7_rawimg.attr,
906471d1714SBenjamin Tissoires 	NULL
907471d1714SBenjamin Tissoires };
908471d1714SBenjamin Tissoires 
909471d1714SBenjamin Tissoires static struct attribute_group intuos4_led_attr_group = {
910471d1714SBenjamin Tissoires 	.name = "wacom_led",
911471d1714SBenjamin Tissoires 	.attrs = intuos4_led_attrs,
912471d1714SBenjamin Tissoires };
913471d1714SBenjamin Tissoires 
914471d1714SBenjamin Tissoires static struct attribute *intuos5_led_attrs[] = {
915471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
916471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
917471d1714SBenjamin Tissoires 	NULL
918471d1714SBenjamin Tissoires };
919471d1714SBenjamin Tissoires 
920471d1714SBenjamin Tissoires static struct attribute_group intuos5_led_attr_group = {
921471d1714SBenjamin Tissoires 	.name = "wacom_led",
922471d1714SBenjamin Tissoires 	.attrs = intuos5_led_attrs,
923471d1714SBenjamin Tissoires };
924471d1714SBenjamin Tissoires 
9252df68a88SBenjamin Tissoires struct wacom_sysfs_group_devres {
9262df68a88SBenjamin Tissoires 	struct attribute_group *group;
9272df68a88SBenjamin Tissoires 	struct kobject *root;
9282df68a88SBenjamin Tissoires };
9292df68a88SBenjamin Tissoires 
9302df68a88SBenjamin Tissoires static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
9312df68a88SBenjamin Tissoires {
9322df68a88SBenjamin Tissoires 	struct wacom_sysfs_group_devres *devres = res;
9332df68a88SBenjamin Tissoires 	struct kobject *kobj = devres->root;
9342df68a88SBenjamin Tissoires 
9352df68a88SBenjamin Tissoires 	dev_dbg(dev, "%s: dropping reference to %s\n",
9362df68a88SBenjamin Tissoires 		__func__, devres->group->name);
9372df68a88SBenjamin Tissoires 	sysfs_remove_group(kobj, devres->group);
9382df68a88SBenjamin Tissoires }
9392df68a88SBenjamin Tissoires 
940f9036bd4SBenjamin Tissoires static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
941f9036bd4SBenjamin Tissoires 					   struct kobject *root,
9422df68a88SBenjamin Tissoires 					   struct attribute_group *group)
9432df68a88SBenjamin Tissoires {
9442df68a88SBenjamin Tissoires 	struct wacom_sysfs_group_devres *devres;
9452df68a88SBenjamin Tissoires 	int error;
9462df68a88SBenjamin Tissoires 
9472df68a88SBenjamin Tissoires 	devres = devres_alloc(wacom_devm_sysfs_group_release,
9482df68a88SBenjamin Tissoires 			      sizeof(struct wacom_sysfs_group_devres),
9492df68a88SBenjamin Tissoires 			      GFP_KERNEL);
9502df68a88SBenjamin Tissoires 	if (!devres)
9512df68a88SBenjamin Tissoires 		return -ENOMEM;
9522df68a88SBenjamin Tissoires 
9532df68a88SBenjamin Tissoires 	devres->group = group;
954f9036bd4SBenjamin Tissoires 	devres->root = root;
9552df68a88SBenjamin Tissoires 
9562df68a88SBenjamin Tissoires 	error = sysfs_create_group(devres->root, group);
9572df68a88SBenjamin Tissoires 	if (error)
9582df68a88SBenjamin Tissoires 		return error;
9592df68a88SBenjamin Tissoires 
9602df68a88SBenjamin Tissoires 	devres_add(&wacom->hdev->dev, devres);
9612df68a88SBenjamin Tissoires 
9622df68a88SBenjamin Tissoires 	return 0;
9632df68a88SBenjamin Tissoires }
9642df68a88SBenjamin Tissoires 
965f9036bd4SBenjamin Tissoires static int wacom_devm_sysfs_create_group(struct wacom *wacom,
966f9036bd4SBenjamin Tissoires 					 struct attribute_group *group)
967f9036bd4SBenjamin Tissoires {
968f9036bd4SBenjamin Tissoires 	return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
969f9036bd4SBenjamin Tissoires 					       group);
970f9036bd4SBenjamin Tissoires }
971f9036bd4SBenjamin Tissoires 
97234736aa9SBenjamin Tissoires enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
97397f5541fSBenjamin Tissoires {
97497f5541fSBenjamin Tissoires 	struct wacom *wacom = led->wacom;
97597f5541fSBenjamin Tissoires 
97697f5541fSBenjamin Tissoires 	if (wacom->led.max_hlv)
97797f5541fSBenjamin Tissoires 		return led->hlv * LED_FULL / wacom->led.max_hlv;
97897f5541fSBenjamin Tissoires 
97997f5541fSBenjamin Tissoires 	if (wacom->led.max_llv)
98097f5541fSBenjamin Tissoires 		return led->llv * LED_FULL / wacom->led.max_llv;
98197f5541fSBenjamin Tissoires 
98297f5541fSBenjamin Tissoires 	/* device doesn't support brightness tuning */
98397f5541fSBenjamin Tissoires 	return LED_FULL;
98497f5541fSBenjamin Tissoires }
98597f5541fSBenjamin Tissoires 
98697f5541fSBenjamin Tissoires static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
98797f5541fSBenjamin Tissoires {
98897f5541fSBenjamin Tissoires 	struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
98997f5541fSBenjamin Tissoires 	struct wacom *wacom = led->wacom;
99097f5541fSBenjamin Tissoires 
99197f5541fSBenjamin Tissoires 	if (wacom->led.groups[led->group].select != led->id)
99297f5541fSBenjamin Tissoires 		return LED_OFF;
99397f5541fSBenjamin Tissoires 
99497f5541fSBenjamin Tissoires 	return wacom_leds_brightness_get(led);
99597f5541fSBenjamin Tissoires }
99697f5541fSBenjamin Tissoires 
99797f5541fSBenjamin Tissoires static int wacom_led_brightness_set(struct led_classdev *cdev,
99897f5541fSBenjamin Tissoires 				    enum led_brightness brightness)
99997f5541fSBenjamin Tissoires {
100097f5541fSBenjamin Tissoires 	struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
100197f5541fSBenjamin Tissoires 	struct wacom *wacom = led->wacom;
100297f5541fSBenjamin Tissoires 	int error;
100397f5541fSBenjamin Tissoires 
100497f5541fSBenjamin Tissoires 	mutex_lock(&wacom->lock);
100597f5541fSBenjamin Tissoires 
100697f5541fSBenjamin Tissoires 	if (!wacom->led.groups || (brightness == LED_OFF &&
100797f5541fSBenjamin Tissoires 	    wacom->led.groups[led->group].select != led->id)) {
100897f5541fSBenjamin Tissoires 		error = 0;
100997f5541fSBenjamin Tissoires 		goto out;
101097f5541fSBenjamin Tissoires 	}
101197f5541fSBenjamin Tissoires 
101297f5541fSBenjamin Tissoires 	led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
101397f5541fSBenjamin Tissoires 	led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
101497f5541fSBenjamin Tissoires 
101597f5541fSBenjamin Tissoires 	wacom->led.groups[led->group].select = led->id;
101697f5541fSBenjamin Tissoires 
101797f5541fSBenjamin Tissoires 	error = wacom_led_control(wacom);
101897f5541fSBenjamin Tissoires 
101997f5541fSBenjamin Tissoires out:
102097f5541fSBenjamin Tissoires 	mutex_unlock(&wacom->lock);
102197f5541fSBenjamin Tissoires 
102297f5541fSBenjamin Tissoires 	return error;
102397f5541fSBenjamin Tissoires }
102497f5541fSBenjamin Tissoires 
102597f5541fSBenjamin Tissoires static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
102697f5541fSBenjamin Tissoires 					       enum led_brightness brightness)
102797f5541fSBenjamin Tissoires {
102897f5541fSBenjamin Tissoires }
102997f5541fSBenjamin Tissoires 
103097f5541fSBenjamin Tissoires static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
103197f5541fSBenjamin Tissoires 				  struct wacom_led *led, unsigned int group,
103297f5541fSBenjamin Tissoires 				  unsigned int id, bool read_only)
103397f5541fSBenjamin Tissoires {
103497f5541fSBenjamin Tissoires 	int error;
103597f5541fSBenjamin Tissoires 	char *name;
103697f5541fSBenjamin Tissoires 
103797f5541fSBenjamin Tissoires 	name = devm_kasprintf(dev, GFP_KERNEL,
103897f5541fSBenjamin Tissoires 			      "%s::wacom-%d.%d",
103997f5541fSBenjamin Tissoires 			      dev_name(dev),
104097f5541fSBenjamin Tissoires 			      group,
104197f5541fSBenjamin Tissoires 			      id);
104297f5541fSBenjamin Tissoires 	if (!name)
104397f5541fSBenjamin Tissoires 		return -ENOMEM;
104497f5541fSBenjamin Tissoires 
104534736aa9SBenjamin Tissoires 	if (!read_only) {
104634736aa9SBenjamin Tissoires 		led->trigger.name = name;
104734736aa9SBenjamin Tissoires 		error = devm_led_trigger_register(dev, &led->trigger);
104834736aa9SBenjamin Tissoires 		if (error) {
104934736aa9SBenjamin Tissoires 			hid_err(wacom->hdev,
105034736aa9SBenjamin Tissoires 				"failed to register LED trigger %s: %d\n",
105134736aa9SBenjamin Tissoires 				led->cdev.name, error);
105234736aa9SBenjamin Tissoires 			return error;
105334736aa9SBenjamin Tissoires 		}
105434736aa9SBenjamin Tissoires 	}
105534736aa9SBenjamin Tissoires 
105697f5541fSBenjamin Tissoires 	led->group = group;
105797f5541fSBenjamin Tissoires 	led->id = id;
105897f5541fSBenjamin Tissoires 	led->wacom = wacom;
105997f5541fSBenjamin Tissoires 	led->llv = wacom->led.llv;
106097f5541fSBenjamin Tissoires 	led->hlv = wacom->led.hlv;
106197f5541fSBenjamin Tissoires 	led->cdev.name = name;
106297f5541fSBenjamin Tissoires 	led->cdev.max_brightness = LED_FULL;
106397f5541fSBenjamin Tissoires 	led->cdev.flags = LED_HW_PLUGGABLE;
106497f5541fSBenjamin Tissoires 	led->cdev.brightness_get = __wacom_led_brightness_get;
106534736aa9SBenjamin Tissoires 	if (!read_only) {
106697f5541fSBenjamin Tissoires 		led->cdev.brightness_set_blocking = wacom_led_brightness_set;
106734736aa9SBenjamin Tissoires 		led->cdev.default_trigger = led->cdev.name;
106834736aa9SBenjamin Tissoires 	} else {
106997f5541fSBenjamin Tissoires 		led->cdev.brightness_set = wacom_led_readonly_brightness_set;
107034736aa9SBenjamin Tissoires 	}
107197f5541fSBenjamin Tissoires 
107297f5541fSBenjamin Tissoires 	error = devm_led_classdev_register(dev, &led->cdev);
107397f5541fSBenjamin Tissoires 	if (error) {
107497f5541fSBenjamin Tissoires 		hid_err(wacom->hdev,
107597f5541fSBenjamin Tissoires 			"failed to register LED %s: %d\n",
107697f5541fSBenjamin Tissoires 			led->cdev.name, error);
107797f5541fSBenjamin Tissoires 		led->cdev.name = NULL;
107897f5541fSBenjamin Tissoires 		return error;
107997f5541fSBenjamin Tissoires 	}
108097f5541fSBenjamin Tissoires 
108197f5541fSBenjamin Tissoires 	return 0;
108297f5541fSBenjamin Tissoires }
108397f5541fSBenjamin Tissoires 
1084589e5060SBenjamin Tissoires static void wacom_led_groups_release_one(void *data)
1085589e5060SBenjamin Tissoires {
1086589e5060SBenjamin Tissoires 	struct wacom_group_leds *group = data;
1087589e5060SBenjamin Tissoires 
1088589e5060SBenjamin Tissoires 	devres_release_group(group->dev, group);
1089589e5060SBenjamin Tissoires }
1090589e5060SBenjamin Tissoires 
109197f5541fSBenjamin Tissoires static int wacom_led_groups_alloc_and_register_one(struct device *dev,
109297f5541fSBenjamin Tissoires 						   struct wacom *wacom,
109397f5541fSBenjamin Tissoires 						   int group_id, int count,
109497f5541fSBenjamin Tissoires 						   bool read_only)
109597f5541fSBenjamin Tissoires {
109697f5541fSBenjamin Tissoires 	struct wacom_led *leds;
109797f5541fSBenjamin Tissoires 	int i, error;
109897f5541fSBenjamin Tissoires 
109997f5541fSBenjamin Tissoires 	if (group_id >= wacom->led.count || count <= 0)
110097f5541fSBenjamin Tissoires 		return -EINVAL;
110197f5541fSBenjamin Tissoires 
110297f5541fSBenjamin Tissoires 	if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
110397f5541fSBenjamin Tissoires 		return -ENOMEM;
110497f5541fSBenjamin Tissoires 
110597f5541fSBenjamin Tissoires 	leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
110697f5541fSBenjamin Tissoires 	if (!leds) {
110797f5541fSBenjamin Tissoires 		error = -ENOMEM;
110897f5541fSBenjamin Tissoires 		goto err;
110997f5541fSBenjamin Tissoires 	}
111097f5541fSBenjamin Tissoires 
111197f5541fSBenjamin Tissoires 	wacom->led.groups[group_id].leds = leds;
111297f5541fSBenjamin Tissoires 	wacom->led.groups[group_id].count = count;
111397f5541fSBenjamin Tissoires 
111497f5541fSBenjamin Tissoires 	for (i = 0; i < count; i++) {
111597f5541fSBenjamin Tissoires 		error = wacom_led_register_one(dev, wacom, &leds[i],
111697f5541fSBenjamin Tissoires 					       group_id, i, read_only);
111797f5541fSBenjamin Tissoires 		if (error)
111897f5541fSBenjamin Tissoires 			goto err;
111997f5541fSBenjamin Tissoires 	}
112097f5541fSBenjamin Tissoires 
1121589e5060SBenjamin Tissoires 	wacom->led.groups[group_id].dev = dev;
1122589e5060SBenjamin Tissoires 
1123589e5060SBenjamin Tissoires 	devres_close_group(dev, &wacom->led.groups[group_id]);
1124589e5060SBenjamin Tissoires 
1125589e5060SBenjamin Tissoires 	/*
1126589e5060SBenjamin Tissoires 	 * There is a bug (?) in devm_led_classdev_register() in which its
1127589e5060SBenjamin Tissoires 	 * increments the refcount of the parent. If the parent is an input
1128589e5060SBenjamin Tissoires 	 * device, that means the ref count never reaches 0 when
1129589e5060SBenjamin Tissoires 	 * devm_input_device_release() gets called.
1130589e5060SBenjamin Tissoires 	 * This means that the LEDs are still there after disconnect.
1131589e5060SBenjamin Tissoires 	 * Manually force the release of the group so that the leds are released
1132589e5060SBenjamin Tissoires 	 * once we are done using them.
1133589e5060SBenjamin Tissoires 	 */
1134589e5060SBenjamin Tissoires 	error = devm_add_action_or_reset(&wacom->hdev->dev,
1135589e5060SBenjamin Tissoires 					 wacom_led_groups_release_one,
1136589e5060SBenjamin Tissoires 					 &wacom->led.groups[group_id]);
1137589e5060SBenjamin Tissoires 	if (error)
1138589e5060SBenjamin Tissoires 		return error;
1139589e5060SBenjamin Tissoires 
114097f5541fSBenjamin Tissoires 	return 0;
114197f5541fSBenjamin Tissoires 
114297f5541fSBenjamin Tissoires err:
114397f5541fSBenjamin Tissoires 	devres_release_group(dev, &wacom->led.groups[group_id]);
114497f5541fSBenjamin Tissoires 	return error;
114597f5541fSBenjamin Tissoires }
114697f5541fSBenjamin Tissoires 
114734736aa9SBenjamin Tissoires struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
114834736aa9SBenjamin Tissoires 				 unsigned int id)
114934736aa9SBenjamin Tissoires {
115034736aa9SBenjamin Tissoires 	struct wacom_group_leds *group;
115134736aa9SBenjamin Tissoires 
115234736aa9SBenjamin Tissoires 	if (group_id >= wacom->led.count)
115334736aa9SBenjamin Tissoires 		return NULL;
115434736aa9SBenjamin Tissoires 
115534736aa9SBenjamin Tissoires 	group = &wacom->led.groups[group_id];
115634736aa9SBenjamin Tissoires 
115734736aa9SBenjamin Tissoires 	if (!group->leds)
115834736aa9SBenjamin Tissoires 		return NULL;
115934736aa9SBenjamin Tissoires 
116034736aa9SBenjamin Tissoires 	id %= group->count;
116134736aa9SBenjamin Tissoires 
116234736aa9SBenjamin Tissoires 	return &group->leds[id];
116334736aa9SBenjamin Tissoires }
116434736aa9SBenjamin Tissoires 
116534736aa9SBenjamin Tissoires /**
116634736aa9SBenjamin Tissoires  * wacom_led_next: gives the next available led with a wacom trigger.
116734736aa9SBenjamin Tissoires  *
116834736aa9SBenjamin Tissoires  * returns the next available struct wacom_led which has its default trigger
116934736aa9SBenjamin Tissoires  * or the current one if none is available.
117034736aa9SBenjamin Tissoires  */
117134736aa9SBenjamin Tissoires struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
117234736aa9SBenjamin Tissoires {
117334736aa9SBenjamin Tissoires 	struct wacom_led *next_led;
117434736aa9SBenjamin Tissoires 	int group, next;
117534736aa9SBenjamin Tissoires 
117634736aa9SBenjamin Tissoires 	if (!wacom || !cur)
117734736aa9SBenjamin Tissoires 		return NULL;
117834736aa9SBenjamin Tissoires 
117934736aa9SBenjamin Tissoires 	group = cur->group;
118034736aa9SBenjamin Tissoires 	next = cur->id;
118134736aa9SBenjamin Tissoires 
118234736aa9SBenjamin Tissoires 	do {
118334736aa9SBenjamin Tissoires 		next_led = wacom_led_find(wacom, group, ++next);
118434736aa9SBenjamin Tissoires 		if (!next_led || next_led == cur)
118534736aa9SBenjamin Tissoires 			return next_led;
118634736aa9SBenjamin Tissoires 	} while (next_led->cdev.trigger != &next_led->trigger);
118734736aa9SBenjamin Tissoires 
118834736aa9SBenjamin Tissoires 	return next_led;
118934736aa9SBenjamin Tissoires }
119034736aa9SBenjamin Tissoires 
1191a50aac71SBenjamin Tissoires static void wacom_led_groups_release(void *data)
1192a50aac71SBenjamin Tissoires {
1193a50aac71SBenjamin Tissoires 	struct wacom *wacom = data;
1194a50aac71SBenjamin Tissoires 
1195a50aac71SBenjamin Tissoires 	wacom->led.groups = NULL;
119697f5541fSBenjamin Tissoires 	wacom->led.count = 0;
1197a50aac71SBenjamin Tissoires }
1198a50aac71SBenjamin Tissoires 
1199a50aac71SBenjamin Tissoires static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1200a50aac71SBenjamin Tissoires {
120197f5541fSBenjamin Tissoires 	struct device *dev = &wacom->hdev->dev;
1202a50aac71SBenjamin Tissoires 	struct wacom_group_leds *groups;
1203a50aac71SBenjamin Tissoires 	int error;
1204a50aac71SBenjamin Tissoires 
120597f5541fSBenjamin Tissoires 	groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
1206a50aac71SBenjamin Tissoires 			      GFP_KERNEL);
1207a50aac71SBenjamin Tissoires 	if (!groups)
1208a50aac71SBenjamin Tissoires 		return -ENOMEM;
1209a50aac71SBenjamin Tissoires 
121097f5541fSBenjamin Tissoires 	error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1211a50aac71SBenjamin Tissoires 	if (error)
1212a50aac71SBenjamin Tissoires 		return error;
1213a50aac71SBenjamin Tissoires 
1214a50aac71SBenjamin Tissoires 	wacom->led.groups = groups;
121597f5541fSBenjamin Tissoires 	wacom->led.count = count;
121697f5541fSBenjamin Tissoires 
121797f5541fSBenjamin Tissoires 	return 0;
121897f5541fSBenjamin Tissoires }
121997f5541fSBenjamin Tissoires 
122097f5541fSBenjamin Tissoires static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
122197f5541fSBenjamin Tissoires 					 int led_per_group, bool read_only)
122297f5541fSBenjamin Tissoires {
122397f5541fSBenjamin Tissoires 	struct device *dev;
122497f5541fSBenjamin Tissoires 	int i, error;
122597f5541fSBenjamin Tissoires 
122697f5541fSBenjamin Tissoires 	if (!wacom->wacom_wac.pad_input)
122797f5541fSBenjamin Tissoires 		return -EINVAL;
122897f5541fSBenjamin Tissoires 
122997f5541fSBenjamin Tissoires 	dev = &wacom->wacom_wac.pad_input->dev;
123097f5541fSBenjamin Tissoires 
123197f5541fSBenjamin Tissoires 	error = wacom_led_groups_allocate(wacom, group_count);
123297f5541fSBenjamin Tissoires 	if (error)
123397f5541fSBenjamin Tissoires 		return error;
123497f5541fSBenjamin Tissoires 
123597f5541fSBenjamin Tissoires 	for (i = 0; i < group_count; i++) {
123697f5541fSBenjamin Tissoires 		error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
123797f5541fSBenjamin Tissoires 								led_per_group,
123897f5541fSBenjamin Tissoires 								read_only);
123997f5541fSBenjamin Tissoires 		if (error)
124097f5541fSBenjamin Tissoires 			return error;
124197f5541fSBenjamin Tissoires 	}
1242a50aac71SBenjamin Tissoires 
1243a50aac71SBenjamin Tissoires 	return 0;
1244a50aac71SBenjamin Tissoires }
1245a50aac71SBenjamin Tissoires 
1246471d1714SBenjamin Tissoires static int wacom_initialize_leds(struct wacom *wacom)
1247471d1714SBenjamin Tissoires {
1248471d1714SBenjamin Tissoires 	int error;
1249471d1714SBenjamin Tissoires 
1250862cf553SJason Gerecke 	if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1251862cf553SJason Gerecke 		return 0;
1252862cf553SJason Gerecke 
1253471d1714SBenjamin Tissoires 	/* Initialize default values */
1254471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
1255471d1714SBenjamin Tissoires 	case INTUOS4S:
1256471d1714SBenjamin Tissoires 	case INTUOS4:
125781af7e61SBenjamin Tissoires 	case INTUOS4WL:
1258471d1714SBenjamin Tissoires 	case INTUOS4L:
1259471d1714SBenjamin Tissoires 		wacom->led.llv = 10;
1260471d1714SBenjamin Tissoires 		wacom->led.hlv = 20;
126197f5541fSBenjamin Tissoires 		wacom->led.max_llv = 127;
126297f5541fSBenjamin Tissoires 		wacom->led.max_hlv = 127;
1263471d1714SBenjamin Tissoires 		wacom->led.img_lum = 10;
1264a50aac71SBenjamin Tissoires 
126597f5541fSBenjamin Tissoires 		error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1266a50aac71SBenjamin Tissoires 		if (error) {
1267a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1268a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1269a50aac71SBenjamin Tissoires 			return error;
1270a50aac71SBenjamin Tissoires 		}
1271a50aac71SBenjamin Tissoires 
12722df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
1273471d1714SBenjamin Tissoires 						      &intuos4_led_attr_group);
1274471d1714SBenjamin Tissoires 		break;
1275471d1714SBenjamin Tissoires 
1276471d1714SBenjamin Tissoires 	case WACOM_24HD:
1277471d1714SBenjamin Tissoires 	case WACOM_21UX2:
1278471d1714SBenjamin Tissoires 		wacom->led.llv = 0;
1279471d1714SBenjamin Tissoires 		wacom->led.hlv = 0;
1280471d1714SBenjamin Tissoires 		wacom->led.img_lum = 0;
1281471d1714SBenjamin Tissoires 
128297f5541fSBenjamin Tissoires 		error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1283a50aac71SBenjamin Tissoires 		if (error) {
1284a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1285a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1286a50aac71SBenjamin Tissoires 			return error;
1287a50aac71SBenjamin Tissoires 		}
1288a50aac71SBenjamin Tissoires 
12892df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
1290471d1714SBenjamin Tissoires 						      &cintiq_led_attr_group);
1291471d1714SBenjamin Tissoires 		break;
1292471d1714SBenjamin Tissoires 
1293471d1714SBenjamin Tissoires 	case INTUOS5S:
1294471d1714SBenjamin Tissoires 	case INTUOS5:
1295471d1714SBenjamin Tissoires 	case INTUOS5L:
1296471d1714SBenjamin Tissoires 	case INTUOSPS:
1297471d1714SBenjamin Tissoires 	case INTUOSPM:
1298471d1714SBenjamin Tissoires 	case INTUOSPL:
1299471d1714SBenjamin Tissoires 		wacom->led.llv = 32;
130097f5541fSBenjamin Tissoires 		wacom->led.max_llv = 96;
1301471d1714SBenjamin Tissoires 
130297f5541fSBenjamin Tissoires 		error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1303a50aac71SBenjamin Tissoires 		if (error) {
1304a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1305a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1306a50aac71SBenjamin Tissoires 			return error;
1307a50aac71SBenjamin Tissoires 		}
1308a50aac71SBenjamin Tissoires 
13092df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
1310471d1714SBenjamin Tissoires 						      &intuos5_led_attr_group);
1311471d1714SBenjamin Tissoires 		break;
1312471d1714SBenjamin Tissoires 
1313a50aac71SBenjamin Tissoires 	case REMOTE:
131497f5541fSBenjamin Tissoires 		wacom->led.llv = 255;
131597f5541fSBenjamin Tissoires 		wacom->led.max_llv = 255;
1316a50aac71SBenjamin Tissoires 		error = wacom_led_groups_allocate(wacom, 5);
1317a50aac71SBenjamin Tissoires 		if (error) {
1318a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1319a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1320a50aac71SBenjamin Tissoires 			return error;
1321a50aac71SBenjamin Tissoires 		}
1322a50aac71SBenjamin Tissoires 		return 0;
1323a50aac71SBenjamin Tissoires 
1324471d1714SBenjamin Tissoires 	default:
1325471d1714SBenjamin Tissoires 		return 0;
1326471d1714SBenjamin Tissoires 	}
1327471d1714SBenjamin Tissoires 
1328471d1714SBenjamin Tissoires 	if (error) {
1329471d1714SBenjamin Tissoires 		hid_err(wacom->hdev,
1330471d1714SBenjamin Tissoires 			"cannot create sysfs group err: %d\n", error);
1331471d1714SBenjamin Tissoires 		return error;
1332471d1714SBenjamin Tissoires 	}
1333471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
1334471d1714SBenjamin Tissoires 
1335471d1714SBenjamin Tissoires 	return 0;
1336471d1714SBenjamin Tissoires }
1337471d1714SBenjamin Tissoires 
1338471d1714SBenjamin Tissoires static enum power_supply_property wacom_battery_props[] = {
133971fa641eSJason Gerecke 	POWER_SUPPLY_PROP_PRESENT,
1340ac8d1010SBenjamin Tissoires 	POWER_SUPPLY_PROP_STATUS,
1341471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
1342471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_CAPACITY
1343471d1714SBenjamin Tissoires };
1344471d1714SBenjamin Tissoires 
1345471d1714SBenjamin Tissoires static int wacom_battery_get_property(struct power_supply *psy,
1346471d1714SBenjamin Tissoires 				      enum power_supply_property psp,
1347471d1714SBenjamin Tissoires 				      union power_supply_propval *val)
1348471d1714SBenjamin Tissoires {
134959d69bc8SBenjamin Tissoires 	struct wacom_battery *battery = power_supply_get_drvdata(psy);
1350471d1714SBenjamin Tissoires 	int ret = 0;
1351471d1714SBenjamin Tissoires 
1352471d1714SBenjamin Tissoires 	switch (psp) {
135371fa641eSJason Gerecke 		case POWER_SUPPLY_PROP_PRESENT:
135459d69bc8SBenjamin Tissoires 			val->intval = battery->bat_connected;
135571fa641eSJason Gerecke 			break;
1356471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_SCOPE:
1357471d1714SBenjamin Tissoires 			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1358471d1714SBenjamin Tissoires 			break;
1359471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_CAPACITY:
136059d69bc8SBenjamin Tissoires 			val->intval = battery->battery_capacity;
1361ac8d1010SBenjamin Tissoires 			break;
1362ac8d1010SBenjamin Tissoires 		case POWER_SUPPLY_PROP_STATUS:
136359d69bc8SBenjamin Tissoires 			if (battery->bat_charging)
1364ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_CHARGING;
136559d69bc8SBenjamin Tissoires 			else if (battery->battery_capacity == 100 &&
136659d69bc8SBenjamin Tissoires 				    battery->ps_connected)
1367ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_FULL;
136859d69bc8SBenjamin Tissoires 			else if (battery->ps_connected)
1369b0882cb7SJason Gerecke 				val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1370ac8d1010SBenjamin Tissoires 			else
1371ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1372471d1714SBenjamin Tissoires 			break;
1373471d1714SBenjamin Tissoires 		default:
1374471d1714SBenjamin Tissoires 			ret = -EINVAL;
1375471d1714SBenjamin Tissoires 			break;
1376471d1714SBenjamin Tissoires 	}
1377471d1714SBenjamin Tissoires 
1378471d1714SBenjamin Tissoires 	return ret;
1379471d1714SBenjamin Tissoires }
1380471d1714SBenjamin Tissoires 
138159d69bc8SBenjamin Tissoires static int __wacom_initialize_battery(struct wacom *wacom,
138259d69bc8SBenjamin Tissoires 				      struct wacom_battery *battery)
1383471d1714SBenjamin Tissoires {
1384d70420b9SBenjamin Tissoires 	static atomic_t battery_no = ATOMIC_INIT(0);
1385b189da90SBenjamin Tissoires 	struct device *dev = &wacom->hdev->dev;
138659d69bc8SBenjamin Tissoires 	struct power_supply_config psy_cfg = { .drv_data = battery, };
1387*136ae5e9SBenjamin Tissoires 	struct power_supply *ps_bat;
138859d69bc8SBenjamin Tissoires 	struct power_supply_desc *bat_desc = &battery->bat_desc;
1389d70420b9SBenjamin Tissoires 	unsigned long n;
1390b189da90SBenjamin Tissoires 	int error;
1391b189da90SBenjamin Tissoires 
1392b189da90SBenjamin Tissoires 	if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1393b189da90SBenjamin Tissoires 		return -ENOMEM;
1394471d1714SBenjamin Tissoires 
1395d70420b9SBenjamin Tissoires 	n = atomic_inc_return(&battery_no) - 1;
13967dbd229eSBenjamin Tissoires 
1397297d716fSKrzysztof Kozlowski 	bat_desc->properties = wacom_battery_props;
1398297d716fSKrzysztof Kozlowski 	bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1399297d716fSKrzysztof Kozlowski 	bat_desc->get_property = wacom_battery_get_property;
140059d69bc8SBenjamin Tissoires 	sprintf(battery->bat_name, "wacom_battery_%ld", n);
140159d69bc8SBenjamin Tissoires 	bat_desc->name = battery->bat_name;
140296983296SBenjamin Tissoires 	bat_desc->type = POWER_SUPPLY_TYPE_USB;
1403297d716fSKrzysztof Kozlowski 	bat_desc->use_for_apm = 0;
1404471d1714SBenjamin Tissoires 
140559d69bc8SBenjamin Tissoires 	ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
140659d69bc8SBenjamin Tissoires 	if (IS_ERR(ps_bat)) {
140759d69bc8SBenjamin Tissoires 		error = PTR_ERR(ps_bat);
1408b189da90SBenjamin Tissoires 		goto err;
1409b189da90SBenjamin Tissoires 	}
1410471d1714SBenjamin Tissoires 
141159d69bc8SBenjamin Tissoires 	power_supply_powers(ps_bat, &wacom->hdev->dev);
141259d69bc8SBenjamin Tissoires 
141359d69bc8SBenjamin Tissoires 	battery->battery = ps_bat;
14147dbd229eSBenjamin Tissoires 
1415b189da90SBenjamin Tissoires 	devres_close_group(dev, bat_desc);
14167dbd229eSBenjamin Tissoires 	return 0;
1417b189da90SBenjamin Tissoires 
1418b189da90SBenjamin Tissoires err:
1419b189da90SBenjamin Tissoires 	devres_release_group(dev, bat_desc);
1420b189da90SBenjamin Tissoires 	return error;
1421471d1714SBenjamin Tissoires }
1422471d1714SBenjamin Tissoires 
142359d69bc8SBenjamin Tissoires static int wacom_initialize_battery(struct wacom *wacom)
142459d69bc8SBenjamin Tissoires {
142559d69bc8SBenjamin Tissoires 	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
142659d69bc8SBenjamin Tissoires 		return __wacom_initialize_battery(wacom, &wacom->battery);
142759d69bc8SBenjamin Tissoires 
142859d69bc8SBenjamin Tissoires 	return 0;
142959d69bc8SBenjamin Tissoires }
143059d69bc8SBenjamin Tissoires 
1431471d1714SBenjamin Tissoires static void wacom_destroy_battery(struct wacom *wacom)
1432471d1714SBenjamin Tissoires {
143359d69bc8SBenjamin Tissoires 	if (wacom->battery.battery) {
143459d69bc8SBenjamin Tissoires 		devres_release_group(&wacom->hdev->dev,
143559d69bc8SBenjamin Tissoires 				     &wacom->battery.bat_desc);
143659d69bc8SBenjamin Tissoires 		wacom->battery.battery = NULL;
1437471d1714SBenjamin Tissoires 	}
1438471d1714SBenjamin Tissoires }
1439471d1714SBenjamin Tissoires 
1440f81a1295SBenjamin Tissoires static ssize_t wacom_show_speed(struct device *dev,
1441f81a1295SBenjamin Tissoires 				struct device_attribute
1442f81a1295SBenjamin Tissoires 				*attr, char *buf)
1443f81a1295SBenjamin Tissoires {
1444ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1445f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1446f81a1295SBenjamin Tissoires 
1447f81a1295SBenjamin Tissoires 	return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1448f81a1295SBenjamin Tissoires }
1449f81a1295SBenjamin Tissoires 
1450f81a1295SBenjamin Tissoires static ssize_t wacom_store_speed(struct device *dev,
1451f81a1295SBenjamin Tissoires 				struct device_attribute *attr,
1452f81a1295SBenjamin Tissoires 				const char *buf, size_t count)
1453f81a1295SBenjamin Tissoires {
1454ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1455f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1456f81a1295SBenjamin Tissoires 	u8 new_speed;
1457f81a1295SBenjamin Tissoires 
1458f81a1295SBenjamin Tissoires 	if (kstrtou8(buf, 0, &new_speed))
1459f81a1295SBenjamin Tissoires 		return -EINVAL;
1460f81a1295SBenjamin Tissoires 
1461f81a1295SBenjamin Tissoires 	if (new_speed != 0 && new_speed != 1)
1462f81a1295SBenjamin Tissoires 		return -EINVAL;
1463f81a1295SBenjamin Tissoires 
1464f81a1295SBenjamin Tissoires 	wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1465f81a1295SBenjamin Tissoires 
1466f81a1295SBenjamin Tissoires 	return count;
1467f81a1295SBenjamin Tissoires }
1468f81a1295SBenjamin Tissoires 
1469e0984bc3SPing Cheng static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1470f81a1295SBenjamin Tissoires 		wacom_show_speed, wacom_store_speed);
1471f81a1295SBenjamin Tissoires 
147272b236d6SAaron Skomra 
147372b236d6SAaron Skomra static ssize_t wacom_show_remote_mode(struct kobject *kobj,
147472b236d6SAaron Skomra 				      struct kobj_attribute *kattr,
147572b236d6SAaron Skomra 				      char *buf, int index)
147672b236d6SAaron Skomra {
14772cf83833SGeliang Tang 	struct device *dev = kobj_to_dev(kobj->parent);
1478ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
147972b236d6SAaron Skomra 	struct wacom *wacom = hid_get_drvdata(hdev);
148072b236d6SAaron Skomra 	u8 mode;
148172b236d6SAaron Skomra 
1482a50aac71SBenjamin Tissoires 	mode = wacom->led.groups[index].select;
148372b236d6SAaron Skomra 	if (mode >= 0 && mode < 3)
148472b236d6SAaron Skomra 		return snprintf(buf, PAGE_SIZE, "%d\n", mode);
148572b236d6SAaron Skomra 	else
148672b236d6SAaron Skomra 		return snprintf(buf, PAGE_SIZE, "%d\n", -1);
148772b236d6SAaron Skomra }
148872b236d6SAaron Skomra 
148972b236d6SAaron Skomra #define DEVICE_EKR_ATTR_GROUP(SET_ID)					\
149072b236d6SAaron Skomra static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj,	\
149172b236d6SAaron Skomra 			       struct kobj_attribute *kattr, char *buf)	\
149272b236d6SAaron Skomra {									\
149372b236d6SAaron Skomra 	return wacom_show_remote_mode(kobj, kattr, buf, SET_ID);	\
149472b236d6SAaron Skomra }									\
149572b236d6SAaron Skomra static struct kobj_attribute remote##SET_ID##_mode_attr = {		\
149672b236d6SAaron Skomra 	.attr = {.name = "remote_mode",					\
149772b236d6SAaron Skomra 		.mode = DEV_ATTR_RO_PERM},				\
149872b236d6SAaron Skomra 	.show = wacom_show_remote##SET_ID##_mode,			\
149972b236d6SAaron Skomra };									\
150072b236d6SAaron Skomra static struct attribute *remote##SET_ID##_serial_attrs[] = {		\
150172b236d6SAaron Skomra 	&remote##SET_ID##_mode_attr.attr,				\
150272b236d6SAaron Skomra 	NULL								\
150372b236d6SAaron Skomra };									\
150472b236d6SAaron Skomra static struct attribute_group remote##SET_ID##_serial_group = {		\
150572b236d6SAaron Skomra 	.name = NULL,							\
150672b236d6SAaron Skomra 	.attrs = remote##SET_ID##_serial_attrs,				\
150772b236d6SAaron Skomra }
150872b236d6SAaron Skomra 
150972b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(0);
151072b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(1);
151172b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(2);
151272b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(3);
151372b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(4);
151472b236d6SAaron Skomra 
1515e6f2813aSBenjamin Tissoires static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1516e6f2813aSBenjamin Tissoires 					  int index)
151772b236d6SAaron Skomra {
151872b236d6SAaron Skomra 	int error = 0;
151983e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
152072b236d6SAaron Skomra 
1521e7749f6eSBenjamin Tissoires 	remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1522c1f5409bSBenjamin Tissoires 							  GFP_KERNEL,
1523c1f5409bSBenjamin Tissoires 							  "%d", serial);
1524e7749f6eSBenjamin Tissoires 	if (!remote->remotes[index].group.name)
152572b236d6SAaron Skomra 		return -ENOMEM;
152672b236d6SAaron Skomra 
1527f9036bd4SBenjamin Tissoires 	error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1528e7749f6eSBenjamin Tissoires 						&remote->remotes[index].group);
152972b236d6SAaron Skomra 	if (error) {
1530e7749f6eSBenjamin Tissoires 		remote->remotes[index].group.name = NULL;
153172b236d6SAaron Skomra 		hid_err(wacom->hdev,
153272b236d6SAaron Skomra 			"cannot create sysfs group err: %d\n", error);
153372b236d6SAaron Skomra 		return error;
153472b236d6SAaron Skomra 	}
153572b236d6SAaron Skomra 
153672b236d6SAaron Skomra 	return 0;
153772b236d6SAaron Skomra }
153872b236d6SAaron Skomra 
153972b236d6SAaron Skomra static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
154072b236d6SAaron Skomra {
154172b236d6SAaron Skomra 	const size_t buf_size = 2;
154272b236d6SAaron Skomra 	unsigned char *buf;
154372b236d6SAaron Skomra 	int retval;
154472b236d6SAaron Skomra 
154572b236d6SAaron Skomra 	buf = kzalloc(buf_size, GFP_KERNEL);
154672b236d6SAaron Skomra 	if (!buf)
154772b236d6SAaron Skomra 		return -ENOMEM;
154872b236d6SAaron Skomra 
154972b236d6SAaron Skomra 	buf[0] = WAC_CMD_DELETE_PAIRING;
155072b236d6SAaron Skomra 	buf[1] = selector;
155172b236d6SAaron Skomra 
155272b236d6SAaron Skomra 	retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
155372b236d6SAaron Skomra 				  buf_size, WAC_CMD_RETRIES);
155472b236d6SAaron Skomra 	kfree(buf);
155572b236d6SAaron Skomra 
155672b236d6SAaron Skomra 	return retval;
155772b236d6SAaron Skomra }
155872b236d6SAaron Skomra 
155972b236d6SAaron Skomra static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
156072b236d6SAaron Skomra 					 struct kobj_attribute *attr,
156172b236d6SAaron Skomra 					 const char *buf, size_t count)
156272b236d6SAaron Skomra {
156372b236d6SAaron Skomra 	unsigned char selector = 0;
15642cf83833SGeliang Tang 	struct device *dev = kobj_to_dev(kobj->parent);
1565ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
156672b236d6SAaron Skomra 	struct wacom *wacom = hid_get_drvdata(hdev);
156772b236d6SAaron Skomra 	int err;
156872b236d6SAaron Skomra 
156972b236d6SAaron Skomra 	if (!strncmp(buf, "*\n", 2)) {
157072b236d6SAaron Skomra 		selector = WAC_CMD_UNPAIR_ALL;
157172b236d6SAaron Skomra 	} else {
157272b236d6SAaron Skomra 		hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
157372b236d6SAaron Skomra 			 buf);
157472b236d6SAaron Skomra 		return -1;
157572b236d6SAaron Skomra 	}
157672b236d6SAaron Skomra 
157772b236d6SAaron Skomra 	mutex_lock(&wacom->lock);
157872b236d6SAaron Skomra 
157972b236d6SAaron Skomra 	err = wacom_cmd_unpair_remote(wacom, selector);
158072b236d6SAaron Skomra 	mutex_unlock(&wacom->lock);
158172b236d6SAaron Skomra 
158272b236d6SAaron Skomra 	return err < 0 ? err : count;
158372b236d6SAaron Skomra }
158472b236d6SAaron Skomra 
158572b236d6SAaron Skomra static struct kobj_attribute unpair_remote_attr = {
158672b236d6SAaron Skomra 	.attr = {.name = "unpair_remote", .mode = 0200},
158772b236d6SAaron Skomra 	.store = wacom_store_unpair_remote,
158872b236d6SAaron Skomra };
158972b236d6SAaron Skomra 
159072b236d6SAaron Skomra static const struct attribute *remote_unpair_attrs[] = {
159172b236d6SAaron Skomra 	&unpair_remote_attr.attr,
159272b236d6SAaron Skomra 	NULL
159372b236d6SAaron Skomra };
159472b236d6SAaron Skomra 
159583e6b40eSBenjamin Tissoires static void wacom_remotes_destroy(void *data)
159683e6b40eSBenjamin Tissoires {
159783e6b40eSBenjamin Tissoires 	struct wacom *wacom = data;
159883e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
159983e6b40eSBenjamin Tissoires 
160083e6b40eSBenjamin Tissoires 	if (!remote)
160183e6b40eSBenjamin Tissoires 		return;
160283e6b40eSBenjamin Tissoires 
160383e6b40eSBenjamin Tissoires 	kobject_put(remote->remote_dir);
160483e6b40eSBenjamin Tissoires 	kfifo_free(&remote->remote_fifo);
160583e6b40eSBenjamin Tissoires 	wacom->remote = NULL;
160683e6b40eSBenjamin Tissoires }
160783e6b40eSBenjamin Tissoires 
160883e6b40eSBenjamin Tissoires static int wacom_initialize_remotes(struct wacom *wacom)
160972b236d6SAaron Skomra {
161072b236d6SAaron Skomra 	int error = 0;
161183e6b40eSBenjamin Tissoires 	struct wacom_remote *remote;
161272b236d6SAaron Skomra 	int i;
161372b236d6SAaron Skomra 
161472b236d6SAaron Skomra 	if (wacom->wacom_wac.features.type != REMOTE)
161572b236d6SAaron Skomra 		return 0;
161672b236d6SAaron Skomra 
161783e6b40eSBenjamin Tissoires 	remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
161883e6b40eSBenjamin Tissoires 			      GFP_KERNEL);
161983e6b40eSBenjamin Tissoires 	if (!remote)
162072b236d6SAaron Skomra 		return -ENOMEM;
162172b236d6SAaron Skomra 
162283e6b40eSBenjamin Tissoires 	wacom->remote = remote;
162383e6b40eSBenjamin Tissoires 
162483e6b40eSBenjamin Tissoires 	spin_lock_init(&remote->remote_lock);
162583e6b40eSBenjamin Tissoires 
162683e6b40eSBenjamin Tissoires 	error = kfifo_alloc(&remote->remote_fifo,
162783e6b40eSBenjamin Tissoires 			5 * sizeof(struct wacom_remote_data),
162883e6b40eSBenjamin Tissoires 			GFP_KERNEL);
162983e6b40eSBenjamin Tissoires 	if (error) {
163083e6b40eSBenjamin Tissoires 		hid_err(wacom->hdev, "failed allocating remote_fifo\n");
163183e6b40eSBenjamin Tissoires 		return -ENOMEM;
163283e6b40eSBenjamin Tissoires 	}
163383e6b40eSBenjamin Tissoires 
1634e7749f6eSBenjamin Tissoires 	remote->remotes[0].group = remote0_serial_group;
1635e7749f6eSBenjamin Tissoires 	remote->remotes[1].group = remote1_serial_group;
1636e7749f6eSBenjamin Tissoires 	remote->remotes[2].group = remote2_serial_group;
1637e7749f6eSBenjamin Tissoires 	remote->remotes[3].group = remote3_serial_group;
1638e7749f6eSBenjamin Tissoires 	remote->remotes[4].group = remote4_serial_group;
163983e6b40eSBenjamin Tissoires 
164083e6b40eSBenjamin Tissoires 	remote->remote_dir = kobject_create_and_add("wacom_remote",
164183e6b40eSBenjamin Tissoires 						    &wacom->hdev->dev.kobj);
164283e6b40eSBenjamin Tissoires 	if (!remote->remote_dir)
164383e6b40eSBenjamin Tissoires 		return -ENOMEM;
164483e6b40eSBenjamin Tissoires 
164583e6b40eSBenjamin Tissoires 	error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
164672b236d6SAaron Skomra 
164772b236d6SAaron Skomra 	if (error) {
164872b236d6SAaron Skomra 		hid_err(wacom->hdev,
164972b236d6SAaron Skomra 			"cannot create sysfs group err: %d\n", error);
165072b236d6SAaron Skomra 		return error;
165172b236d6SAaron Skomra 	}
165272b236d6SAaron Skomra 
165372b236d6SAaron Skomra 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1654a50aac71SBenjamin Tissoires 		wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
1655e7749f6eSBenjamin Tissoires 		remote->remotes[i].serial = 0;
165672b236d6SAaron Skomra 	}
165772b236d6SAaron Skomra 
165883e6b40eSBenjamin Tissoires 	error = devm_add_action_or_reset(&wacom->hdev->dev,
165983e6b40eSBenjamin Tissoires 					 wacom_remotes_destroy, wacom);
166083e6b40eSBenjamin Tissoires 	if (error)
166183e6b40eSBenjamin Tissoires 		return error;
166283e6b40eSBenjamin Tissoires 
166372b236d6SAaron Skomra 	return 0;
166472b236d6SAaron Skomra }
166572b236d6SAaron Skomra 
1666471d1714SBenjamin Tissoires static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1667471d1714SBenjamin Tissoires {
1668471d1714SBenjamin Tissoires 	struct input_dev *input_dev;
1669471d1714SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
1670471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1671471d1714SBenjamin Tissoires 
16723dad188eSBenjamin Tissoires 	input_dev = devm_input_allocate_device(&hdev->dev);
1673471d1714SBenjamin Tissoires 	if (!input_dev)
1674471d1714SBenjamin Tissoires 		return NULL;
1675471d1714SBenjamin Tissoires 
16762bdd163cSJason Gerecke 	input_dev->name = wacom_wac->features.name;
1677471d1714SBenjamin Tissoires 	input_dev->phys = hdev->phys;
1678471d1714SBenjamin Tissoires 	input_dev->dev.parent = &hdev->dev;
1679471d1714SBenjamin Tissoires 	input_dev->open = wacom_open;
1680471d1714SBenjamin Tissoires 	input_dev->close = wacom_close;
1681471d1714SBenjamin Tissoires 	input_dev->uniq = hdev->uniq;
1682471d1714SBenjamin Tissoires 	input_dev->id.bustype = hdev->bus;
1683471d1714SBenjamin Tissoires 	input_dev->id.vendor  = hdev->vendor;
168412969e3bSBenjamin Tissoires 	input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1685471d1714SBenjamin Tissoires 	input_dev->id.version = hdev->version;
1686471d1714SBenjamin Tissoires 	input_set_drvdata(input_dev, wacom);
1687471d1714SBenjamin Tissoires 
1688471d1714SBenjamin Tissoires 	return input_dev;
1689471d1714SBenjamin Tissoires }
1690471d1714SBenjamin Tissoires 
1691d9f2d203SJason Gerecke static int wacom_allocate_inputs(struct wacom *wacom)
1692d9f2d203SJason Gerecke {
1693d9f2d203SJason Gerecke 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1694d9f2d203SJason Gerecke 
1695d9f2d203SJason Gerecke 	wacom_wac->pen_input = wacom_allocate_input(wacom);
1696d9f2d203SJason Gerecke 	wacom_wac->touch_input = wacom_allocate_input(wacom);
1697d9f2d203SJason Gerecke 	wacom_wac->pad_input = wacom_allocate_input(wacom);
16983dad188eSBenjamin Tissoires 	if (!wacom_wac->pen_input ||
16993dad188eSBenjamin Tissoires 	    !wacom_wac->touch_input ||
17003dad188eSBenjamin Tissoires 	    !wacom_wac->pad_input)
1701d9f2d203SJason Gerecke 		return -ENOMEM;
1702d9f2d203SJason Gerecke 
17032bdd163cSJason Gerecke 	wacom_wac->pen_input->name = wacom_wac->pen_name;
1704d9f2d203SJason Gerecke 	wacom_wac->touch_input->name = wacom_wac->touch_name;
1705d9f2d203SJason Gerecke 	wacom_wac->pad_input->name = wacom_wac->pad_name;
1706d9f2d203SJason Gerecke 
1707d9f2d203SJason Gerecke 	return 0;
1708d9f2d203SJason Gerecke }
1709d9f2d203SJason Gerecke 
1710471d1714SBenjamin Tissoires static int wacom_register_inputs(struct wacom *wacom)
1711471d1714SBenjamin Tissoires {
17122a6cdbddSJason Gerecke 	struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
1713471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
17142636a3f2SJason Gerecke 	int error = 0;
1715471d1714SBenjamin Tissoires 
17162a6cdbddSJason Gerecke 	pen_input_dev = wacom_wac->pen_input;
17172a6cdbddSJason Gerecke 	touch_input_dev = wacom_wac->touch_input;
17182546dacdSBenjamin Tissoires 	pad_input_dev = wacom_wac->pad_input;
1719471d1714SBenjamin Tissoires 
17202a6cdbddSJason Gerecke 	if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
17212546dacdSBenjamin Tissoires 		return -EINVAL;
1722471d1714SBenjamin Tissoires 
17232a6cdbddSJason Gerecke 	error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
17242a6cdbddSJason Gerecke 	if (error) {
17252a6cdbddSJason Gerecke 		/* no pen in use on this interface */
17262a6cdbddSJason Gerecke 		input_free_device(pen_input_dev);
17272a6cdbddSJason Gerecke 		wacom_wac->pen_input = NULL;
17282a6cdbddSJason Gerecke 		pen_input_dev = NULL;
17292a6cdbddSJason Gerecke 	} else {
17302a6cdbddSJason Gerecke 		error = input_register_device(pen_input_dev);
1731471d1714SBenjamin Tissoires 		if (error)
17323dad188eSBenjamin Tissoires 			goto fail;
17332a6cdbddSJason Gerecke 	}
17342a6cdbddSJason Gerecke 
17352a6cdbddSJason Gerecke 	error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
17362a6cdbddSJason Gerecke 	if (error) {
17372a6cdbddSJason Gerecke 		/* no touch in use on this interface */
17382a6cdbddSJason Gerecke 		input_free_device(touch_input_dev);
17392a6cdbddSJason Gerecke 		wacom_wac->touch_input = NULL;
17402a6cdbddSJason Gerecke 		touch_input_dev = NULL;
17412a6cdbddSJason Gerecke 	} else {
17422a6cdbddSJason Gerecke 		error = input_register_device(touch_input_dev);
17432a6cdbddSJason Gerecke 		if (error)
17443dad188eSBenjamin Tissoires 			goto fail;
174530ebc1aeSPing Cheng 	}
1746471d1714SBenjamin Tissoires 
1747471d1714SBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1748471d1714SBenjamin Tissoires 	if (error) {
1749471d1714SBenjamin Tissoires 		/* no pad in use on this interface */
1750471d1714SBenjamin Tissoires 		input_free_device(pad_input_dev);
1751471d1714SBenjamin Tissoires 		wacom_wac->pad_input = NULL;
1752471d1714SBenjamin Tissoires 		pad_input_dev = NULL;
1753471d1714SBenjamin Tissoires 	} else {
1754471d1714SBenjamin Tissoires 		error = input_register_device(pad_input_dev);
1755471d1714SBenjamin Tissoires 		if (error)
17563dad188eSBenjamin Tissoires 			goto fail;
1757471d1714SBenjamin Tissoires 	}
1758471d1714SBenjamin Tissoires 
1759471d1714SBenjamin Tissoires 	return 0;
1760471d1714SBenjamin Tissoires 
17613dad188eSBenjamin Tissoires fail:
17623dad188eSBenjamin Tissoires 	wacom_wac->pad_input = NULL;
17632a6cdbddSJason Gerecke 	wacom_wac->touch_input = NULL;
17642a6cdbddSJason Gerecke 	wacom_wac->pen_input = NULL;
1765471d1714SBenjamin Tissoires 	return error;
1766471d1714SBenjamin Tissoires }
1767471d1714SBenjamin Tissoires 
17680be01712SJason Gerecke /*
17690be01712SJason Gerecke  * Not all devices report physical dimensions from HID.
17700be01712SJason Gerecke  * Compute the default from hardcoded logical dimension
17710be01712SJason Gerecke  * and resolution before driver overwrites them.
17720be01712SJason Gerecke  */
17730be01712SJason Gerecke static void wacom_set_default_phy(struct wacom_features *features)
17740be01712SJason Gerecke {
17750be01712SJason Gerecke 	if (features->x_resolution) {
17760be01712SJason Gerecke 		features->x_phy = (features->x_max * 100) /
17770be01712SJason Gerecke 					features->x_resolution;
17780be01712SJason Gerecke 		features->y_phy = (features->y_max * 100) /
17790be01712SJason Gerecke 					features->y_resolution;
17800be01712SJason Gerecke 	}
17810be01712SJason Gerecke }
17820be01712SJason Gerecke 
17830be01712SJason Gerecke static void wacom_calculate_res(struct wacom_features *features)
17840be01712SJason Gerecke {
17850be01712SJason Gerecke 	/* set unit to "100th of a mm" for devices not reported by HID */
17860be01712SJason Gerecke 	if (!features->unit) {
17870be01712SJason Gerecke 		features->unit = 0x11;
17880be01712SJason Gerecke 		features->unitExpo = -3;
17890be01712SJason Gerecke 	}
17900be01712SJason Gerecke 
17910be01712SJason Gerecke 	features->x_resolution = wacom_calc_hid_res(features->x_max,
17920be01712SJason Gerecke 						    features->x_phy,
17930be01712SJason Gerecke 						    features->unit,
17940be01712SJason Gerecke 						    features->unitExpo);
17950be01712SJason Gerecke 	features->y_resolution = wacom_calc_hid_res(features->y_max,
17960be01712SJason Gerecke 						    features->y_phy,
17970be01712SJason Gerecke 						    features->unit,
17980be01712SJason Gerecke 						    features->unitExpo);
17990be01712SJason Gerecke }
18000be01712SJason Gerecke 
1801fce9957dSJason Gerecke void wacom_battery_work(struct work_struct *work)
1802fce9957dSJason Gerecke {
1803d17d1f17SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, battery_work);
1804fce9957dSJason Gerecke 
1805fce9957dSJason Gerecke 	if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
180659d69bc8SBenjamin Tissoires 	     !wacom->battery.battery) {
1807fce9957dSJason Gerecke 		wacom_initialize_battery(wacom);
1808fce9957dSJason Gerecke 	}
1809fce9957dSJason Gerecke 	else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
181059d69bc8SBenjamin Tissoires 		 wacom->battery.battery) {
1811fce9957dSJason Gerecke 		wacom_destroy_battery(wacom);
1812fce9957dSJason Gerecke 	}
1813fce9957dSJason Gerecke }
1814fce9957dSJason Gerecke 
1815471d1714SBenjamin Tissoires static size_t wacom_compute_pktlen(struct hid_device *hdev)
1816471d1714SBenjamin Tissoires {
1817471d1714SBenjamin Tissoires 	struct hid_report_enum *report_enum;
1818471d1714SBenjamin Tissoires 	struct hid_report *report;
1819471d1714SBenjamin Tissoires 	size_t size = 0;
1820471d1714SBenjamin Tissoires 
1821471d1714SBenjamin Tissoires 	report_enum = hdev->report_enum + HID_INPUT_REPORT;
1822471d1714SBenjamin Tissoires 
1823471d1714SBenjamin Tissoires 	list_for_each_entry(report, &report_enum->report_list, list) {
1824dabb05c6SMathieu Magnaudet 		size_t report_size = hid_report_len(report);
1825471d1714SBenjamin Tissoires 		if (report_size > size)
1826471d1714SBenjamin Tissoires 			size = report_size;
1827471d1714SBenjamin Tissoires 	}
1828471d1714SBenjamin Tissoires 
1829471d1714SBenjamin Tissoires 	return size;
1830471d1714SBenjamin Tissoires }
1831471d1714SBenjamin Tissoires 
1832fd5f92b6SBenjamin Tissoires static void wacom_update_name(struct wacom *wacom, const char *suffix)
1833c24eab4eSPing Cheng {
1834c24eab4eSPing Cheng 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1835c24eab4eSPing Cheng 	struct wacom_features *features = &wacom_wac->features;
183644b5250bSJason Gerecke 	char name[WACOM_NAME_MAX];
1837c24eab4eSPing Cheng 
1838c24eab4eSPing Cheng 	/* Generic devices name unspecified */
1839c24eab4eSPing Cheng 	if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1840c24eab4eSPing Cheng 		if (strstr(wacom->hdev->name, "Wacom") ||
1841c24eab4eSPing Cheng 		    strstr(wacom->hdev->name, "wacom") ||
1842c24eab4eSPing Cheng 		    strstr(wacom->hdev->name, "WACOM")) {
1843c24eab4eSPing Cheng 			/* name is in HID descriptor, use it */
184444b5250bSJason Gerecke 			strlcpy(name, wacom->hdev->name, sizeof(name));
1845c24eab4eSPing Cheng 
1846c24eab4eSPing Cheng 			/* strip out excess whitespaces */
1847c24eab4eSPing Cheng 			while (1) {
184844b5250bSJason Gerecke 				char *gap = strstr(name, "  ");
1849c24eab4eSPing Cheng 				if (gap == NULL)
1850c24eab4eSPing Cheng 					break;
1851c24eab4eSPing Cheng 				/* shift everything including the terminator */
1852c24eab4eSPing Cheng 				memmove(gap, gap+1, strlen(gap));
1853c24eab4eSPing Cheng 			}
1854c24eab4eSPing Cheng 			/* get rid of trailing whitespace */
185544b5250bSJason Gerecke 			if (name[strlen(name)-1] == ' ')
185644b5250bSJason Gerecke 				name[strlen(name)-1] = '\0';
1857c24eab4eSPing Cheng 		} else {
1858c24eab4eSPing Cheng 			/* no meaningful name retrieved. use product ID */
185944b5250bSJason Gerecke 			snprintf(name, sizeof(name),
1860c24eab4eSPing Cheng 				 "%s %X", features->name, wacom->hdev->product);
1861c24eab4eSPing Cheng 		}
1862c24eab4eSPing Cheng 	} else {
186344b5250bSJason Gerecke 		strlcpy(name, features->name, sizeof(name));
1864c24eab4eSPing Cheng 	}
1865c24eab4eSPing Cheng 
1866c24eab4eSPing Cheng 	/* Append the device type to the name */
18672a6cdbddSJason Gerecke 	snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
1868fd5f92b6SBenjamin Tissoires 		"%s%s Pen", name, suffix);
18692a6cdbddSJason Gerecke 	snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
1870fd5f92b6SBenjamin Tissoires 		"%s%s Finger", name, suffix);
1871c24eab4eSPing Cheng 	snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1872fd5f92b6SBenjamin Tissoires 		"%s%s Pad", name, suffix);
18738d80f790SJason Gerecke }
1874c24eab4eSPing Cheng 
187584dfbd7fSBenjamin Tissoires static void wacom_release_resources(struct wacom *wacom)
187684dfbd7fSBenjamin Tissoires {
187784dfbd7fSBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
187884dfbd7fSBenjamin Tissoires 
187984dfbd7fSBenjamin Tissoires 	if (!wacom->resources)
188084dfbd7fSBenjamin Tissoires 		return;
188184dfbd7fSBenjamin Tissoires 
188284dfbd7fSBenjamin Tissoires 	devres_release_group(&hdev->dev, wacom);
188384dfbd7fSBenjamin Tissoires 
188484dfbd7fSBenjamin Tissoires 	wacom->resources = false;
188584dfbd7fSBenjamin Tissoires 
188684dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.pen_input = NULL;
188784dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.touch_input = NULL;
188884dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.pad_input = NULL;
188984dfbd7fSBenjamin Tissoires }
189084dfbd7fSBenjamin Tissoires 
1891fd5f92b6SBenjamin Tissoires static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
1892471d1714SBenjamin Tissoires {
1893c58ac3a8SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1894c58ac3a8SBenjamin Tissoires 	struct wacom_features *features = &wacom_wac->features;
1895c58ac3a8SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
1896471d1714SBenjamin Tissoires 	int error;
18977704ac93SBenjamin Tissoires 	unsigned int connect_mask = HID_CONNECT_HIDRAW;
1898471d1714SBenjamin Tissoires 
1899471d1714SBenjamin Tissoires 	features->pktlen = wacom_compute_pktlen(hdev);
1900c58ac3a8SBenjamin Tissoires 	if (features->pktlen > WACOM_PKGLEN_MAX)
1901c58ac3a8SBenjamin Tissoires 		return -EINVAL;
1902471d1714SBenjamin Tissoires 
190384dfbd7fSBenjamin Tissoires 	if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
190484dfbd7fSBenjamin Tissoires 		return -ENOMEM;
190584dfbd7fSBenjamin Tissoires 
190684dfbd7fSBenjamin Tissoires 	wacom->resources = true;
190784dfbd7fSBenjamin Tissoires 
1908494078b0SBenjamin Tissoires 	error = wacom_allocate_inputs(wacom);
1909494078b0SBenjamin Tissoires 	if (error)
19103888b0d5SBenjamin Tissoires 		goto fail;
1911494078b0SBenjamin Tissoires 
19128c97a765SBenjamin Tissoires 	/*
19138c97a765SBenjamin Tissoires 	 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
19148c97a765SBenjamin Tissoires 	 * into debug mode for the touch part.
19158c97a765SBenjamin Tissoires 	 * We ignore the other interfaces.
19168c97a765SBenjamin Tissoires 	 */
19178c97a765SBenjamin Tissoires 	if (features->type == BAMBOO_PAD) {
19188c97a765SBenjamin Tissoires 		if (features->pktlen == WACOM_PKGLEN_PENABLED) {
19198c97a765SBenjamin Tissoires 			features->type = HID_GENERIC;
19208c97a765SBenjamin Tissoires 		} else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
19218c97a765SBenjamin Tissoires 			   (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
19228c97a765SBenjamin Tissoires 			error = -ENODEV;
19233888b0d5SBenjamin Tissoires 			goto fail;
19248c97a765SBenjamin Tissoires 		}
19258c97a765SBenjamin Tissoires 	}
19268c97a765SBenjamin Tissoires 
1927471d1714SBenjamin Tissoires 	/* set the default size in case we do not get them from hid */
1928471d1714SBenjamin Tissoires 	wacom_set_default_phy(features);
1929471d1714SBenjamin Tissoires 
1930471d1714SBenjamin Tissoires 	/* Retrieve the physical and logical size for touch devices */
1931471d1714SBenjamin Tissoires 	wacom_retrieve_hid_descriptor(hdev, features);
193242f4f272SPing Cheng 	wacom_setup_device_quirks(wacom);
1933042628abSJason Gerecke 
1934aa86b18cSJason Gerecke 	if (features->device_type == WACOM_DEVICETYPE_NONE &&
1935aa86b18cSJason Gerecke 	    features->type != WIRELESS) {
19368e116d31SJason Gerecke 		error = features->type == HID_GENERIC ? -ENODEV : 0;
19378e116d31SJason Gerecke 
1938042628abSJason Gerecke 		dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
19398e116d31SJason Gerecke 			 hdev->name,
19408e116d31SJason Gerecke 			 error ? "Ignoring" : "Assuming pen");
19418e116d31SJason Gerecke 
19428e116d31SJason Gerecke 		if (error)
19433888b0d5SBenjamin Tissoires 			goto fail;
1944042628abSJason Gerecke 
1945aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_PEN;
1946042628abSJason Gerecke 	}
1947042628abSJason Gerecke 
1948471d1714SBenjamin Tissoires 	wacom_calculate_res(features);
1949471d1714SBenjamin Tissoires 
1950fd5f92b6SBenjamin Tissoires 	wacom_update_name(wacom, wireless ? " (WL)" : "");
1951471d1714SBenjamin Tissoires 
1952471d1714SBenjamin Tissoires 	error = wacom_add_shared_data(hdev);
1953471d1714SBenjamin Tissoires 	if (error)
19543888b0d5SBenjamin Tissoires 		goto fail;
1955471d1714SBenjamin Tissoires 
1956ccad85ccSJason Gerecke 	if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
1957f81a1295SBenjamin Tissoires 	     (features->quirks & WACOM_QUIRK_BATTERY)) {
1958f81a1295SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1959f81a1295SBenjamin Tissoires 		if (error)
19603888b0d5SBenjamin Tissoires 			goto fail;
1961f81a1295SBenjamin Tissoires 	}
1962f81a1295SBenjamin Tissoires 
1963494078b0SBenjamin Tissoires 	error = wacom_register_inputs(wacom);
1964471d1714SBenjamin Tissoires 	if (error)
19653888b0d5SBenjamin Tissoires 		goto fail;
1966f81a1295SBenjamin Tissoires 
1967b62f6465SBenjamin Tissoires 	if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
196885d2c77bSBenjamin Tissoires 		error = wacom_initialize_leds(wacom);
196985d2c77bSBenjamin Tissoires 		if (error)
19703888b0d5SBenjamin Tissoires 			goto fail;
197185d2c77bSBenjamin Tissoires 
197283e6b40eSBenjamin Tissoires 		error = wacom_initialize_remotes(wacom);
1973b62f6465SBenjamin Tissoires 		if (error)
19743888b0d5SBenjamin Tissoires 			goto fail;
1975b62f6465SBenjamin Tissoires 	}
1976b62f6465SBenjamin Tissoires 
19777704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
19787704ac93SBenjamin Tissoires 		connect_mask |= HID_CONNECT_DRIVER;
19797704ac93SBenjamin Tissoires 
1980471d1714SBenjamin Tissoires 	/* Regular HID work starts now */
19817704ac93SBenjamin Tissoires 	error = hid_hw_start(hdev, connect_mask);
1982471d1714SBenjamin Tissoires 	if (error) {
1983471d1714SBenjamin Tissoires 		hid_err(hdev, "hw start failed\n");
19843888b0d5SBenjamin Tissoires 		goto fail;
1985471d1714SBenjamin Tissoires 	}
1986471d1714SBenjamin Tissoires 
1987fd5f92b6SBenjamin Tissoires 	if (!wireless) {
198886e88f0eSJason Gerecke 		/* Note that if query fails it is not a hard failure */
198986e88f0eSJason Gerecke 		wacom_query_tablet_data(hdev, features);
1990fd5f92b6SBenjamin Tissoires 	}
199186e88f0eSJason Gerecke 
199286e88f0eSJason Gerecke 	/* touch only Bamboo doesn't support pen */
199386e88f0eSJason Gerecke 	if ((features->type == BAMBOO_TOUCH) &&
199486e88f0eSJason Gerecke 	    (features->device_type & WACOM_DEVICETYPE_PEN)) {
199586e88f0eSJason Gerecke 		error = -ENODEV;
1996b62f6465SBenjamin Tissoires 		goto fail_quirks;
199786e88f0eSJason Gerecke 	}
199886e88f0eSJason Gerecke 
199986e88f0eSJason Gerecke 	/* pen only Bamboo neither support touch nor pad */
200086e88f0eSJason Gerecke 	if ((features->type == BAMBOO_PEN) &&
200186e88f0eSJason Gerecke 	    ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
200286e88f0eSJason Gerecke 	    (features->device_type & WACOM_DEVICETYPE_PAD))) {
200386e88f0eSJason Gerecke 		error = -ENODEV;
2004b62f6465SBenjamin Tissoires 		goto fail_quirks;
200586e88f0eSJason Gerecke 	}
200686e88f0eSJason Gerecke 
2007ccad85ccSJason Gerecke 	if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2008471d1714SBenjamin Tissoires 		error = hid_hw_open(hdev);
2009471d1714SBenjamin Tissoires 
2010eda01dabSPing Cheng 	if ((wacom_wac->features.type == INTUOSHT ||
2011eda01dabSPing Cheng 	     wacom_wac->features.type == INTUOSHT2) &&
2012eda01dabSPing Cheng 	    (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
201397f9afa4SBenjamin Tissoires 		wacom_wac->shared->type = wacom_wac->features.type;
20142a6cdbddSJason Gerecke 		wacom_wac->shared->touch_input = wacom_wac->touch_input;
2015471d1714SBenjamin Tissoires 	}
2016471d1714SBenjamin Tissoires 
201784dfbd7fSBenjamin Tissoires 	devres_close_group(&hdev->dev, wacom);
201884dfbd7fSBenjamin Tissoires 
2019471d1714SBenjamin Tissoires 	return 0;
2020471d1714SBenjamin Tissoires 
2021b62f6465SBenjamin Tissoires fail_quirks:
2022c58ac3a8SBenjamin Tissoires 	hid_hw_stop(hdev);
20233888b0d5SBenjamin Tissoires fail:
202484dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom);
2025c58ac3a8SBenjamin Tissoires 	return error;
2026c58ac3a8SBenjamin Tissoires }
2027c58ac3a8SBenjamin Tissoires 
2028a2f091afSBenjamin Tissoires static void wacom_wireless_work(struct work_struct *work)
2029a2f091afSBenjamin Tissoires {
2030d17d1f17SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2031a2f091afSBenjamin Tissoires 	struct usb_device *usbdev = wacom->usbdev;
2032a2f091afSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2033a2f091afSBenjamin Tissoires 	struct hid_device *hdev1, *hdev2;
2034a2f091afSBenjamin Tissoires 	struct wacom *wacom1, *wacom2;
2035a2f091afSBenjamin Tissoires 	struct wacom_wac *wacom_wac1, *wacom_wac2;
2036a2f091afSBenjamin Tissoires 	int error;
2037a2f091afSBenjamin Tissoires 
2038a2f091afSBenjamin Tissoires 	/*
2039a2f091afSBenjamin Tissoires 	 * Regardless if this is a disconnect or a new tablet,
2040a2f091afSBenjamin Tissoires 	 * remove any existing input and battery devices.
2041a2f091afSBenjamin Tissoires 	 */
2042a2f091afSBenjamin Tissoires 
2043a2f091afSBenjamin Tissoires 	wacom_destroy_battery(wacom);
2044a2f091afSBenjamin Tissoires 
2045a2f091afSBenjamin Tissoires 	/* Stylus interface */
2046a2f091afSBenjamin Tissoires 	hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2047a2f091afSBenjamin Tissoires 	wacom1 = hid_get_drvdata(hdev1);
2048a2f091afSBenjamin Tissoires 	wacom_wac1 = &(wacom1->wacom_wac);
204984dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom1);
2050a2f091afSBenjamin Tissoires 
2051a2f091afSBenjamin Tissoires 	/* Touch interface */
2052a2f091afSBenjamin Tissoires 	hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2053a2f091afSBenjamin Tissoires 	wacom2 = hid_get_drvdata(hdev2);
2054a2f091afSBenjamin Tissoires 	wacom_wac2 = &(wacom2->wacom_wac);
205584dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom2);
2056a2f091afSBenjamin Tissoires 
2057a2f091afSBenjamin Tissoires 	if (wacom_wac->pid == 0) {
2058a2f091afSBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet disconnected\n");
2059a2f091afSBenjamin Tissoires 	} else {
2060a2f091afSBenjamin Tissoires 		const struct hid_device_id *id = wacom_ids;
2061a2f091afSBenjamin Tissoires 
2062a2f091afSBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2063a2f091afSBenjamin Tissoires 			 wacom_wac->pid);
2064a2f091afSBenjamin Tissoires 
2065a2f091afSBenjamin Tissoires 		while (id->bus) {
2066a2f091afSBenjamin Tissoires 			if (id->vendor == USB_VENDOR_ID_WACOM &&
2067a2f091afSBenjamin Tissoires 			    id->product == wacom_wac->pid)
2068a2f091afSBenjamin Tissoires 				break;
2069a2f091afSBenjamin Tissoires 			id++;
2070a2f091afSBenjamin Tissoires 		}
2071a2f091afSBenjamin Tissoires 
2072a2f091afSBenjamin Tissoires 		if (!id->bus) {
2073a2f091afSBenjamin Tissoires 			hid_info(wacom->hdev, "ignoring unknown PID.\n");
2074a2f091afSBenjamin Tissoires 			return;
2075a2f091afSBenjamin Tissoires 		}
2076a2f091afSBenjamin Tissoires 
2077a2f091afSBenjamin Tissoires 		/* Stylus interface */
2078a2f091afSBenjamin Tissoires 		wacom_wac1->features =
2079a2f091afSBenjamin Tissoires 			*((struct wacom_features *)id->driver_data);
2080fd5f92b6SBenjamin Tissoires 
2081a2f091afSBenjamin Tissoires 		wacom_wac1->pid = wacom_wac->pid;
2082fd5f92b6SBenjamin Tissoires 		hid_hw_stop(hdev1);
2083fd5f92b6SBenjamin Tissoires 		error = wacom_parse_and_register(wacom1, true);
2084a2f091afSBenjamin Tissoires 		if (error)
2085a2f091afSBenjamin Tissoires 			goto fail;
2086a2f091afSBenjamin Tissoires 
2087a2f091afSBenjamin Tissoires 		/* Touch interface */
2088a2f091afSBenjamin Tissoires 		if (wacom_wac1->features.touch_max ||
2089a2f091afSBenjamin Tissoires 		    (wacom_wac1->features.type >= INTUOSHT &&
2090a2f091afSBenjamin Tissoires 		    wacom_wac1->features.type <= BAMBOO_PT)) {
2091a2f091afSBenjamin Tissoires 			wacom_wac2->features =
2092a2f091afSBenjamin Tissoires 				*((struct wacom_features *)id->driver_data);
2093a2f091afSBenjamin Tissoires 			wacom_wac2->pid = wacom_wac->pid;
2094fd5f92b6SBenjamin Tissoires 			hid_hw_stop(hdev2);
2095fd5f92b6SBenjamin Tissoires 			error = wacom_parse_and_register(wacom2, true);
2096a2f091afSBenjamin Tissoires 			if (error)
2097a2f091afSBenjamin Tissoires 				goto fail;
2098a2f091afSBenjamin Tissoires 		}
2099a2f091afSBenjamin Tissoires 
2100a2f091afSBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
2101a2f091afSBenjamin Tissoires 		if (error)
2102a2f091afSBenjamin Tissoires 			goto fail;
2103a2f091afSBenjamin Tissoires 	}
2104a2f091afSBenjamin Tissoires 
2105a2f091afSBenjamin Tissoires 	return;
2106a2f091afSBenjamin Tissoires 
2107a2f091afSBenjamin Tissoires fail:
210884dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom1);
210984dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom2);
2110a2f091afSBenjamin Tissoires 	return;
2111a2f091afSBenjamin Tissoires }
2112a2f091afSBenjamin Tissoires 
211304bfa27bSBenjamin Tissoires static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
211404bfa27bSBenjamin Tissoires {
211504bfa27bSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
2116e7749f6eSBenjamin Tissoires 	u32 serial = remote->remotes[index].serial;
211704bfa27bSBenjamin Tissoires 	int i;
21187c35dc3cSBenjamin Tissoires 	unsigned long flags;
21197c35dc3cSBenjamin Tissoires 
21207c35dc3cSBenjamin Tissoires 	spin_lock_irqsave(&remote->remote_lock, flags);
21217c35dc3cSBenjamin Tissoires 	remote->remotes[index].registered = false;
21227c35dc3cSBenjamin Tissoires 	spin_unlock_irqrestore(&remote->remote_lock, flags);
212304bfa27bSBenjamin Tissoires 
21249f1015d4SBenjamin Tissoires 	if (remote->remotes[index].battery.battery)
21259f1015d4SBenjamin Tissoires 		devres_release_group(&wacom->hdev->dev,
21269f1015d4SBenjamin Tissoires 				     &remote->remotes[index].battery.bat_desc);
21279f1015d4SBenjamin Tissoires 
2128e7749f6eSBenjamin Tissoires 	if (remote->remotes[index].group.name)
2129e7749f6eSBenjamin Tissoires 		devres_release_group(&wacom->hdev->dev,
2130e7749f6eSBenjamin Tissoires 				     &remote->remotes[index]);
213104bfa27bSBenjamin Tissoires 
213204bfa27bSBenjamin Tissoires 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2133e7749f6eSBenjamin Tissoires 		if (remote->remotes[i].serial == serial) {
2134e7749f6eSBenjamin Tissoires 			remote->remotes[i].serial = 0;
2135e7749f6eSBenjamin Tissoires 			remote->remotes[i].group.name = NULL;
21367c35dc3cSBenjamin Tissoires 			remote->remotes[i].registered = false;
21379f1015d4SBenjamin Tissoires 			remote->remotes[i].battery.battery = NULL;
213804bfa27bSBenjamin Tissoires 			wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
213904bfa27bSBenjamin Tissoires 		}
214004bfa27bSBenjamin Tissoires 	}
214104bfa27bSBenjamin Tissoires }
214204bfa27bSBenjamin Tissoires 
214304bfa27bSBenjamin Tissoires static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
214404bfa27bSBenjamin Tissoires 				   unsigned int index)
214504bfa27bSBenjamin Tissoires {
214604bfa27bSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
2147f9036bd4SBenjamin Tissoires 	struct device *dev = &wacom->hdev->dev;
214804bfa27bSBenjamin Tissoires 	int error, k;
214904bfa27bSBenjamin Tissoires 
215004bfa27bSBenjamin Tissoires 	/* A remote can pair more than once with an EKR,
215104bfa27bSBenjamin Tissoires 	 * check to make sure this serial isn't already paired.
215204bfa27bSBenjamin Tissoires 	 */
215304bfa27bSBenjamin Tissoires 	for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2154e7749f6eSBenjamin Tissoires 		if (remote->remotes[k].serial == serial)
215504bfa27bSBenjamin Tissoires 			break;
215604bfa27bSBenjamin Tissoires 	}
215704bfa27bSBenjamin Tissoires 
215804bfa27bSBenjamin Tissoires 	if (k < WACOM_MAX_REMOTES) {
2159e7749f6eSBenjamin Tissoires 		remote->remotes[index].serial = serial;
216004bfa27bSBenjamin Tissoires 		return 0;
216104bfa27bSBenjamin Tissoires 	}
216204bfa27bSBenjamin Tissoires 
2163e7749f6eSBenjamin Tissoires 	if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2164f9036bd4SBenjamin Tissoires 		return -ENOMEM;
2165f9036bd4SBenjamin Tissoires 
216604bfa27bSBenjamin Tissoires 	error = wacom_remote_create_attr_group(wacom, serial, index);
216704bfa27bSBenjamin Tissoires 	if (error)
2168f9036bd4SBenjamin Tissoires 		goto fail;
216904bfa27bSBenjamin Tissoires 
21707c35dc3cSBenjamin Tissoires 	remote->remotes[index].input = wacom_allocate_input(wacom);
21717c35dc3cSBenjamin Tissoires 	if (!remote->remotes[index].input) {
21727c35dc3cSBenjamin Tissoires 		error = -ENOMEM;
21737c35dc3cSBenjamin Tissoires 		goto fail;
21747c35dc3cSBenjamin Tissoires 	}
21757c35dc3cSBenjamin Tissoires 	remote->remotes[index].input->uniq = remote->remotes[index].group.name;
21767c35dc3cSBenjamin Tissoires 	remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
21777c35dc3cSBenjamin Tissoires 
21787c35dc3cSBenjamin Tissoires 	if (!remote->remotes[index].input->name) {
21797c35dc3cSBenjamin Tissoires 		error = -EINVAL;
21807c35dc3cSBenjamin Tissoires 		goto fail;
21817c35dc3cSBenjamin Tissoires 	}
21827c35dc3cSBenjamin Tissoires 
21837c35dc3cSBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
21847c35dc3cSBenjamin Tissoires 						   &wacom->wacom_wac);
21857c35dc3cSBenjamin Tissoires 	if (error)
21867c35dc3cSBenjamin Tissoires 		goto fail;
21877c35dc3cSBenjamin Tissoires 
2188e7749f6eSBenjamin Tissoires 	remote->remotes[index].serial = serial;
218904bfa27bSBenjamin Tissoires 
21907c35dc3cSBenjamin Tissoires 	error = input_register_device(remote->remotes[index].input);
21917c35dc3cSBenjamin Tissoires 	if (error)
21927c35dc3cSBenjamin Tissoires 		goto fail;
21937c35dc3cSBenjamin Tissoires 
219497f5541fSBenjamin Tissoires 	error = wacom_led_groups_alloc_and_register_one(
219597f5541fSBenjamin Tissoires 					&remote->remotes[index].input->dev,
219697f5541fSBenjamin Tissoires 					wacom, index, 3, true);
219797f5541fSBenjamin Tissoires 	if (error)
219897f5541fSBenjamin Tissoires 		goto fail;
219997f5541fSBenjamin Tissoires 
22007c35dc3cSBenjamin Tissoires 	remote->remotes[index].registered = true;
22017c35dc3cSBenjamin Tissoires 
2202e7749f6eSBenjamin Tissoires 	devres_close_group(dev, &remote->remotes[index]);
220304bfa27bSBenjamin Tissoires 	return 0;
2204f9036bd4SBenjamin Tissoires 
2205f9036bd4SBenjamin Tissoires fail:
2206e7749f6eSBenjamin Tissoires 	devres_release_group(dev, &remote->remotes[index]);
2207e7749f6eSBenjamin Tissoires 	remote->remotes[index].serial = 0;
2208f9036bd4SBenjamin Tissoires 	return error;
220904bfa27bSBenjamin Tissoires }
221004bfa27bSBenjamin Tissoires 
22119f1015d4SBenjamin Tissoires static int wacom_remote_attach_battery(struct wacom *wacom, int index)
22129f1015d4SBenjamin Tissoires {
22139f1015d4SBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
22149f1015d4SBenjamin Tissoires 	int error;
22159f1015d4SBenjamin Tissoires 
22169f1015d4SBenjamin Tissoires 	if (!remote->remotes[index].registered)
22179f1015d4SBenjamin Tissoires 		return 0;
22189f1015d4SBenjamin Tissoires 
22199f1015d4SBenjamin Tissoires 	if (remote->remotes[index].battery.battery)
22209f1015d4SBenjamin Tissoires 		return 0;
22219f1015d4SBenjamin Tissoires 
22229f1015d4SBenjamin Tissoires 	if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
22239f1015d4SBenjamin Tissoires 		return 0;
22249f1015d4SBenjamin Tissoires 
22259f1015d4SBenjamin Tissoires 	error = __wacom_initialize_battery(wacom,
22269f1015d4SBenjamin Tissoires 					&wacom->remote->remotes[index].battery);
22279f1015d4SBenjamin Tissoires 	if (error)
22289f1015d4SBenjamin Tissoires 		return error;
22299f1015d4SBenjamin Tissoires 
22309f1015d4SBenjamin Tissoires 	return 0;
22319f1015d4SBenjamin Tissoires }
22329f1015d4SBenjamin Tissoires 
2233e6f2813aSBenjamin Tissoires static void wacom_remote_work(struct work_struct *work)
2234e6f2813aSBenjamin Tissoires {
2235e6f2813aSBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, remote_work);
223683e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
2237e6f2813aSBenjamin Tissoires 	struct wacom_remote_data data;
2238e6f2813aSBenjamin Tissoires 	unsigned long flags;
2239e6f2813aSBenjamin Tissoires 	unsigned int count;
2240e6f2813aSBenjamin Tissoires 	u32 serial;
224104bfa27bSBenjamin Tissoires 	int i;
2242e6f2813aSBenjamin Tissoires 
224383e6b40eSBenjamin Tissoires 	spin_lock_irqsave(&remote->remote_lock, flags);
2244e6f2813aSBenjamin Tissoires 
224583e6b40eSBenjamin Tissoires 	count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2246e6f2813aSBenjamin Tissoires 
2247e6f2813aSBenjamin Tissoires 	if (count != sizeof(data)) {
2248e6f2813aSBenjamin Tissoires 		hid_err(wacom->hdev,
2249e6f2813aSBenjamin Tissoires 			"workitem triggered without status available\n");
225083e6b40eSBenjamin Tissoires 		spin_unlock_irqrestore(&remote->remote_lock, flags);
2251e6f2813aSBenjamin Tissoires 		return;
2252e6f2813aSBenjamin Tissoires 	}
2253e6f2813aSBenjamin Tissoires 
225483e6b40eSBenjamin Tissoires 	if (!kfifo_is_empty(&remote->remote_fifo))
2255e6f2813aSBenjamin Tissoires 		wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2256e6f2813aSBenjamin Tissoires 
225783e6b40eSBenjamin Tissoires 	spin_unlock_irqrestore(&remote->remote_lock, flags);
2258e6f2813aSBenjamin Tissoires 
2259e6f2813aSBenjamin Tissoires 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2260e6f2813aSBenjamin Tissoires 		serial = data.remote[i].serial;
2261e6f2813aSBenjamin Tissoires 		if (data.remote[i].connected) {
2262e6f2813aSBenjamin Tissoires 
22639f1015d4SBenjamin Tissoires 			if (remote->remotes[i].serial == serial) {
22649f1015d4SBenjamin Tissoires 				wacom_remote_attach_battery(wacom, i);
2265e6f2813aSBenjamin Tissoires 				continue;
22669f1015d4SBenjamin Tissoires 			}
2267e6f2813aSBenjamin Tissoires 
2268e7749f6eSBenjamin Tissoires 			if (remote->remotes[i].serial)
226904bfa27bSBenjamin Tissoires 				wacom_remote_destroy_one(wacom, i);
2270e6f2813aSBenjamin Tissoires 
227104bfa27bSBenjamin Tissoires 			wacom_remote_create_one(wacom, serial, i);
2272e6f2813aSBenjamin Tissoires 
2273e7749f6eSBenjamin Tissoires 		} else if (remote->remotes[i].serial) {
227404bfa27bSBenjamin Tissoires 			wacom_remote_destroy_one(wacom, i);
2275e6f2813aSBenjamin Tissoires 		}
2276e6f2813aSBenjamin Tissoires 	}
2277e6f2813aSBenjamin Tissoires }
2278e6f2813aSBenjamin Tissoires 
2279c58ac3a8SBenjamin Tissoires static int wacom_probe(struct hid_device *hdev,
2280c58ac3a8SBenjamin Tissoires 		const struct hid_device_id *id)
2281c58ac3a8SBenjamin Tissoires {
2282c58ac3a8SBenjamin Tissoires 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2283c58ac3a8SBenjamin Tissoires 	struct usb_device *dev = interface_to_usbdev(intf);
2284c58ac3a8SBenjamin Tissoires 	struct wacom *wacom;
2285c58ac3a8SBenjamin Tissoires 	struct wacom_wac *wacom_wac;
2286c58ac3a8SBenjamin Tissoires 	struct wacom_features *features;
2287c58ac3a8SBenjamin Tissoires 	int error;
2288c58ac3a8SBenjamin Tissoires 
2289c58ac3a8SBenjamin Tissoires 	if (!id->driver_data)
2290c58ac3a8SBenjamin Tissoires 		return -EINVAL;
2291c58ac3a8SBenjamin Tissoires 
2292c58ac3a8SBenjamin Tissoires 	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2293c58ac3a8SBenjamin Tissoires 
2294c58ac3a8SBenjamin Tissoires 	/* hid-core sets this quirk for the boot interface */
2295c58ac3a8SBenjamin Tissoires 	hdev->quirks &= ~HID_QUIRK_NOGET;
2296c58ac3a8SBenjamin Tissoires 
229719b64330SBenjamin Tissoires 	wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2298c58ac3a8SBenjamin Tissoires 	if (!wacom)
2299c58ac3a8SBenjamin Tissoires 		return -ENOMEM;
2300c58ac3a8SBenjamin Tissoires 
2301c58ac3a8SBenjamin Tissoires 	hid_set_drvdata(hdev, wacom);
2302c58ac3a8SBenjamin Tissoires 	wacom->hdev = hdev;
2303c58ac3a8SBenjamin Tissoires 
2304c58ac3a8SBenjamin Tissoires 	wacom_wac = &wacom->wacom_wac;
2305c58ac3a8SBenjamin Tissoires 	wacom_wac->features = *((struct wacom_features *)id->driver_data);
2306c58ac3a8SBenjamin Tissoires 	features = &wacom_wac->features;
2307c58ac3a8SBenjamin Tissoires 
2308c58ac3a8SBenjamin Tissoires 	if (features->check_for_hid_type && features->hid_type != hdev->type) {
2309c58ac3a8SBenjamin Tissoires 		error = -ENODEV;
23103888b0d5SBenjamin Tissoires 		goto fail;
2311c58ac3a8SBenjamin Tissoires 	}
2312c58ac3a8SBenjamin Tissoires 
2313c6fa1aebSJason Gerecke 	wacom_wac->hid_data.inputmode = -1;
2314326ea2a9SJason Gerecke 	wacom_wac->mode_report = -1;
2315c6fa1aebSJason Gerecke 
2316c58ac3a8SBenjamin Tissoires 	wacom->usbdev = dev;
2317c58ac3a8SBenjamin Tissoires 	wacom->intf = intf;
2318c58ac3a8SBenjamin Tissoires 	mutex_init(&wacom->lock);
2319d17d1f17SBenjamin Tissoires 	INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2320d17d1f17SBenjamin Tissoires 	INIT_WORK(&wacom->battery_work, wacom_battery_work);
2321e6f2813aSBenjamin Tissoires 	INIT_WORK(&wacom->remote_work, wacom_remote_work);
2322c58ac3a8SBenjamin Tissoires 
2323c58ac3a8SBenjamin Tissoires 	/* ask for the report descriptor to be loaded by HID */
2324c58ac3a8SBenjamin Tissoires 	error = hid_parse(hdev);
2325c58ac3a8SBenjamin Tissoires 	if (error) {
2326c58ac3a8SBenjamin Tissoires 		hid_err(hdev, "parse failed\n");
23273888b0d5SBenjamin Tissoires 		goto fail;
2328c58ac3a8SBenjamin Tissoires 	}
2329c58ac3a8SBenjamin Tissoires 
2330fd5f92b6SBenjamin Tissoires 	error = wacom_parse_and_register(wacom, false);
2331c58ac3a8SBenjamin Tissoires 	if (error)
23323888b0d5SBenjamin Tissoires 		goto fail;
2333c58ac3a8SBenjamin Tissoires 
2334c58ac3a8SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
2335c58ac3a8SBenjamin Tissoires 		error = device_create_file(&hdev->dev, &dev_attr_speed);
2336c58ac3a8SBenjamin Tissoires 		if (error)
2337c58ac3a8SBenjamin Tissoires 			hid_warn(hdev,
2338c58ac3a8SBenjamin Tissoires 				 "can't create sysfs speed attribute err: %d\n",
2339c58ac3a8SBenjamin Tissoires 				 error);
2340c58ac3a8SBenjamin Tissoires 	}
2341c58ac3a8SBenjamin Tissoires 
2342c58ac3a8SBenjamin Tissoires 	return 0;
2343c58ac3a8SBenjamin Tissoires 
23443888b0d5SBenjamin Tissoires fail:
2345471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
2346471d1714SBenjamin Tissoires 	return error;
2347471d1714SBenjamin Tissoires }
2348471d1714SBenjamin Tissoires 
2349471d1714SBenjamin Tissoires static void wacom_remove(struct hid_device *hdev)
2350471d1714SBenjamin Tissoires {
2351471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
2352f6205161SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2353f6205161SBenjamin Tissoires 	struct wacom_features *features = &wacom_wac->features;
2354f6205161SBenjamin Tissoires 
2355f6205161SBenjamin Tissoires 	if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2356f6205161SBenjamin Tissoires 		hid_hw_close(hdev);
2357471d1714SBenjamin Tissoires 
2358471d1714SBenjamin Tissoires 	hid_hw_stop(hdev);
2359471d1714SBenjamin Tissoires 
2360d17d1f17SBenjamin Tissoires 	cancel_work_sync(&wacom->wireless_work);
2361d17d1f17SBenjamin Tissoires 	cancel_work_sync(&wacom->battery_work);
2362e6f2813aSBenjamin Tissoires 	cancel_work_sync(&wacom->remote_work);
2363f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
2364f81a1295SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
2365471d1714SBenjamin Tissoires 
2366471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
2367471d1714SBenjamin Tissoires }
2368471d1714SBenjamin Tissoires 
236941a74581SGeert Uytterhoeven #ifdef CONFIG_PM
2370471d1714SBenjamin Tissoires static int wacom_resume(struct hid_device *hdev)
2371471d1714SBenjamin Tissoires {
2372471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
2373471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
2374471d1714SBenjamin Tissoires 
2375471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
2376471d1714SBenjamin Tissoires 
2377471d1714SBenjamin Tissoires 	/* switch to wacom mode first */
2378471d1714SBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
2379471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
2380471d1714SBenjamin Tissoires 
2381471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
2382471d1714SBenjamin Tissoires 
2383471d1714SBenjamin Tissoires 	return 0;
2384471d1714SBenjamin Tissoires }
2385471d1714SBenjamin Tissoires 
2386471d1714SBenjamin Tissoires static int wacom_reset_resume(struct hid_device *hdev)
2387471d1714SBenjamin Tissoires {
2388471d1714SBenjamin Tissoires 	return wacom_resume(hdev);
2389471d1714SBenjamin Tissoires }
239041a74581SGeert Uytterhoeven #endif /* CONFIG_PM */
2391471d1714SBenjamin Tissoires 
2392471d1714SBenjamin Tissoires static struct hid_driver wacom_driver = {
2393471d1714SBenjamin Tissoires 	.name =		"wacom",
2394471d1714SBenjamin Tissoires 	.id_table =	wacom_ids,
2395471d1714SBenjamin Tissoires 	.probe =	wacom_probe,
2396471d1714SBenjamin Tissoires 	.remove =	wacom_remove,
23977704ac93SBenjamin Tissoires 	.report =	wacom_wac_report,
2398471d1714SBenjamin Tissoires #ifdef CONFIG_PM
2399471d1714SBenjamin Tissoires 	.resume =	wacom_resume,
2400471d1714SBenjamin Tissoires 	.reset_resume =	wacom_reset_resume,
2401471d1714SBenjamin Tissoires #endif
2402471d1714SBenjamin Tissoires 	.raw_event =	wacom_raw_event,
2403471d1714SBenjamin Tissoires };
2404471d1714SBenjamin Tissoires module_hid_driver(wacom_driver);
2405f2e0a7d4SBenjamin Tissoires 
2406f2e0a7d4SBenjamin Tissoires MODULE_VERSION(DRIVER_VERSION);
2407f2e0a7d4SBenjamin Tissoires MODULE_AUTHOR(DRIVER_AUTHOR);
2408f2e0a7d4SBenjamin Tissoires MODULE_DESCRIPTION(DRIVER_DESC);
2409f2e0a7d4SBenjamin Tissoires MODULE_LICENSE(DRIVER_LICENSE);
2410