xref: /openbmc/linux/drivers/hid/wacom_sys.c (revision 8de82280e3f5c4569505f475ce1a0b9ccfbd8de8)
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 &&
162*8de82280SJason Gerecke 		    (field->application == WACOM_HID_G9_PEN ||
163*8de82280SJason Gerecke 		     field->application == WACOM_HID_G11_PEN)) {
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 
169*8de82280SJason Gerecke 	case WACOM_HID_UP_G9:
170*8de82280SJason Gerecke 	case WACOM_HID_UP_G11:
171326ea2a9SJason Gerecke 		if (field->report->id == 0x03 &&
172*8de82280SJason Gerecke 		    (field->application == WACOM_HID_G9_TOUCHSCREEN ||
173*8de82280SJason Gerecke 		     field->application == WACOM_HID_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 
53041372d5dSJason Gerecke static bool compare_device_paths(struct hid_device *hdev_a,
53141372d5dSJason Gerecke 		struct hid_device *hdev_b, char separator)
53241372d5dSJason Gerecke {
53341372d5dSJason Gerecke 	int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
53441372d5dSJason Gerecke 	int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
53541372d5dSJason Gerecke 
53641372d5dSJason Gerecke 	if (n1 != n2 || n1 <= 0 || n2 <= 0)
53741372d5dSJason Gerecke 		return false;
53841372d5dSJason Gerecke 
53941372d5dSJason Gerecke 	return !strncmp(hdev_a->phys, hdev_b->phys, n1);
54041372d5dSJason Gerecke }
54141372d5dSJason Gerecke 
542471d1714SBenjamin Tissoires static bool wacom_are_sibling(struct hid_device *hdev,
543471d1714SBenjamin Tissoires 		struct hid_device *sibling)
544471d1714SBenjamin Tissoires {
545471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
546471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
54741372d5dSJason Gerecke 	struct wacom *sibling_wacom = hid_get_drvdata(sibling);
54841372d5dSJason Gerecke 	struct wacom_features *sibling_features = &sibling_wacom->wacom_wac.features;
54941372d5dSJason Gerecke 	__u32 oVid = features->oVid ? features->oVid : hdev->vendor;
55041372d5dSJason Gerecke 	__u32 oPid = features->oPid ? features->oPid : hdev->product;
551471d1714SBenjamin Tissoires 
55241372d5dSJason Gerecke 	/* The defined oVid/oPid must match that of the sibling */
55341372d5dSJason Gerecke 	if (features->oVid != HID_ANY_ID && sibling->vendor != oVid)
55441372d5dSJason Gerecke 		return false;
55541372d5dSJason Gerecke 	if (features->oPid != HID_ANY_ID && sibling->product != oPid)
55641372d5dSJason Gerecke 		return false;
55741372d5dSJason Gerecke 
55841372d5dSJason Gerecke 	/*
55941372d5dSJason Gerecke 	 * Devices with the same VID/PID must share the same physical
56041372d5dSJason Gerecke 	 * device path, while those with different VID/PID must share
56141372d5dSJason Gerecke 	 * the same physical parent device path.
56241372d5dSJason Gerecke 	 */
56341372d5dSJason Gerecke 	if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
56441372d5dSJason Gerecke 		if (!compare_device_paths(hdev, sibling, '/'))
56541372d5dSJason Gerecke 			return false;
56641372d5dSJason Gerecke 	} else {
56741372d5dSJason Gerecke 		if (!compare_device_paths(hdev, sibling, '.'))
56841372d5dSJason Gerecke 			return false;
569471d1714SBenjamin Tissoires 	}
570471d1714SBenjamin Tissoires 
57141372d5dSJason Gerecke 	/* Skip the remaining heuristics unless you are a HID_GENERIC device */
57241372d5dSJason Gerecke 	if (features->type != HID_GENERIC)
57341372d5dSJason Gerecke 		return true;
57441372d5dSJason Gerecke 
57541372d5dSJason Gerecke 	/*
57641372d5dSJason Gerecke 	 * Direct-input devices may not be siblings of indirect-input
57741372d5dSJason Gerecke 	 * devices.
57841372d5dSJason Gerecke 	 */
57941372d5dSJason Gerecke 	if ((features->device_type & WACOM_DEVICETYPE_DIRECT) &&
58041372d5dSJason Gerecke 	    !(sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
581471d1714SBenjamin Tissoires 		return false;
582471d1714SBenjamin Tissoires 
58341372d5dSJason Gerecke 	/*
58441372d5dSJason Gerecke 	 * Indirect-input devices may not be siblings of direct-input
58541372d5dSJason Gerecke 	 * devices.
58641372d5dSJason Gerecke 	 */
58741372d5dSJason Gerecke 	if (!(features->device_type & WACOM_DEVICETYPE_DIRECT) &&
58841372d5dSJason Gerecke 	    (sibling_features->device_type & WACOM_DEVICETYPE_DIRECT))
589471d1714SBenjamin Tissoires 		return false;
590471d1714SBenjamin Tissoires 
59141372d5dSJason Gerecke 	/* Pen devices may only be siblings of touch devices */
59241372d5dSJason Gerecke 	if ((features->device_type & WACOM_DEVICETYPE_PEN) &&
59341372d5dSJason Gerecke 	    !(sibling_features->device_type & WACOM_DEVICETYPE_TOUCH))
59441372d5dSJason Gerecke 		return false;
59541372d5dSJason Gerecke 
59641372d5dSJason Gerecke 	/* Touch devices may only be siblings of pen devices */
59741372d5dSJason Gerecke 	if ((features->device_type & WACOM_DEVICETYPE_TOUCH) &&
59841372d5dSJason Gerecke 	    !(sibling_features->device_type & WACOM_DEVICETYPE_PEN))
59941372d5dSJason Gerecke 		return false;
60041372d5dSJason Gerecke 
60141372d5dSJason Gerecke 	/*
60241372d5dSJason Gerecke 	 * No reason could be found for these two devices to NOT be
60341372d5dSJason Gerecke 	 * siblings, so there's a good chance they ARE siblings
60441372d5dSJason Gerecke 	 */
60541372d5dSJason Gerecke 	return true;
606471d1714SBenjamin Tissoires }
607471d1714SBenjamin Tissoires 
608471d1714SBenjamin Tissoires static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
609471d1714SBenjamin Tissoires {
610471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
611471d1714SBenjamin Tissoires 
61241372d5dSJason Gerecke 	/* Try to find an already-probed interface from the same device */
61341372d5dSJason Gerecke 	list_for_each_entry(data, &wacom_udev_list, list) {
61441372d5dSJason Gerecke 		if (compare_device_paths(hdev, data->dev, '/'))
61541372d5dSJason Gerecke 			return data;
61641372d5dSJason Gerecke 	}
61741372d5dSJason Gerecke 
61841372d5dSJason Gerecke 	/* Fallback to finding devices that appear to be "siblings" */
619471d1714SBenjamin Tissoires 	list_for_each_entry(data, &wacom_udev_list, list) {
620471d1714SBenjamin Tissoires 		if (wacom_are_sibling(hdev, data->dev)) {
621471d1714SBenjamin Tissoires 			kref_get(&data->kref);
622471d1714SBenjamin Tissoires 			return data;
623471d1714SBenjamin Tissoires 		}
624471d1714SBenjamin Tissoires 	}
625471d1714SBenjamin Tissoires 
626471d1714SBenjamin Tissoires 	return NULL;
627471d1714SBenjamin Tissoires }
628471d1714SBenjamin Tissoires 
6291c817c83SBenjamin Tissoires static void wacom_release_shared_data(struct kref *kref)
6301c817c83SBenjamin Tissoires {
6311c817c83SBenjamin Tissoires 	struct wacom_hdev_data *data =
6321c817c83SBenjamin Tissoires 		container_of(kref, struct wacom_hdev_data, kref);
6331c817c83SBenjamin Tissoires 
6341c817c83SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
6351c817c83SBenjamin Tissoires 	list_del(&data->list);
6361c817c83SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
6371c817c83SBenjamin Tissoires 
6381c817c83SBenjamin Tissoires 	kfree(data);
6391c817c83SBenjamin Tissoires }
6401c817c83SBenjamin Tissoires 
6411c817c83SBenjamin Tissoires static void wacom_remove_shared_data(void *res)
6421c817c83SBenjamin Tissoires {
6431c817c83SBenjamin Tissoires 	struct wacom *wacom = res;
6441c817c83SBenjamin Tissoires 	struct wacom_hdev_data *data;
6451c817c83SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
6461c817c83SBenjamin Tissoires 
6471c817c83SBenjamin Tissoires 	if (wacom_wac->shared) {
6481c817c83SBenjamin Tissoires 		data = container_of(wacom_wac->shared, struct wacom_hdev_data,
6491c817c83SBenjamin Tissoires 				    shared);
6501c817c83SBenjamin Tissoires 
6511c817c83SBenjamin Tissoires 		if (wacom_wac->shared->touch == wacom->hdev)
6521c817c83SBenjamin Tissoires 			wacom_wac->shared->touch = NULL;
6531c817c83SBenjamin Tissoires 		else if (wacom_wac->shared->pen == wacom->hdev)
6541c817c83SBenjamin Tissoires 			wacom_wac->shared->pen = NULL;
6551c817c83SBenjamin Tissoires 
6561c817c83SBenjamin Tissoires 		kref_put(&data->kref, wacom_release_shared_data);
6571c817c83SBenjamin Tissoires 		wacom_wac->shared = NULL;
6581c817c83SBenjamin Tissoires 	}
6591c817c83SBenjamin Tissoires }
6601c817c83SBenjamin Tissoires 
661471d1714SBenjamin Tissoires static int wacom_add_shared_data(struct hid_device *hdev)
662471d1714SBenjamin Tissoires {
663471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
664471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
665471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
666471d1714SBenjamin Tissoires 	int retval = 0;
667471d1714SBenjamin Tissoires 
668471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
669471d1714SBenjamin Tissoires 
670471d1714SBenjamin Tissoires 	data = wacom_get_hdev_data(hdev);
671471d1714SBenjamin Tissoires 	if (!data) {
672471d1714SBenjamin Tissoires 		data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
673471d1714SBenjamin Tissoires 		if (!data) {
674471d1714SBenjamin Tissoires 			retval = -ENOMEM;
675471d1714SBenjamin Tissoires 			goto out;
676471d1714SBenjamin Tissoires 		}
677471d1714SBenjamin Tissoires 
678471d1714SBenjamin Tissoires 		kref_init(&data->kref);
679471d1714SBenjamin Tissoires 		data->dev = hdev;
680471d1714SBenjamin Tissoires 		list_add_tail(&data->list, &wacom_udev_list);
681471d1714SBenjamin Tissoires 	}
682471d1714SBenjamin Tissoires 
683471d1714SBenjamin Tissoires 	wacom_wac->shared = &data->shared;
684471d1714SBenjamin Tissoires 
6851c817c83SBenjamin Tissoires 	retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
6861c817c83SBenjamin Tissoires 	if (retval) {
6871c817c83SBenjamin Tissoires 		mutex_unlock(&wacom_udev_list_lock);
6881c817c83SBenjamin Tissoires 		wacom_remove_shared_data(wacom);
6891c817c83SBenjamin Tissoires 		return retval;
6901c817c83SBenjamin Tissoires 	}
6911c817c83SBenjamin Tissoires 
692aa86b18cSJason Gerecke 	if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
693a97ac104SBenjamin Tissoires 		wacom_wac->shared->touch = hdev;
694aa86b18cSJason Gerecke 	else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
695a97ac104SBenjamin Tissoires 		wacom_wac->shared->pen = hdev;
696a97ac104SBenjamin Tissoires 
697471d1714SBenjamin Tissoires out:
698471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
699471d1714SBenjamin Tissoires 	return retval;
700471d1714SBenjamin Tissoires }
701471d1714SBenjamin Tissoires 
702471d1714SBenjamin Tissoires static int wacom_led_control(struct wacom *wacom)
703471d1714SBenjamin Tissoires {
704471d1714SBenjamin Tissoires 	unsigned char *buf;
705471d1714SBenjamin Tissoires 	int retval;
706912ca216SPing Cheng 	unsigned char report_id = WAC_CMD_LED_CONTROL;
707912ca216SPing Cheng 	int buf_size = 9;
708471d1714SBenjamin Tissoires 
70997f5541fSBenjamin Tissoires 	if (!hid_get_drvdata(wacom->hdev))
71097f5541fSBenjamin Tissoires 		return -ENODEV;
71197f5541fSBenjamin Tissoires 
712a50aac71SBenjamin Tissoires 	if (!wacom->led.groups)
713a50aac71SBenjamin Tissoires 		return -ENOTSUPP;
714a50aac71SBenjamin Tissoires 
715912ca216SPing Cheng 	if (wacom->wacom_wac.pid) { /* wireless connected */
716912ca216SPing Cheng 		report_id = WAC_CMD_WL_LED_CONTROL;
717912ca216SPing Cheng 		buf_size = 13;
718912ca216SPing Cheng 	}
719912ca216SPing Cheng 	buf = kzalloc(buf_size, GFP_KERNEL);
720471d1714SBenjamin Tissoires 	if (!buf)
721471d1714SBenjamin Tissoires 		return -ENOMEM;
722471d1714SBenjamin Tissoires 
723471d1714SBenjamin Tissoires 	if (wacom->wacom_wac.features.type >= INTUOS5S &&
724471d1714SBenjamin Tissoires 	    wacom->wacom_wac.features.type <= INTUOSPL) {
725471d1714SBenjamin Tissoires 		/*
726471d1714SBenjamin Tissoires 		 * Touch Ring and crop mark LED luminance may take on
727471d1714SBenjamin Tissoires 		 * one of four values:
728471d1714SBenjamin Tissoires 		 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
729471d1714SBenjamin Tissoires 		 */
730a50aac71SBenjamin Tissoires 		int ring_led = wacom->led.groups[0].select & 0x03;
731471d1714SBenjamin Tissoires 		int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
732471d1714SBenjamin Tissoires 		int crop_lum = 0;
733912ca216SPing Cheng 		unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
734471d1714SBenjamin Tissoires 
735912ca216SPing Cheng 		buf[0] = report_id;
736912ca216SPing Cheng 		if (wacom->wacom_wac.pid) {
737912ca216SPing Cheng 			wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
738912ca216SPing Cheng 					 buf, buf_size, WAC_CMD_RETRIES);
739912ca216SPing Cheng 			buf[0] = report_id;
740912ca216SPing Cheng 			buf[4] = led_bits;
741912ca216SPing Cheng 		} else
742912ca216SPing Cheng 			buf[1] = led_bits;
743471d1714SBenjamin Tissoires 	}
744471d1714SBenjamin Tissoires 	else {
745a50aac71SBenjamin Tissoires 		int led = wacom->led.groups[0].select | 0x4;
746471d1714SBenjamin Tissoires 
747471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
748471d1714SBenjamin Tissoires 		    wacom->wacom_wac.features.type == WACOM_24HD)
749a50aac71SBenjamin Tissoires 			led |= (wacom->led.groups[1].select << 4) | 0x40;
750471d1714SBenjamin Tissoires 
751912ca216SPing Cheng 		buf[0] = report_id;
752471d1714SBenjamin Tissoires 		buf[1] = led;
753471d1714SBenjamin Tissoires 		buf[2] = wacom->led.llv;
754471d1714SBenjamin Tissoires 		buf[3] = wacom->led.hlv;
755471d1714SBenjamin Tissoires 		buf[4] = wacom->led.img_lum;
756471d1714SBenjamin Tissoires 	}
757471d1714SBenjamin Tissoires 
758912ca216SPing Cheng 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
759296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
760471d1714SBenjamin Tissoires 	kfree(buf);
761471d1714SBenjamin Tissoires 
762471d1714SBenjamin Tissoires 	return retval;
763471d1714SBenjamin Tissoires }
764471d1714SBenjamin Tissoires 
765849e2f06SBenjamin Tissoires static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
766849e2f06SBenjamin Tissoires 		const unsigned len, const void *img)
767471d1714SBenjamin Tissoires {
768471d1714SBenjamin Tissoires 	unsigned char *buf;
769471d1714SBenjamin Tissoires 	int i, retval;
770849e2f06SBenjamin Tissoires 	const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
771471d1714SBenjamin Tissoires 
772849e2f06SBenjamin Tissoires 	buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
773471d1714SBenjamin Tissoires 	if (!buf)
774471d1714SBenjamin Tissoires 		return -ENOMEM;
775471d1714SBenjamin Tissoires 
776471d1714SBenjamin Tissoires 	/* Send 'start' command */
777471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
778471d1714SBenjamin Tissoires 	buf[1] = 1;
779296b7378SPrzemo Firszt 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
780296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
781471d1714SBenjamin Tissoires 	if (retval < 0)
782471d1714SBenjamin Tissoires 		goto out;
783471d1714SBenjamin Tissoires 
784849e2f06SBenjamin Tissoires 	buf[0] = xfer_id;
785471d1714SBenjamin Tissoires 	buf[1] = button_id & 0x07;
786471d1714SBenjamin Tissoires 	for (i = 0; i < 4; i++) {
787471d1714SBenjamin Tissoires 		buf[2] = i;
788849e2f06SBenjamin Tissoires 		memcpy(buf + 3, img + i * chunk_len, chunk_len);
789471d1714SBenjamin Tissoires 
790471d1714SBenjamin Tissoires 		retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
791296b7378SPrzemo Firszt 					  buf, chunk_len + 3, WAC_CMD_RETRIES);
792471d1714SBenjamin Tissoires 		if (retval < 0)
793471d1714SBenjamin Tissoires 			break;
794471d1714SBenjamin Tissoires 	}
795471d1714SBenjamin Tissoires 
796471d1714SBenjamin Tissoires 	/* Send 'stop' */
797471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
798471d1714SBenjamin Tissoires 	buf[1] = 0;
799296b7378SPrzemo Firszt 	wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
800296b7378SPrzemo Firszt 			 WAC_CMD_RETRIES);
801471d1714SBenjamin Tissoires 
802471d1714SBenjamin Tissoires out:
803471d1714SBenjamin Tissoires 	kfree(buf);
804471d1714SBenjamin Tissoires 	return retval;
805471d1714SBenjamin Tissoires }
806471d1714SBenjamin Tissoires 
807471d1714SBenjamin Tissoires static ssize_t wacom_led_select_store(struct device *dev, int set_id,
808471d1714SBenjamin Tissoires 				      const char *buf, size_t count)
809471d1714SBenjamin Tissoires {
810ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
811471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
812471d1714SBenjamin Tissoires 	unsigned int id;
813471d1714SBenjamin Tissoires 	int err;
814471d1714SBenjamin Tissoires 
815471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &id);
816471d1714SBenjamin Tissoires 	if (err)
817471d1714SBenjamin Tissoires 		return err;
818471d1714SBenjamin Tissoires 
819471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
820471d1714SBenjamin Tissoires 
821a50aac71SBenjamin Tissoires 	wacom->led.groups[set_id].select = id & 0x3;
822471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
823471d1714SBenjamin Tissoires 
824471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
825471d1714SBenjamin Tissoires 
826471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
827471d1714SBenjamin Tissoires }
828471d1714SBenjamin Tissoires 
829471d1714SBenjamin Tissoires #define DEVICE_LED_SELECT_ATTR(SET_ID)					\
830471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
831471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
832471d1714SBenjamin Tissoires {									\
833471d1714SBenjamin Tissoires 	return wacom_led_select_store(dev, SET_ID, buf, count);		\
834471d1714SBenjamin Tissoires }									\
835471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
836471d1714SBenjamin Tissoires 	struct device_attribute *attr, char *buf)			\
837471d1714SBenjamin Tissoires {									\
838ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);\
839471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
84037449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n",			\
841a50aac71SBenjamin Tissoires 			 wacom->led.groups[SET_ID].select);		\
842471d1714SBenjamin Tissoires }									\
843e0984bc3SPing Cheng static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,	\
844471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_show,			\
845471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_store)
846471d1714SBenjamin Tissoires 
847471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(0);
848471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(1);
849471d1714SBenjamin Tissoires 
850471d1714SBenjamin Tissoires static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
851471d1714SBenjamin Tissoires 				     const char *buf, size_t count)
852471d1714SBenjamin Tissoires {
853471d1714SBenjamin Tissoires 	unsigned int value;
854471d1714SBenjamin Tissoires 	int err;
855471d1714SBenjamin Tissoires 
856471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &value);
857471d1714SBenjamin Tissoires 	if (err)
858471d1714SBenjamin Tissoires 		return err;
859471d1714SBenjamin Tissoires 
860471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
861471d1714SBenjamin Tissoires 
862471d1714SBenjamin Tissoires 	*dest = value & 0x7f;
863471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
864471d1714SBenjamin Tissoires 
865471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
866471d1714SBenjamin Tissoires 
867471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
868471d1714SBenjamin Tissoires }
869471d1714SBenjamin Tissoires 
870471d1714SBenjamin Tissoires #define DEVICE_LUMINANCE_ATTR(name, field)				\
871471d1714SBenjamin Tissoires static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
872471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
873471d1714SBenjamin Tissoires {									\
874ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);\
875471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
876471d1714SBenjamin Tissoires 									\
877471d1714SBenjamin Tissoires 	return wacom_luminance_store(wacom, &wacom->led.field,		\
878471d1714SBenjamin Tissoires 				     buf, count);			\
879471d1714SBenjamin Tissoires }									\
88037449adcSPing Cheng static ssize_t wacom_##name##_luminance_show(struct device *dev,	\
88137449adcSPing Cheng 	struct device_attribute *attr, char *buf)			\
88237449adcSPing Cheng {									\
88337449adcSPing Cheng 	struct wacom *wacom = dev_get_drvdata(dev);			\
88437449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);	\
88537449adcSPing Cheng }									\
886e0984bc3SPing Cheng static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,			\
88737449adcSPing Cheng 		   wacom_##name##_luminance_show,			\
88837449adcSPing Cheng 		   wacom_##name##_luminance_store)
889471d1714SBenjamin Tissoires 
890471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status0, llv);
891471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status1, hlv);
892471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(buttons, img_lum);
893471d1714SBenjamin Tissoires 
894471d1714SBenjamin Tissoires static ssize_t wacom_button_image_store(struct device *dev, int button_id,
895471d1714SBenjamin Tissoires 					const char *buf, size_t count)
896471d1714SBenjamin Tissoires {
897ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
898471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
899471d1714SBenjamin Tissoires 	int err;
900849e2f06SBenjamin Tissoires 	unsigned len;
901849e2f06SBenjamin Tissoires 	u8 xfer_id;
902471d1714SBenjamin Tissoires 
903849e2f06SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
904849e2f06SBenjamin Tissoires 		len = 256;
905849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_BT_XFER;
906849e2f06SBenjamin Tissoires 	} else {
907849e2f06SBenjamin Tissoires 		len = 1024;
908849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_XFER;
909849e2f06SBenjamin Tissoires 	}
910849e2f06SBenjamin Tissoires 
911849e2f06SBenjamin Tissoires 	if (count != len)
912471d1714SBenjamin Tissoires 		return -EINVAL;
913471d1714SBenjamin Tissoires 
914471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
915471d1714SBenjamin Tissoires 
916849e2f06SBenjamin Tissoires 	err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
917471d1714SBenjamin Tissoires 
918471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
919471d1714SBenjamin Tissoires 
920471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
921471d1714SBenjamin Tissoires }
922471d1714SBenjamin Tissoires 
923471d1714SBenjamin Tissoires #define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
924471d1714SBenjamin Tissoires static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
925471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
926471d1714SBenjamin Tissoires {									\
927471d1714SBenjamin Tissoires 	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
928471d1714SBenjamin Tissoires }									\
929e0984bc3SPing Cheng static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,	\
930471d1714SBenjamin Tissoires 		   NULL, wacom_btnimg##BUTTON_ID##_store)
931471d1714SBenjamin Tissoires 
932471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(0);
933471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(1);
934471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(2);
935471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(3);
936471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(4);
937471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(5);
938471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(6);
939471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(7);
940471d1714SBenjamin Tissoires 
941471d1714SBenjamin Tissoires static struct attribute *cintiq_led_attrs[] = {
942471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
943471d1714SBenjamin Tissoires 	&dev_attr_status_led1_select.attr,
944471d1714SBenjamin Tissoires 	NULL
945471d1714SBenjamin Tissoires };
946471d1714SBenjamin Tissoires 
947471d1714SBenjamin Tissoires static struct attribute_group cintiq_led_attr_group = {
948471d1714SBenjamin Tissoires 	.name = "wacom_led",
949471d1714SBenjamin Tissoires 	.attrs = cintiq_led_attrs,
950471d1714SBenjamin Tissoires };
951471d1714SBenjamin Tissoires 
952471d1714SBenjamin Tissoires static struct attribute *intuos4_led_attrs[] = {
953471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
954471d1714SBenjamin Tissoires 	&dev_attr_status1_luminance.attr,
955471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
956471d1714SBenjamin Tissoires 	&dev_attr_buttons_luminance.attr,
957471d1714SBenjamin Tissoires 	&dev_attr_button0_rawimg.attr,
958471d1714SBenjamin Tissoires 	&dev_attr_button1_rawimg.attr,
959471d1714SBenjamin Tissoires 	&dev_attr_button2_rawimg.attr,
960471d1714SBenjamin Tissoires 	&dev_attr_button3_rawimg.attr,
961471d1714SBenjamin Tissoires 	&dev_attr_button4_rawimg.attr,
962471d1714SBenjamin Tissoires 	&dev_attr_button5_rawimg.attr,
963471d1714SBenjamin Tissoires 	&dev_attr_button6_rawimg.attr,
964471d1714SBenjamin Tissoires 	&dev_attr_button7_rawimg.attr,
965471d1714SBenjamin Tissoires 	NULL
966471d1714SBenjamin Tissoires };
967471d1714SBenjamin Tissoires 
968471d1714SBenjamin Tissoires static struct attribute_group intuos4_led_attr_group = {
969471d1714SBenjamin Tissoires 	.name = "wacom_led",
970471d1714SBenjamin Tissoires 	.attrs = intuos4_led_attrs,
971471d1714SBenjamin Tissoires };
972471d1714SBenjamin Tissoires 
973471d1714SBenjamin Tissoires static struct attribute *intuos5_led_attrs[] = {
974471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
975471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
976471d1714SBenjamin Tissoires 	NULL
977471d1714SBenjamin Tissoires };
978471d1714SBenjamin Tissoires 
979471d1714SBenjamin Tissoires static struct attribute_group intuos5_led_attr_group = {
980471d1714SBenjamin Tissoires 	.name = "wacom_led",
981471d1714SBenjamin Tissoires 	.attrs = intuos5_led_attrs,
982471d1714SBenjamin Tissoires };
983471d1714SBenjamin Tissoires 
9842df68a88SBenjamin Tissoires struct wacom_sysfs_group_devres {
9852df68a88SBenjamin Tissoires 	struct attribute_group *group;
9862df68a88SBenjamin Tissoires 	struct kobject *root;
9872df68a88SBenjamin Tissoires };
9882df68a88SBenjamin Tissoires 
9892df68a88SBenjamin Tissoires static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
9902df68a88SBenjamin Tissoires {
9912df68a88SBenjamin Tissoires 	struct wacom_sysfs_group_devres *devres = res;
9922df68a88SBenjamin Tissoires 	struct kobject *kobj = devres->root;
9932df68a88SBenjamin Tissoires 
9942df68a88SBenjamin Tissoires 	dev_dbg(dev, "%s: dropping reference to %s\n",
9952df68a88SBenjamin Tissoires 		__func__, devres->group->name);
9962df68a88SBenjamin Tissoires 	sysfs_remove_group(kobj, devres->group);
9972df68a88SBenjamin Tissoires }
9982df68a88SBenjamin Tissoires 
999f9036bd4SBenjamin Tissoires static int __wacom_devm_sysfs_create_group(struct wacom *wacom,
1000f9036bd4SBenjamin Tissoires 					   struct kobject *root,
10012df68a88SBenjamin Tissoires 					   struct attribute_group *group)
10022df68a88SBenjamin Tissoires {
10032df68a88SBenjamin Tissoires 	struct wacom_sysfs_group_devres *devres;
10042df68a88SBenjamin Tissoires 	int error;
10052df68a88SBenjamin Tissoires 
10062df68a88SBenjamin Tissoires 	devres = devres_alloc(wacom_devm_sysfs_group_release,
10072df68a88SBenjamin Tissoires 			      sizeof(struct wacom_sysfs_group_devres),
10082df68a88SBenjamin Tissoires 			      GFP_KERNEL);
10092df68a88SBenjamin Tissoires 	if (!devres)
10102df68a88SBenjamin Tissoires 		return -ENOMEM;
10112df68a88SBenjamin Tissoires 
10122df68a88SBenjamin Tissoires 	devres->group = group;
1013f9036bd4SBenjamin Tissoires 	devres->root = root;
10142df68a88SBenjamin Tissoires 
10152df68a88SBenjamin Tissoires 	error = sysfs_create_group(devres->root, group);
10162df68a88SBenjamin Tissoires 	if (error)
10172df68a88SBenjamin Tissoires 		return error;
10182df68a88SBenjamin Tissoires 
10192df68a88SBenjamin Tissoires 	devres_add(&wacom->hdev->dev, devres);
10202df68a88SBenjamin Tissoires 
10212df68a88SBenjamin Tissoires 	return 0;
10222df68a88SBenjamin Tissoires }
10232df68a88SBenjamin Tissoires 
1024f9036bd4SBenjamin Tissoires static int wacom_devm_sysfs_create_group(struct wacom *wacom,
1025f9036bd4SBenjamin Tissoires 					 struct attribute_group *group)
1026f9036bd4SBenjamin Tissoires {
1027f9036bd4SBenjamin Tissoires 	return __wacom_devm_sysfs_create_group(wacom, &wacom->hdev->dev.kobj,
1028f9036bd4SBenjamin Tissoires 					       group);
1029f9036bd4SBenjamin Tissoires }
1030f9036bd4SBenjamin Tissoires 
103134736aa9SBenjamin Tissoires enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
103297f5541fSBenjamin Tissoires {
103397f5541fSBenjamin Tissoires 	struct wacom *wacom = led->wacom;
103497f5541fSBenjamin Tissoires 
103597f5541fSBenjamin Tissoires 	if (wacom->led.max_hlv)
103697f5541fSBenjamin Tissoires 		return led->hlv * LED_FULL / wacom->led.max_hlv;
103797f5541fSBenjamin Tissoires 
103897f5541fSBenjamin Tissoires 	if (wacom->led.max_llv)
103997f5541fSBenjamin Tissoires 		return led->llv * LED_FULL / wacom->led.max_llv;
104097f5541fSBenjamin Tissoires 
104197f5541fSBenjamin Tissoires 	/* device doesn't support brightness tuning */
104297f5541fSBenjamin Tissoires 	return LED_FULL;
104397f5541fSBenjamin Tissoires }
104497f5541fSBenjamin Tissoires 
104597f5541fSBenjamin Tissoires static enum led_brightness __wacom_led_brightness_get(struct led_classdev *cdev)
104697f5541fSBenjamin Tissoires {
104797f5541fSBenjamin Tissoires 	struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
104897f5541fSBenjamin Tissoires 	struct wacom *wacom = led->wacom;
104997f5541fSBenjamin Tissoires 
105097f5541fSBenjamin Tissoires 	if (wacom->led.groups[led->group].select != led->id)
105197f5541fSBenjamin Tissoires 		return LED_OFF;
105297f5541fSBenjamin Tissoires 
105397f5541fSBenjamin Tissoires 	return wacom_leds_brightness_get(led);
105497f5541fSBenjamin Tissoires }
105597f5541fSBenjamin Tissoires 
105697f5541fSBenjamin Tissoires static int wacom_led_brightness_set(struct led_classdev *cdev,
105797f5541fSBenjamin Tissoires 				    enum led_brightness brightness)
105897f5541fSBenjamin Tissoires {
105997f5541fSBenjamin Tissoires 	struct wacom_led *led = container_of(cdev, struct wacom_led, cdev);
106097f5541fSBenjamin Tissoires 	struct wacom *wacom = led->wacom;
106197f5541fSBenjamin Tissoires 	int error;
106297f5541fSBenjamin Tissoires 
106397f5541fSBenjamin Tissoires 	mutex_lock(&wacom->lock);
106497f5541fSBenjamin Tissoires 
106597f5541fSBenjamin Tissoires 	if (!wacom->led.groups || (brightness == LED_OFF &&
106697f5541fSBenjamin Tissoires 	    wacom->led.groups[led->group].select != led->id)) {
106797f5541fSBenjamin Tissoires 		error = 0;
106897f5541fSBenjamin Tissoires 		goto out;
106997f5541fSBenjamin Tissoires 	}
107097f5541fSBenjamin Tissoires 
107197f5541fSBenjamin Tissoires 	led->llv = wacom->led.llv = wacom->led.max_llv * brightness / LED_FULL;
107297f5541fSBenjamin Tissoires 	led->hlv = wacom->led.hlv = wacom->led.max_hlv * brightness / LED_FULL;
107397f5541fSBenjamin Tissoires 
107497f5541fSBenjamin Tissoires 	wacom->led.groups[led->group].select = led->id;
107597f5541fSBenjamin Tissoires 
107697f5541fSBenjamin Tissoires 	error = wacom_led_control(wacom);
107797f5541fSBenjamin Tissoires 
107897f5541fSBenjamin Tissoires out:
107997f5541fSBenjamin Tissoires 	mutex_unlock(&wacom->lock);
108097f5541fSBenjamin Tissoires 
108197f5541fSBenjamin Tissoires 	return error;
108297f5541fSBenjamin Tissoires }
108397f5541fSBenjamin Tissoires 
108497f5541fSBenjamin Tissoires static void wacom_led_readonly_brightness_set(struct led_classdev *cdev,
108597f5541fSBenjamin Tissoires 					       enum led_brightness brightness)
108697f5541fSBenjamin Tissoires {
108797f5541fSBenjamin Tissoires }
108897f5541fSBenjamin Tissoires 
108997f5541fSBenjamin Tissoires static int wacom_led_register_one(struct device *dev, struct wacom *wacom,
109097f5541fSBenjamin Tissoires 				  struct wacom_led *led, unsigned int group,
109197f5541fSBenjamin Tissoires 				  unsigned int id, bool read_only)
109297f5541fSBenjamin Tissoires {
109397f5541fSBenjamin Tissoires 	int error;
109497f5541fSBenjamin Tissoires 	char *name;
109597f5541fSBenjamin Tissoires 
109697f5541fSBenjamin Tissoires 	name = devm_kasprintf(dev, GFP_KERNEL,
109797f5541fSBenjamin Tissoires 			      "%s::wacom-%d.%d",
109897f5541fSBenjamin Tissoires 			      dev_name(dev),
109997f5541fSBenjamin Tissoires 			      group,
110097f5541fSBenjamin Tissoires 			      id);
110197f5541fSBenjamin Tissoires 	if (!name)
110297f5541fSBenjamin Tissoires 		return -ENOMEM;
110397f5541fSBenjamin Tissoires 
110434736aa9SBenjamin Tissoires 	if (!read_only) {
110534736aa9SBenjamin Tissoires 		led->trigger.name = name;
110634736aa9SBenjamin Tissoires 		error = devm_led_trigger_register(dev, &led->trigger);
110734736aa9SBenjamin Tissoires 		if (error) {
110834736aa9SBenjamin Tissoires 			hid_err(wacom->hdev,
110934736aa9SBenjamin Tissoires 				"failed to register LED trigger %s: %d\n",
111034736aa9SBenjamin Tissoires 				led->cdev.name, error);
111134736aa9SBenjamin Tissoires 			return error;
111234736aa9SBenjamin Tissoires 		}
111334736aa9SBenjamin Tissoires 	}
111434736aa9SBenjamin Tissoires 
111597f5541fSBenjamin Tissoires 	led->group = group;
111697f5541fSBenjamin Tissoires 	led->id = id;
111797f5541fSBenjamin Tissoires 	led->wacom = wacom;
111897f5541fSBenjamin Tissoires 	led->llv = wacom->led.llv;
111997f5541fSBenjamin Tissoires 	led->hlv = wacom->led.hlv;
112097f5541fSBenjamin Tissoires 	led->cdev.name = name;
112197f5541fSBenjamin Tissoires 	led->cdev.max_brightness = LED_FULL;
112297f5541fSBenjamin Tissoires 	led->cdev.flags = LED_HW_PLUGGABLE;
112397f5541fSBenjamin Tissoires 	led->cdev.brightness_get = __wacom_led_brightness_get;
112434736aa9SBenjamin Tissoires 	if (!read_only) {
112597f5541fSBenjamin Tissoires 		led->cdev.brightness_set_blocking = wacom_led_brightness_set;
112634736aa9SBenjamin Tissoires 		led->cdev.default_trigger = led->cdev.name;
112734736aa9SBenjamin Tissoires 	} else {
112897f5541fSBenjamin Tissoires 		led->cdev.brightness_set = wacom_led_readonly_brightness_set;
112934736aa9SBenjamin Tissoires 	}
113097f5541fSBenjamin Tissoires 
113197f5541fSBenjamin Tissoires 	error = devm_led_classdev_register(dev, &led->cdev);
113297f5541fSBenjamin Tissoires 	if (error) {
113397f5541fSBenjamin Tissoires 		hid_err(wacom->hdev,
113497f5541fSBenjamin Tissoires 			"failed to register LED %s: %d\n",
113597f5541fSBenjamin Tissoires 			led->cdev.name, error);
113697f5541fSBenjamin Tissoires 		led->cdev.name = NULL;
113797f5541fSBenjamin Tissoires 		return error;
113897f5541fSBenjamin Tissoires 	}
113997f5541fSBenjamin Tissoires 
114097f5541fSBenjamin Tissoires 	return 0;
114197f5541fSBenjamin Tissoires }
114297f5541fSBenjamin Tissoires 
1143589e5060SBenjamin Tissoires static void wacom_led_groups_release_one(void *data)
1144589e5060SBenjamin Tissoires {
1145589e5060SBenjamin Tissoires 	struct wacom_group_leds *group = data;
1146589e5060SBenjamin Tissoires 
1147589e5060SBenjamin Tissoires 	devres_release_group(group->dev, group);
1148589e5060SBenjamin Tissoires }
1149589e5060SBenjamin Tissoires 
115097f5541fSBenjamin Tissoires static int wacom_led_groups_alloc_and_register_one(struct device *dev,
115197f5541fSBenjamin Tissoires 						   struct wacom *wacom,
115297f5541fSBenjamin Tissoires 						   int group_id, int count,
115397f5541fSBenjamin Tissoires 						   bool read_only)
115497f5541fSBenjamin Tissoires {
115597f5541fSBenjamin Tissoires 	struct wacom_led *leds;
115697f5541fSBenjamin Tissoires 	int i, error;
115797f5541fSBenjamin Tissoires 
115897f5541fSBenjamin Tissoires 	if (group_id >= wacom->led.count || count <= 0)
115997f5541fSBenjamin Tissoires 		return -EINVAL;
116097f5541fSBenjamin Tissoires 
116197f5541fSBenjamin Tissoires 	if (!devres_open_group(dev, &wacom->led.groups[group_id], GFP_KERNEL))
116297f5541fSBenjamin Tissoires 		return -ENOMEM;
116397f5541fSBenjamin Tissoires 
116497f5541fSBenjamin Tissoires 	leds = devm_kzalloc(dev, sizeof(struct wacom_led) * count, GFP_KERNEL);
116597f5541fSBenjamin Tissoires 	if (!leds) {
116697f5541fSBenjamin Tissoires 		error = -ENOMEM;
116797f5541fSBenjamin Tissoires 		goto err;
116897f5541fSBenjamin Tissoires 	}
116997f5541fSBenjamin Tissoires 
117097f5541fSBenjamin Tissoires 	wacom->led.groups[group_id].leds = leds;
117197f5541fSBenjamin Tissoires 	wacom->led.groups[group_id].count = count;
117297f5541fSBenjamin Tissoires 
117397f5541fSBenjamin Tissoires 	for (i = 0; i < count; i++) {
117497f5541fSBenjamin Tissoires 		error = wacom_led_register_one(dev, wacom, &leds[i],
117597f5541fSBenjamin Tissoires 					       group_id, i, read_only);
117697f5541fSBenjamin Tissoires 		if (error)
117797f5541fSBenjamin Tissoires 			goto err;
117897f5541fSBenjamin Tissoires 	}
117997f5541fSBenjamin Tissoires 
1180589e5060SBenjamin Tissoires 	wacom->led.groups[group_id].dev = dev;
1181589e5060SBenjamin Tissoires 
1182589e5060SBenjamin Tissoires 	devres_close_group(dev, &wacom->led.groups[group_id]);
1183589e5060SBenjamin Tissoires 
1184589e5060SBenjamin Tissoires 	/*
1185589e5060SBenjamin Tissoires 	 * There is a bug (?) in devm_led_classdev_register() in which its
1186589e5060SBenjamin Tissoires 	 * increments the refcount of the parent. If the parent is an input
1187589e5060SBenjamin Tissoires 	 * device, that means the ref count never reaches 0 when
1188589e5060SBenjamin Tissoires 	 * devm_input_device_release() gets called.
1189589e5060SBenjamin Tissoires 	 * This means that the LEDs are still there after disconnect.
1190589e5060SBenjamin Tissoires 	 * Manually force the release of the group so that the leds are released
1191589e5060SBenjamin Tissoires 	 * once we are done using them.
1192589e5060SBenjamin Tissoires 	 */
1193589e5060SBenjamin Tissoires 	error = devm_add_action_or_reset(&wacom->hdev->dev,
1194589e5060SBenjamin Tissoires 					 wacom_led_groups_release_one,
1195589e5060SBenjamin Tissoires 					 &wacom->led.groups[group_id]);
1196589e5060SBenjamin Tissoires 	if (error)
1197589e5060SBenjamin Tissoires 		return error;
1198589e5060SBenjamin Tissoires 
119997f5541fSBenjamin Tissoires 	return 0;
120097f5541fSBenjamin Tissoires 
120197f5541fSBenjamin Tissoires err:
120297f5541fSBenjamin Tissoires 	devres_release_group(dev, &wacom->led.groups[group_id]);
120397f5541fSBenjamin Tissoires 	return error;
120497f5541fSBenjamin Tissoires }
120597f5541fSBenjamin Tissoires 
120634736aa9SBenjamin Tissoires struct wacom_led *wacom_led_find(struct wacom *wacom, unsigned int group_id,
120734736aa9SBenjamin Tissoires 				 unsigned int id)
120834736aa9SBenjamin Tissoires {
120934736aa9SBenjamin Tissoires 	struct wacom_group_leds *group;
121034736aa9SBenjamin Tissoires 
121134736aa9SBenjamin Tissoires 	if (group_id >= wacom->led.count)
121234736aa9SBenjamin Tissoires 		return NULL;
121334736aa9SBenjamin Tissoires 
121434736aa9SBenjamin Tissoires 	group = &wacom->led.groups[group_id];
121534736aa9SBenjamin Tissoires 
121634736aa9SBenjamin Tissoires 	if (!group->leds)
121734736aa9SBenjamin Tissoires 		return NULL;
121834736aa9SBenjamin Tissoires 
121934736aa9SBenjamin Tissoires 	id %= group->count;
122034736aa9SBenjamin Tissoires 
122134736aa9SBenjamin Tissoires 	return &group->leds[id];
122234736aa9SBenjamin Tissoires }
122334736aa9SBenjamin Tissoires 
122434736aa9SBenjamin Tissoires /**
122534736aa9SBenjamin Tissoires  * wacom_led_next: gives the next available led with a wacom trigger.
122634736aa9SBenjamin Tissoires  *
122734736aa9SBenjamin Tissoires  * returns the next available struct wacom_led which has its default trigger
122834736aa9SBenjamin Tissoires  * or the current one if none is available.
122934736aa9SBenjamin Tissoires  */
123034736aa9SBenjamin Tissoires struct wacom_led *wacom_led_next(struct wacom *wacom, struct wacom_led *cur)
123134736aa9SBenjamin Tissoires {
123234736aa9SBenjamin Tissoires 	struct wacom_led *next_led;
123334736aa9SBenjamin Tissoires 	int group, next;
123434736aa9SBenjamin Tissoires 
123534736aa9SBenjamin Tissoires 	if (!wacom || !cur)
123634736aa9SBenjamin Tissoires 		return NULL;
123734736aa9SBenjamin Tissoires 
123834736aa9SBenjamin Tissoires 	group = cur->group;
123934736aa9SBenjamin Tissoires 	next = cur->id;
124034736aa9SBenjamin Tissoires 
124134736aa9SBenjamin Tissoires 	do {
124234736aa9SBenjamin Tissoires 		next_led = wacom_led_find(wacom, group, ++next);
124334736aa9SBenjamin Tissoires 		if (!next_led || next_led == cur)
124434736aa9SBenjamin Tissoires 			return next_led;
124534736aa9SBenjamin Tissoires 	} while (next_led->cdev.trigger != &next_led->trigger);
124634736aa9SBenjamin Tissoires 
124734736aa9SBenjamin Tissoires 	return next_led;
124834736aa9SBenjamin Tissoires }
124934736aa9SBenjamin Tissoires 
1250a50aac71SBenjamin Tissoires static void wacom_led_groups_release(void *data)
1251a50aac71SBenjamin Tissoires {
1252a50aac71SBenjamin Tissoires 	struct wacom *wacom = data;
1253a50aac71SBenjamin Tissoires 
1254a50aac71SBenjamin Tissoires 	wacom->led.groups = NULL;
125597f5541fSBenjamin Tissoires 	wacom->led.count = 0;
1256a50aac71SBenjamin Tissoires }
1257a50aac71SBenjamin Tissoires 
1258a50aac71SBenjamin Tissoires static int wacom_led_groups_allocate(struct wacom *wacom, int count)
1259a50aac71SBenjamin Tissoires {
126097f5541fSBenjamin Tissoires 	struct device *dev = &wacom->hdev->dev;
1261a50aac71SBenjamin Tissoires 	struct wacom_group_leds *groups;
1262a50aac71SBenjamin Tissoires 	int error;
1263a50aac71SBenjamin Tissoires 
126497f5541fSBenjamin Tissoires 	groups = devm_kzalloc(dev, sizeof(struct wacom_group_leds) * count,
1265a50aac71SBenjamin Tissoires 			      GFP_KERNEL);
1266a50aac71SBenjamin Tissoires 	if (!groups)
1267a50aac71SBenjamin Tissoires 		return -ENOMEM;
1268a50aac71SBenjamin Tissoires 
126997f5541fSBenjamin Tissoires 	error = devm_add_action_or_reset(dev, wacom_led_groups_release, wacom);
1270a50aac71SBenjamin Tissoires 	if (error)
1271a50aac71SBenjamin Tissoires 		return error;
1272a50aac71SBenjamin Tissoires 
1273a50aac71SBenjamin Tissoires 	wacom->led.groups = groups;
127497f5541fSBenjamin Tissoires 	wacom->led.count = count;
127597f5541fSBenjamin Tissoires 
127697f5541fSBenjamin Tissoires 	return 0;
127797f5541fSBenjamin Tissoires }
127897f5541fSBenjamin Tissoires 
127997f5541fSBenjamin Tissoires static int wacom_leds_alloc_and_register(struct wacom *wacom, int group_count,
128097f5541fSBenjamin Tissoires 					 int led_per_group, bool read_only)
128197f5541fSBenjamin Tissoires {
128297f5541fSBenjamin Tissoires 	struct device *dev;
128397f5541fSBenjamin Tissoires 	int i, error;
128497f5541fSBenjamin Tissoires 
128597f5541fSBenjamin Tissoires 	if (!wacom->wacom_wac.pad_input)
128697f5541fSBenjamin Tissoires 		return -EINVAL;
128797f5541fSBenjamin Tissoires 
128897f5541fSBenjamin Tissoires 	dev = &wacom->wacom_wac.pad_input->dev;
128997f5541fSBenjamin Tissoires 
129097f5541fSBenjamin Tissoires 	error = wacom_led_groups_allocate(wacom, group_count);
129197f5541fSBenjamin Tissoires 	if (error)
129297f5541fSBenjamin Tissoires 		return error;
129397f5541fSBenjamin Tissoires 
129497f5541fSBenjamin Tissoires 	for (i = 0; i < group_count; i++) {
129597f5541fSBenjamin Tissoires 		error = wacom_led_groups_alloc_and_register_one(dev, wacom, i,
129697f5541fSBenjamin Tissoires 								led_per_group,
129797f5541fSBenjamin Tissoires 								read_only);
129897f5541fSBenjamin Tissoires 		if (error)
129997f5541fSBenjamin Tissoires 			return error;
130097f5541fSBenjamin Tissoires 	}
1301a50aac71SBenjamin Tissoires 
1302a50aac71SBenjamin Tissoires 	return 0;
1303a50aac71SBenjamin Tissoires }
1304a50aac71SBenjamin Tissoires 
1305471d1714SBenjamin Tissoires static int wacom_initialize_leds(struct wacom *wacom)
1306471d1714SBenjamin Tissoires {
1307471d1714SBenjamin Tissoires 	int error;
1308471d1714SBenjamin Tissoires 
1309862cf553SJason Gerecke 	if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
1310862cf553SJason Gerecke 		return 0;
1311862cf553SJason Gerecke 
1312471d1714SBenjamin Tissoires 	/* Initialize default values */
1313471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
1314471d1714SBenjamin Tissoires 	case INTUOS4S:
1315471d1714SBenjamin Tissoires 	case INTUOS4:
131681af7e61SBenjamin Tissoires 	case INTUOS4WL:
1317471d1714SBenjamin Tissoires 	case INTUOS4L:
1318471d1714SBenjamin Tissoires 		wacom->led.llv = 10;
1319471d1714SBenjamin Tissoires 		wacom->led.hlv = 20;
132097f5541fSBenjamin Tissoires 		wacom->led.max_llv = 127;
132197f5541fSBenjamin Tissoires 		wacom->led.max_hlv = 127;
1322471d1714SBenjamin Tissoires 		wacom->led.img_lum = 10;
1323a50aac71SBenjamin Tissoires 
132497f5541fSBenjamin Tissoires 		error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1325a50aac71SBenjamin Tissoires 		if (error) {
1326a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1327a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1328a50aac71SBenjamin Tissoires 			return error;
1329a50aac71SBenjamin Tissoires 		}
1330a50aac71SBenjamin Tissoires 
13312df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
1332471d1714SBenjamin Tissoires 						      &intuos4_led_attr_group);
1333471d1714SBenjamin Tissoires 		break;
1334471d1714SBenjamin Tissoires 
1335471d1714SBenjamin Tissoires 	case WACOM_24HD:
1336471d1714SBenjamin Tissoires 	case WACOM_21UX2:
1337471d1714SBenjamin Tissoires 		wacom->led.llv = 0;
1338471d1714SBenjamin Tissoires 		wacom->led.hlv = 0;
1339471d1714SBenjamin Tissoires 		wacom->led.img_lum = 0;
1340471d1714SBenjamin Tissoires 
134197f5541fSBenjamin Tissoires 		error = wacom_leds_alloc_and_register(wacom, 2, 4, false);
1342a50aac71SBenjamin Tissoires 		if (error) {
1343a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1344a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1345a50aac71SBenjamin Tissoires 			return error;
1346a50aac71SBenjamin Tissoires 		}
1347a50aac71SBenjamin Tissoires 
13482df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
1349471d1714SBenjamin Tissoires 						      &cintiq_led_attr_group);
1350471d1714SBenjamin Tissoires 		break;
1351471d1714SBenjamin Tissoires 
1352471d1714SBenjamin Tissoires 	case INTUOS5S:
1353471d1714SBenjamin Tissoires 	case INTUOS5:
1354471d1714SBenjamin Tissoires 	case INTUOS5L:
1355471d1714SBenjamin Tissoires 	case INTUOSPS:
1356471d1714SBenjamin Tissoires 	case INTUOSPM:
1357471d1714SBenjamin Tissoires 	case INTUOSPL:
1358471d1714SBenjamin Tissoires 		wacom->led.llv = 32;
135997f5541fSBenjamin Tissoires 		wacom->led.max_llv = 96;
1360471d1714SBenjamin Tissoires 
136197f5541fSBenjamin Tissoires 		error = wacom_leds_alloc_and_register(wacom, 1, 4, false);
1362a50aac71SBenjamin Tissoires 		if (error) {
1363a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1364a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1365a50aac71SBenjamin Tissoires 			return error;
1366a50aac71SBenjamin Tissoires 		}
1367a50aac71SBenjamin Tissoires 
13682df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
1369471d1714SBenjamin Tissoires 						      &intuos5_led_attr_group);
1370471d1714SBenjamin Tissoires 		break;
1371471d1714SBenjamin Tissoires 
1372a50aac71SBenjamin Tissoires 	case REMOTE:
137397f5541fSBenjamin Tissoires 		wacom->led.llv = 255;
137497f5541fSBenjamin Tissoires 		wacom->led.max_llv = 255;
1375a50aac71SBenjamin Tissoires 		error = wacom_led_groups_allocate(wacom, 5);
1376a50aac71SBenjamin Tissoires 		if (error) {
1377a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1378a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1379a50aac71SBenjamin Tissoires 			return error;
1380a50aac71SBenjamin Tissoires 		}
1381a50aac71SBenjamin Tissoires 		return 0;
1382a50aac71SBenjamin Tissoires 
1383471d1714SBenjamin Tissoires 	default:
1384471d1714SBenjamin Tissoires 		return 0;
1385471d1714SBenjamin Tissoires 	}
1386471d1714SBenjamin Tissoires 
1387471d1714SBenjamin Tissoires 	if (error) {
1388471d1714SBenjamin Tissoires 		hid_err(wacom->hdev,
1389471d1714SBenjamin Tissoires 			"cannot create sysfs group err: %d\n", error);
1390471d1714SBenjamin Tissoires 		return error;
1391471d1714SBenjamin Tissoires 	}
1392471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
1393471d1714SBenjamin Tissoires 
1394471d1714SBenjamin Tissoires 	return 0;
1395471d1714SBenjamin Tissoires }
1396471d1714SBenjamin Tissoires 
1397471d1714SBenjamin Tissoires static enum power_supply_property wacom_battery_props[] = {
13989956953eSBenjamin Tissoires 	POWER_SUPPLY_PROP_MODEL_NAME,
139971fa641eSJason Gerecke 	POWER_SUPPLY_PROP_PRESENT,
1400ac8d1010SBenjamin Tissoires 	POWER_SUPPLY_PROP_STATUS,
1401471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
1402471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_CAPACITY
1403471d1714SBenjamin Tissoires };
1404471d1714SBenjamin Tissoires 
1405471d1714SBenjamin Tissoires static int wacom_battery_get_property(struct power_supply *psy,
1406471d1714SBenjamin Tissoires 				      enum power_supply_property psp,
1407471d1714SBenjamin Tissoires 				      union power_supply_propval *val)
1408471d1714SBenjamin Tissoires {
140959d69bc8SBenjamin Tissoires 	struct wacom_battery *battery = power_supply_get_drvdata(psy);
1410471d1714SBenjamin Tissoires 	int ret = 0;
1411471d1714SBenjamin Tissoires 
1412471d1714SBenjamin Tissoires 	switch (psp) {
14139956953eSBenjamin Tissoires 		case POWER_SUPPLY_PROP_MODEL_NAME:
14149956953eSBenjamin Tissoires 			val->strval = battery->wacom->wacom_wac.name;
14159956953eSBenjamin Tissoires 			break;
141671fa641eSJason Gerecke 		case POWER_SUPPLY_PROP_PRESENT:
141759d69bc8SBenjamin Tissoires 			val->intval = battery->bat_connected;
141871fa641eSJason Gerecke 			break;
1419471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_SCOPE:
1420471d1714SBenjamin Tissoires 			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1421471d1714SBenjamin Tissoires 			break;
1422471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_CAPACITY:
142359d69bc8SBenjamin Tissoires 			val->intval = battery->battery_capacity;
1424ac8d1010SBenjamin Tissoires 			break;
1425ac8d1010SBenjamin Tissoires 		case POWER_SUPPLY_PROP_STATUS:
142659d69bc8SBenjamin Tissoires 			if (battery->bat_charging)
1427ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_CHARGING;
142859d69bc8SBenjamin Tissoires 			else if (battery->battery_capacity == 100 &&
142959d69bc8SBenjamin Tissoires 				    battery->ps_connected)
1430ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_FULL;
143159d69bc8SBenjamin Tissoires 			else if (battery->ps_connected)
1432b0882cb7SJason Gerecke 				val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1433ac8d1010SBenjamin Tissoires 			else
1434ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1435471d1714SBenjamin Tissoires 			break;
1436471d1714SBenjamin Tissoires 		default:
1437471d1714SBenjamin Tissoires 			ret = -EINVAL;
1438471d1714SBenjamin Tissoires 			break;
1439471d1714SBenjamin Tissoires 	}
1440471d1714SBenjamin Tissoires 
1441471d1714SBenjamin Tissoires 	return ret;
1442471d1714SBenjamin Tissoires }
1443471d1714SBenjamin Tissoires 
144459d69bc8SBenjamin Tissoires static int __wacom_initialize_battery(struct wacom *wacom,
144559d69bc8SBenjamin Tissoires 				      struct wacom_battery *battery)
1446471d1714SBenjamin Tissoires {
1447d70420b9SBenjamin Tissoires 	static atomic_t battery_no = ATOMIC_INIT(0);
1448b189da90SBenjamin Tissoires 	struct device *dev = &wacom->hdev->dev;
144959d69bc8SBenjamin Tissoires 	struct power_supply_config psy_cfg = { .drv_data = battery, };
1450136ae5e9SBenjamin Tissoires 	struct power_supply *ps_bat;
145159d69bc8SBenjamin Tissoires 	struct power_supply_desc *bat_desc = &battery->bat_desc;
1452d70420b9SBenjamin Tissoires 	unsigned long n;
1453b189da90SBenjamin Tissoires 	int error;
1454b189da90SBenjamin Tissoires 
1455b189da90SBenjamin Tissoires 	if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1456b189da90SBenjamin Tissoires 		return -ENOMEM;
1457471d1714SBenjamin Tissoires 
14589956953eSBenjamin Tissoires 	battery->wacom = wacom;
14599956953eSBenjamin Tissoires 
1460d70420b9SBenjamin Tissoires 	n = atomic_inc_return(&battery_no) - 1;
14617dbd229eSBenjamin Tissoires 
1462297d716fSKrzysztof Kozlowski 	bat_desc->properties = wacom_battery_props;
1463297d716fSKrzysztof Kozlowski 	bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1464297d716fSKrzysztof Kozlowski 	bat_desc->get_property = wacom_battery_get_property;
146559d69bc8SBenjamin Tissoires 	sprintf(battery->bat_name, "wacom_battery_%ld", n);
146659d69bc8SBenjamin Tissoires 	bat_desc->name = battery->bat_name;
146796983296SBenjamin Tissoires 	bat_desc->type = POWER_SUPPLY_TYPE_USB;
1468297d716fSKrzysztof Kozlowski 	bat_desc->use_for_apm = 0;
1469471d1714SBenjamin Tissoires 
147059d69bc8SBenjamin Tissoires 	ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
147159d69bc8SBenjamin Tissoires 	if (IS_ERR(ps_bat)) {
147259d69bc8SBenjamin Tissoires 		error = PTR_ERR(ps_bat);
1473b189da90SBenjamin Tissoires 		goto err;
1474b189da90SBenjamin Tissoires 	}
1475471d1714SBenjamin Tissoires 
147659d69bc8SBenjamin Tissoires 	power_supply_powers(ps_bat, &wacom->hdev->dev);
147759d69bc8SBenjamin Tissoires 
147859d69bc8SBenjamin Tissoires 	battery->battery = ps_bat;
14797dbd229eSBenjamin Tissoires 
1480b189da90SBenjamin Tissoires 	devres_close_group(dev, bat_desc);
14817dbd229eSBenjamin Tissoires 	return 0;
1482b189da90SBenjamin Tissoires 
1483b189da90SBenjamin Tissoires err:
1484b189da90SBenjamin Tissoires 	devres_release_group(dev, bat_desc);
1485b189da90SBenjamin Tissoires 	return error;
1486471d1714SBenjamin Tissoires }
1487471d1714SBenjamin Tissoires 
148859d69bc8SBenjamin Tissoires static int wacom_initialize_battery(struct wacom *wacom)
148959d69bc8SBenjamin Tissoires {
149059d69bc8SBenjamin Tissoires 	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY)
149159d69bc8SBenjamin Tissoires 		return __wacom_initialize_battery(wacom, &wacom->battery);
149259d69bc8SBenjamin Tissoires 
149359d69bc8SBenjamin Tissoires 	return 0;
149459d69bc8SBenjamin Tissoires }
149559d69bc8SBenjamin Tissoires 
1496471d1714SBenjamin Tissoires static void wacom_destroy_battery(struct wacom *wacom)
1497471d1714SBenjamin Tissoires {
149859d69bc8SBenjamin Tissoires 	if (wacom->battery.battery) {
149959d69bc8SBenjamin Tissoires 		devres_release_group(&wacom->hdev->dev,
150059d69bc8SBenjamin Tissoires 				     &wacom->battery.bat_desc);
150159d69bc8SBenjamin Tissoires 		wacom->battery.battery = NULL;
1502471d1714SBenjamin Tissoires 	}
1503471d1714SBenjamin Tissoires }
1504471d1714SBenjamin Tissoires 
1505f81a1295SBenjamin Tissoires static ssize_t wacom_show_speed(struct device *dev,
1506f81a1295SBenjamin Tissoires 				struct device_attribute
1507f81a1295SBenjamin Tissoires 				*attr, char *buf)
1508f81a1295SBenjamin Tissoires {
1509ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1510f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1511f81a1295SBenjamin Tissoires 
1512f81a1295SBenjamin Tissoires 	return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1513f81a1295SBenjamin Tissoires }
1514f81a1295SBenjamin Tissoires 
1515f81a1295SBenjamin Tissoires static ssize_t wacom_store_speed(struct device *dev,
1516f81a1295SBenjamin Tissoires 				struct device_attribute *attr,
1517f81a1295SBenjamin Tissoires 				const char *buf, size_t count)
1518f81a1295SBenjamin Tissoires {
1519ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1520f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1521f81a1295SBenjamin Tissoires 	u8 new_speed;
1522f81a1295SBenjamin Tissoires 
1523f81a1295SBenjamin Tissoires 	if (kstrtou8(buf, 0, &new_speed))
1524f81a1295SBenjamin Tissoires 		return -EINVAL;
1525f81a1295SBenjamin Tissoires 
1526f81a1295SBenjamin Tissoires 	if (new_speed != 0 && new_speed != 1)
1527f81a1295SBenjamin Tissoires 		return -EINVAL;
1528f81a1295SBenjamin Tissoires 
1529f81a1295SBenjamin Tissoires 	wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1530f81a1295SBenjamin Tissoires 
1531f81a1295SBenjamin Tissoires 	return count;
1532f81a1295SBenjamin Tissoires }
1533f81a1295SBenjamin Tissoires 
1534e0984bc3SPing Cheng static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1535f81a1295SBenjamin Tissoires 		wacom_show_speed, wacom_store_speed);
1536f81a1295SBenjamin Tissoires 
153772b236d6SAaron Skomra 
153872b236d6SAaron Skomra static ssize_t wacom_show_remote_mode(struct kobject *kobj,
153972b236d6SAaron Skomra 				      struct kobj_attribute *kattr,
154072b236d6SAaron Skomra 				      char *buf, int index)
154172b236d6SAaron Skomra {
15422cf83833SGeliang Tang 	struct device *dev = kobj_to_dev(kobj->parent);
1543ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
154472b236d6SAaron Skomra 	struct wacom *wacom = hid_get_drvdata(hdev);
154572b236d6SAaron Skomra 	u8 mode;
154672b236d6SAaron Skomra 
1547a50aac71SBenjamin Tissoires 	mode = wacom->led.groups[index].select;
154872b236d6SAaron Skomra 	if (mode >= 0 && mode < 3)
154972b236d6SAaron Skomra 		return snprintf(buf, PAGE_SIZE, "%d\n", mode);
155072b236d6SAaron Skomra 	else
155172b236d6SAaron Skomra 		return snprintf(buf, PAGE_SIZE, "%d\n", -1);
155272b236d6SAaron Skomra }
155372b236d6SAaron Skomra 
155472b236d6SAaron Skomra #define DEVICE_EKR_ATTR_GROUP(SET_ID)					\
155572b236d6SAaron Skomra static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj,	\
155672b236d6SAaron Skomra 			       struct kobj_attribute *kattr, char *buf)	\
155772b236d6SAaron Skomra {									\
155872b236d6SAaron Skomra 	return wacom_show_remote_mode(kobj, kattr, buf, SET_ID);	\
155972b236d6SAaron Skomra }									\
156072b236d6SAaron Skomra static struct kobj_attribute remote##SET_ID##_mode_attr = {		\
156172b236d6SAaron Skomra 	.attr = {.name = "remote_mode",					\
156272b236d6SAaron Skomra 		.mode = DEV_ATTR_RO_PERM},				\
156372b236d6SAaron Skomra 	.show = wacom_show_remote##SET_ID##_mode,			\
156472b236d6SAaron Skomra };									\
156572b236d6SAaron Skomra static struct attribute *remote##SET_ID##_serial_attrs[] = {		\
156672b236d6SAaron Skomra 	&remote##SET_ID##_mode_attr.attr,				\
156772b236d6SAaron Skomra 	NULL								\
156872b236d6SAaron Skomra };									\
156972b236d6SAaron Skomra static struct attribute_group remote##SET_ID##_serial_group = {		\
157072b236d6SAaron Skomra 	.name = NULL,							\
157172b236d6SAaron Skomra 	.attrs = remote##SET_ID##_serial_attrs,				\
157272b236d6SAaron Skomra }
157372b236d6SAaron Skomra 
157472b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(0);
157572b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(1);
157672b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(2);
157772b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(3);
157872b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(4);
157972b236d6SAaron Skomra 
1580e6f2813aSBenjamin Tissoires static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1581e6f2813aSBenjamin Tissoires 					  int index)
158272b236d6SAaron Skomra {
158372b236d6SAaron Skomra 	int error = 0;
158483e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
158572b236d6SAaron Skomra 
1586e7749f6eSBenjamin Tissoires 	remote->remotes[index].group.name = devm_kasprintf(&wacom->hdev->dev,
1587c1f5409bSBenjamin Tissoires 							  GFP_KERNEL,
1588c1f5409bSBenjamin Tissoires 							  "%d", serial);
1589e7749f6eSBenjamin Tissoires 	if (!remote->remotes[index].group.name)
159072b236d6SAaron Skomra 		return -ENOMEM;
159172b236d6SAaron Skomra 
1592f9036bd4SBenjamin Tissoires 	error = __wacom_devm_sysfs_create_group(wacom, remote->remote_dir,
1593e7749f6eSBenjamin Tissoires 						&remote->remotes[index].group);
159472b236d6SAaron Skomra 	if (error) {
1595e7749f6eSBenjamin Tissoires 		remote->remotes[index].group.name = NULL;
159672b236d6SAaron Skomra 		hid_err(wacom->hdev,
159772b236d6SAaron Skomra 			"cannot create sysfs group err: %d\n", error);
159872b236d6SAaron Skomra 		return error;
159972b236d6SAaron Skomra 	}
160072b236d6SAaron Skomra 
160172b236d6SAaron Skomra 	return 0;
160272b236d6SAaron Skomra }
160372b236d6SAaron Skomra 
160472b236d6SAaron Skomra static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
160572b236d6SAaron Skomra {
160672b236d6SAaron Skomra 	const size_t buf_size = 2;
160772b236d6SAaron Skomra 	unsigned char *buf;
160872b236d6SAaron Skomra 	int retval;
160972b236d6SAaron Skomra 
161072b236d6SAaron Skomra 	buf = kzalloc(buf_size, GFP_KERNEL);
161172b236d6SAaron Skomra 	if (!buf)
161272b236d6SAaron Skomra 		return -ENOMEM;
161372b236d6SAaron Skomra 
161472b236d6SAaron Skomra 	buf[0] = WAC_CMD_DELETE_PAIRING;
161572b236d6SAaron Skomra 	buf[1] = selector;
161672b236d6SAaron Skomra 
161772b236d6SAaron Skomra 	retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
161872b236d6SAaron Skomra 				  buf_size, WAC_CMD_RETRIES);
161972b236d6SAaron Skomra 	kfree(buf);
162072b236d6SAaron Skomra 
162172b236d6SAaron Skomra 	return retval;
162272b236d6SAaron Skomra }
162372b236d6SAaron Skomra 
162472b236d6SAaron Skomra static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
162572b236d6SAaron Skomra 					 struct kobj_attribute *attr,
162672b236d6SAaron Skomra 					 const char *buf, size_t count)
162772b236d6SAaron Skomra {
162872b236d6SAaron Skomra 	unsigned char selector = 0;
16292cf83833SGeliang Tang 	struct device *dev = kobj_to_dev(kobj->parent);
1630ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
163172b236d6SAaron Skomra 	struct wacom *wacom = hid_get_drvdata(hdev);
163272b236d6SAaron Skomra 	int err;
163372b236d6SAaron Skomra 
163472b236d6SAaron Skomra 	if (!strncmp(buf, "*\n", 2)) {
163572b236d6SAaron Skomra 		selector = WAC_CMD_UNPAIR_ALL;
163672b236d6SAaron Skomra 	} else {
163772b236d6SAaron Skomra 		hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
163872b236d6SAaron Skomra 			 buf);
163972b236d6SAaron Skomra 		return -1;
164072b236d6SAaron Skomra 	}
164172b236d6SAaron Skomra 
164272b236d6SAaron Skomra 	mutex_lock(&wacom->lock);
164372b236d6SAaron Skomra 
164472b236d6SAaron Skomra 	err = wacom_cmd_unpair_remote(wacom, selector);
164572b236d6SAaron Skomra 	mutex_unlock(&wacom->lock);
164672b236d6SAaron Skomra 
164772b236d6SAaron Skomra 	return err < 0 ? err : count;
164872b236d6SAaron Skomra }
164972b236d6SAaron Skomra 
165072b236d6SAaron Skomra static struct kobj_attribute unpair_remote_attr = {
165172b236d6SAaron Skomra 	.attr = {.name = "unpair_remote", .mode = 0200},
165272b236d6SAaron Skomra 	.store = wacom_store_unpair_remote,
165372b236d6SAaron Skomra };
165472b236d6SAaron Skomra 
165572b236d6SAaron Skomra static const struct attribute *remote_unpair_attrs[] = {
165672b236d6SAaron Skomra 	&unpair_remote_attr.attr,
165772b236d6SAaron Skomra 	NULL
165872b236d6SAaron Skomra };
165972b236d6SAaron Skomra 
166083e6b40eSBenjamin Tissoires static void wacom_remotes_destroy(void *data)
166183e6b40eSBenjamin Tissoires {
166283e6b40eSBenjamin Tissoires 	struct wacom *wacom = data;
166383e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
166483e6b40eSBenjamin Tissoires 
166583e6b40eSBenjamin Tissoires 	if (!remote)
166683e6b40eSBenjamin Tissoires 		return;
166783e6b40eSBenjamin Tissoires 
166883e6b40eSBenjamin Tissoires 	kobject_put(remote->remote_dir);
166983e6b40eSBenjamin Tissoires 	kfifo_free(&remote->remote_fifo);
167083e6b40eSBenjamin Tissoires 	wacom->remote = NULL;
167183e6b40eSBenjamin Tissoires }
167283e6b40eSBenjamin Tissoires 
167383e6b40eSBenjamin Tissoires static int wacom_initialize_remotes(struct wacom *wacom)
167472b236d6SAaron Skomra {
167572b236d6SAaron Skomra 	int error = 0;
167683e6b40eSBenjamin Tissoires 	struct wacom_remote *remote;
167772b236d6SAaron Skomra 	int i;
167872b236d6SAaron Skomra 
167972b236d6SAaron Skomra 	if (wacom->wacom_wac.features.type != REMOTE)
168072b236d6SAaron Skomra 		return 0;
168172b236d6SAaron Skomra 
168283e6b40eSBenjamin Tissoires 	remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
168383e6b40eSBenjamin Tissoires 			      GFP_KERNEL);
168483e6b40eSBenjamin Tissoires 	if (!remote)
168572b236d6SAaron Skomra 		return -ENOMEM;
168672b236d6SAaron Skomra 
168783e6b40eSBenjamin Tissoires 	wacom->remote = remote;
168883e6b40eSBenjamin Tissoires 
168983e6b40eSBenjamin Tissoires 	spin_lock_init(&remote->remote_lock);
169083e6b40eSBenjamin Tissoires 
169183e6b40eSBenjamin Tissoires 	error = kfifo_alloc(&remote->remote_fifo,
169283e6b40eSBenjamin Tissoires 			5 * sizeof(struct wacom_remote_data),
169383e6b40eSBenjamin Tissoires 			GFP_KERNEL);
169483e6b40eSBenjamin Tissoires 	if (error) {
169583e6b40eSBenjamin Tissoires 		hid_err(wacom->hdev, "failed allocating remote_fifo\n");
169683e6b40eSBenjamin Tissoires 		return -ENOMEM;
169783e6b40eSBenjamin Tissoires 	}
169883e6b40eSBenjamin Tissoires 
1699e7749f6eSBenjamin Tissoires 	remote->remotes[0].group = remote0_serial_group;
1700e7749f6eSBenjamin Tissoires 	remote->remotes[1].group = remote1_serial_group;
1701e7749f6eSBenjamin Tissoires 	remote->remotes[2].group = remote2_serial_group;
1702e7749f6eSBenjamin Tissoires 	remote->remotes[3].group = remote3_serial_group;
1703e7749f6eSBenjamin Tissoires 	remote->remotes[4].group = remote4_serial_group;
170483e6b40eSBenjamin Tissoires 
170583e6b40eSBenjamin Tissoires 	remote->remote_dir = kobject_create_and_add("wacom_remote",
170683e6b40eSBenjamin Tissoires 						    &wacom->hdev->dev.kobj);
170783e6b40eSBenjamin Tissoires 	if (!remote->remote_dir)
170883e6b40eSBenjamin Tissoires 		return -ENOMEM;
170983e6b40eSBenjamin Tissoires 
171083e6b40eSBenjamin Tissoires 	error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
171172b236d6SAaron Skomra 
171272b236d6SAaron Skomra 	if (error) {
171372b236d6SAaron Skomra 		hid_err(wacom->hdev,
171472b236d6SAaron Skomra 			"cannot create sysfs group err: %d\n", error);
171572b236d6SAaron Skomra 		return error;
171672b236d6SAaron Skomra 	}
171772b236d6SAaron Skomra 
171872b236d6SAaron Skomra 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1719a50aac71SBenjamin Tissoires 		wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
1720e7749f6eSBenjamin Tissoires 		remote->remotes[i].serial = 0;
172172b236d6SAaron Skomra 	}
172272b236d6SAaron Skomra 
172383e6b40eSBenjamin Tissoires 	error = devm_add_action_or_reset(&wacom->hdev->dev,
172483e6b40eSBenjamin Tissoires 					 wacom_remotes_destroy, wacom);
172583e6b40eSBenjamin Tissoires 	if (error)
172683e6b40eSBenjamin Tissoires 		return error;
172783e6b40eSBenjamin Tissoires 
172872b236d6SAaron Skomra 	return 0;
172972b236d6SAaron Skomra }
173072b236d6SAaron Skomra 
1731471d1714SBenjamin Tissoires static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1732471d1714SBenjamin Tissoires {
1733471d1714SBenjamin Tissoires 	struct input_dev *input_dev;
1734471d1714SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
1735471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1736471d1714SBenjamin Tissoires 
17373dad188eSBenjamin Tissoires 	input_dev = devm_input_allocate_device(&hdev->dev);
1738471d1714SBenjamin Tissoires 	if (!input_dev)
1739471d1714SBenjamin Tissoires 		return NULL;
1740471d1714SBenjamin Tissoires 
17412bdd163cSJason Gerecke 	input_dev->name = wacom_wac->features.name;
1742471d1714SBenjamin Tissoires 	input_dev->phys = hdev->phys;
1743471d1714SBenjamin Tissoires 	input_dev->dev.parent = &hdev->dev;
1744471d1714SBenjamin Tissoires 	input_dev->open = wacom_open;
1745471d1714SBenjamin Tissoires 	input_dev->close = wacom_close;
1746471d1714SBenjamin Tissoires 	input_dev->uniq = hdev->uniq;
1747471d1714SBenjamin Tissoires 	input_dev->id.bustype = hdev->bus;
1748471d1714SBenjamin Tissoires 	input_dev->id.vendor  = hdev->vendor;
174912969e3bSBenjamin Tissoires 	input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1750471d1714SBenjamin Tissoires 	input_dev->id.version = hdev->version;
1751471d1714SBenjamin Tissoires 	input_set_drvdata(input_dev, wacom);
1752471d1714SBenjamin Tissoires 
1753471d1714SBenjamin Tissoires 	return input_dev;
1754471d1714SBenjamin Tissoires }
1755471d1714SBenjamin Tissoires 
1756d9f2d203SJason Gerecke static int wacom_allocate_inputs(struct wacom *wacom)
1757d9f2d203SJason Gerecke {
1758d9f2d203SJason Gerecke 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1759d9f2d203SJason Gerecke 
1760d9f2d203SJason Gerecke 	wacom_wac->pen_input = wacom_allocate_input(wacom);
1761d9f2d203SJason Gerecke 	wacom_wac->touch_input = wacom_allocate_input(wacom);
1762d9f2d203SJason Gerecke 	wacom_wac->pad_input = wacom_allocate_input(wacom);
17633dad188eSBenjamin Tissoires 	if (!wacom_wac->pen_input ||
17643dad188eSBenjamin Tissoires 	    !wacom_wac->touch_input ||
17653dad188eSBenjamin Tissoires 	    !wacom_wac->pad_input)
1766d9f2d203SJason Gerecke 		return -ENOMEM;
1767d9f2d203SJason Gerecke 
17682bdd163cSJason Gerecke 	wacom_wac->pen_input->name = wacom_wac->pen_name;
1769d9f2d203SJason Gerecke 	wacom_wac->touch_input->name = wacom_wac->touch_name;
1770d9f2d203SJason Gerecke 	wacom_wac->pad_input->name = wacom_wac->pad_name;
1771d9f2d203SJason Gerecke 
1772d9f2d203SJason Gerecke 	return 0;
1773d9f2d203SJason Gerecke }
1774d9f2d203SJason Gerecke 
1775471d1714SBenjamin Tissoires static int wacom_register_inputs(struct wacom *wacom)
1776471d1714SBenjamin Tissoires {
17772a6cdbddSJason Gerecke 	struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
1778471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
17792636a3f2SJason Gerecke 	int error = 0;
1780471d1714SBenjamin Tissoires 
17812a6cdbddSJason Gerecke 	pen_input_dev = wacom_wac->pen_input;
17822a6cdbddSJason Gerecke 	touch_input_dev = wacom_wac->touch_input;
17832546dacdSBenjamin Tissoires 	pad_input_dev = wacom_wac->pad_input;
1784471d1714SBenjamin Tissoires 
17852a6cdbddSJason Gerecke 	if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
17862546dacdSBenjamin Tissoires 		return -EINVAL;
1787471d1714SBenjamin Tissoires 
17882a6cdbddSJason Gerecke 	error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
17892a6cdbddSJason Gerecke 	if (error) {
17902a6cdbddSJason Gerecke 		/* no pen in use on this interface */
17912a6cdbddSJason Gerecke 		input_free_device(pen_input_dev);
17922a6cdbddSJason Gerecke 		wacom_wac->pen_input = NULL;
17932a6cdbddSJason Gerecke 		pen_input_dev = NULL;
17942a6cdbddSJason Gerecke 	} else {
17952a6cdbddSJason Gerecke 		error = input_register_device(pen_input_dev);
1796471d1714SBenjamin Tissoires 		if (error)
17973dad188eSBenjamin Tissoires 			goto fail;
17982a6cdbddSJason Gerecke 	}
17992a6cdbddSJason Gerecke 
18002a6cdbddSJason Gerecke 	error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
18012a6cdbddSJason Gerecke 	if (error) {
18022a6cdbddSJason Gerecke 		/* no touch in use on this interface */
18032a6cdbddSJason Gerecke 		input_free_device(touch_input_dev);
18042a6cdbddSJason Gerecke 		wacom_wac->touch_input = NULL;
18052a6cdbddSJason Gerecke 		touch_input_dev = NULL;
18062a6cdbddSJason Gerecke 	} else {
18072a6cdbddSJason Gerecke 		error = input_register_device(touch_input_dev);
18082a6cdbddSJason Gerecke 		if (error)
18093dad188eSBenjamin Tissoires 			goto fail;
181030ebc1aeSPing Cheng 	}
1811471d1714SBenjamin Tissoires 
1812471d1714SBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1813471d1714SBenjamin Tissoires 	if (error) {
1814471d1714SBenjamin Tissoires 		/* no pad in use on this interface */
1815471d1714SBenjamin Tissoires 		input_free_device(pad_input_dev);
1816471d1714SBenjamin Tissoires 		wacom_wac->pad_input = NULL;
1817471d1714SBenjamin Tissoires 		pad_input_dev = NULL;
1818471d1714SBenjamin Tissoires 	} else {
1819471d1714SBenjamin Tissoires 		error = input_register_device(pad_input_dev);
1820471d1714SBenjamin Tissoires 		if (error)
18213dad188eSBenjamin Tissoires 			goto fail;
1822471d1714SBenjamin Tissoires 	}
1823471d1714SBenjamin Tissoires 
1824471d1714SBenjamin Tissoires 	return 0;
1825471d1714SBenjamin Tissoires 
18263dad188eSBenjamin Tissoires fail:
18273dad188eSBenjamin Tissoires 	wacom_wac->pad_input = NULL;
18282a6cdbddSJason Gerecke 	wacom_wac->touch_input = NULL;
18292a6cdbddSJason Gerecke 	wacom_wac->pen_input = NULL;
1830471d1714SBenjamin Tissoires 	return error;
1831471d1714SBenjamin Tissoires }
1832471d1714SBenjamin Tissoires 
18330be01712SJason Gerecke /*
18340be01712SJason Gerecke  * Not all devices report physical dimensions from HID.
18350be01712SJason Gerecke  * Compute the default from hardcoded logical dimension
18360be01712SJason Gerecke  * and resolution before driver overwrites them.
18370be01712SJason Gerecke  */
18380be01712SJason Gerecke static void wacom_set_default_phy(struct wacom_features *features)
18390be01712SJason Gerecke {
18400be01712SJason Gerecke 	if (features->x_resolution) {
18410be01712SJason Gerecke 		features->x_phy = (features->x_max * 100) /
18420be01712SJason Gerecke 					features->x_resolution;
18430be01712SJason Gerecke 		features->y_phy = (features->y_max * 100) /
18440be01712SJason Gerecke 					features->y_resolution;
18450be01712SJason Gerecke 	}
18460be01712SJason Gerecke }
18470be01712SJason Gerecke 
18480be01712SJason Gerecke static void wacom_calculate_res(struct wacom_features *features)
18490be01712SJason Gerecke {
18500be01712SJason Gerecke 	/* set unit to "100th of a mm" for devices not reported by HID */
18510be01712SJason Gerecke 	if (!features->unit) {
18520be01712SJason Gerecke 		features->unit = 0x11;
18530be01712SJason Gerecke 		features->unitExpo = -3;
18540be01712SJason Gerecke 	}
18550be01712SJason Gerecke 
18560be01712SJason Gerecke 	features->x_resolution = wacom_calc_hid_res(features->x_max,
18570be01712SJason Gerecke 						    features->x_phy,
18580be01712SJason Gerecke 						    features->unit,
18590be01712SJason Gerecke 						    features->unitExpo);
18600be01712SJason Gerecke 	features->y_resolution = wacom_calc_hid_res(features->y_max,
18610be01712SJason Gerecke 						    features->y_phy,
18620be01712SJason Gerecke 						    features->unit,
18630be01712SJason Gerecke 						    features->unitExpo);
18640be01712SJason Gerecke }
18650be01712SJason Gerecke 
1866fce9957dSJason Gerecke void wacom_battery_work(struct work_struct *work)
1867fce9957dSJason Gerecke {
1868d17d1f17SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, battery_work);
1869fce9957dSJason Gerecke 
1870fce9957dSJason Gerecke 	if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
187159d69bc8SBenjamin Tissoires 	     !wacom->battery.battery) {
1872fce9957dSJason Gerecke 		wacom_initialize_battery(wacom);
1873fce9957dSJason Gerecke 	}
1874fce9957dSJason Gerecke 	else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
187559d69bc8SBenjamin Tissoires 		 wacom->battery.battery) {
1876fce9957dSJason Gerecke 		wacom_destroy_battery(wacom);
1877fce9957dSJason Gerecke 	}
1878fce9957dSJason Gerecke }
1879fce9957dSJason Gerecke 
1880471d1714SBenjamin Tissoires static size_t wacom_compute_pktlen(struct hid_device *hdev)
1881471d1714SBenjamin Tissoires {
1882471d1714SBenjamin Tissoires 	struct hid_report_enum *report_enum;
1883471d1714SBenjamin Tissoires 	struct hid_report *report;
1884471d1714SBenjamin Tissoires 	size_t size = 0;
1885471d1714SBenjamin Tissoires 
1886471d1714SBenjamin Tissoires 	report_enum = hdev->report_enum + HID_INPUT_REPORT;
1887471d1714SBenjamin Tissoires 
1888471d1714SBenjamin Tissoires 	list_for_each_entry(report, &report_enum->report_list, list) {
1889dabb05c6SMathieu Magnaudet 		size_t report_size = hid_report_len(report);
1890471d1714SBenjamin Tissoires 		if (report_size > size)
1891471d1714SBenjamin Tissoires 			size = report_size;
1892471d1714SBenjamin Tissoires 	}
1893471d1714SBenjamin Tissoires 
1894471d1714SBenjamin Tissoires 	return size;
1895471d1714SBenjamin Tissoires }
1896471d1714SBenjamin Tissoires 
1897fd5f92b6SBenjamin Tissoires static void wacom_update_name(struct wacom *wacom, const char *suffix)
1898c24eab4eSPing Cheng {
1899c24eab4eSPing Cheng 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1900c24eab4eSPing Cheng 	struct wacom_features *features = &wacom_wac->features;
190144b5250bSJason Gerecke 	char name[WACOM_NAME_MAX];
1902c24eab4eSPing Cheng 
1903c24eab4eSPing Cheng 	/* Generic devices name unspecified */
1904c24eab4eSPing Cheng 	if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1905c24eab4eSPing Cheng 		if (strstr(wacom->hdev->name, "Wacom") ||
1906c24eab4eSPing Cheng 		    strstr(wacom->hdev->name, "wacom") ||
1907c24eab4eSPing Cheng 		    strstr(wacom->hdev->name, "WACOM")) {
1908c24eab4eSPing Cheng 			/* name is in HID descriptor, use it */
190944b5250bSJason Gerecke 			strlcpy(name, wacom->hdev->name, sizeof(name));
1910c24eab4eSPing Cheng 
1911c24eab4eSPing Cheng 			/* strip out excess whitespaces */
1912c24eab4eSPing Cheng 			while (1) {
191344b5250bSJason Gerecke 				char *gap = strstr(name, "  ");
1914c24eab4eSPing Cheng 				if (gap == NULL)
1915c24eab4eSPing Cheng 					break;
1916c24eab4eSPing Cheng 				/* shift everything including the terminator */
1917c24eab4eSPing Cheng 				memmove(gap, gap+1, strlen(gap));
1918c24eab4eSPing Cheng 			}
1919c24eab4eSPing Cheng 			/* get rid of trailing whitespace */
192044b5250bSJason Gerecke 			if (name[strlen(name)-1] == ' ')
192144b5250bSJason Gerecke 				name[strlen(name)-1] = '\0';
1922c24eab4eSPing Cheng 		} else {
1923c24eab4eSPing Cheng 			/* no meaningful name retrieved. use product ID */
192444b5250bSJason Gerecke 			snprintf(name, sizeof(name),
1925c24eab4eSPing Cheng 				 "%s %X", features->name, wacom->hdev->product);
1926c24eab4eSPing Cheng 		}
1927c24eab4eSPing Cheng 	} else {
192844b5250bSJason Gerecke 		strlcpy(name, features->name, sizeof(name));
1929c24eab4eSPing Cheng 	}
1930c24eab4eSPing Cheng 
19319956953eSBenjamin Tissoires 	snprintf(wacom_wac->name, sizeof(wacom_wac->name), "%s%s",
19329956953eSBenjamin Tissoires 		 name, suffix);
19339956953eSBenjamin Tissoires 
1934c24eab4eSPing Cheng 	/* Append the device type to the name */
19352a6cdbddSJason Gerecke 	snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
1936fd5f92b6SBenjamin Tissoires 		"%s%s Pen", name, suffix);
19372a6cdbddSJason Gerecke 	snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
1938fd5f92b6SBenjamin Tissoires 		"%s%s Finger", name, suffix);
1939c24eab4eSPing Cheng 	snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1940fd5f92b6SBenjamin Tissoires 		"%s%s Pad", name, suffix);
19418d80f790SJason Gerecke }
1942c24eab4eSPing Cheng 
194384dfbd7fSBenjamin Tissoires static void wacom_release_resources(struct wacom *wacom)
194484dfbd7fSBenjamin Tissoires {
194584dfbd7fSBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
194684dfbd7fSBenjamin Tissoires 
194784dfbd7fSBenjamin Tissoires 	if (!wacom->resources)
194884dfbd7fSBenjamin Tissoires 		return;
194984dfbd7fSBenjamin Tissoires 
195084dfbd7fSBenjamin Tissoires 	devres_release_group(&hdev->dev, wacom);
195184dfbd7fSBenjamin Tissoires 
195284dfbd7fSBenjamin Tissoires 	wacom->resources = false;
195384dfbd7fSBenjamin Tissoires 
195484dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.pen_input = NULL;
195584dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.touch_input = NULL;
195684dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.pad_input = NULL;
195784dfbd7fSBenjamin Tissoires }
195884dfbd7fSBenjamin Tissoires 
1959fd5f92b6SBenjamin Tissoires static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
1960471d1714SBenjamin Tissoires {
1961c58ac3a8SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1962c58ac3a8SBenjamin Tissoires 	struct wacom_features *features = &wacom_wac->features;
1963c58ac3a8SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
1964471d1714SBenjamin Tissoires 	int error;
19657704ac93SBenjamin Tissoires 	unsigned int connect_mask = HID_CONNECT_HIDRAW;
1966471d1714SBenjamin Tissoires 
1967471d1714SBenjamin Tissoires 	features->pktlen = wacom_compute_pktlen(hdev);
1968c58ac3a8SBenjamin Tissoires 	if (features->pktlen > WACOM_PKGLEN_MAX)
1969c58ac3a8SBenjamin Tissoires 		return -EINVAL;
1970471d1714SBenjamin Tissoires 
197184dfbd7fSBenjamin Tissoires 	if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
197284dfbd7fSBenjamin Tissoires 		return -ENOMEM;
197384dfbd7fSBenjamin Tissoires 
197484dfbd7fSBenjamin Tissoires 	wacom->resources = true;
197584dfbd7fSBenjamin Tissoires 
1976494078b0SBenjamin Tissoires 	error = wacom_allocate_inputs(wacom);
1977494078b0SBenjamin Tissoires 	if (error)
19783888b0d5SBenjamin Tissoires 		goto fail;
1979494078b0SBenjamin Tissoires 
19808c97a765SBenjamin Tissoires 	/*
19818c97a765SBenjamin Tissoires 	 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
19828c97a765SBenjamin Tissoires 	 * into debug mode for the touch part.
19838c97a765SBenjamin Tissoires 	 * We ignore the other interfaces.
19848c97a765SBenjamin Tissoires 	 */
19858c97a765SBenjamin Tissoires 	if (features->type == BAMBOO_PAD) {
19868c97a765SBenjamin Tissoires 		if (features->pktlen == WACOM_PKGLEN_PENABLED) {
19878c97a765SBenjamin Tissoires 			features->type = HID_GENERIC;
19888c97a765SBenjamin Tissoires 		} else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
19898c97a765SBenjamin Tissoires 			   (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
19908c97a765SBenjamin Tissoires 			error = -ENODEV;
19913888b0d5SBenjamin Tissoires 			goto fail;
19928c97a765SBenjamin Tissoires 		}
19938c97a765SBenjamin Tissoires 	}
19948c97a765SBenjamin Tissoires 
1995471d1714SBenjamin Tissoires 	/* set the default size in case we do not get them from hid */
1996471d1714SBenjamin Tissoires 	wacom_set_default_phy(features);
1997471d1714SBenjamin Tissoires 
1998471d1714SBenjamin Tissoires 	/* Retrieve the physical and logical size for touch devices */
1999471d1714SBenjamin Tissoires 	wacom_retrieve_hid_descriptor(hdev, features);
200042f4f272SPing Cheng 	wacom_setup_device_quirks(wacom);
2001042628abSJason Gerecke 
2002aa86b18cSJason Gerecke 	if (features->device_type == WACOM_DEVICETYPE_NONE &&
2003aa86b18cSJason Gerecke 	    features->type != WIRELESS) {
20048e116d31SJason Gerecke 		error = features->type == HID_GENERIC ? -ENODEV : 0;
20058e116d31SJason Gerecke 
2006042628abSJason Gerecke 		dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
20078e116d31SJason Gerecke 			 hdev->name,
20088e116d31SJason Gerecke 			 error ? "Ignoring" : "Assuming pen");
20098e116d31SJason Gerecke 
20108e116d31SJason Gerecke 		if (error)
20113888b0d5SBenjamin Tissoires 			goto fail;
2012042628abSJason Gerecke 
2013aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_PEN;
2014042628abSJason Gerecke 	}
2015042628abSJason Gerecke 
2016471d1714SBenjamin Tissoires 	wacom_calculate_res(features);
2017471d1714SBenjamin Tissoires 
2018fd5f92b6SBenjamin Tissoires 	wacom_update_name(wacom, wireless ? " (WL)" : "");
2019471d1714SBenjamin Tissoires 
2020471d1714SBenjamin Tissoires 	error = wacom_add_shared_data(hdev);
2021471d1714SBenjamin Tissoires 	if (error)
20223888b0d5SBenjamin Tissoires 		goto fail;
2023471d1714SBenjamin Tissoires 
2024ccad85ccSJason Gerecke 	if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
2025f81a1295SBenjamin Tissoires 	     (features->quirks & WACOM_QUIRK_BATTERY)) {
2026f81a1295SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
2027f81a1295SBenjamin Tissoires 		if (error)
20283888b0d5SBenjamin Tissoires 			goto fail;
2029f81a1295SBenjamin Tissoires 	}
2030f81a1295SBenjamin Tissoires 
2031494078b0SBenjamin Tissoires 	error = wacom_register_inputs(wacom);
2032471d1714SBenjamin Tissoires 	if (error)
20333888b0d5SBenjamin Tissoires 		goto fail;
2034f81a1295SBenjamin Tissoires 
2035b62f6465SBenjamin Tissoires 	if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
203685d2c77bSBenjamin Tissoires 		error = wacom_initialize_leds(wacom);
203785d2c77bSBenjamin Tissoires 		if (error)
20383888b0d5SBenjamin Tissoires 			goto fail;
203985d2c77bSBenjamin Tissoires 
204083e6b40eSBenjamin Tissoires 		error = wacom_initialize_remotes(wacom);
2041b62f6465SBenjamin Tissoires 		if (error)
20423888b0d5SBenjamin Tissoires 			goto fail;
2043b62f6465SBenjamin Tissoires 	}
2044b62f6465SBenjamin Tissoires 
20457704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
20467704ac93SBenjamin Tissoires 		connect_mask |= HID_CONNECT_DRIVER;
20477704ac93SBenjamin Tissoires 
2048471d1714SBenjamin Tissoires 	/* Regular HID work starts now */
20497704ac93SBenjamin Tissoires 	error = hid_hw_start(hdev, connect_mask);
2050471d1714SBenjamin Tissoires 	if (error) {
2051471d1714SBenjamin Tissoires 		hid_err(hdev, "hw start failed\n");
20523888b0d5SBenjamin Tissoires 		goto fail;
2053471d1714SBenjamin Tissoires 	}
2054471d1714SBenjamin Tissoires 
2055fd5f92b6SBenjamin Tissoires 	if (!wireless) {
205686e88f0eSJason Gerecke 		/* Note that if query fails it is not a hard failure */
205786e88f0eSJason Gerecke 		wacom_query_tablet_data(hdev, features);
2058fd5f92b6SBenjamin Tissoires 	}
205986e88f0eSJason Gerecke 
206086e88f0eSJason Gerecke 	/* touch only Bamboo doesn't support pen */
206186e88f0eSJason Gerecke 	if ((features->type == BAMBOO_TOUCH) &&
206286e88f0eSJason Gerecke 	    (features->device_type & WACOM_DEVICETYPE_PEN)) {
206386e88f0eSJason Gerecke 		error = -ENODEV;
2064b62f6465SBenjamin Tissoires 		goto fail_quirks;
206586e88f0eSJason Gerecke 	}
206686e88f0eSJason Gerecke 
206786e88f0eSJason Gerecke 	/* pen only Bamboo neither support touch nor pad */
206886e88f0eSJason Gerecke 	if ((features->type == BAMBOO_PEN) &&
206986e88f0eSJason Gerecke 	    ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
207086e88f0eSJason Gerecke 	    (features->device_type & WACOM_DEVICETYPE_PAD))) {
207186e88f0eSJason Gerecke 		error = -ENODEV;
2072b62f6465SBenjamin Tissoires 		goto fail_quirks;
207386e88f0eSJason Gerecke 	}
207486e88f0eSJason Gerecke 
2075ccad85ccSJason Gerecke 	if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2076471d1714SBenjamin Tissoires 		error = hid_hw_open(hdev);
2077471d1714SBenjamin Tissoires 
2078eda01dabSPing Cheng 	if ((wacom_wac->features.type == INTUOSHT ||
2079eda01dabSPing Cheng 	     wacom_wac->features.type == INTUOSHT2) &&
2080eda01dabSPing Cheng 	    (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
208197f9afa4SBenjamin Tissoires 		wacom_wac->shared->type = wacom_wac->features.type;
20822a6cdbddSJason Gerecke 		wacom_wac->shared->touch_input = wacom_wac->touch_input;
2083471d1714SBenjamin Tissoires 	}
2084471d1714SBenjamin Tissoires 
208584dfbd7fSBenjamin Tissoires 	devres_close_group(&hdev->dev, wacom);
208684dfbd7fSBenjamin Tissoires 
2087471d1714SBenjamin Tissoires 	return 0;
2088471d1714SBenjamin Tissoires 
2089b62f6465SBenjamin Tissoires fail_quirks:
2090c58ac3a8SBenjamin Tissoires 	hid_hw_stop(hdev);
20913888b0d5SBenjamin Tissoires fail:
209284dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom);
2093c58ac3a8SBenjamin Tissoires 	return error;
2094c58ac3a8SBenjamin Tissoires }
2095c58ac3a8SBenjamin Tissoires 
2096a2f091afSBenjamin Tissoires static void wacom_wireless_work(struct work_struct *work)
2097a2f091afSBenjamin Tissoires {
2098d17d1f17SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, wireless_work);
2099a2f091afSBenjamin Tissoires 	struct usb_device *usbdev = wacom->usbdev;
2100a2f091afSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2101a2f091afSBenjamin Tissoires 	struct hid_device *hdev1, *hdev2;
2102a2f091afSBenjamin Tissoires 	struct wacom *wacom1, *wacom2;
2103a2f091afSBenjamin Tissoires 	struct wacom_wac *wacom_wac1, *wacom_wac2;
2104a2f091afSBenjamin Tissoires 	int error;
2105a2f091afSBenjamin Tissoires 
2106a2f091afSBenjamin Tissoires 	/*
2107a2f091afSBenjamin Tissoires 	 * Regardless if this is a disconnect or a new tablet,
2108a2f091afSBenjamin Tissoires 	 * remove any existing input and battery devices.
2109a2f091afSBenjamin Tissoires 	 */
2110a2f091afSBenjamin Tissoires 
2111a2f091afSBenjamin Tissoires 	wacom_destroy_battery(wacom);
2112a2f091afSBenjamin Tissoires 
2113a2f091afSBenjamin Tissoires 	/* Stylus interface */
2114a2f091afSBenjamin Tissoires 	hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
2115a2f091afSBenjamin Tissoires 	wacom1 = hid_get_drvdata(hdev1);
2116a2f091afSBenjamin Tissoires 	wacom_wac1 = &(wacom1->wacom_wac);
211784dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom1);
2118a2f091afSBenjamin Tissoires 
2119a2f091afSBenjamin Tissoires 	/* Touch interface */
2120a2f091afSBenjamin Tissoires 	hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
2121a2f091afSBenjamin Tissoires 	wacom2 = hid_get_drvdata(hdev2);
2122a2f091afSBenjamin Tissoires 	wacom_wac2 = &(wacom2->wacom_wac);
212384dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom2);
2124a2f091afSBenjamin Tissoires 
2125a2f091afSBenjamin Tissoires 	if (wacom_wac->pid == 0) {
2126a2f091afSBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet disconnected\n");
2127a2f091afSBenjamin Tissoires 	} else {
2128a2f091afSBenjamin Tissoires 		const struct hid_device_id *id = wacom_ids;
2129a2f091afSBenjamin Tissoires 
2130a2f091afSBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
2131a2f091afSBenjamin Tissoires 			 wacom_wac->pid);
2132a2f091afSBenjamin Tissoires 
2133a2f091afSBenjamin Tissoires 		while (id->bus) {
2134a2f091afSBenjamin Tissoires 			if (id->vendor == USB_VENDOR_ID_WACOM &&
2135a2f091afSBenjamin Tissoires 			    id->product == wacom_wac->pid)
2136a2f091afSBenjamin Tissoires 				break;
2137a2f091afSBenjamin Tissoires 			id++;
2138a2f091afSBenjamin Tissoires 		}
2139a2f091afSBenjamin Tissoires 
2140a2f091afSBenjamin Tissoires 		if (!id->bus) {
2141a2f091afSBenjamin Tissoires 			hid_info(wacom->hdev, "ignoring unknown PID.\n");
2142a2f091afSBenjamin Tissoires 			return;
2143a2f091afSBenjamin Tissoires 		}
2144a2f091afSBenjamin Tissoires 
2145a2f091afSBenjamin Tissoires 		/* Stylus interface */
2146a2f091afSBenjamin Tissoires 		wacom_wac1->features =
2147a2f091afSBenjamin Tissoires 			*((struct wacom_features *)id->driver_data);
2148fd5f92b6SBenjamin Tissoires 
2149a2f091afSBenjamin Tissoires 		wacom_wac1->pid = wacom_wac->pid;
2150fd5f92b6SBenjamin Tissoires 		hid_hw_stop(hdev1);
2151fd5f92b6SBenjamin Tissoires 		error = wacom_parse_and_register(wacom1, true);
2152a2f091afSBenjamin Tissoires 		if (error)
2153a2f091afSBenjamin Tissoires 			goto fail;
2154a2f091afSBenjamin Tissoires 
2155a2f091afSBenjamin Tissoires 		/* Touch interface */
2156a2f091afSBenjamin Tissoires 		if (wacom_wac1->features.touch_max ||
2157a2f091afSBenjamin Tissoires 		    (wacom_wac1->features.type >= INTUOSHT &&
2158a2f091afSBenjamin Tissoires 		    wacom_wac1->features.type <= BAMBOO_PT)) {
2159a2f091afSBenjamin Tissoires 			wacom_wac2->features =
2160a2f091afSBenjamin Tissoires 				*((struct wacom_features *)id->driver_data);
2161a2f091afSBenjamin Tissoires 			wacom_wac2->pid = wacom_wac->pid;
2162fd5f92b6SBenjamin Tissoires 			hid_hw_stop(hdev2);
2163fd5f92b6SBenjamin Tissoires 			error = wacom_parse_and_register(wacom2, true);
2164a2f091afSBenjamin Tissoires 			if (error)
2165a2f091afSBenjamin Tissoires 				goto fail;
2166a2f091afSBenjamin Tissoires 		}
2167a2f091afSBenjamin Tissoires 
21689956953eSBenjamin Tissoires 		strlcpy(wacom_wac->name, wacom_wac1->name,
21699956953eSBenjamin Tissoires 			sizeof(wacom_wac->name));
2170a2f091afSBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
2171a2f091afSBenjamin Tissoires 		if (error)
2172a2f091afSBenjamin Tissoires 			goto fail;
2173a2f091afSBenjamin Tissoires 	}
2174a2f091afSBenjamin Tissoires 
2175a2f091afSBenjamin Tissoires 	return;
2176a2f091afSBenjamin Tissoires 
2177a2f091afSBenjamin Tissoires fail:
217884dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom1);
217984dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom2);
2180a2f091afSBenjamin Tissoires 	return;
2181a2f091afSBenjamin Tissoires }
2182a2f091afSBenjamin Tissoires 
218304bfa27bSBenjamin Tissoires static void wacom_remote_destroy_one(struct wacom *wacom, unsigned int index)
218404bfa27bSBenjamin Tissoires {
218504bfa27bSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
2186e7749f6eSBenjamin Tissoires 	u32 serial = remote->remotes[index].serial;
218704bfa27bSBenjamin Tissoires 	int i;
21887c35dc3cSBenjamin Tissoires 	unsigned long flags;
21897c35dc3cSBenjamin Tissoires 
21907c35dc3cSBenjamin Tissoires 	spin_lock_irqsave(&remote->remote_lock, flags);
21917c35dc3cSBenjamin Tissoires 	remote->remotes[index].registered = false;
21927c35dc3cSBenjamin Tissoires 	spin_unlock_irqrestore(&remote->remote_lock, flags);
219304bfa27bSBenjamin Tissoires 
21949f1015d4SBenjamin Tissoires 	if (remote->remotes[index].battery.battery)
21959f1015d4SBenjamin Tissoires 		devres_release_group(&wacom->hdev->dev,
21969f1015d4SBenjamin Tissoires 				     &remote->remotes[index].battery.bat_desc);
21979f1015d4SBenjamin Tissoires 
2198e7749f6eSBenjamin Tissoires 	if (remote->remotes[index].group.name)
2199e7749f6eSBenjamin Tissoires 		devres_release_group(&wacom->hdev->dev,
2200e7749f6eSBenjamin Tissoires 				     &remote->remotes[index]);
220104bfa27bSBenjamin Tissoires 
220204bfa27bSBenjamin Tissoires 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2203e7749f6eSBenjamin Tissoires 		if (remote->remotes[i].serial == serial) {
2204e7749f6eSBenjamin Tissoires 			remote->remotes[i].serial = 0;
2205e7749f6eSBenjamin Tissoires 			remote->remotes[i].group.name = NULL;
22067c35dc3cSBenjamin Tissoires 			remote->remotes[i].registered = false;
22079f1015d4SBenjamin Tissoires 			remote->remotes[i].battery.battery = NULL;
220804bfa27bSBenjamin Tissoires 			wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
220904bfa27bSBenjamin Tissoires 		}
221004bfa27bSBenjamin Tissoires 	}
221104bfa27bSBenjamin Tissoires }
221204bfa27bSBenjamin Tissoires 
221304bfa27bSBenjamin Tissoires static int wacom_remote_create_one(struct wacom *wacom, u32 serial,
221404bfa27bSBenjamin Tissoires 				   unsigned int index)
221504bfa27bSBenjamin Tissoires {
221604bfa27bSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
2217f9036bd4SBenjamin Tissoires 	struct device *dev = &wacom->hdev->dev;
221804bfa27bSBenjamin Tissoires 	int error, k;
221904bfa27bSBenjamin Tissoires 
222004bfa27bSBenjamin Tissoires 	/* A remote can pair more than once with an EKR,
222104bfa27bSBenjamin Tissoires 	 * check to make sure this serial isn't already paired.
222204bfa27bSBenjamin Tissoires 	 */
222304bfa27bSBenjamin Tissoires 	for (k = 0; k < WACOM_MAX_REMOTES; k++) {
2224e7749f6eSBenjamin Tissoires 		if (remote->remotes[k].serial == serial)
222504bfa27bSBenjamin Tissoires 			break;
222604bfa27bSBenjamin Tissoires 	}
222704bfa27bSBenjamin Tissoires 
222804bfa27bSBenjamin Tissoires 	if (k < WACOM_MAX_REMOTES) {
2229e7749f6eSBenjamin Tissoires 		remote->remotes[index].serial = serial;
223004bfa27bSBenjamin Tissoires 		return 0;
223104bfa27bSBenjamin Tissoires 	}
223204bfa27bSBenjamin Tissoires 
2233e7749f6eSBenjamin Tissoires 	if (!devres_open_group(dev, &remote->remotes[index], GFP_KERNEL))
2234f9036bd4SBenjamin Tissoires 		return -ENOMEM;
2235f9036bd4SBenjamin Tissoires 
223604bfa27bSBenjamin Tissoires 	error = wacom_remote_create_attr_group(wacom, serial, index);
223704bfa27bSBenjamin Tissoires 	if (error)
2238f9036bd4SBenjamin Tissoires 		goto fail;
223904bfa27bSBenjamin Tissoires 
22407c35dc3cSBenjamin Tissoires 	remote->remotes[index].input = wacom_allocate_input(wacom);
22417c35dc3cSBenjamin Tissoires 	if (!remote->remotes[index].input) {
22427c35dc3cSBenjamin Tissoires 		error = -ENOMEM;
22437c35dc3cSBenjamin Tissoires 		goto fail;
22447c35dc3cSBenjamin Tissoires 	}
22457c35dc3cSBenjamin Tissoires 	remote->remotes[index].input->uniq = remote->remotes[index].group.name;
22467c35dc3cSBenjamin Tissoires 	remote->remotes[index].input->name = wacom->wacom_wac.pad_name;
22477c35dc3cSBenjamin Tissoires 
22487c35dc3cSBenjamin Tissoires 	if (!remote->remotes[index].input->name) {
22497c35dc3cSBenjamin Tissoires 		error = -EINVAL;
22507c35dc3cSBenjamin Tissoires 		goto fail;
22517c35dc3cSBenjamin Tissoires 	}
22527c35dc3cSBenjamin Tissoires 
22537c35dc3cSBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(remote->remotes[index].input,
22547c35dc3cSBenjamin Tissoires 						   &wacom->wacom_wac);
22557c35dc3cSBenjamin Tissoires 	if (error)
22567c35dc3cSBenjamin Tissoires 		goto fail;
22577c35dc3cSBenjamin Tissoires 
2258e7749f6eSBenjamin Tissoires 	remote->remotes[index].serial = serial;
225904bfa27bSBenjamin Tissoires 
22607c35dc3cSBenjamin Tissoires 	error = input_register_device(remote->remotes[index].input);
22617c35dc3cSBenjamin Tissoires 	if (error)
22627c35dc3cSBenjamin Tissoires 		goto fail;
22637c35dc3cSBenjamin Tissoires 
226497f5541fSBenjamin Tissoires 	error = wacom_led_groups_alloc_and_register_one(
226597f5541fSBenjamin Tissoires 					&remote->remotes[index].input->dev,
226697f5541fSBenjamin Tissoires 					wacom, index, 3, true);
226797f5541fSBenjamin Tissoires 	if (error)
226897f5541fSBenjamin Tissoires 		goto fail;
226997f5541fSBenjamin Tissoires 
22707c35dc3cSBenjamin Tissoires 	remote->remotes[index].registered = true;
22717c35dc3cSBenjamin Tissoires 
2272e7749f6eSBenjamin Tissoires 	devres_close_group(dev, &remote->remotes[index]);
227304bfa27bSBenjamin Tissoires 	return 0;
2274f9036bd4SBenjamin Tissoires 
2275f9036bd4SBenjamin Tissoires fail:
2276e7749f6eSBenjamin Tissoires 	devres_release_group(dev, &remote->remotes[index]);
2277e7749f6eSBenjamin Tissoires 	remote->remotes[index].serial = 0;
2278f9036bd4SBenjamin Tissoires 	return error;
227904bfa27bSBenjamin Tissoires }
228004bfa27bSBenjamin Tissoires 
22819f1015d4SBenjamin Tissoires static int wacom_remote_attach_battery(struct wacom *wacom, int index)
22829f1015d4SBenjamin Tissoires {
22839f1015d4SBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
22849f1015d4SBenjamin Tissoires 	int error;
22859f1015d4SBenjamin Tissoires 
22869f1015d4SBenjamin Tissoires 	if (!remote->remotes[index].registered)
22879f1015d4SBenjamin Tissoires 		return 0;
22889f1015d4SBenjamin Tissoires 
22899f1015d4SBenjamin Tissoires 	if (remote->remotes[index].battery.battery)
22909f1015d4SBenjamin Tissoires 		return 0;
22919f1015d4SBenjamin Tissoires 
22929f1015d4SBenjamin Tissoires 	if (wacom->led.groups[index].select == WACOM_STATUS_UNKNOWN)
22939f1015d4SBenjamin Tissoires 		return 0;
22949f1015d4SBenjamin Tissoires 
22959f1015d4SBenjamin Tissoires 	error = __wacom_initialize_battery(wacom,
22969f1015d4SBenjamin Tissoires 					&wacom->remote->remotes[index].battery);
22979f1015d4SBenjamin Tissoires 	if (error)
22989f1015d4SBenjamin Tissoires 		return error;
22999f1015d4SBenjamin Tissoires 
23009f1015d4SBenjamin Tissoires 	return 0;
23019f1015d4SBenjamin Tissoires }
23029f1015d4SBenjamin Tissoires 
2303e6f2813aSBenjamin Tissoires static void wacom_remote_work(struct work_struct *work)
2304e6f2813aSBenjamin Tissoires {
2305e6f2813aSBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, remote_work);
230683e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
2307e6f2813aSBenjamin Tissoires 	struct wacom_remote_data data;
2308e6f2813aSBenjamin Tissoires 	unsigned long flags;
2309e6f2813aSBenjamin Tissoires 	unsigned int count;
2310e6f2813aSBenjamin Tissoires 	u32 serial;
231104bfa27bSBenjamin Tissoires 	int i;
2312e6f2813aSBenjamin Tissoires 
231383e6b40eSBenjamin Tissoires 	spin_lock_irqsave(&remote->remote_lock, flags);
2314e6f2813aSBenjamin Tissoires 
231583e6b40eSBenjamin Tissoires 	count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
2316e6f2813aSBenjamin Tissoires 
2317e6f2813aSBenjamin Tissoires 	if (count != sizeof(data)) {
2318e6f2813aSBenjamin Tissoires 		hid_err(wacom->hdev,
2319e6f2813aSBenjamin Tissoires 			"workitem triggered without status available\n");
232083e6b40eSBenjamin Tissoires 		spin_unlock_irqrestore(&remote->remote_lock, flags);
2321e6f2813aSBenjamin Tissoires 		return;
2322e6f2813aSBenjamin Tissoires 	}
2323e6f2813aSBenjamin Tissoires 
232483e6b40eSBenjamin Tissoires 	if (!kfifo_is_empty(&remote->remote_fifo))
2325e6f2813aSBenjamin Tissoires 		wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
2326e6f2813aSBenjamin Tissoires 
232783e6b40eSBenjamin Tissoires 	spin_unlock_irqrestore(&remote->remote_lock, flags);
2328e6f2813aSBenjamin Tissoires 
2329e6f2813aSBenjamin Tissoires 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
2330e6f2813aSBenjamin Tissoires 		serial = data.remote[i].serial;
2331e6f2813aSBenjamin Tissoires 		if (data.remote[i].connected) {
2332e6f2813aSBenjamin Tissoires 
23339f1015d4SBenjamin Tissoires 			if (remote->remotes[i].serial == serial) {
23349f1015d4SBenjamin Tissoires 				wacom_remote_attach_battery(wacom, i);
2335e6f2813aSBenjamin Tissoires 				continue;
23369f1015d4SBenjamin Tissoires 			}
2337e6f2813aSBenjamin Tissoires 
2338e7749f6eSBenjamin Tissoires 			if (remote->remotes[i].serial)
233904bfa27bSBenjamin Tissoires 				wacom_remote_destroy_one(wacom, i);
2340e6f2813aSBenjamin Tissoires 
234104bfa27bSBenjamin Tissoires 			wacom_remote_create_one(wacom, serial, i);
2342e6f2813aSBenjamin Tissoires 
2343e7749f6eSBenjamin Tissoires 		} else if (remote->remotes[i].serial) {
234404bfa27bSBenjamin Tissoires 			wacom_remote_destroy_one(wacom, i);
2345e6f2813aSBenjamin Tissoires 		}
2346e6f2813aSBenjamin Tissoires 	}
2347e6f2813aSBenjamin Tissoires }
2348e6f2813aSBenjamin Tissoires 
2349c58ac3a8SBenjamin Tissoires static int wacom_probe(struct hid_device *hdev,
2350c58ac3a8SBenjamin Tissoires 		const struct hid_device_id *id)
2351c58ac3a8SBenjamin Tissoires {
2352c58ac3a8SBenjamin Tissoires 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
2353c58ac3a8SBenjamin Tissoires 	struct usb_device *dev = interface_to_usbdev(intf);
2354c58ac3a8SBenjamin Tissoires 	struct wacom *wacom;
2355c58ac3a8SBenjamin Tissoires 	struct wacom_wac *wacom_wac;
2356c58ac3a8SBenjamin Tissoires 	struct wacom_features *features;
2357c58ac3a8SBenjamin Tissoires 	int error;
2358c58ac3a8SBenjamin Tissoires 
2359c58ac3a8SBenjamin Tissoires 	if (!id->driver_data)
2360c58ac3a8SBenjamin Tissoires 		return -EINVAL;
2361c58ac3a8SBenjamin Tissoires 
2362c58ac3a8SBenjamin Tissoires 	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2363c58ac3a8SBenjamin Tissoires 
2364c58ac3a8SBenjamin Tissoires 	/* hid-core sets this quirk for the boot interface */
2365c58ac3a8SBenjamin Tissoires 	hdev->quirks &= ~HID_QUIRK_NOGET;
2366c58ac3a8SBenjamin Tissoires 
236719b64330SBenjamin Tissoires 	wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2368c58ac3a8SBenjamin Tissoires 	if (!wacom)
2369c58ac3a8SBenjamin Tissoires 		return -ENOMEM;
2370c58ac3a8SBenjamin Tissoires 
2371c58ac3a8SBenjamin Tissoires 	hid_set_drvdata(hdev, wacom);
2372c58ac3a8SBenjamin Tissoires 	wacom->hdev = hdev;
2373c58ac3a8SBenjamin Tissoires 
2374c58ac3a8SBenjamin Tissoires 	wacom_wac = &wacom->wacom_wac;
2375c58ac3a8SBenjamin Tissoires 	wacom_wac->features = *((struct wacom_features *)id->driver_data);
2376c58ac3a8SBenjamin Tissoires 	features = &wacom_wac->features;
2377c58ac3a8SBenjamin Tissoires 
2378c58ac3a8SBenjamin Tissoires 	if (features->check_for_hid_type && features->hid_type != hdev->type) {
2379c58ac3a8SBenjamin Tissoires 		error = -ENODEV;
23803888b0d5SBenjamin Tissoires 		goto fail;
2381c58ac3a8SBenjamin Tissoires 	}
2382c58ac3a8SBenjamin Tissoires 
2383c6fa1aebSJason Gerecke 	wacom_wac->hid_data.inputmode = -1;
2384326ea2a9SJason Gerecke 	wacom_wac->mode_report = -1;
2385c6fa1aebSJason Gerecke 
2386c58ac3a8SBenjamin Tissoires 	wacom->usbdev = dev;
2387c58ac3a8SBenjamin Tissoires 	wacom->intf = intf;
2388c58ac3a8SBenjamin Tissoires 	mutex_init(&wacom->lock);
2389d17d1f17SBenjamin Tissoires 	INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2390d17d1f17SBenjamin Tissoires 	INIT_WORK(&wacom->battery_work, wacom_battery_work);
2391e6f2813aSBenjamin Tissoires 	INIT_WORK(&wacom->remote_work, wacom_remote_work);
2392c58ac3a8SBenjamin Tissoires 
2393c58ac3a8SBenjamin Tissoires 	/* ask for the report descriptor to be loaded by HID */
2394c58ac3a8SBenjamin Tissoires 	error = hid_parse(hdev);
2395c58ac3a8SBenjamin Tissoires 	if (error) {
2396c58ac3a8SBenjamin Tissoires 		hid_err(hdev, "parse failed\n");
23973888b0d5SBenjamin Tissoires 		goto fail;
2398c58ac3a8SBenjamin Tissoires 	}
2399c58ac3a8SBenjamin Tissoires 
2400fd5f92b6SBenjamin Tissoires 	error = wacom_parse_and_register(wacom, false);
2401c58ac3a8SBenjamin Tissoires 	if (error)
24023888b0d5SBenjamin Tissoires 		goto fail;
2403c58ac3a8SBenjamin Tissoires 
2404c58ac3a8SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
2405c58ac3a8SBenjamin Tissoires 		error = device_create_file(&hdev->dev, &dev_attr_speed);
2406c58ac3a8SBenjamin Tissoires 		if (error)
2407c58ac3a8SBenjamin Tissoires 			hid_warn(hdev,
2408c58ac3a8SBenjamin Tissoires 				 "can't create sysfs speed attribute err: %d\n",
2409c58ac3a8SBenjamin Tissoires 				 error);
2410c58ac3a8SBenjamin Tissoires 	}
2411c58ac3a8SBenjamin Tissoires 
2412c58ac3a8SBenjamin Tissoires 	return 0;
2413c58ac3a8SBenjamin Tissoires 
24143888b0d5SBenjamin Tissoires fail:
2415471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
2416471d1714SBenjamin Tissoires 	return error;
2417471d1714SBenjamin Tissoires }
2418471d1714SBenjamin Tissoires 
2419471d1714SBenjamin Tissoires static void wacom_remove(struct hid_device *hdev)
2420471d1714SBenjamin Tissoires {
2421471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
2422f6205161SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2423f6205161SBenjamin Tissoires 	struct wacom_features *features = &wacom_wac->features;
2424f6205161SBenjamin Tissoires 
2425f6205161SBenjamin Tissoires 	if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2426f6205161SBenjamin Tissoires 		hid_hw_close(hdev);
2427471d1714SBenjamin Tissoires 
2428471d1714SBenjamin Tissoires 	hid_hw_stop(hdev);
2429471d1714SBenjamin Tissoires 
2430d17d1f17SBenjamin Tissoires 	cancel_work_sync(&wacom->wireless_work);
2431d17d1f17SBenjamin Tissoires 	cancel_work_sync(&wacom->battery_work);
2432e6f2813aSBenjamin Tissoires 	cancel_work_sync(&wacom->remote_work);
2433f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
2434f81a1295SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
2435471d1714SBenjamin Tissoires 
2436471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
2437471d1714SBenjamin Tissoires }
2438471d1714SBenjamin Tissoires 
243941a74581SGeert Uytterhoeven #ifdef CONFIG_PM
2440471d1714SBenjamin Tissoires static int wacom_resume(struct hid_device *hdev)
2441471d1714SBenjamin Tissoires {
2442471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
2443471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
2444471d1714SBenjamin Tissoires 
2445471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
2446471d1714SBenjamin Tissoires 
2447471d1714SBenjamin Tissoires 	/* switch to wacom mode first */
2448471d1714SBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
2449471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
2450471d1714SBenjamin Tissoires 
2451471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
2452471d1714SBenjamin Tissoires 
2453471d1714SBenjamin Tissoires 	return 0;
2454471d1714SBenjamin Tissoires }
2455471d1714SBenjamin Tissoires 
2456471d1714SBenjamin Tissoires static int wacom_reset_resume(struct hid_device *hdev)
2457471d1714SBenjamin Tissoires {
2458471d1714SBenjamin Tissoires 	return wacom_resume(hdev);
2459471d1714SBenjamin Tissoires }
246041a74581SGeert Uytterhoeven #endif /* CONFIG_PM */
2461471d1714SBenjamin Tissoires 
2462471d1714SBenjamin Tissoires static struct hid_driver wacom_driver = {
2463471d1714SBenjamin Tissoires 	.name =		"wacom",
2464471d1714SBenjamin Tissoires 	.id_table =	wacom_ids,
2465471d1714SBenjamin Tissoires 	.probe =	wacom_probe,
2466471d1714SBenjamin Tissoires 	.remove =	wacom_remove,
24677704ac93SBenjamin Tissoires 	.report =	wacom_wac_report,
2468471d1714SBenjamin Tissoires #ifdef CONFIG_PM
2469471d1714SBenjamin Tissoires 	.resume =	wacom_resume,
2470471d1714SBenjamin Tissoires 	.reset_resume =	wacom_reset_resume,
2471471d1714SBenjamin Tissoires #endif
2472471d1714SBenjamin Tissoires 	.raw_event =	wacom_raw_event,
2473471d1714SBenjamin Tissoires };
2474471d1714SBenjamin Tissoires module_hid_driver(wacom_driver);
2475f2e0a7d4SBenjamin Tissoires 
2476f2e0a7d4SBenjamin Tissoires MODULE_VERSION(DRIVER_VERSION);
2477f2e0a7d4SBenjamin Tissoires MODULE_AUTHOR(DRIVER_AUTHOR);
2478f2e0a7d4SBenjamin Tissoires MODULE_DESCRIPTION(DRIVER_DESC);
2479f2e0a7d4SBenjamin Tissoires MODULE_LICENSE(DRIVER_LICENSE);
2480