xref: /openbmc/linux/drivers/hid/wacom_sys.c (revision 83e6b40e2de6f8416ce6c8c3ca6c4bc8acfa8b32)
1471d1714SBenjamin Tissoires /*
2471d1714SBenjamin Tissoires  * drivers/input/tablet/wacom_sys.c
3471d1714SBenjamin Tissoires  *
4471d1714SBenjamin Tissoires  *  USB Wacom tablet support - system specific code
5471d1714SBenjamin Tissoires  */
6471d1714SBenjamin Tissoires 
7471d1714SBenjamin Tissoires /*
8471d1714SBenjamin Tissoires  * This program is free software; you can redistribute it and/or modify
9471d1714SBenjamin Tissoires  * it under the terms of the GNU General Public License as published by
10471d1714SBenjamin Tissoires  * the Free Software Foundation; either version 2 of the License, or
11471d1714SBenjamin Tissoires  * (at your option) any later version.
12471d1714SBenjamin Tissoires  */
13471d1714SBenjamin Tissoires 
14471d1714SBenjamin Tissoires #include "wacom_wac.h"
15471d1714SBenjamin Tissoires #include "wacom.h"
16b58ba1baSJason Gerecke #include <linux/input/mt.h>
17471d1714SBenjamin Tissoires 
18471d1714SBenjamin Tissoires #define WAC_MSG_RETRIES		5
19471d1714SBenjamin Tissoires 
20912ca216SPing Cheng #define WAC_CMD_WL_LED_CONTROL	0x03
21471d1714SBenjamin Tissoires #define WAC_CMD_LED_CONTROL	0x20
22471d1714SBenjamin Tissoires #define WAC_CMD_ICON_START	0x21
23471d1714SBenjamin Tissoires #define WAC_CMD_ICON_XFER	0x23
24849e2f06SBenjamin Tissoires #define WAC_CMD_ICON_BT_XFER	0x26
25471d1714SBenjamin Tissoires #define WAC_CMD_RETRIES		10
2672b236d6SAaron Skomra #define WAC_CMD_DELETE_PAIRING	0x20
2772b236d6SAaron Skomra #define WAC_CMD_UNPAIR_ALL	0xFF
28471d1714SBenjamin Tissoires 
29e0984bc3SPing Cheng #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
30e0984bc3SPing Cheng #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
3172b236d6SAaron Skomra #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
32e0984bc3SPing Cheng 
33c64d8834SPing Cheng static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
34c64d8834SPing Cheng 			    size_t size, unsigned int retries)
35471d1714SBenjamin Tissoires {
36471d1714SBenjamin Tissoires 	int retval;
37471d1714SBenjamin Tissoires 
38471d1714SBenjamin Tissoires 	do {
39c64d8834SPing Cheng 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
40471d1714SBenjamin Tissoires 				HID_REQ_GET_REPORT);
41aef3156dSJason Gerecke 	} while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
42aef3156dSJason Gerecke 
43aef3156dSJason Gerecke 	if (retval < 0)
44aef3156dSJason Gerecke 		hid_err(hdev, "wacom_get_report: ran out of retries "
45aef3156dSJason Gerecke 			"(last error = %d)\n", retval);
46471d1714SBenjamin Tissoires 
47471d1714SBenjamin Tissoires 	return retval;
48471d1714SBenjamin Tissoires }
49471d1714SBenjamin Tissoires 
50296b7378SPrzemo Firszt static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
51296b7378SPrzemo Firszt 			    size_t size, unsigned int retries)
52471d1714SBenjamin Tissoires {
53471d1714SBenjamin Tissoires 	int retval;
54471d1714SBenjamin Tissoires 
55471d1714SBenjamin Tissoires 	do {
56296b7378SPrzemo Firszt 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
57471d1714SBenjamin Tissoires 				HID_REQ_SET_REPORT);
58aef3156dSJason Gerecke 	} while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
59aef3156dSJason Gerecke 
60aef3156dSJason Gerecke 	if (retval < 0)
61aef3156dSJason Gerecke 		hid_err(hdev, "wacom_set_report: ran out of retries "
62aef3156dSJason Gerecke 			"(last error = %d)\n", retval);
63471d1714SBenjamin Tissoires 
64471d1714SBenjamin Tissoires 	return retval;
65471d1714SBenjamin Tissoires }
66471d1714SBenjamin Tissoires 
67471d1714SBenjamin Tissoires static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
68471d1714SBenjamin Tissoires 		u8 *raw_data, int size)
69471d1714SBenjamin Tissoires {
70471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
71471d1714SBenjamin Tissoires 
72471d1714SBenjamin Tissoires 	if (size > WACOM_PKGLEN_MAX)
73471d1714SBenjamin Tissoires 		return 1;
74471d1714SBenjamin Tissoires 
75471d1714SBenjamin Tissoires 	memcpy(wacom->wacom_wac.data, raw_data, size);
76471d1714SBenjamin Tissoires 
77471d1714SBenjamin Tissoires 	wacom_wac_irq(&wacom->wacom_wac, size);
78471d1714SBenjamin Tissoires 
79471d1714SBenjamin Tissoires 	return 0;
80471d1714SBenjamin Tissoires }
81471d1714SBenjamin Tissoires 
82471d1714SBenjamin Tissoires static int wacom_open(struct input_dev *dev)
83471d1714SBenjamin Tissoires {
84471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
85471d1714SBenjamin Tissoires 
86dff67416SBenjamin Tissoires 	return hid_hw_open(wacom->hdev);
87471d1714SBenjamin Tissoires }
88471d1714SBenjamin Tissoires 
89471d1714SBenjamin Tissoires static void wacom_close(struct input_dev *dev)
90471d1714SBenjamin Tissoires {
91471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
92471d1714SBenjamin Tissoires 
933dad188eSBenjamin Tissoires 	/*
943dad188eSBenjamin Tissoires 	 * wacom->hdev should never be null, but surprisingly, I had the case
953dad188eSBenjamin Tissoires 	 * once while unplugging the Wacom Wireless Receiver.
963dad188eSBenjamin Tissoires 	 */
973dad188eSBenjamin Tissoires 	if (wacom->hdev)
98471d1714SBenjamin Tissoires 		hid_hw_close(wacom->hdev);
99471d1714SBenjamin Tissoires }
100471d1714SBenjamin Tissoires 
101471d1714SBenjamin Tissoires /*
102471d1714SBenjamin Tissoires  * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
103471d1714SBenjamin Tissoires  */
104471d1714SBenjamin Tissoires static int wacom_calc_hid_res(int logical_extents, int physical_extents,
105471d1714SBenjamin Tissoires 			       unsigned unit, int exponent)
106471d1714SBenjamin Tissoires {
107471d1714SBenjamin Tissoires 	struct hid_field field = {
108471d1714SBenjamin Tissoires 		.logical_maximum = logical_extents,
109471d1714SBenjamin Tissoires 		.physical_maximum = physical_extents,
110471d1714SBenjamin Tissoires 		.unit = unit,
111471d1714SBenjamin Tissoires 		.unit_exponent = exponent,
112471d1714SBenjamin Tissoires 	};
113471d1714SBenjamin Tissoires 
114471d1714SBenjamin Tissoires 	return hidinput_calc_abs_res(&field, ABS_X);
115471d1714SBenjamin Tissoires }
116471d1714SBenjamin Tissoires 
117471d1714SBenjamin Tissoires static void wacom_feature_mapping(struct hid_device *hdev,
118471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
119471d1714SBenjamin Tissoires {
120471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
121471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
1225ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
1238ffffd52SBenjamin Tissoires 	u8 *data;
1248ffffd52SBenjamin Tissoires 	int ret;
125471d1714SBenjamin Tissoires 
126471d1714SBenjamin Tissoires 	switch (usage->hid) {
127471d1714SBenjamin Tissoires 	case HID_DG_CONTACTMAX:
128471d1714SBenjamin Tissoires 		/* leave touch_max as is if predefined */
1298ffffd52SBenjamin Tissoires 		if (!features->touch_max) {
1308ffffd52SBenjamin Tissoires 			/* read manually */
1318ffffd52SBenjamin Tissoires 			data = kzalloc(2, GFP_KERNEL);
1328ffffd52SBenjamin Tissoires 			if (!data)
1338ffffd52SBenjamin Tissoires 				break;
1348ffffd52SBenjamin Tissoires 			data[0] = field->report->id;
1358ffffd52SBenjamin Tissoires 			ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
13605e8fd92SJason Gerecke 						data, 2, WAC_CMD_RETRIES);
13705e8fd92SJason Gerecke 			if (ret == 2) {
1388ffffd52SBenjamin Tissoires 				features->touch_max = data[1];
13905e8fd92SJason Gerecke 			} else {
14005e8fd92SJason Gerecke 				features->touch_max = 16;
14105e8fd92SJason Gerecke 				hid_warn(hdev, "wacom_feature_mapping: "
14205e8fd92SJason Gerecke 					 "could not get HID_DG_CONTACTMAX, "
14305e8fd92SJason Gerecke 					 "defaulting to %d\n",
14405e8fd92SJason Gerecke 					  features->touch_max);
14505e8fd92SJason Gerecke 			}
1468ffffd52SBenjamin Tissoires 			kfree(data);
1478ffffd52SBenjamin Tissoires 		}
148471d1714SBenjamin Tissoires 		break;
1495ae6e89fSBenjamin Tissoires 	case HID_DG_INPUTMODE:
1505ae6e89fSBenjamin Tissoires 		/* Ignore if value index is out of bounds. */
1515ae6e89fSBenjamin Tissoires 		if (usage->usage_index >= field->report_count) {
1525ae6e89fSBenjamin Tissoires 			dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
1535ae6e89fSBenjamin Tissoires 			break;
1545ae6e89fSBenjamin Tissoires 		}
1555ae6e89fSBenjamin Tissoires 
1565ae6e89fSBenjamin Tissoires 		hid_data->inputmode = field->report->id;
1575ae6e89fSBenjamin Tissoires 		hid_data->inputmode_index = usage->usage_index;
1585ae6e89fSBenjamin Tissoires 		break;
159326ea2a9SJason Gerecke 
160326ea2a9SJason Gerecke 	case HID_UP_DIGITIZER:
161326ea2a9SJason Gerecke 		if (field->report->id == 0x0B &&
162326ea2a9SJason Gerecke 		    (field->application == WACOM_G9_DIGITIZER ||
163326ea2a9SJason Gerecke 		     field->application == WACOM_G11_DIGITIZER)) {
164326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_report = field->report->id;
165326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_value = 0;
166326ea2a9SJason Gerecke 		}
167326ea2a9SJason Gerecke 		break;
168326ea2a9SJason Gerecke 
169326ea2a9SJason Gerecke 	case WACOM_G9_PAGE:
170326ea2a9SJason Gerecke 	case WACOM_G11_PAGE:
171326ea2a9SJason Gerecke 		if (field->report->id == 0x03 &&
172326ea2a9SJason Gerecke 		    (field->application == WACOM_G9_TOUCHSCREEN ||
173326ea2a9SJason Gerecke 		     field->application == WACOM_G11_TOUCHSCREEN)) {
174326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_report = field->report->id;
175326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_value = 0;
176326ea2a9SJason Gerecke 		}
177326ea2a9SJason Gerecke 		break;
178471d1714SBenjamin Tissoires 	}
179471d1714SBenjamin Tissoires }
180471d1714SBenjamin Tissoires 
181471d1714SBenjamin Tissoires /*
182471d1714SBenjamin Tissoires  * Interface Descriptor of wacom devices can be incomplete and
183471d1714SBenjamin Tissoires  * inconsistent so wacom_features table is used to store stylus
184471d1714SBenjamin Tissoires  * device's packet lengths, various maximum values, and tablet
185471d1714SBenjamin Tissoires  * resolution based on product ID's.
186471d1714SBenjamin Tissoires  *
187471d1714SBenjamin Tissoires  * For devices that contain 2 interfaces, wacom_features table is
188471d1714SBenjamin Tissoires  * inaccurate for the touch interface.  Since the Interface Descriptor
189471d1714SBenjamin Tissoires  * for touch interfaces has pretty complete data, this function exists
190471d1714SBenjamin Tissoires  * to query tablet for this missing information instead of hard coding in
191471d1714SBenjamin Tissoires  * an additional table.
192471d1714SBenjamin Tissoires  *
193471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a stylus will contain a
194471d1714SBenjamin Tissoires  * boot mouse application collection that is not of interest and this
195471d1714SBenjamin Tissoires  * function will ignore it.
196471d1714SBenjamin Tissoires  *
197471d1714SBenjamin Tissoires  * It also contains a digitizer application collection that also is not
198471d1714SBenjamin Tissoires  * of interest since any information it contains would be duplicate
199471d1714SBenjamin Tissoires  * of what is in wacom_features. Usually it defines a report of an array
200471d1714SBenjamin Tissoires  * of bytes that could be used as max length of the stylus packet returned.
201471d1714SBenjamin Tissoires  * If it happens to define a Digitizer-Stylus Physical Collection then
202471d1714SBenjamin Tissoires  * the X and Y logical values contain valid data but it is ignored.
203471d1714SBenjamin Tissoires  *
204471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a touch interface will contain a
205471d1714SBenjamin Tissoires  * Digitizer-Finger Physical Collection which will define both logical
206471d1714SBenjamin Tissoires  * X/Y maximum as well as the physical size of tablet. Since touch
207471d1714SBenjamin Tissoires  * interfaces haven't supported pressure or distance, this is enough
208471d1714SBenjamin Tissoires  * information to override invalid values in the wacom_features table.
209471d1714SBenjamin Tissoires  *
210471d1714SBenjamin Tissoires  * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
211471d1714SBenjamin Tissoires  * data. We deal with them after returning from this function.
212471d1714SBenjamin Tissoires  */
213471d1714SBenjamin Tissoires static void wacom_usage_mapping(struct hid_device *hdev,
214471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
215471d1714SBenjamin Tissoires {
216471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
217471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
218d97a5522SBenjamin Tissoires 	bool finger = WACOM_FINGER_FIELD(field);
219d97a5522SBenjamin Tissoires 	bool pen = WACOM_PEN_FIELD(field);
220471d1714SBenjamin Tissoires 
221471d1714SBenjamin Tissoires 	/*
222471d1714SBenjamin Tissoires 	* Requiring Stylus Usage will ignore boot mouse
223471d1714SBenjamin Tissoires 	* X/Y values and some cases of invalid Digitizer X/Y
224471d1714SBenjamin Tissoires 	* values commonly reported.
225471d1714SBenjamin Tissoires 	*/
226042628abSJason Gerecke 	if (pen)
227aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_PEN;
228042628abSJason Gerecke 	else if (finger)
229aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_TOUCH;
230042628abSJason Gerecke 	else
231471d1714SBenjamin Tissoires 		return;
232471d1714SBenjamin Tissoires 
23330ebc1aeSPing Cheng 	/*
23430ebc1aeSPing Cheng 	 * Bamboo models do not support HID_DG_CONTACTMAX.
23530ebc1aeSPing Cheng 	 * And, Bamboo Pen only descriptor contains touch.
23630ebc1aeSPing Cheng 	 */
2373b164a00SPing Cheng 	if (features->type > BAMBOO_PT) {
23830ebc1aeSPing Cheng 		/* ISDv4 touch devices at least supports one touch point */
239471d1714SBenjamin Tissoires 		if (finger && !features->touch_max)
240471d1714SBenjamin Tissoires 			features->touch_max = 1;
24130ebc1aeSPing Cheng 	}
242471d1714SBenjamin Tissoires 
243471d1714SBenjamin Tissoires 	switch (usage->hid) {
244471d1714SBenjamin Tissoires 	case HID_GD_X:
245471d1714SBenjamin Tissoires 		features->x_max = field->logical_maximum;
246471d1714SBenjamin Tissoires 		if (finger) {
247471d1714SBenjamin Tissoires 			features->x_phy = field->physical_maximum;
2483b164a00SPing Cheng 			if ((features->type != BAMBOO_PT) &&
2493b164a00SPing Cheng 			    (features->type != BAMBOO_TOUCH)) {
250471d1714SBenjamin Tissoires 				features->unit = field->unit;
251471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
252471d1714SBenjamin Tissoires 			}
253471d1714SBenjamin Tissoires 		}
254471d1714SBenjamin Tissoires 		break;
255471d1714SBenjamin Tissoires 	case HID_GD_Y:
256471d1714SBenjamin Tissoires 		features->y_max = field->logical_maximum;
257471d1714SBenjamin Tissoires 		if (finger) {
258471d1714SBenjamin Tissoires 			features->y_phy = field->physical_maximum;
2593b164a00SPing Cheng 			if ((features->type != BAMBOO_PT) &&
2603b164a00SPing Cheng 			    (features->type != BAMBOO_TOUCH)) {
261471d1714SBenjamin Tissoires 				features->unit = field->unit;
262471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
263471d1714SBenjamin Tissoires 			}
264471d1714SBenjamin Tissoires 		}
265471d1714SBenjamin Tissoires 		break;
266471d1714SBenjamin Tissoires 	case HID_DG_TIPPRESSURE:
267471d1714SBenjamin Tissoires 		if (pen)
268471d1714SBenjamin Tissoires 			features->pressure_max = field->logical_maximum;
269471d1714SBenjamin Tissoires 		break;
270471d1714SBenjamin Tissoires 	}
2717704ac93SBenjamin Tissoires 
2727704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
2737704ac93SBenjamin Tissoires 		wacom_wac_usage_mapping(hdev, field, usage);
274471d1714SBenjamin Tissoires }
275471d1714SBenjamin Tissoires 
276b58ba1baSJason Gerecke static void wacom_post_parse_hid(struct hid_device *hdev,
277b58ba1baSJason Gerecke 				 struct wacom_features *features)
278b58ba1baSJason Gerecke {
279b58ba1baSJason Gerecke 	struct wacom *wacom = hid_get_drvdata(hdev);
280b58ba1baSJason Gerecke 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
281b58ba1baSJason Gerecke 
282b58ba1baSJason Gerecke 	if (features->type == HID_GENERIC) {
283b58ba1baSJason Gerecke 		/* Any last-minute generic device setup */
284b58ba1baSJason Gerecke 		if (features->touch_max > 1) {
2852a6cdbddSJason Gerecke 			input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
286b58ba1baSJason Gerecke 				    INPUT_MT_DIRECT);
287b58ba1baSJason Gerecke 		}
288b58ba1baSJason Gerecke 	}
289b58ba1baSJason Gerecke }
290b58ba1baSJason Gerecke 
291471d1714SBenjamin Tissoires static void wacom_parse_hid(struct hid_device *hdev,
292471d1714SBenjamin Tissoires 			   struct wacom_features *features)
293471d1714SBenjamin Tissoires {
294471d1714SBenjamin Tissoires 	struct hid_report_enum *rep_enum;
295471d1714SBenjamin Tissoires 	struct hid_report *hreport;
296471d1714SBenjamin Tissoires 	int i, j;
297471d1714SBenjamin Tissoires 
298471d1714SBenjamin Tissoires 	/* check features first */
299471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
300471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
301471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++) {
302471d1714SBenjamin Tissoires 			/* Ignore if report count is out of bounds. */
303471d1714SBenjamin Tissoires 			if (hreport->field[i]->report_count < 1)
304471d1714SBenjamin Tissoires 				continue;
305471d1714SBenjamin Tissoires 
306471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++) {
307471d1714SBenjamin Tissoires 				wacom_feature_mapping(hdev, hreport->field[i],
308471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
309471d1714SBenjamin Tissoires 			}
310471d1714SBenjamin Tissoires 		}
311471d1714SBenjamin Tissoires 	}
312471d1714SBenjamin Tissoires 
313471d1714SBenjamin Tissoires 	/* now check the input usages */
314471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
315471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
316471d1714SBenjamin Tissoires 
317471d1714SBenjamin Tissoires 		if (!hreport->maxfield)
318471d1714SBenjamin Tissoires 			continue;
319471d1714SBenjamin Tissoires 
320471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++)
321471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++)
322471d1714SBenjamin Tissoires 				wacom_usage_mapping(hdev, hreport->field[i],
323471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
324471d1714SBenjamin Tissoires 	}
325b58ba1baSJason Gerecke 
326b58ba1baSJason Gerecke 	wacom_post_parse_hid(hdev, features);
327471d1714SBenjamin Tissoires }
328471d1714SBenjamin Tissoires 
3295ae6e89fSBenjamin Tissoires static int wacom_hid_set_device_mode(struct hid_device *hdev)
3305ae6e89fSBenjamin Tissoires {
3315ae6e89fSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
3325ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
3335ae6e89fSBenjamin Tissoires 	struct hid_report *r;
3345ae6e89fSBenjamin Tissoires 	struct hid_report_enum *re;
3355ae6e89fSBenjamin Tissoires 
3365ae6e89fSBenjamin Tissoires 	if (hid_data->inputmode < 0)
3375ae6e89fSBenjamin Tissoires 		return 0;
3385ae6e89fSBenjamin Tissoires 
3395ae6e89fSBenjamin Tissoires 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
3405ae6e89fSBenjamin Tissoires 	r = re->report_id_hash[hid_data->inputmode];
3415ae6e89fSBenjamin Tissoires 	if (r) {
3425ae6e89fSBenjamin Tissoires 		r->field[0]->value[hid_data->inputmode_index] = 2;
3435ae6e89fSBenjamin Tissoires 		hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
3445ae6e89fSBenjamin Tissoires 	}
3455ae6e89fSBenjamin Tissoires 	return 0;
3465ae6e89fSBenjamin Tissoires }
3475ae6e89fSBenjamin Tissoires 
348326ea2a9SJason Gerecke static int wacom_set_device_mode(struct hid_device *hdev,
349326ea2a9SJason Gerecke 				 struct wacom_wac *wacom_wac)
350471d1714SBenjamin Tissoires {
351326ea2a9SJason Gerecke 	u8 *rep_data;
352326ea2a9SJason Gerecke 	struct hid_report *r;
353326ea2a9SJason Gerecke 	struct hid_report_enum *re;
354326ea2a9SJason Gerecke 	int length;
355471d1714SBenjamin Tissoires 	int error = -ENOMEM, limit = 0;
356471d1714SBenjamin Tissoires 
357326ea2a9SJason Gerecke 	if (wacom_wac->mode_report < 0)
358326ea2a9SJason Gerecke 		return 0;
359326ea2a9SJason Gerecke 
360326ea2a9SJason Gerecke 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
361326ea2a9SJason Gerecke 	r = re->report_id_hash[wacom_wac->mode_report];
362326ea2a9SJason Gerecke 	if (!r)
363326ea2a9SJason Gerecke 		return -EINVAL;
364326ea2a9SJason Gerecke 
365326ea2a9SJason Gerecke 	rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
366471d1714SBenjamin Tissoires 	if (!rep_data)
367326ea2a9SJason Gerecke 		return -ENOMEM;
368326ea2a9SJason Gerecke 
369326ea2a9SJason Gerecke 	length = hid_report_len(r);
370471d1714SBenjamin Tissoires 
371471d1714SBenjamin Tissoires 	do {
372326ea2a9SJason Gerecke 		rep_data[0] = wacom_wac->mode_report;
373326ea2a9SJason Gerecke 		rep_data[1] = wacom_wac->mode_value;
374471d1714SBenjamin Tissoires 
375296b7378SPrzemo Firszt 		error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
376296b7378SPrzemo Firszt 					 length, 1);
377471d1714SBenjamin Tissoires 		if (error >= 0)
378471d1714SBenjamin Tissoires 			error = wacom_get_report(hdev, HID_FEATURE_REPORT,
379c64d8834SPing Cheng 			                         rep_data, length, 1);
380326ea2a9SJason Gerecke 	} while (error >= 0 &&
381326ea2a9SJason Gerecke 		 rep_data[1] != wacom_wac->mode_report &&
382326ea2a9SJason Gerecke 		 limit++ < WAC_MSG_RETRIES);
383471d1714SBenjamin Tissoires 
384471d1714SBenjamin Tissoires 	kfree(rep_data);
385471d1714SBenjamin Tissoires 
386471d1714SBenjamin Tissoires 	return error < 0 ? error : 0;
387471d1714SBenjamin Tissoires }
388471d1714SBenjamin Tissoires 
389f81a1295SBenjamin Tissoires static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
390f81a1295SBenjamin Tissoires 		struct wacom_features *features)
391f81a1295SBenjamin Tissoires {
392387142bbSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
393387142bbSBenjamin Tissoires 	int ret;
394387142bbSBenjamin Tissoires 	u8 rep_data[2];
395387142bbSBenjamin Tissoires 
396387142bbSBenjamin Tissoires 	switch (features->type) {
397387142bbSBenjamin Tissoires 	case GRAPHIRE_BT:
398387142bbSBenjamin Tissoires 		rep_data[0] = 0x03;
399387142bbSBenjamin Tissoires 		rep_data[1] = 0x00;
400296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
401296b7378SPrzemo Firszt 					3);
402387142bbSBenjamin Tissoires 
403387142bbSBenjamin Tissoires 		if (ret >= 0) {
404387142bbSBenjamin Tissoires 			rep_data[0] = speed == 0 ? 0x05 : 0x06;
405387142bbSBenjamin Tissoires 			rep_data[1] = 0x00;
406387142bbSBenjamin Tissoires 
407387142bbSBenjamin Tissoires 			ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
408296b7378SPrzemo Firszt 						rep_data, 2, 3);
409387142bbSBenjamin Tissoires 
410387142bbSBenjamin Tissoires 			if (ret >= 0) {
411387142bbSBenjamin Tissoires 				wacom->wacom_wac.bt_high_speed = speed;
412387142bbSBenjamin Tissoires 				return 0;
413387142bbSBenjamin Tissoires 			}
414387142bbSBenjamin Tissoires 		}
415387142bbSBenjamin Tissoires 
416387142bbSBenjamin Tissoires 		/*
417387142bbSBenjamin Tissoires 		 * Note that if the raw queries fail, it's not a hard failure
418387142bbSBenjamin Tissoires 		 * and it is safe to continue
419387142bbSBenjamin Tissoires 		 */
420387142bbSBenjamin Tissoires 		hid_warn(hdev, "failed to poke device, command %d, err %d\n",
421387142bbSBenjamin Tissoires 			 rep_data[0], ret);
422387142bbSBenjamin Tissoires 		break;
42381af7e61SBenjamin Tissoires 	case INTUOS4WL:
42481af7e61SBenjamin Tissoires 		if (speed == 1)
42581af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features &= ~0x20;
42681af7e61SBenjamin Tissoires 		else
42781af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features |= 0x20;
42881af7e61SBenjamin Tissoires 
42981af7e61SBenjamin Tissoires 		rep_data[0] = 0x03;
43081af7e61SBenjamin Tissoires 		rep_data[1] = wacom->wacom_wac.bt_features;
43181af7e61SBenjamin Tissoires 
432296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
433296b7378SPrzemo Firszt 					1);
43481af7e61SBenjamin Tissoires 		if (ret >= 0)
43581af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_high_speed = speed;
43681af7e61SBenjamin Tissoires 		break;
437387142bbSBenjamin Tissoires 	}
438387142bbSBenjamin Tissoires 
439f81a1295SBenjamin Tissoires 	return 0;
440f81a1295SBenjamin Tissoires }
441f81a1295SBenjamin Tissoires 
442471d1714SBenjamin Tissoires /*
443471d1714SBenjamin Tissoires  * Switch the tablet into its most-capable mode. Wacom tablets are
444471d1714SBenjamin Tissoires  * typically configured to power-up in a mode which sends mouse-like
445471d1714SBenjamin Tissoires  * reports to the OS. To get absolute position, pressure data, etc.
446471d1714SBenjamin Tissoires  * from the tablet, it is necessary to switch the tablet out of this
447471d1714SBenjamin Tissoires  * mode and into one which sends the full range of tablet data.
448471d1714SBenjamin Tissoires  */
449471d1714SBenjamin Tissoires static int wacom_query_tablet_data(struct hid_device *hdev,
450471d1714SBenjamin Tissoires 		struct wacom_features *features)
451471d1714SBenjamin Tissoires {
452326ea2a9SJason Gerecke 	struct wacom *wacom = hid_get_drvdata(hdev);
453326ea2a9SJason Gerecke 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
454326ea2a9SJason Gerecke 
455f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
456f81a1295SBenjamin Tissoires 		return wacom_bt_query_tablet_data(hdev, 1, features);
457f81a1295SBenjamin Tissoires 
458326ea2a9SJason Gerecke 	if (features->type != HID_GENERIC) {
459aa86b18cSJason Gerecke 		if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
460471d1714SBenjamin Tissoires 			if (features->type > TABLETPC) {
461471d1714SBenjamin Tissoires 				/* MT Tablet PC touch */
462326ea2a9SJason Gerecke 				wacom_wac->mode_report = 3;
463326ea2a9SJason Gerecke 				wacom_wac->mode_value = 4;
464326ea2a9SJason Gerecke 			} else if (features->type == WACOM_24HDT) {
465326ea2a9SJason Gerecke 				wacom_wac->mode_report = 18;
466326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
467326ea2a9SJason Gerecke 			} else if (features->type == WACOM_27QHDT) {
468326ea2a9SJason Gerecke 				wacom_wac->mode_report = 131;
469326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
470326ea2a9SJason Gerecke 			} else if (features->type == BAMBOO_PAD) {
471326ea2a9SJason Gerecke 				wacom_wac->mode_report = 2;
472326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
4738c97a765SBenjamin Tissoires 			}
474aa86b18cSJason Gerecke 		} else if (features->device_type & WACOM_DEVICETYPE_PEN) {
4757adb91bdSPing Cheng 			if (features->type <= BAMBOO_PT) {
476326ea2a9SJason Gerecke 				wacom_wac->mode_report = 2;
477326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
478471d1714SBenjamin Tissoires 			}
479471d1714SBenjamin Tissoires 		}
480326ea2a9SJason Gerecke 	}
481326ea2a9SJason Gerecke 
482326ea2a9SJason Gerecke 	wacom_set_device_mode(hdev, wacom_wac);
483326ea2a9SJason Gerecke 
484326ea2a9SJason Gerecke 	if (features->type == HID_GENERIC)
485326ea2a9SJason Gerecke 		return wacom_hid_set_device_mode(hdev);
486471d1714SBenjamin Tissoires 
487471d1714SBenjamin Tissoires 	return 0;
488471d1714SBenjamin Tissoires }
489471d1714SBenjamin Tissoires 
490471d1714SBenjamin Tissoires static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
491471d1714SBenjamin Tissoires 					 struct wacom_features *features)
492471d1714SBenjamin Tissoires {
493471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
494471d1714SBenjamin Tissoires 	struct usb_interface *intf = wacom->intf;
495471d1714SBenjamin Tissoires 
496471d1714SBenjamin Tissoires 	/* default features */
497471d1714SBenjamin Tissoires 	features->x_fuzz = 4;
498471d1714SBenjamin Tissoires 	features->y_fuzz = 4;
499471d1714SBenjamin Tissoires 	features->pressure_fuzz = 0;
500bef7e200SJason Gerecke 	features->distance_fuzz = 1;
501bef7e200SJason Gerecke 	features->tilt_fuzz = 1;
502471d1714SBenjamin Tissoires 
503471d1714SBenjamin Tissoires 	/*
504471d1714SBenjamin Tissoires 	 * The wireless device HID is basic and layout conflicts with
505471d1714SBenjamin Tissoires 	 * other tablets (monitor and touch interface can look like pen).
506471d1714SBenjamin Tissoires 	 * Skip the query for this type and modify defaults based on
507471d1714SBenjamin Tissoires 	 * interface number.
508471d1714SBenjamin Tissoires 	 */
509471d1714SBenjamin Tissoires 	if (features->type == WIRELESS) {
5103f14a63aSJason Gerecke 		if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
511ccad85ccSJason Gerecke 			features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
5123f14a63aSJason Gerecke 		else
513aa86b18cSJason Gerecke 			features->device_type = WACOM_DEVICETYPE_NONE;
5143f14a63aSJason Gerecke 		return;
515471d1714SBenjamin Tissoires 	}
516471d1714SBenjamin Tissoires 
517471d1714SBenjamin Tissoires 	wacom_parse_hid(hdev, features);
518471d1714SBenjamin Tissoires }
519471d1714SBenjamin Tissoires 
520471d1714SBenjamin Tissoires struct wacom_hdev_data {
521471d1714SBenjamin Tissoires 	struct list_head list;
522471d1714SBenjamin Tissoires 	struct kref kref;
523471d1714SBenjamin Tissoires 	struct hid_device *dev;
524471d1714SBenjamin Tissoires 	struct wacom_shared shared;
525471d1714SBenjamin Tissoires };
526471d1714SBenjamin Tissoires 
527471d1714SBenjamin Tissoires static LIST_HEAD(wacom_udev_list);
528471d1714SBenjamin Tissoires static DEFINE_MUTEX(wacom_udev_list_lock);
529471d1714SBenjamin Tissoires 
530471d1714SBenjamin Tissoires static bool wacom_are_sibling(struct hid_device *hdev,
531471d1714SBenjamin Tissoires 		struct hid_device *sibling)
532471d1714SBenjamin Tissoires {
533471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
534471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
535471d1714SBenjamin Tissoires 	int vid = features->oVid;
536471d1714SBenjamin Tissoires 	int pid = features->oPid;
537471d1714SBenjamin Tissoires 	int n1,n2;
538471d1714SBenjamin Tissoires 
539471d1714SBenjamin Tissoires 	if (vid == 0 && pid == 0) {
540471d1714SBenjamin Tissoires 		vid = hdev->vendor;
541471d1714SBenjamin Tissoires 		pid = hdev->product;
542471d1714SBenjamin Tissoires 	}
543471d1714SBenjamin Tissoires 
544471d1714SBenjamin Tissoires 	if (vid != sibling->vendor || pid != sibling->product)
545471d1714SBenjamin Tissoires 		return false;
546471d1714SBenjamin Tissoires 
547471d1714SBenjamin Tissoires 	/* Compare the physical path. */
548471d1714SBenjamin Tissoires 	n1 = strrchr(hdev->phys, '.') - hdev->phys;
549471d1714SBenjamin Tissoires 	n2 = strrchr(sibling->phys, '.') - sibling->phys;
550471d1714SBenjamin Tissoires 	if (n1 != n2 || n1 <= 0 || n2 <= 0)
551471d1714SBenjamin Tissoires 		return false;
552471d1714SBenjamin Tissoires 
553471d1714SBenjamin Tissoires 	return !strncmp(hdev->phys, sibling->phys, n1);
554471d1714SBenjamin Tissoires }
555471d1714SBenjamin Tissoires 
556471d1714SBenjamin Tissoires static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
557471d1714SBenjamin Tissoires {
558471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
559471d1714SBenjamin Tissoires 
560471d1714SBenjamin Tissoires 	list_for_each_entry(data, &wacom_udev_list, list) {
561471d1714SBenjamin Tissoires 		if (wacom_are_sibling(hdev, data->dev)) {
562471d1714SBenjamin Tissoires 			kref_get(&data->kref);
563471d1714SBenjamin Tissoires 			return data;
564471d1714SBenjamin Tissoires 		}
565471d1714SBenjamin Tissoires 	}
566471d1714SBenjamin Tissoires 
567471d1714SBenjamin Tissoires 	return NULL;
568471d1714SBenjamin Tissoires }
569471d1714SBenjamin Tissoires 
5701c817c83SBenjamin Tissoires static void wacom_release_shared_data(struct kref *kref)
5711c817c83SBenjamin Tissoires {
5721c817c83SBenjamin Tissoires 	struct wacom_hdev_data *data =
5731c817c83SBenjamin Tissoires 		container_of(kref, struct wacom_hdev_data, kref);
5741c817c83SBenjamin Tissoires 
5751c817c83SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
5761c817c83SBenjamin Tissoires 	list_del(&data->list);
5771c817c83SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
5781c817c83SBenjamin Tissoires 
5791c817c83SBenjamin Tissoires 	kfree(data);
5801c817c83SBenjamin Tissoires }
5811c817c83SBenjamin Tissoires 
5821c817c83SBenjamin Tissoires static void wacom_remove_shared_data(void *res)
5831c817c83SBenjamin Tissoires {
5841c817c83SBenjamin Tissoires 	struct wacom *wacom = res;
5851c817c83SBenjamin Tissoires 	struct wacom_hdev_data *data;
5861c817c83SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
5871c817c83SBenjamin Tissoires 
5881c817c83SBenjamin Tissoires 	if (wacom_wac->shared) {
5891c817c83SBenjamin Tissoires 		data = container_of(wacom_wac->shared, struct wacom_hdev_data,
5901c817c83SBenjamin Tissoires 				    shared);
5911c817c83SBenjamin Tissoires 
5921c817c83SBenjamin Tissoires 		if (wacom_wac->shared->touch == wacom->hdev)
5931c817c83SBenjamin Tissoires 			wacom_wac->shared->touch = NULL;
5941c817c83SBenjamin Tissoires 		else if (wacom_wac->shared->pen == wacom->hdev)
5951c817c83SBenjamin Tissoires 			wacom_wac->shared->pen = NULL;
5961c817c83SBenjamin Tissoires 
5971c817c83SBenjamin Tissoires 		kref_put(&data->kref, wacom_release_shared_data);
5981c817c83SBenjamin Tissoires 		wacom_wac->shared = NULL;
5991c817c83SBenjamin Tissoires 	}
6001c817c83SBenjamin Tissoires }
6011c817c83SBenjamin Tissoires 
602471d1714SBenjamin Tissoires static int wacom_add_shared_data(struct hid_device *hdev)
603471d1714SBenjamin Tissoires {
604471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
605471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
606471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
607471d1714SBenjamin Tissoires 	int retval = 0;
608471d1714SBenjamin Tissoires 
609471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
610471d1714SBenjamin Tissoires 
611471d1714SBenjamin Tissoires 	data = wacom_get_hdev_data(hdev);
612471d1714SBenjamin Tissoires 	if (!data) {
613471d1714SBenjamin Tissoires 		data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
614471d1714SBenjamin Tissoires 		if (!data) {
615471d1714SBenjamin Tissoires 			retval = -ENOMEM;
616471d1714SBenjamin Tissoires 			goto out;
617471d1714SBenjamin Tissoires 		}
618471d1714SBenjamin Tissoires 
619471d1714SBenjamin Tissoires 		kref_init(&data->kref);
620471d1714SBenjamin Tissoires 		data->dev = hdev;
621471d1714SBenjamin Tissoires 		list_add_tail(&data->list, &wacom_udev_list);
622471d1714SBenjamin Tissoires 	}
623471d1714SBenjamin Tissoires 
624471d1714SBenjamin Tissoires 	wacom_wac->shared = &data->shared;
625471d1714SBenjamin Tissoires 
6261c817c83SBenjamin Tissoires 	retval = devm_add_action(&hdev->dev, wacom_remove_shared_data, wacom);
6271c817c83SBenjamin Tissoires 	if (retval) {
6281c817c83SBenjamin Tissoires 		mutex_unlock(&wacom_udev_list_lock);
6291c817c83SBenjamin Tissoires 		wacom_remove_shared_data(wacom);
6301c817c83SBenjamin Tissoires 		return retval;
6311c817c83SBenjamin Tissoires 	}
6321c817c83SBenjamin Tissoires 
633aa86b18cSJason Gerecke 	if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
634a97ac104SBenjamin Tissoires 		wacom_wac->shared->touch = hdev;
635aa86b18cSJason Gerecke 	else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
636a97ac104SBenjamin Tissoires 		wacom_wac->shared->pen = hdev;
637a97ac104SBenjamin Tissoires 
638471d1714SBenjamin Tissoires out:
639471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
640471d1714SBenjamin Tissoires 	return retval;
641471d1714SBenjamin Tissoires }
642471d1714SBenjamin Tissoires 
643471d1714SBenjamin Tissoires static int wacom_led_control(struct wacom *wacom)
644471d1714SBenjamin Tissoires {
645471d1714SBenjamin Tissoires 	unsigned char *buf;
646471d1714SBenjamin Tissoires 	int retval;
647912ca216SPing Cheng 	unsigned char report_id = WAC_CMD_LED_CONTROL;
648912ca216SPing Cheng 	int buf_size = 9;
649471d1714SBenjamin Tissoires 
650a50aac71SBenjamin Tissoires 	if (!wacom->led.groups)
651a50aac71SBenjamin Tissoires 		return -ENOTSUPP;
652a50aac71SBenjamin Tissoires 
653912ca216SPing Cheng 	if (wacom->wacom_wac.pid) { /* wireless connected */
654912ca216SPing Cheng 		report_id = WAC_CMD_WL_LED_CONTROL;
655912ca216SPing Cheng 		buf_size = 13;
656912ca216SPing Cheng 	}
657912ca216SPing Cheng 	buf = kzalloc(buf_size, GFP_KERNEL);
658471d1714SBenjamin Tissoires 	if (!buf)
659471d1714SBenjamin Tissoires 		return -ENOMEM;
660471d1714SBenjamin Tissoires 
661471d1714SBenjamin Tissoires 	if (wacom->wacom_wac.features.type >= INTUOS5S &&
662471d1714SBenjamin Tissoires 	    wacom->wacom_wac.features.type <= INTUOSPL) {
663471d1714SBenjamin Tissoires 		/*
664471d1714SBenjamin Tissoires 		 * Touch Ring and crop mark LED luminance may take on
665471d1714SBenjamin Tissoires 		 * one of four values:
666471d1714SBenjamin Tissoires 		 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
667471d1714SBenjamin Tissoires 		 */
668a50aac71SBenjamin Tissoires 		int ring_led = wacom->led.groups[0].select & 0x03;
669471d1714SBenjamin Tissoires 		int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
670471d1714SBenjamin Tissoires 		int crop_lum = 0;
671912ca216SPing Cheng 		unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
672471d1714SBenjamin Tissoires 
673912ca216SPing Cheng 		buf[0] = report_id;
674912ca216SPing Cheng 		if (wacom->wacom_wac.pid) {
675912ca216SPing Cheng 			wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
676912ca216SPing Cheng 					 buf, buf_size, WAC_CMD_RETRIES);
677912ca216SPing Cheng 			buf[0] = report_id;
678912ca216SPing Cheng 			buf[4] = led_bits;
679912ca216SPing Cheng 		} else
680912ca216SPing Cheng 			buf[1] = led_bits;
681471d1714SBenjamin Tissoires 	}
682471d1714SBenjamin Tissoires 	else {
683a50aac71SBenjamin Tissoires 		int led = wacom->led.groups[0].select | 0x4;
684471d1714SBenjamin Tissoires 
685471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
686471d1714SBenjamin Tissoires 		    wacom->wacom_wac.features.type == WACOM_24HD)
687a50aac71SBenjamin Tissoires 			led |= (wacom->led.groups[1].select << 4) | 0x40;
688471d1714SBenjamin Tissoires 
689912ca216SPing Cheng 		buf[0] = report_id;
690471d1714SBenjamin Tissoires 		buf[1] = led;
691471d1714SBenjamin Tissoires 		buf[2] = wacom->led.llv;
692471d1714SBenjamin Tissoires 		buf[3] = wacom->led.hlv;
693471d1714SBenjamin Tissoires 		buf[4] = wacom->led.img_lum;
694471d1714SBenjamin Tissoires 	}
695471d1714SBenjamin Tissoires 
696912ca216SPing Cheng 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
697296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
698471d1714SBenjamin Tissoires 	kfree(buf);
699471d1714SBenjamin Tissoires 
700471d1714SBenjamin Tissoires 	return retval;
701471d1714SBenjamin Tissoires }
702471d1714SBenjamin Tissoires 
703849e2f06SBenjamin Tissoires static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
704849e2f06SBenjamin Tissoires 		const unsigned len, const void *img)
705471d1714SBenjamin Tissoires {
706471d1714SBenjamin Tissoires 	unsigned char *buf;
707471d1714SBenjamin Tissoires 	int i, retval;
708849e2f06SBenjamin Tissoires 	const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
709471d1714SBenjamin Tissoires 
710849e2f06SBenjamin Tissoires 	buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
711471d1714SBenjamin Tissoires 	if (!buf)
712471d1714SBenjamin Tissoires 		return -ENOMEM;
713471d1714SBenjamin Tissoires 
714471d1714SBenjamin Tissoires 	/* Send 'start' command */
715471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
716471d1714SBenjamin Tissoires 	buf[1] = 1;
717296b7378SPrzemo Firszt 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
718296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
719471d1714SBenjamin Tissoires 	if (retval < 0)
720471d1714SBenjamin Tissoires 		goto out;
721471d1714SBenjamin Tissoires 
722849e2f06SBenjamin Tissoires 	buf[0] = xfer_id;
723471d1714SBenjamin Tissoires 	buf[1] = button_id & 0x07;
724471d1714SBenjamin Tissoires 	for (i = 0; i < 4; i++) {
725471d1714SBenjamin Tissoires 		buf[2] = i;
726849e2f06SBenjamin Tissoires 		memcpy(buf + 3, img + i * chunk_len, chunk_len);
727471d1714SBenjamin Tissoires 
728471d1714SBenjamin Tissoires 		retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
729296b7378SPrzemo Firszt 					  buf, chunk_len + 3, WAC_CMD_RETRIES);
730471d1714SBenjamin Tissoires 		if (retval < 0)
731471d1714SBenjamin Tissoires 			break;
732471d1714SBenjamin Tissoires 	}
733471d1714SBenjamin Tissoires 
734471d1714SBenjamin Tissoires 	/* Send 'stop' */
735471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
736471d1714SBenjamin Tissoires 	buf[1] = 0;
737296b7378SPrzemo Firszt 	wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
738296b7378SPrzemo Firszt 			 WAC_CMD_RETRIES);
739471d1714SBenjamin Tissoires 
740471d1714SBenjamin Tissoires out:
741471d1714SBenjamin Tissoires 	kfree(buf);
742471d1714SBenjamin Tissoires 	return retval;
743471d1714SBenjamin Tissoires }
744471d1714SBenjamin Tissoires 
745471d1714SBenjamin Tissoires static ssize_t wacom_led_select_store(struct device *dev, int set_id,
746471d1714SBenjamin Tissoires 				      const char *buf, size_t count)
747471d1714SBenjamin Tissoires {
748ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
749471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
750471d1714SBenjamin Tissoires 	unsigned int id;
751471d1714SBenjamin Tissoires 	int err;
752471d1714SBenjamin Tissoires 
753471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &id);
754471d1714SBenjamin Tissoires 	if (err)
755471d1714SBenjamin Tissoires 		return err;
756471d1714SBenjamin Tissoires 
757471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
758471d1714SBenjamin Tissoires 
759a50aac71SBenjamin Tissoires 	wacom->led.groups[set_id].select = id & 0x3;
760471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
761471d1714SBenjamin Tissoires 
762471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
763471d1714SBenjamin Tissoires 
764471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
765471d1714SBenjamin Tissoires }
766471d1714SBenjamin Tissoires 
767471d1714SBenjamin Tissoires #define DEVICE_LED_SELECT_ATTR(SET_ID)					\
768471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
769471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
770471d1714SBenjamin Tissoires {									\
771471d1714SBenjamin Tissoires 	return wacom_led_select_store(dev, SET_ID, buf, count);		\
772471d1714SBenjamin Tissoires }									\
773471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
774471d1714SBenjamin Tissoires 	struct device_attribute *attr, char *buf)			\
775471d1714SBenjamin Tissoires {									\
776ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);\
777471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
77837449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n",			\
779a50aac71SBenjamin Tissoires 			 wacom->led.groups[SET_ID].select);		\
780471d1714SBenjamin Tissoires }									\
781e0984bc3SPing Cheng static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,	\
782471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_show,			\
783471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_store)
784471d1714SBenjamin Tissoires 
785471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(0);
786471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(1);
787471d1714SBenjamin Tissoires 
788471d1714SBenjamin Tissoires static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
789471d1714SBenjamin Tissoires 				     const char *buf, size_t count)
790471d1714SBenjamin Tissoires {
791471d1714SBenjamin Tissoires 	unsigned int value;
792471d1714SBenjamin Tissoires 	int err;
793471d1714SBenjamin Tissoires 
794471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &value);
795471d1714SBenjamin Tissoires 	if (err)
796471d1714SBenjamin Tissoires 		return err;
797471d1714SBenjamin Tissoires 
798471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
799471d1714SBenjamin Tissoires 
800471d1714SBenjamin Tissoires 	*dest = value & 0x7f;
801471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
802471d1714SBenjamin Tissoires 
803471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
804471d1714SBenjamin Tissoires 
805471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
806471d1714SBenjamin Tissoires }
807471d1714SBenjamin Tissoires 
808471d1714SBenjamin Tissoires #define DEVICE_LUMINANCE_ATTR(name, field)				\
809471d1714SBenjamin Tissoires static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
810471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
811471d1714SBenjamin Tissoires {									\
812ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);\
813471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
814471d1714SBenjamin Tissoires 									\
815471d1714SBenjamin Tissoires 	return wacom_luminance_store(wacom, &wacom->led.field,		\
816471d1714SBenjamin Tissoires 				     buf, count);			\
817471d1714SBenjamin Tissoires }									\
81837449adcSPing Cheng static ssize_t wacom_##name##_luminance_show(struct device *dev,	\
81937449adcSPing Cheng 	struct device_attribute *attr, char *buf)			\
82037449adcSPing Cheng {									\
82137449adcSPing Cheng 	struct wacom *wacom = dev_get_drvdata(dev);			\
82237449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);	\
82337449adcSPing Cheng }									\
824e0984bc3SPing Cheng static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,			\
82537449adcSPing Cheng 		   wacom_##name##_luminance_show,			\
82637449adcSPing Cheng 		   wacom_##name##_luminance_store)
827471d1714SBenjamin Tissoires 
828471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status0, llv);
829471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status1, hlv);
830471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(buttons, img_lum);
831471d1714SBenjamin Tissoires 
832471d1714SBenjamin Tissoires static ssize_t wacom_button_image_store(struct device *dev, int button_id,
833471d1714SBenjamin Tissoires 					const char *buf, size_t count)
834471d1714SBenjamin Tissoires {
835ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
836471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
837471d1714SBenjamin Tissoires 	int err;
838849e2f06SBenjamin Tissoires 	unsigned len;
839849e2f06SBenjamin Tissoires 	u8 xfer_id;
840471d1714SBenjamin Tissoires 
841849e2f06SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
842849e2f06SBenjamin Tissoires 		len = 256;
843849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_BT_XFER;
844849e2f06SBenjamin Tissoires 	} else {
845849e2f06SBenjamin Tissoires 		len = 1024;
846849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_XFER;
847849e2f06SBenjamin Tissoires 	}
848849e2f06SBenjamin Tissoires 
849849e2f06SBenjamin Tissoires 	if (count != len)
850471d1714SBenjamin Tissoires 		return -EINVAL;
851471d1714SBenjamin Tissoires 
852471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
853471d1714SBenjamin Tissoires 
854849e2f06SBenjamin Tissoires 	err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
855471d1714SBenjamin Tissoires 
856471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
857471d1714SBenjamin Tissoires 
858471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
859471d1714SBenjamin Tissoires }
860471d1714SBenjamin Tissoires 
861471d1714SBenjamin Tissoires #define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
862471d1714SBenjamin Tissoires static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
863471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
864471d1714SBenjamin Tissoires {									\
865471d1714SBenjamin Tissoires 	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
866471d1714SBenjamin Tissoires }									\
867e0984bc3SPing Cheng static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,	\
868471d1714SBenjamin Tissoires 		   NULL, wacom_btnimg##BUTTON_ID##_store)
869471d1714SBenjamin Tissoires 
870471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(0);
871471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(1);
872471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(2);
873471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(3);
874471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(4);
875471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(5);
876471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(6);
877471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(7);
878471d1714SBenjamin Tissoires 
879471d1714SBenjamin Tissoires static struct attribute *cintiq_led_attrs[] = {
880471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
881471d1714SBenjamin Tissoires 	&dev_attr_status_led1_select.attr,
882471d1714SBenjamin Tissoires 	NULL
883471d1714SBenjamin Tissoires };
884471d1714SBenjamin Tissoires 
885471d1714SBenjamin Tissoires static struct attribute_group cintiq_led_attr_group = {
886471d1714SBenjamin Tissoires 	.name = "wacom_led",
887471d1714SBenjamin Tissoires 	.attrs = cintiq_led_attrs,
888471d1714SBenjamin Tissoires };
889471d1714SBenjamin Tissoires 
890471d1714SBenjamin Tissoires static struct attribute *intuos4_led_attrs[] = {
891471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
892471d1714SBenjamin Tissoires 	&dev_attr_status1_luminance.attr,
893471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
894471d1714SBenjamin Tissoires 	&dev_attr_buttons_luminance.attr,
895471d1714SBenjamin Tissoires 	&dev_attr_button0_rawimg.attr,
896471d1714SBenjamin Tissoires 	&dev_attr_button1_rawimg.attr,
897471d1714SBenjamin Tissoires 	&dev_attr_button2_rawimg.attr,
898471d1714SBenjamin Tissoires 	&dev_attr_button3_rawimg.attr,
899471d1714SBenjamin Tissoires 	&dev_attr_button4_rawimg.attr,
900471d1714SBenjamin Tissoires 	&dev_attr_button5_rawimg.attr,
901471d1714SBenjamin Tissoires 	&dev_attr_button6_rawimg.attr,
902471d1714SBenjamin Tissoires 	&dev_attr_button7_rawimg.attr,
903471d1714SBenjamin Tissoires 	NULL
904471d1714SBenjamin Tissoires };
905471d1714SBenjamin Tissoires 
906471d1714SBenjamin Tissoires static struct attribute_group intuos4_led_attr_group = {
907471d1714SBenjamin Tissoires 	.name = "wacom_led",
908471d1714SBenjamin Tissoires 	.attrs = intuos4_led_attrs,
909471d1714SBenjamin Tissoires };
910471d1714SBenjamin Tissoires 
911471d1714SBenjamin Tissoires static struct attribute *intuos5_led_attrs[] = {
912471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
913471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
914471d1714SBenjamin Tissoires 	NULL
915471d1714SBenjamin Tissoires };
916471d1714SBenjamin Tissoires 
917471d1714SBenjamin Tissoires static struct attribute_group intuos5_led_attr_group = {
918471d1714SBenjamin Tissoires 	.name = "wacom_led",
919471d1714SBenjamin Tissoires 	.attrs = intuos5_led_attrs,
920471d1714SBenjamin Tissoires };
921471d1714SBenjamin Tissoires 
9222df68a88SBenjamin Tissoires struct wacom_sysfs_group_devres {
9232df68a88SBenjamin Tissoires 	struct attribute_group *group;
9242df68a88SBenjamin Tissoires 	struct kobject *root;
9252df68a88SBenjamin Tissoires };
9262df68a88SBenjamin Tissoires 
9272df68a88SBenjamin Tissoires static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
9282df68a88SBenjamin Tissoires {
9292df68a88SBenjamin Tissoires 	struct wacom_sysfs_group_devres *devres = res;
9302df68a88SBenjamin Tissoires 	struct kobject *kobj = devres->root;
9312df68a88SBenjamin Tissoires 
9322df68a88SBenjamin Tissoires 	dev_dbg(dev, "%s: dropping reference to %s\n",
9332df68a88SBenjamin Tissoires 		__func__, devres->group->name);
9342df68a88SBenjamin Tissoires 	sysfs_remove_group(kobj, devres->group);
9352df68a88SBenjamin Tissoires }
9362df68a88SBenjamin Tissoires 
9372df68a88SBenjamin Tissoires static int wacom_devm_sysfs_create_group(struct wacom *wacom,
9382df68a88SBenjamin Tissoires 					 struct attribute_group *group)
9392df68a88SBenjamin Tissoires {
9402df68a88SBenjamin Tissoires 	struct wacom_sysfs_group_devres *devres;
9412df68a88SBenjamin Tissoires 	int error;
9422df68a88SBenjamin Tissoires 
9432df68a88SBenjamin Tissoires 	devres = devres_alloc(wacom_devm_sysfs_group_release,
9442df68a88SBenjamin Tissoires 			      sizeof(struct wacom_sysfs_group_devres),
9452df68a88SBenjamin Tissoires 			      GFP_KERNEL);
9462df68a88SBenjamin Tissoires 	if (!devres)
9472df68a88SBenjamin Tissoires 		return -ENOMEM;
9482df68a88SBenjamin Tissoires 
9492df68a88SBenjamin Tissoires 	devres->group = group;
9502df68a88SBenjamin Tissoires 	devres->root = &wacom->hdev->dev.kobj;
9512df68a88SBenjamin Tissoires 
9522df68a88SBenjamin Tissoires 	error = sysfs_create_group(devres->root, group);
9532df68a88SBenjamin Tissoires 	if (error)
9542df68a88SBenjamin Tissoires 		return error;
9552df68a88SBenjamin Tissoires 
9562df68a88SBenjamin Tissoires 	devres_add(&wacom->hdev->dev, devres);
9572df68a88SBenjamin Tissoires 
9582df68a88SBenjamin Tissoires 	return 0;
9592df68a88SBenjamin Tissoires }
9602df68a88SBenjamin Tissoires 
961a50aac71SBenjamin Tissoires static void wacom_led_groups_release(void *data)
962a50aac71SBenjamin Tissoires {
963a50aac71SBenjamin Tissoires 	struct wacom *wacom = data;
964a50aac71SBenjamin Tissoires 
965a50aac71SBenjamin Tissoires 	wacom->led.groups = NULL;
966a50aac71SBenjamin Tissoires }
967a50aac71SBenjamin Tissoires 
968a50aac71SBenjamin Tissoires static int wacom_led_groups_allocate(struct wacom *wacom, int count)
969a50aac71SBenjamin Tissoires {
970a50aac71SBenjamin Tissoires 	struct wacom_group_leds *groups;
971a50aac71SBenjamin Tissoires 	int error;
972a50aac71SBenjamin Tissoires 
973a50aac71SBenjamin Tissoires 	groups = devm_kzalloc(&wacom->hdev->dev,
974a50aac71SBenjamin Tissoires 			      sizeof(struct wacom_group_leds) * count,
975a50aac71SBenjamin Tissoires 			      GFP_KERNEL);
976a50aac71SBenjamin Tissoires 	if (!groups)
977a50aac71SBenjamin Tissoires 		return -ENOMEM;
978a50aac71SBenjamin Tissoires 
979a50aac71SBenjamin Tissoires 	error = devm_add_action_or_reset(&wacom->hdev->dev,
980a50aac71SBenjamin Tissoires 					 wacom_led_groups_release,
981a50aac71SBenjamin Tissoires 					 wacom);
982a50aac71SBenjamin Tissoires 	if (error)
983a50aac71SBenjamin Tissoires 		return error;
984a50aac71SBenjamin Tissoires 
985a50aac71SBenjamin Tissoires 	wacom->led.groups = groups;
986a50aac71SBenjamin Tissoires 
987a50aac71SBenjamin Tissoires 	return 0;
988a50aac71SBenjamin Tissoires }
989a50aac71SBenjamin Tissoires 
990471d1714SBenjamin Tissoires static int wacom_initialize_leds(struct wacom *wacom)
991471d1714SBenjamin Tissoires {
992471d1714SBenjamin Tissoires 	int error;
993471d1714SBenjamin Tissoires 
994862cf553SJason Gerecke 	if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
995862cf553SJason Gerecke 		return 0;
996862cf553SJason Gerecke 
997471d1714SBenjamin Tissoires 	/* Initialize default values */
998471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
999471d1714SBenjamin Tissoires 	case INTUOS4S:
1000471d1714SBenjamin Tissoires 	case INTUOS4:
100181af7e61SBenjamin Tissoires 	case INTUOS4WL:
1002471d1714SBenjamin Tissoires 	case INTUOS4L:
1003471d1714SBenjamin Tissoires 		wacom->led.llv = 10;
1004471d1714SBenjamin Tissoires 		wacom->led.hlv = 20;
1005471d1714SBenjamin Tissoires 		wacom->led.img_lum = 10;
1006a50aac71SBenjamin Tissoires 
1007a50aac71SBenjamin Tissoires 		error = wacom_led_groups_allocate(wacom, 1);
1008a50aac71SBenjamin Tissoires 		if (error) {
1009a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1010a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1011a50aac71SBenjamin Tissoires 			return error;
1012a50aac71SBenjamin Tissoires 		}
1013a50aac71SBenjamin Tissoires 
10142df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
1015471d1714SBenjamin Tissoires 						      &intuos4_led_attr_group);
1016471d1714SBenjamin Tissoires 		break;
1017471d1714SBenjamin Tissoires 
1018471d1714SBenjamin Tissoires 	case WACOM_24HD:
1019471d1714SBenjamin Tissoires 	case WACOM_21UX2:
1020471d1714SBenjamin Tissoires 		wacom->led.llv = 0;
1021471d1714SBenjamin Tissoires 		wacom->led.hlv = 0;
1022471d1714SBenjamin Tissoires 		wacom->led.img_lum = 0;
1023471d1714SBenjamin Tissoires 
1024a50aac71SBenjamin Tissoires 		error = wacom_led_groups_allocate(wacom, 2);
1025a50aac71SBenjamin Tissoires 		if (error) {
1026a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1027a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1028a50aac71SBenjamin Tissoires 			return error;
1029a50aac71SBenjamin Tissoires 		}
1030a50aac71SBenjamin Tissoires 
10312df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
1032471d1714SBenjamin Tissoires 						      &cintiq_led_attr_group);
1033471d1714SBenjamin Tissoires 		break;
1034471d1714SBenjamin Tissoires 
1035471d1714SBenjamin Tissoires 	case INTUOS5S:
1036471d1714SBenjamin Tissoires 	case INTUOS5:
1037471d1714SBenjamin Tissoires 	case INTUOS5L:
1038471d1714SBenjamin Tissoires 	case INTUOSPS:
1039471d1714SBenjamin Tissoires 	case INTUOSPM:
1040471d1714SBenjamin Tissoires 	case INTUOSPL:
1041471d1714SBenjamin Tissoires 		wacom->led.llv = 32;
1042471d1714SBenjamin Tissoires 		wacom->led.hlv = 0;
1043471d1714SBenjamin Tissoires 		wacom->led.img_lum = 0;
1044471d1714SBenjamin Tissoires 
1045a50aac71SBenjamin Tissoires 		error = wacom_led_groups_allocate(wacom, 1);
1046a50aac71SBenjamin Tissoires 		if (error) {
1047a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1048a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1049a50aac71SBenjamin Tissoires 			return error;
1050a50aac71SBenjamin Tissoires 		}
1051a50aac71SBenjamin Tissoires 
10522df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
1053471d1714SBenjamin Tissoires 						      &intuos5_led_attr_group);
1054471d1714SBenjamin Tissoires 		break;
1055471d1714SBenjamin Tissoires 
1056a50aac71SBenjamin Tissoires 	case REMOTE:
1057a50aac71SBenjamin Tissoires 		error = wacom_led_groups_allocate(wacom, 5);
1058a50aac71SBenjamin Tissoires 		if (error) {
1059a50aac71SBenjamin Tissoires 			hid_err(wacom->hdev,
1060a50aac71SBenjamin Tissoires 				"cannot create leds err: %d\n", error);
1061a50aac71SBenjamin Tissoires 			return error;
1062a50aac71SBenjamin Tissoires 		}
1063a50aac71SBenjamin Tissoires 		return 0;
1064a50aac71SBenjamin Tissoires 
1065471d1714SBenjamin Tissoires 	default:
1066471d1714SBenjamin Tissoires 		return 0;
1067471d1714SBenjamin Tissoires 	}
1068471d1714SBenjamin Tissoires 
1069471d1714SBenjamin Tissoires 	if (error) {
1070471d1714SBenjamin Tissoires 		hid_err(wacom->hdev,
1071471d1714SBenjamin Tissoires 			"cannot create sysfs group err: %d\n", error);
1072471d1714SBenjamin Tissoires 		return error;
1073471d1714SBenjamin Tissoires 	}
1074471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
1075471d1714SBenjamin Tissoires 
1076471d1714SBenjamin Tissoires 	return 0;
1077471d1714SBenjamin Tissoires }
1078471d1714SBenjamin Tissoires 
1079471d1714SBenjamin Tissoires static enum power_supply_property wacom_battery_props[] = {
108071fa641eSJason Gerecke 	POWER_SUPPLY_PROP_PRESENT,
1081ac8d1010SBenjamin Tissoires 	POWER_SUPPLY_PROP_STATUS,
1082471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
1083471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_CAPACITY
1084471d1714SBenjamin Tissoires };
1085471d1714SBenjamin Tissoires 
10867dbd229eSBenjamin Tissoires static enum power_supply_property wacom_ac_props[] = {
10877dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_PRESENT,
10887dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_ONLINE,
10897dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
10907dbd229eSBenjamin Tissoires };
10917dbd229eSBenjamin Tissoires 
1092471d1714SBenjamin Tissoires static int wacom_battery_get_property(struct power_supply *psy,
1093471d1714SBenjamin Tissoires 				      enum power_supply_property psp,
1094471d1714SBenjamin Tissoires 				      union power_supply_propval *val)
1095471d1714SBenjamin Tissoires {
1096297d716fSKrzysztof Kozlowski 	struct wacom *wacom = power_supply_get_drvdata(psy);
1097471d1714SBenjamin Tissoires 	int ret = 0;
1098471d1714SBenjamin Tissoires 
1099471d1714SBenjamin Tissoires 	switch (psp) {
110071fa641eSJason Gerecke 		case POWER_SUPPLY_PROP_PRESENT:
110171fa641eSJason Gerecke 			val->intval = wacom->wacom_wac.bat_connected;
110271fa641eSJason Gerecke 			break;
1103471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_SCOPE:
1104471d1714SBenjamin Tissoires 			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1105471d1714SBenjamin Tissoires 			break;
1106471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_CAPACITY:
1107471d1714SBenjamin Tissoires 			val->intval =
1108ac8d1010SBenjamin Tissoires 				wacom->wacom_wac.battery_capacity;
1109ac8d1010SBenjamin Tissoires 			break;
1110ac8d1010SBenjamin Tissoires 		case POWER_SUPPLY_PROP_STATUS:
1111ac8d1010SBenjamin Tissoires 			if (wacom->wacom_wac.bat_charging)
1112ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_CHARGING;
1113ac8d1010SBenjamin Tissoires 			else if (wacom->wacom_wac.battery_capacity == 100 &&
1114ac8d1010SBenjamin Tissoires 				    wacom->wacom_wac.ps_connected)
1115ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_FULL;
1116b0882cb7SJason Gerecke 			else if (wacom->wacom_wac.ps_connected)
1117b0882cb7SJason Gerecke 				val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1118ac8d1010SBenjamin Tissoires 			else
1119ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1120471d1714SBenjamin Tissoires 			break;
1121471d1714SBenjamin Tissoires 		default:
1122471d1714SBenjamin Tissoires 			ret = -EINVAL;
1123471d1714SBenjamin Tissoires 			break;
1124471d1714SBenjamin Tissoires 	}
1125471d1714SBenjamin Tissoires 
1126471d1714SBenjamin Tissoires 	return ret;
1127471d1714SBenjamin Tissoires }
1128471d1714SBenjamin Tissoires 
11297dbd229eSBenjamin Tissoires static int wacom_ac_get_property(struct power_supply *psy,
11307dbd229eSBenjamin Tissoires 				enum power_supply_property psp,
11317dbd229eSBenjamin Tissoires 				union power_supply_propval *val)
11327dbd229eSBenjamin Tissoires {
1133297d716fSKrzysztof Kozlowski 	struct wacom *wacom = power_supply_get_drvdata(psy);
11347dbd229eSBenjamin Tissoires 	int ret = 0;
11357dbd229eSBenjamin Tissoires 
11367dbd229eSBenjamin Tissoires 	switch (psp) {
11377dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_PRESENT:
11387dbd229eSBenjamin Tissoires 		/* fall through */
11397dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_ONLINE:
11407dbd229eSBenjamin Tissoires 		val->intval = wacom->wacom_wac.ps_connected;
11417dbd229eSBenjamin Tissoires 		break;
11427dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_SCOPE:
11437dbd229eSBenjamin Tissoires 		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
11447dbd229eSBenjamin Tissoires 		break;
11457dbd229eSBenjamin Tissoires 	default:
11467dbd229eSBenjamin Tissoires 		ret = -EINVAL;
11477dbd229eSBenjamin Tissoires 		break;
11487dbd229eSBenjamin Tissoires 	}
11497dbd229eSBenjamin Tissoires 	return ret;
11507dbd229eSBenjamin Tissoires }
11517dbd229eSBenjamin Tissoires 
1152471d1714SBenjamin Tissoires static int wacom_initialize_battery(struct wacom *wacom)
1153471d1714SBenjamin Tissoires {
1154d70420b9SBenjamin Tissoires 	static atomic_t battery_no = ATOMIC_INIT(0);
1155b189da90SBenjamin Tissoires 	struct device *dev = &wacom->hdev->dev;
1156297d716fSKrzysztof Kozlowski 	struct power_supply_config psy_cfg = { .drv_data = wacom, };
1157b189da90SBenjamin Tissoires 	struct power_supply_desc *bat_desc = &wacom->battery_desc;
1158d70420b9SBenjamin Tissoires 	unsigned long n;
1159b189da90SBenjamin Tissoires 	int error;
1160b189da90SBenjamin Tissoires 
1161b189da90SBenjamin Tissoires 	if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1162b189da90SBenjamin Tissoires 		return -ENOMEM;
1163471d1714SBenjamin Tissoires 
1164ac8d1010SBenjamin Tissoires 	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
1165297d716fSKrzysztof Kozlowski 		struct power_supply_desc *ac_desc = &wacom->ac_desc;
1166d70420b9SBenjamin Tissoires 		n = atomic_inc_return(&battery_no) - 1;
11677dbd229eSBenjamin Tissoires 
1168297d716fSKrzysztof Kozlowski 		bat_desc->properties = wacom_battery_props;
1169297d716fSKrzysztof Kozlowski 		bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1170297d716fSKrzysztof Kozlowski 		bat_desc->get_property = wacom_battery_get_property;
1171d70420b9SBenjamin Tissoires 		sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
1172297d716fSKrzysztof Kozlowski 		bat_desc->name = wacom->wacom_wac.bat_name;
1173297d716fSKrzysztof Kozlowski 		bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1174297d716fSKrzysztof Kozlowski 		bat_desc->use_for_apm = 0;
1175471d1714SBenjamin Tissoires 
1176297d716fSKrzysztof Kozlowski 		ac_desc->properties = wacom_ac_props;
1177297d716fSKrzysztof Kozlowski 		ac_desc->num_properties = ARRAY_SIZE(wacom_ac_props);
1178297d716fSKrzysztof Kozlowski 		ac_desc->get_property = wacom_ac_get_property;
11797dbd229eSBenjamin Tissoires 		sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
1180297d716fSKrzysztof Kozlowski 		ac_desc->name = wacom->wacom_wac.ac_name;
1181297d716fSKrzysztof Kozlowski 		ac_desc->type = POWER_SUPPLY_TYPE_MAINS;
1182297d716fSKrzysztof Kozlowski 		ac_desc->use_for_apm = 0;
11837dbd229eSBenjamin Tissoires 
1184b189da90SBenjamin Tissoires 		wacom->battery = devm_power_supply_register(dev,
1185b189da90SBenjamin Tissoires 							   &wacom->battery_desc,
1186b189da90SBenjamin Tissoires 							   &psy_cfg);
1187b189da90SBenjamin Tissoires 		if (IS_ERR(wacom->battery)) {
1188b189da90SBenjamin Tissoires 			error = PTR_ERR(wacom->battery);
1189b189da90SBenjamin Tissoires 			goto err;
1190b189da90SBenjamin Tissoires 		}
1191471d1714SBenjamin Tissoires 
1192297d716fSKrzysztof Kozlowski 		power_supply_powers(wacom->battery, &wacom->hdev->dev);
11937dbd229eSBenjamin Tissoires 
1194b189da90SBenjamin Tissoires 		wacom->ac = devm_power_supply_register(dev,
1195297d716fSKrzysztof Kozlowski 						       &wacom->ac_desc,
1196297d716fSKrzysztof Kozlowski 						       &psy_cfg);
1197297d716fSKrzysztof Kozlowski 		if (IS_ERR(wacom->ac)) {
1198b189da90SBenjamin Tissoires 			error = PTR_ERR(wacom->ac);
1199b189da90SBenjamin Tissoires 			goto err;
1200471d1714SBenjamin Tissoires 		}
1201471d1714SBenjamin Tissoires 
1202297d716fSKrzysztof Kozlowski 		power_supply_powers(wacom->ac, &wacom->hdev->dev);
12037dbd229eSBenjamin Tissoires 	}
12047dbd229eSBenjamin Tissoires 
1205b189da90SBenjamin Tissoires 	devres_close_group(dev, bat_desc);
12067dbd229eSBenjamin Tissoires 	return 0;
1207b189da90SBenjamin Tissoires 
1208b189da90SBenjamin Tissoires err:
1209b189da90SBenjamin Tissoires 	devres_release_group(dev, bat_desc);
1210b189da90SBenjamin Tissoires 	return error;
1211471d1714SBenjamin Tissoires }
1212471d1714SBenjamin Tissoires 
1213471d1714SBenjamin Tissoires static void wacom_destroy_battery(struct wacom *wacom)
1214471d1714SBenjamin Tissoires {
12158de29a35SLinus Torvalds 	if (wacom->battery) {
1216b189da90SBenjamin Tissoires 		devres_release_group(&wacom->hdev->dev, &wacom->battery_desc);
1217297d716fSKrzysztof Kozlowski 		wacom->battery = NULL;
1218297d716fSKrzysztof Kozlowski 		wacom->ac = NULL;
1219471d1714SBenjamin Tissoires 	}
1220471d1714SBenjamin Tissoires }
1221471d1714SBenjamin Tissoires 
1222f81a1295SBenjamin Tissoires static ssize_t wacom_show_speed(struct device *dev,
1223f81a1295SBenjamin Tissoires 				struct device_attribute
1224f81a1295SBenjamin Tissoires 				*attr, char *buf)
1225f81a1295SBenjamin Tissoires {
1226ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1227f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1228f81a1295SBenjamin Tissoires 
1229f81a1295SBenjamin Tissoires 	return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1230f81a1295SBenjamin Tissoires }
1231f81a1295SBenjamin Tissoires 
1232f81a1295SBenjamin Tissoires static ssize_t wacom_store_speed(struct device *dev,
1233f81a1295SBenjamin Tissoires 				struct device_attribute *attr,
1234f81a1295SBenjamin Tissoires 				const char *buf, size_t count)
1235f81a1295SBenjamin Tissoires {
1236ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1237f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1238f81a1295SBenjamin Tissoires 	u8 new_speed;
1239f81a1295SBenjamin Tissoires 
1240f81a1295SBenjamin Tissoires 	if (kstrtou8(buf, 0, &new_speed))
1241f81a1295SBenjamin Tissoires 		return -EINVAL;
1242f81a1295SBenjamin Tissoires 
1243f81a1295SBenjamin Tissoires 	if (new_speed != 0 && new_speed != 1)
1244f81a1295SBenjamin Tissoires 		return -EINVAL;
1245f81a1295SBenjamin Tissoires 
1246f81a1295SBenjamin Tissoires 	wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1247f81a1295SBenjamin Tissoires 
1248f81a1295SBenjamin Tissoires 	return count;
1249f81a1295SBenjamin Tissoires }
1250f81a1295SBenjamin Tissoires 
1251e0984bc3SPing Cheng static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1252f81a1295SBenjamin Tissoires 		wacom_show_speed, wacom_store_speed);
1253f81a1295SBenjamin Tissoires 
125472b236d6SAaron Skomra 
125572b236d6SAaron Skomra static ssize_t wacom_show_remote_mode(struct kobject *kobj,
125672b236d6SAaron Skomra 				      struct kobj_attribute *kattr,
125772b236d6SAaron Skomra 				      char *buf, int index)
125872b236d6SAaron Skomra {
12592cf83833SGeliang Tang 	struct device *dev = kobj_to_dev(kobj->parent);
1260ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
126172b236d6SAaron Skomra 	struct wacom *wacom = hid_get_drvdata(hdev);
126272b236d6SAaron Skomra 	u8 mode;
126372b236d6SAaron Skomra 
1264a50aac71SBenjamin Tissoires 	mode = wacom->led.groups[index].select;
126572b236d6SAaron Skomra 	if (mode >= 0 && mode < 3)
126672b236d6SAaron Skomra 		return snprintf(buf, PAGE_SIZE, "%d\n", mode);
126772b236d6SAaron Skomra 	else
126872b236d6SAaron Skomra 		return snprintf(buf, PAGE_SIZE, "%d\n", -1);
126972b236d6SAaron Skomra }
127072b236d6SAaron Skomra 
127172b236d6SAaron Skomra #define DEVICE_EKR_ATTR_GROUP(SET_ID)					\
127272b236d6SAaron Skomra static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj,	\
127372b236d6SAaron Skomra 			       struct kobj_attribute *kattr, char *buf)	\
127472b236d6SAaron Skomra {									\
127572b236d6SAaron Skomra 	return wacom_show_remote_mode(kobj, kattr, buf, SET_ID);	\
127672b236d6SAaron Skomra }									\
127772b236d6SAaron Skomra static struct kobj_attribute remote##SET_ID##_mode_attr = {		\
127872b236d6SAaron Skomra 	.attr = {.name = "remote_mode",					\
127972b236d6SAaron Skomra 		.mode = DEV_ATTR_RO_PERM},				\
128072b236d6SAaron Skomra 	.show = wacom_show_remote##SET_ID##_mode,			\
128172b236d6SAaron Skomra };									\
128272b236d6SAaron Skomra static struct attribute *remote##SET_ID##_serial_attrs[] = {		\
128372b236d6SAaron Skomra 	&remote##SET_ID##_mode_attr.attr,				\
128472b236d6SAaron Skomra 	NULL								\
128572b236d6SAaron Skomra };									\
128672b236d6SAaron Skomra static struct attribute_group remote##SET_ID##_serial_group = {		\
128772b236d6SAaron Skomra 	.name = NULL,							\
128872b236d6SAaron Skomra 	.attrs = remote##SET_ID##_serial_attrs,				\
128972b236d6SAaron Skomra }
129072b236d6SAaron Skomra 
129172b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(0);
129272b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(1);
129372b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(2);
129472b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(3);
129572b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(4);
129672b236d6SAaron Skomra 
1297e6f2813aSBenjamin Tissoires static int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial,
1298e6f2813aSBenjamin Tissoires 					  int index)
129972b236d6SAaron Skomra {
130072b236d6SAaron Skomra 	int error = 0;
1301*83e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
130272b236d6SAaron Skomra 
1303*83e6b40eSBenjamin Tissoires 	remote->serial[index] = serial;
130472b236d6SAaron Skomra 
1305*83e6b40eSBenjamin Tissoires 	remote->remote_group[index].name = devm_kasprintf(&wacom->hdev->dev,
1306c1f5409bSBenjamin Tissoires 							  GFP_KERNEL,
1307c1f5409bSBenjamin Tissoires 							  "%d", serial);
1308*83e6b40eSBenjamin Tissoires 	if (!remote->remote_group[index].name)
130972b236d6SAaron Skomra 		return -ENOMEM;
131072b236d6SAaron Skomra 
1311*83e6b40eSBenjamin Tissoires 	error = sysfs_create_group(remote->remote_dir,
1312*83e6b40eSBenjamin Tissoires 				   &remote->remote_group[index]);
131372b236d6SAaron Skomra 	if (error) {
131472b236d6SAaron Skomra 		hid_err(wacom->hdev,
131572b236d6SAaron Skomra 			"cannot create sysfs group err: %d\n", error);
131672b236d6SAaron Skomra 		return error;
131772b236d6SAaron Skomra 	}
131872b236d6SAaron Skomra 
131972b236d6SAaron Skomra 	return 0;
132072b236d6SAaron Skomra }
132172b236d6SAaron Skomra 
1322e6f2813aSBenjamin Tissoires static void wacom_remote_destroy_attr_group(struct wacom *wacom, __u32 serial)
132372b236d6SAaron Skomra {
1324*83e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
132572b236d6SAaron Skomra 	int i;
132672b236d6SAaron Skomra 
132772b236d6SAaron Skomra 	if (!serial)
132872b236d6SAaron Skomra 		return;
132972b236d6SAaron Skomra 
133072b236d6SAaron Skomra 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1331*83e6b40eSBenjamin Tissoires 		if (remote->serial[i] == serial) {
1332*83e6b40eSBenjamin Tissoires 			remote->serial[i] = 0;
1333a50aac71SBenjamin Tissoires 			wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
1334*83e6b40eSBenjamin Tissoires 			if (remote->remote_group[i].name) {
1335*83e6b40eSBenjamin Tissoires 				sysfs_remove_group(remote->remote_dir,
1336*83e6b40eSBenjamin Tissoires 						   &remote->remote_group[i]);
1337c1f5409bSBenjamin Tissoires 				devm_kfree(&wacom->hdev->dev,
1338*83e6b40eSBenjamin Tissoires 					  (char *)remote->remote_group[i].name);
1339*83e6b40eSBenjamin Tissoires 				remote->remote_group[i].name = NULL;
134072b236d6SAaron Skomra 			}
134172b236d6SAaron Skomra 		}
134272b236d6SAaron Skomra 	}
134372b236d6SAaron Skomra }
134472b236d6SAaron Skomra 
134572b236d6SAaron Skomra static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
134672b236d6SAaron Skomra {
134772b236d6SAaron Skomra 	const size_t buf_size = 2;
134872b236d6SAaron Skomra 	unsigned char *buf;
134972b236d6SAaron Skomra 	int retval;
135072b236d6SAaron Skomra 
135172b236d6SAaron Skomra 	buf = kzalloc(buf_size, GFP_KERNEL);
135272b236d6SAaron Skomra 	if (!buf)
135372b236d6SAaron Skomra 		return -ENOMEM;
135472b236d6SAaron Skomra 
135572b236d6SAaron Skomra 	buf[0] = WAC_CMD_DELETE_PAIRING;
135672b236d6SAaron Skomra 	buf[1] = selector;
135772b236d6SAaron Skomra 
135872b236d6SAaron Skomra 	retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
135972b236d6SAaron Skomra 				  buf_size, WAC_CMD_RETRIES);
136072b236d6SAaron Skomra 	kfree(buf);
136172b236d6SAaron Skomra 
136272b236d6SAaron Skomra 	return retval;
136372b236d6SAaron Skomra }
136472b236d6SAaron Skomra 
136572b236d6SAaron Skomra static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
136672b236d6SAaron Skomra 					 struct kobj_attribute *attr,
136772b236d6SAaron Skomra 					 const char *buf, size_t count)
136872b236d6SAaron Skomra {
136972b236d6SAaron Skomra 	unsigned char selector = 0;
13702cf83833SGeliang Tang 	struct device *dev = kobj_to_dev(kobj->parent);
1371ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
137272b236d6SAaron Skomra 	struct wacom *wacom = hid_get_drvdata(hdev);
137372b236d6SAaron Skomra 	int err;
137472b236d6SAaron Skomra 
137572b236d6SAaron Skomra 	if (!strncmp(buf, "*\n", 2)) {
137672b236d6SAaron Skomra 		selector = WAC_CMD_UNPAIR_ALL;
137772b236d6SAaron Skomra 	} else {
137872b236d6SAaron Skomra 		hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
137972b236d6SAaron Skomra 			 buf);
138072b236d6SAaron Skomra 		return -1;
138172b236d6SAaron Skomra 	}
138272b236d6SAaron Skomra 
138372b236d6SAaron Skomra 	mutex_lock(&wacom->lock);
138472b236d6SAaron Skomra 
138572b236d6SAaron Skomra 	err = wacom_cmd_unpair_remote(wacom, selector);
138672b236d6SAaron Skomra 	mutex_unlock(&wacom->lock);
138772b236d6SAaron Skomra 
138872b236d6SAaron Skomra 	return err < 0 ? err : count;
138972b236d6SAaron Skomra }
139072b236d6SAaron Skomra 
139172b236d6SAaron Skomra static struct kobj_attribute unpair_remote_attr = {
139272b236d6SAaron Skomra 	.attr = {.name = "unpair_remote", .mode = 0200},
139372b236d6SAaron Skomra 	.store = wacom_store_unpair_remote,
139472b236d6SAaron Skomra };
139572b236d6SAaron Skomra 
139672b236d6SAaron Skomra static const struct attribute *remote_unpair_attrs[] = {
139772b236d6SAaron Skomra 	&unpair_remote_attr.attr,
139872b236d6SAaron Skomra 	NULL
139972b236d6SAaron Skomra };
140072b236d6SAaron Skomra 
1401*83e6b40eSBenjamin Tissoires static void wacom_remotes_destroy(void *data)
1402*83e6b40eSBenjamin Tissoires {
1403*83e6b40eSBenjamin Tissoires 	struct wacom *wacom = data;
1404*83e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
1405*83e6b40eSBenjamin Tissoires 
1406*83e6b40eSBenjamin Tissoires 	if (!remote)
1407*83e6b40eSBenjamin Tissoires 		return;
1408*83e6b40eSBenjamin Tissoires 
1409*83e6b40eSBenjamin Tissoires 	kobject_put(remote->remote_dir);
1410*83e6b40eSBenjamin Tissoires 	kfifo_free(&remote->remote_fifo);
1411*83e6b40eSBenjamin Tissoires 	wacom->remote = NULL;
1412*83e6b40eSBenjamin Tissoires }
1413*83e6b40eSBenjamin Tissoires 
1414*83e6b40eSBenjamin Tissoires static int wacom_initialize_remotes(struct wacom *wacom)
141572b236d6SAaron Skomra {
141672b236d6SAaron Skomra 	int error = 0;
1417*83e6b40eSBenjamin Tissoires 	struct wacom_remote *remote;
141872b236d6SAaron Skomra 	int i;
141972b236d6SAaron Skomra 
142072b236d6SAaron Skomra 	if (wacom->wacom_wac.features.type != REMOTE)
142172b236d6SAaron Skomra 		return 0;
142272b236d6SAaron Skomra 
1423*83e6b40eSBenjamin Tissoires 	remote = devm_kzalloc(&wacom->hdev->dev, sizeof(*wacom->remote),
1424*83e6b40eSBenjamin Tissoires 			      GFP_KERNEL);
1425*83e6b40eSBenjamin Tissoires 	if (!remote)
142672b236d6SAaron Skomra 		return -ENOMEM;
142772b236d6SAaron Skomra 
1428*83e6b40eSBenjamin Tissoires 	wacom->remote = remote;
1429*83e6b40eSBenjamin Tissoires 
1430*83e6b40eSBenjamin Tissoires 	spin_lock_init(&remote->remote_lock);
1431*83e6b40eSBenjamin Tissoires 
1432*83e6b40eSBenjamin Tissoires 	error = kfifo_alloc(&remote->remote_fifo,
1433*83e6b40eSBenjamin Tissoires 			5 * sizeof(struct wacom_remote_data),
1434*83e6b40eSBenjamin Tissoires 			GFP_KERNEL);
1435*83e6b40eSBenjamin Tissoires 	if (error) {
1436*83e6b40eSBenjamin Tissoires 		hid_err(wacom->hdev, "failed allocating remote_fifo\n");
1437*83e6b40eSBenjamin Tissoires 		return -ENOMEM;
1438*83e6b40eSBenjamin Tissoires 	}
1439*83e6b40eSBenjamin Tissoires 
1440*83e6b40eSBenjamin Tissoires 	remote->remote_group[0] = remote0_serial_group;
1441*83e6b40eSBenjamin Tissoires 	remote->remote_group[1] = remote1_serial_group;
1442*83e6b40eSBenjamin Tissoires 	remote->remote_group[2] = remote2_serial_group;
1443*83e6b40eSBenjamin Tissoires 	remote->remote_group[3] = remote3_serial_group;
1444*83e6b40eSBenjamin Tissoires 	remote->remote_group[4] = remote4_serial_group;
1445*83e6b40eSBenjamin Tissoires 
1446*83e6b40eSBenjamin Tissoires 	remote->remote_dir = kobject_create_and_add("wacom_remote",
1447*83e6b40eSBenjamin Tissoires 						    &wacom->hdev->dev.kobj);
1448*83e6b40eSBenjamin Tissoires 	if (!remote->remote_dir)
1449*83e6b40eSBenjamin Tissoires 		return -ENOMEM;
1450*83e6b40eSBenjamin Tissoires 
1451*83e6b40eSBenjamin Tissoires 	error = sysfs_create_files(remote->remote_dir, remote_unpair_attrs);
145272b236d6SAaron Skomra 
145372b236d6SAaron Skomra 	if (error) {
145472b236d6SAaron Skomra 		hid_err(wacom->hdev,
145572b236d6SAaron Skomra 			"cannot create sysfs group err: %d\n", error);
145672b236d6SAaron Skomra 		return error;
145772b236d6SAaron Skomra 	}
145872b236d6SAaron Skomra 
145972b236d6SAaron Skomra 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1460a50aac71SBenjamin Tissoires 		wacom->led.groups[i].select = WACOM_STATUS_UNKNOWN;
1461*83e6b40eSBenjamin Tissoires 		remote->serial[i] = 0;
146272b236d6SAaron Skomra 	}
146372b236d6SAaron Skomra 
1464*83e6b40eSBenjamin Tissoires 	error = devm_add_action_or_reset(&wacom->hdev->dev,
1465*83e6b40eSBenjamin Tissoires 					 wacom_remotes_destroy, wacom);
1466*83e6b40eSBenjamin Tissoires 	if (error)
1467*83e6b40eSBenjamin Tissoires 		return error;
1468*83e6b40eSBenjamin Tissoires 
146972b236d6SAaron Skomra 	return 0;
147072b236d6SAaron Skomra }
147172b236d6SAaron Skomra 
1472471d1714SBenjamin Tissoires static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1473471d1714SBenjamin Tissoires {
1474471d1714SBenjamin Tissoires 	struct input_dev *input_dev;
1475471d1714SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
1476471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1477471d1714SBenjamin Tissoires 
14783dad188eSBenjamin Tissoires 	input_dev = devm_input_allocate_device(&hdev->dev);
1479471d1714SBenjamin Tissoires 	if (!input_dev)
1480471d1714SBenjamin Tissoires 		return NULL;
1481471d1714SBenjamin Tissoires 
14822bdd163cSJason Gerecke 	input_dev->name = wacom_wac->features.name;
1483471d1714SBenjamin Tissoires 	input_dev->phys = hdev->phys;
1484471d1714SBenjamin Tissoires 	input_dev->dev.parent = &hdev->dev;
1485471d1714SBenjamin Tissoires 	input_dev->open = wacom_open;
1486471d1714SBenjamin Tissoires 	input_dev->close = wacom_close;
1487471d1714SBenjamin Tissoires 	input_dev->uniq = hdev->uniq;
1488471d1714SBenjamin Tissoires 	input_dev->id.bustype = hdev->bus;
1489471d1714SBenjamin Tissoires 	input_dev->id.vendor  = hdev->vendor;
149012969e3bSBenjamin Tissoires 	input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1491471d1714SBenjamin Tissoires 	input_dev->id.version = hdev->version;
1492471d1714SBenjamin Tissoires 	input_set_drvdata(input_dev, wacom);
1493471d1714SBenjamin Tissoires 
1494471d1714SBenjamin Tissoires 	return input_dev;
1495471d1714SBenjamin Tissoires }
1496471d1714SBenjamin Tissoires 
1497d9f2d203SJason Gerecke static int wacom_allocate_inputs(struct wacom *wacom)
1498d9f2d203SJason Gerecke {
1499d9f2d203SJason Gerecke 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1500d9f2d203SJason Gerecke 
1501d9f2d203SJason Gerecke 	wacom_wac->pen_input = wacom_allocate_input(wacom);
1502d9f2d203SJason Gerecke 	wacom_wac->touch_input = wacom_allocate_input(wacom);
1503d9f2d203SJason Gerecke 	wacom_wac->pad_input = wacom_allocate_input(wacom);
15043dad188eSBenjamin Tissoires 	if (!wacom_wac->pen_input ||
15053dad188eSBenjamin Tissoires 	    !wacom_wac->touch_input ||
15063dad188eSBenjamin Tissoires 	    !wacom_wac->pad_input)
1507d9f2d203SJason Gerecke 		return -ENOMEM;
1508d9f2d203SJason Gerecke 
15092bdd163cSJason Gerecke 	wacom_wac->pen_input->name = wacom_wac->pen_name;
1510d9f2d203SJason Gerecke 	wacom_wac->touch_input->name = wacom_wac->touch_name;
1511d9f2d203SJason Gerecke 	wacom_wac->pad_input->name = wacom_wac->pad_name;
1512d9f2d203SJason Gerecke 
1513d9f2d203SJason Gerecke 	return 0;
1514d9f2d203SJason Gerecke }
1515d9f2d203SJason Gerecke 
1516471d1714SBenjamin Tissoires static int wacom_register_inputs(struct wacom *wacom)
1517471d1714SBenjamin Tissoires {
15182a6cdbddSJason Gerecke 	struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
1519471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
15202636a3f2SJason Gerecke 	int error = 0;
1521471d1714SBenjamin Tissoires 
15222a6cdbddSJason Gerecke 	pen_input_dev = wacom_wac->pen_input;
15232a6cdbddSJason Gerecke 	touch_input_dev = wacom_wac->touch_input;
15242546dacdSBenjamin Tissoires 	pad_input_dev = wacom_wac->pad_input;
1525471d1714SBenjamin Tissoires 
15262a6cdbddSJason Gerecke 	if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
15272546dacdSBenjamin Tissoires 		return -EINVAL;
1528471d1714SBenjamin Tissoires 
15292a6cdbddSJason Gerecke 	error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
15302a6cdbddSJason Gerecke 	if (error) {
15312a6cdbddSJason Gerecke 		/* no pen in use on this interface */
15322a6cdbddSJason Gerecke 		input_free_device(pen_input_dev);
15332a6cdbddSJason Gerecke 		wacom_wac->pen_input = NULL;
15342a6cdbddSJason Gerecke 		pen_input_dev = NULL;
15352a6cdbddSJason Gerecke 	} else {
15362a6cdbddSJason Gerecke 		error = input_register_device(pen_input_dev);
1537471d1714SBenjamin Tissoires 		if (error)
15383dad188eSBenjamin Tissoires 			goto fail;
15392a6cdbddSJason Gerecke 	}
15402a6cdbddSJason Gerecke 
15412a6cdbddSJason Gerecke 	error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
15422a6cdbddSJason Gerecke 	if (error) {
15432a6cdbddSJason Gerecke 		/* no touch in use on this interface */
15442a6cdbddSJason Gerecke 		input_free_device(touch_input_dev);
15452a6cdbddSJason Gerecke 		wacom_wac->touch_input = NULL;
15462a6cdbddSJason Gerecke 		touch_input_dev = NULL;
15472a6cdbddSJason Gerecke 	} else {
15482a6cdbddSJason Gerecke 		error = input_register_device(touch_input_dev);
15492a6cdbddSJason Gerecke 		if (error)
15503dad188eSBenjamin Tissoires 			goto fail;
155130ebc1aeSPing Cheng 	}
1552471d1714SBenjamin Tissoires 
1553471d1714SBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1554471d1714SBenjamin Tissoires 	if (error) {
1555471d1714SBenjamin Tissoires 		/* no pad in use on this interface */
1556471d1714SBenjamin Tissoires 		input_free_device(pad_input_dev);
1557471d1714SBenjamin Tissoires 		wacom_wac->pad_input = NULL;
1558471d1714SBenjamin Tissoires 		pad_input_dev = NULL;
1559471d1714SBenjamin Tissoires 	} else {
1560471d1714SBenjamin Tissoires 		error = input_register_device(pad_input_dev);
1561471d1714SBenjamin Tissoires 		if (error)
15623dad188eSBenjamin Tissoires 			goto fail;
1563471d1714SBenjamin Tissoires 	}
1564471d1714SBenjamin Tissoires 
1565471d1714SBenjamin Tissoires 	return 0;
1566471d1714SBenjamin Tissoires 
15673dad188eSBenjamin Tissoires fail:
15683dad188eSBenjamin Tissoires 	wacom_wac->pad_input = NULL;
15692a6cdbddSJason Gerecke 	wacom_wac->touch_input = NULL;
15702a6cdbddSJason Gerecke 	wacom_wac->pen_input = NULL;
1571471d1714SBenjamin Tissoires 	return error;
1572471d1714SBenjamin Tissoires }
1573471d1714SBenjamin Tissoires 
15740be01712SJason Gerecke /*
15750be01712SJason Gerecke  * Not all devices report physical dimensions from HID.
15760be01712SJason Gerecke  * Compute the default from hardcoded logical dimension
15770be01712SJason Gerecke  * and resolution before driver overwrites them.
15780be01712SJason Gerecke  */
15790be01712SJason Gerecke static void wacom_set_default_phy(struct wacom_features *features)
15800be01712SJason Gerecke {
15810be01712SJason Gerecke 	if (features->x_resolution) {
15820be01712SJason Gerecke 		features->x_phy = (features->x_max * 100) /
15830be01712SJason Gerecke 					features->x_resolution;
15840be01712SJason Gerecke 		features->y_phy = (features->y_max * 100) /
15850be01712SJason Gerecke 					features->y_resolution;
15860be01712SJason Gerecke 	}
15870be01712SJason Gerecke }
15880be01712SJason Gerecke 
15890be01712SJason Gerecke static void wacom_calculate_res(struct wacom_features *features)
15900be01712SJason Gerecke {
15910be01712SJason Gerecke 	/* set unit to "100th of a mm" for devices not reported by HID */
15920be01712SJason Gerecke 	if (!features->unit) {
15930be01712SJason Gerecke 		features->unit = 0x11;
15940be01712SJason Gerecke 		features->unitExpo = -3;
15950be01712SJason Gerecke 	}
15960be01712SJason Gerecke 
15970be01712SJason Gerecke 	features->x_resolution = wacom_calc_hid_res(features->x_max,
15980be01712SJason Gerecke 						    features->x_phy,
15990be01712SJason Gerecke 						    features->unit,
16000be01712SJason Gerecke 						    features->unitExpo);
16010be01712SJason Gerecke 	features->y_resolution = wacom_calc_hid_res(features->y_max,
16020be01712SJason Gerecke 						    features->y_phy,
16030be01712SJason Gerecke 						    features->unit,
16040be01712SJason Gerecke 						    features->unitExpo);
16050be01712SJason Gerecke }
16060be01712SJason Gerecke 
1607fce9957dSJason Gerecke void wacom_battery_work(struct work_struct *work)
1608fce9957dSJason Gerecke {
1609d17d1f17SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, battery_work);
1610fce9957dSJason Gerecke 
1611fce9957dSJason Gerecke 	if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
16128de29a35SLinus Torvalds 	     !wacom->battery) {
1613fce9957dSJason Gerecke 		wacom_initialize_battery(wacom);
1614fce9957dSJason Gerecke 	}
1615fce9957dSJason Gerecke 	else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
16168de29a35SLinus Torvalds 		 wacom->battery) {
1617fce9957dSJason Gerecke 		wacom_destroy_battery(wacom);
1618fce9957dSJason Gerecke 	}
1619fce9957dSJason Gerecke }
1620fce9957dSJason Gerecke 
1621471d1714SBenjamin Tissoires static size_t wacom_compute_pktlen(struct hid_device *hdev)
1622471d1714SBenjamin Tissoires {
1623471d1714SBenjamin Tissoires 	struct hid_report_enum *report_enum;
1624471d1714SBenjamin Tissoires 	struct hid_report *report;
1625471d1714SBenjamin Tissoires 	size_t size = 0;
1626471d1714SBenjamin Tissoires 
1627471d1714SBenjamin Tissoires 	report_enum = hdev->report_enum + HID_INPUT_REPORT;
1628471d1714SBenjamin Tissoires 
1629471d1714SBenjamin Tissoires 	list_for_each_entry(report, &report_enum->report_list, list) {
1630dabb05c6SMathieu Magnaudet 		size_t report_size = hid_report_len(report);
1631471d1714SBenjamin Tissoires 		if (report_size > size)
1632471d1714SBenjamin Tissoires 			size = report_size;
1633471d1714SBenjamin Tissoires 	}
1634471d1714SBenjamin Tissoires 
1635471d1714SBenjamin Tissoires 	return size;
1636471d1714SBenjamin Tissoires }
1637471d1714SBenjamin Tissoires 
1638fd5f92b6SBenjamin Tissoires static void wacom_update_name(struct wacom *wacom, const char *suffix)
1639c24eab4eSPing Cheng {
1640c24eab4eSPing Cheng 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1641c24eab4eSPing Cheng 	struct wacom_features *features = &wacom_wac->features;
164244b5250bSJason Gerecke 	char name[WACOM_NAME_MAX];
1643c24eab4eSPing Cheng 
1644c24eab4eSPing Cheng 	/* Generic devices name unspecified */
1645c24eab4eSPing Cheng 	if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1646c24eab4eSPing Cheng 		if (strstr(wacom->hdev->name, "Wacom") ||
1647c24eab4eSPing Cheng 		    strstr(wacom->hdev->name, "wacom") ||
1648c24eab4eSPing Cheng 		    strstr(wacom->hdev->name, "WACOM")) {
1649c24eab4eSPing Cheng 			/* name is in HID descriptor, use it */
165044b5250bSJason Gerecke 			strlcpy(name, wacom->hdev->name, sizeof(name));
1651c24eab4eSPing Cheng 
1652c24eab4eSPing Cheng 			/* strip out excess whitespaces */
1653c24eab4eSPing Cheng 			while (1) {
165444b5250bSJason Gerecke 				char *gap = strstr(name, "  ");
1655c24eab4eSPing Cheng 				if (gap == NULL)
1656c24eab4eSPing Cheng 					break;
1657c24eab4eSPing Cheng 				/* shift everything including the terminator */
1658c24eab4eSPing Cheng 				memmove(gap, gap+1, strlen(gap));
1659c24eab4eSPing Cheng 			}
1660c24eab4eSPing Cheng 			/* get rid of trailing whitespace */
166144b5250bSJason Gerecke 			if (name[strlen(name)-1] == ' ')
166244b5250bSJason Gerecke 				name[strlen(name)-1] = '\0';
1663c24eab4eSPing Cheng 		} else {
1664c24eab4eSPing Cheng 			/* no meaningful name retrieved. use product ID */
166544b5250bSJason Gerecke 			snprintf(name, sizeof(name),
1666c24eab4eSPing Cheng 				 "%s %X", features->name, wacom->hdev->product);
1667c24eab4eSPing Cheng 		}
1668c24eab4eSPing Cheng 	} else {
166944b5250bSJason Gerecke 		strlcpy(name, features->name, sizeof(name));
1670c24eab4eSPing Cheng 	}
1671c24eab4eSPing Cheng 
1672c24eab4eSPing Cheng 	/* Append the device type to the name */
16732a6cdbddSJason Gerecke 	snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
1674fd5f92b6SBenjamin Tissoires 		"%s%s Pen", name, suffix);
16752a6cdbddSJason Gerecke 	snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
1676fd5f92b6SBenjamin Tissoires 		"%s%s Finger", name, suffix);
1677c24eab4eSPing Cheng 	snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1678fd5f92b6SBenjamin Tissoires 		"%s%s Pad", name, suffix);
16798d80f790SJason Gerecke }
1680c24eab4eSPing Cheng 
168184dfbd7fSBenjamin Tissoires static void wacom_release_resources(struct wacom *wacom)
168284dfbd7fSBenjamin Tissoires {
168384dfbd7fSBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
168484dfbd7fSBenjamin Tissoires 
168584dfbd7fSBenjamin Tissoires 	if (!wacom->resources)
168684dfbd7fSBenjamin Tissoires 		return;
168784dfbd7fSBenjamin Tissoires 
168884dfbd7fSBenjamin Tissoires 	devres_release_group(&hdev->dev, wacom);
168984dfbd7fSBenjamin Tissoires 
169084dfbd7fSBenjamin Tissoires 	wacom->resources = false;
169184dfbd7fSBenjamin Tissoires 
169284dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.pen_input = NULL;
169384dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.touch_input = NULL;
169484dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.pad_input = NULL;
169584dfbd7fSBenjamin Tissoires }
169684dfbd7fSBenjamin Tissoires 
1697fd5f92b6SBenjamin Tissoires static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
1698471d1714SBenjamin Tissoires {
1699c58ac3a8SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1700c58ac3a8SBenjamin Tissoires 	struct wacom_features *features = &wacom_wac->features;
1701c58ac3a8SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
1702471d1714SBenjamin Tissoires 	int error;
17037704ac93SBenjamin Tissoires 	unsigned int connect_mask = HID_CONNECT_HIDRAW;
1704471d1714SBenjamin Tissoires 
1705471d1714SBenjamin Tissoires 	features->pktlen = wacom_compute_pktlen(hdev);
1706c58ac3a8SBenjamin Tissoires 	if (features->pktlen > WACOM_PKGLEN_MAX)
1707c58ac3a8SBenjamin Tissoires 		return -EINVAL;
1708471d1714SBenjamin Tissoires 
170984dfbd7fSBenjamin Tissoires 	if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
171084dfbd7fSBenjamin Tissoires 		return -ENOMEM;
171184dfbd7fSBenjamin Tissoires 
171284dfbd7fSBenjamin Tissoires 	wacom->resources = true;
171384dfbd7fSBenjamin Tissoires 
1714494078b0SBenjamin Tissoires 	error = wacom_allocate_inputs(wacom);
1715494078b0SBenjamin Tissoires 	if (error)
171684dfbd7fSBenjamin Tissoires 		goto fail_open_group;
1717494078b0SBenjamin Tissoires 
17188c97a765SBenjamin Tissoires 	/*
17198c97a765SBenjamin Tissoires 	 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
17208c97a765SBenjamin Tissoires 	 * into debug mode for the touch part.
17218c97a765SBenjamin Tissoires 	 * We ignore the other interfaces.
17228c97a765SBenjamin Tissoires 	 */
17238c97a765SBenjamin Tissoires 	if (features->type == BAMBOO_PAD) {
17248c97a765SBenjamin Tissoires 		if (features->pktlen == WACOM_PKGLEN_PENABLED) {
17258c97a765SBenjamin Tissoires 			features->type = HID_GENERIC;
17268c97a765SBenjamin Tissoires 		} else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
17278c97a765SBenjamin Tissoires 			   (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
17288c97a765SBenjamin Tissoires 			error = -ENODEV;
1729c58ac3a8SBenjamin Tissoires 			goto fail_allocate_inputs;
17308c97a765SBenjamin Tissoires 		}
17318c97a765SBenjamin Tissoires 	}
17328c97a765SBenjamin Tissoires 
1733471d1714SBenjamin Tissoires 	/* set the default size in case we do not get them from hid */
1734471d1714SBenjamin Tissoires 	wacom_set_default_phy(features);
1735471d1714SBenjamin Tissoires 
1736471d1714SBenjamin Tissoires 	/* Retrieve the physical and logical size for touch devices */
1737471d1714SBenjamin Tissoires 	wacom_retrieve_hid_descriptor(hdev, features);
173842f4f272SPing Cheng 	wacom_setup_device_quirks(wacom);
1739042628abSJason Gerecke 
1740aa86b18cSJason Gerecke 	if (features->device_type == WACOM_DEVICETYPE_NONE &&
1741aa86b18cSJason Gerecke 	    features->type != WIRELESS) {
17428e116d31SJason Gerecke 		error = features->type == HID_GENERIC ? -ENODEV : 0;
17438e116d31SJason Gerecke 
1744042628abSJason Gerecke 		dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
17458e116d31SJason Gerecke 			 hdev->name,
17468e116d31SJason Gerecke 			 error ? "Ignoring" : "Assuming pen");
17478e116d31SJason Gerecke 
17488e116d31SJason Gerecke 		if (error)
1749c58ac3a8SBenjamin Tissoires 			goto fail_parsed;
1750042628abSJason Gerecke 
1751aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_PEN;
1752042628abSJason Gerecke 	}
1753042628abSJason Gerecke 
1754471d1714SBenjamin Tissoires 	wacom_calculate_res(features);
1755471d1714SBenjamin Tissoires 
1756fd5f92b6SBenjamin Tissoires 	wacom_update_name(wacom, wireless ? " (WL)" : "");
1757471d1714SBenjamin Tissoires 
1758471d1714SBenjamin Tissoires 	error = wacom_add_shared_data(hdev);
1759471d1714SBenjamin Tissoires 	if (error)
17607fefeec5SBenjamin Tissoires 		goto fail_shared_data;
1761471d1714SBenjamin Tissoires 
1762ccad85ccSJason Gerecke 	if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
1763f81a1295SBenjamin Tissoires 	     (features->quirks & WACOM_QUIRK_BATTERY)) {
1764f81a1295SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1765f81a1295SBenjamin Tissoires 		if (error)
17667fefeec5SBenjamin Tissoires 			goto fail_battery;
1767f81a1295SBenjamin Tissoires 	}
1768f81a1295SBenjamin Tissoires 
1769494078b0SBenjamin Tissoires 	error = wacom_register_inputs(wacom);
1770471d1714SBenjamin Tissoires 	if (error)
17717fefeec5SBenjamin Tissoires 		goto fail_register_inputs;
1772f81a1295SBenjamin Tissoires 
1773b62f6465SBenjamin Tissoires 	if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
177485d2c77bSBenjamin Tissoires 		error = wacom_initialize_leds(wacom);
177585d2c77bSBenjamin Tissoires 		if (error)
177685d2c77bSBenjamin Tissoires 			goto fail_leds;
177785d2c77bSBenjamin Tissoires 
1778*83e6b40eSBenjamin Tissoires 		error = wacom_initialize_remotes(wacom);
1779b62f6465SBenjamin Tissoires 		if (error)
1780b62f6465SBenjamin Tissoires 			goto fail_remote;
1781b62f6465SBenjamin Tissoires 	}
1782b62f6465SBenjamin Tissoires 
17837704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
17847704ac93SBenjamin Tissoires 		connect_mask |= HID_CONNECT_DRIVER;
17857704ac93SBenjamin Tissoires 
1786471d1714SBenjamin Tissoires 	/* Regular HID work starts now */
17877704ac93SBenjamin Tissoires 	error = hid_hw_start(hdev, connect_mask);
1788471d1714SBenjamin Tissoires 	if (error) {
1789471d1714SBenjamin Tissoires 		hid_err(hdev, "hw start failed\n");
17907fefeec5SBenjamin Tissoires 		goto fail_hw_start;
1791471d1714SBenjamin Tissoires 	}
1792471d1714SBenjamin Tissoires 
1793fd5f92b6SBenjamin Tissoires 	if (!wireless) {
179486e88f0eSJason Gerecke 		/* Note that if query fails it is not a hard failure */
179586e88f0eSJason Gerecke 		wacom_query_tablet_data(hdev, features);
1796fd5f92b6SBenjamin Tissoires 	}
179786e88f0eSJason Gerecke 
179886e88f0eSJason Gerecke 	/* touch only Bamboo doesn't support pen */
179986e88f0eSJason Gerecke 	if ((features->type == BAMBOO_TOUCH) &&
180086e88f0eSJason Gerecke 	    (features->device_type & WACOM_DEVICETYPE_PEN)) {
180186e88f0eSJason Gerecke 		error = -ENODEV;
1802b62f6465SBenjamin Tissoires 		goto fail_quirks;
180386e88f0eSJason Gerecke 	}
180486e88f0eSJason Gerecke 
180586e88f0eSJason Gerecke 	/* pen only Bamboo neither support touch nor pad */
180686e88f0eSJason Gerecke 	if ((features->type == BAMBOO_PEN) &&
180786e88f0eSJason Gerecke 	    ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
180886e88f0eSJason Gerecke 	    (features->device_type & WACOM_DEVICETYPE_PAD))) {
180986e88f0eSJason Gerecke 		error = -ENODEV;
1810b62f6465SBenjamin Tissoires 		goto fail_quirks;
181186e88f0eSJason Gerecke 	}
181286e88f0eSJason Gerecke 
1813ccad85ccSJason Gerecke 	if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
1814471d1714SBenjamin Tissoires 		error = hid_hw_open(hdev);
1815471d1714SBenjamin Tissoires 
1816eda01dabSPing Cheng 	if ((wacom_wac->features.type == INTUOSHT ||
1817eda01dabSPing Cheng 	     wacom_wac->features.type == INTUOSHT2) &&
1818eda01dabSPing Cheng 	    (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
181997f9afa4SBenjamin Tissoires 		wacom_wac->shared->type = wacom_wac->features.type;
18202a6cdbddSJason Gerecke 		wacom_wac->shared->touch_input = wacom_wac->touch_input;
1821471d1714SBenjamin Tissoires 	}
1822471d1714SBenjamin Tissoires 
182384dfbd7fSBenjamin Tissoires 	devres_close_group(&hdev->dev, wacom);
182484dfbd7fSBenjamin Tissoires 
1825471d1714SBenjamin Tissoires 	return 0;
1826471d1714SBenjamin Tissoires 
1827b62f6465SBenjamin Tissoires fail_quirks:
1828c58ac3a8SBenjamin Tissoires 	hid_hw_stop(hdev);
1829b62f6465SBenjamin Tissoires fail_hw_start:
1830b62f6465SBenjamin Tissoires fail_remote:
183185d2c77bSBenjamin Tissoires fail_leds:
1832b62f6465SBenjamin Tissoires fail_register_inputs:
18337fefeec5SBenjamin Tissoires fail_battery:
18347fefeec5SBenjamin Tissoires fail_shared_data:
1835c58ac3a8SBenjamin Tissoires fail_parsed:
1836494078b0SBenjamin Tissoires fail_allocate_inputs:
183784dfbd7fSBenjamin Tissoires fail_open_group:
183884dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom);
1839c58ac3a8SBenjamin Tissoires 	return error;
1840c58ac3a8SBenjamin Tissoires }
1841c58ac3a8SBenjamin Tissoires 
1842a2f091afSBenjamin Tissoires static void wacom_wireless_work(struct work_struct *work)
1843a2f091afSBenjamin Tissoires {
1844d17d1f17SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, wireless_work);
1845a2f091afSBenjamin Tissoires 	struct usb_device *usbdev = wacom->usbdev;
1846a2f091afSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1847a2f091afSBenjamin Tissoires 	struct hid_device *hdev1, *hdev2;
1848a2f091afSBenjamin Tissoires 	struct wacom *wacom1, *wacom2;
1849a2f091afSBenjamin Tissoires 	struct wacom_wac *wacom_wac1, *wacom_wac2;
1850a2f091afSBenjamin Tissoires 	int error;
1851a2f091afSBenjamin Tissoires 
1852a2f091afSBenjamin Tissoires 	/*
1853a2f091afSBenjamin Tissoires 	 * Regardless if this is a disconnect or a new tablet,
1854a2f091afSBenjamin Tissoires 	 * remove any existing input and battery devices.
1855a2f091afSBenjamin Tissoires 	 */
1856a2f091afSBenjamin Tissoires 
1857a2f091afSBenjamin Tissoires 	wacom_destroy_battery(wacom);
1858a2f091afSBenjamin Tissoires 
1859a2f091afSBenjamin Tissoires 	/* Stylus interface */
1860a2f091afSBenjamin Tissoires 	hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1861a2f091afSBenjamin Tissoires 	wacom1 = hid_get_drvdata(hdev1);
1862a2f091afSBenjamin Tissoires 	wacom_wac1 = &(wacom1->wacom_wac);
186384dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom1);
1864a2f091afSBenjamin Tissoires 
1865a2f091afSBenjamin Tissoires 	/* Touch interface */
1866a2f091afSBenjamin Tissoires 	hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1867a2f091afSBenjamin Tissoires 	wacom2 = hid_get_drvdata(hdev2);
1868a2f091afSBenjamin Tissoires 	wacom_wac2 = &(wacom2->wacom_wac);
186984dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom2);
1870a2f091afSBenjamin Tissoires 
1871a2f091afSBenjamin Tissoires 	if (wacom_wac->pid == 0) {
1872a2f091afSBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet disconnected\n");
1873a2f091afSBenjamin Tissoires 	} else {
1874a2f091afSBenjamin Tissoires 		const struct hid_device_id *id = wacom_ids;
1875a2f091afSBenjamin Tissoires 
1876a2f091afSBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1877a2f091afSBenjamin Tissoires 			 wacom_wac->pid);
1878a2f091afSBenjamin Tissoires 
1879a2f091afSBenjamin Tissoires 		while (id->bus) {
1880a2f091afSBenjamin Tissoires 			if (id->vendor == USB_VENDOR_ID_WACOM &&
1881a2f091afSBenjamin Tissoires 			    id->product == wacom_wac->pid)
1882a2f091afSBenjamin Tissoires 				break;
1883a2f091afSBenjamin Tissoires 			id++;
1884a2f091afSBenjamin Tissoires 		}
1885a2f091afSBenjamin Tissoires 
1886a2f091afSBenjamin Tissoires 		if (!id->bus) {
1887a2f091afSBenjamin Tissoires 			hid_info(wacom->hdev, "ignoring unknown PID.\n");
1888a2f091afSBenjamin Tissoires 			return;
1889a2f091afSBenjamin Tissoires 		}
1890a2f091afSBenjamin Tissoires 
1891a2f091afSBenjamin Tissoires 		/* Stylus interface */
1892a2f091afSBenjamin Tissoires 		wacom_wac1->features =
1893a2f091afSBenjamin Tissoires 			*((struct wacom_features *)id->driver_data);
1894fd5f92b6SBenjamin Tissoires 
1895a2f091afSBenjamin Tissoires 		wacom_wac1->pid = wacom_wac->pid;
1896fd5f92b6SBenjamin Tissoires 		hid_hw_stop(hdev1);
1897fd5f92b6SBenjamin Tissoires 		error = wacom_parse_and_register(wacom1, true);
1898a2f091afSBenjamin Tissoires 		if (error)
1899a2f091afSBenjamin Tissoires 			goto fail;
1900a2f091afSBenjamin Tissoires 
1901a2f091afSBenjamin Tissoires 		/* Touch interface */
1902a2f091afSBenjamin Tissoires 		if (wacom_wac1->features.touch_max ||
1903a2f091afSBenjamin Tissoires 		    (wacom_wac1->features.type >= INTUOSHT &&
1904a2f091afSBenjamin Tissoires 		    wacom_wac1->features.type <= BAMBOO_PT)) {
1905a2f091afSBenjamin Tissoires 			wacom_wac2->features =
1906a2f091afSBenjamin Tissoires 				*((struct wacom_features *)id->driver_data);
1907a2f091afSBenjamin Tissoires 			wacom_wac2->pid = wacom_wac->pid;
1908fd5f92b6SBenjamin Tissoires 			hid_hw_stop(hdev2);
1909fd5f92b6SBenjamin Tissoires 			error = wacom_parse_and_register(wacom2, true);
1910a2f091afSBenjamin Tissoires 			if (error)
1911a2f091afSBenjamin Tissoires 				goto fail;
1912a2f091afSBenjamin Tissoires 		}
1913a2f091afSBenjamin Tissoires 
1914a2f091afSBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1915a2f091afSBenjamin Tissoires 		if (error)
1916a2f091afSBenjamin Tissoires 			goto fail;
1917a2f091afSBenjamin Tissoires 	}
1918a2f091afSBenjamin Tissoires 
1919a2f091afSBenjamin Tissoires 	return;
1920a2f091afSBenjamin Tissoires 
1921a2f091afSBenjamin Tissoires fail:
192284dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom1);
192384dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom2);
1924a2f091afSBenjamin Tissoires 	return;
1925a2f091afSBenjamin Tissoires }
1926a2f091afSBenjamin Tissoires 
1927e6f2813aSBenjamin Tissoires static void wacom_remote_work(struct work_struct *work)
1928e6f2813aSBenjamin Tissoires {
1929e6f2813aSBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, remote_work);
1930*83e6b40eSBenjamin Tissoires 	struct wacom_remote *remote = wacom->remote;
1931e6f2813aSBenjamin Tissoires 	struct wacom_remote_data data;
1932e6f2813aSBenjamin Tissoires 	unsigned long flags;
1933e6f2813aSBenjamin Tissoires 	unsigned int count;
1934e6f2813aSBenjamin Tissoires 	u32 serial;
1935e6f2813aSBenjamin Tissoires 	int i, k;
1936e6f2813aSBenjamin Tissoires 
1937*83e6b40eSBenjamin Tissoires 	spin_lock_irqsave(&remote->remote_lock, flags);
1938e6f2813aSBenjamin Tissoires 
1939*83e6b40eSBenjamin Tissoires 	count = kfifo_out(&remote->remote_fifo, &data, sizeof(data));
1940e6f2813aSBenjamin Tissoires 
1941e6f2813aSBenjamin Tissoires 	if (count != sizeof(data)) {
1942e6f2813aSBenjamin Tissoires 		hid_err(wacom->hdev,
1943e6f2813aSBenjamin Tissoires 			"workitem triggered without status available\n");
1944*83e6b40eSBenjamin Tissoires 		spin_unlock_irqrestore(&remote->remote_lock, flags);
1945e6f2813aSBenjamin Tissoires 		return;
1946e6f2813aSBenjamin Tissoires 	}
1947e6f2813aSBenjamin Tissoires 
1948*83e6b40eSBenjamin Tissoires 	if (!kfifo_is_empty(&remote->remote_fifo))
1949e6f2813aSBenjamin Tissoires 		wacom_schedule_work(&wacom->wacom_wac, WACOM_WORKER_REMOTE);
1950e6f2813aSBenjamin Tissoires 
1951*83e6b40eSBenjamin Tissoires 	spin_unlock_irqrestore(&remote->remote_lock, flags);
1952e6f2813aSBenjamin Tissoires 
1953e6f2813aSBenjamin Tissoires 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
1954e6f2813aSBenjamin Tissoires 		serial = data.remote[i].serial;
1955e6f2813aSBenjamin Tissoires 		if (data.remote[i].connected) {
1956e6f2813aSBenjamin Tissoires 
1957*83e6b40eSBenjamin Tissoires 			if (remote->serial[i] == serial)
1958e6f2813aSBenjamin Tissoires 				continue;
1959e6f2813aSBenjamin Tissoires 
1960*83e6b40eSBenjamin Tissoires 			if (remote->serial[i]) {
1961e6f2813aSBenjamin Tissoires 				wacom_remote_destroy_attr_group(wacom,
1962*83e6b40eSBenjamin Tissoires 							remote->serial[i]);
1963e6f2813aSBenjamin Tissoires 			}
1964e6f2813aSBenjamin Tissoires 
1965e6f2813aSBenjamin Tissoires 			/* A remote can pair more than once with an EKR,
1966e6f2813aSBenjamin Tissoires 			 * check to make sure this serial isn't already paired.
1967e6f2813aSBenjamin Tissoires 			 */
1968e6f2813aSBenjamin Tissoires 			for (k = 0; k < WACOM_MAX_REMOTES; k++) {
1969*83e6b40eSBenjamin Tissoires 				if (remote->serial[k] == serial)
1970e6f2813aSBenjamin Tissoires 					break;
1971e6f2813aSBenjamin Tissoires 			}
1972e6f2813aSBenjamin Tissoires 
1973e6f2813aSBenjamin Tissoires 			if (k < WACOM_MAX_REMOTES) {
1974*83e6b40eSBenjamin Tissoires 				remote->serial[i] = serial;
1975e6f2813aSBenjamin Tissoires 				continue;
1976e6f2813aSBenjamin Tissoires 			}
1977e6f2813aSBenjamin Tissoires 			wacom_remote_create_attr_group(wacom, serial, i);
1978e6f2813aSBenjamin Tissoires 
1979*83e6b40eSBenjamin Tissoires 		} else if (remote->serial[i]) {
1980e6f2813aSBenjamin Tissoires 			wacom_remote_destroy_attr_group(wacom,
1981*83e6b40eSBenjamin Tissoires 							remote->serial[i]);
1982e6f2813aSBenjamin Tissoires 		}
1983e6f2813aSBenjamin Tissoires 	}
1984e6f2813aSBenjamin Tissoires }
1985e6f2813aSBenjamin Tissoires 
1986c58ac3a8SBenjamin Tissoires static int wacom_probe(struct hid_device *hdev,
1987c58ac3a8SBenjamin Tissoires 		const struct hid_device_id *id)
1988c58ac3a8SBenjamin Tissoires {
1989c58ac3a8SBenjamin Tissoires 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1990c58ac3a8SBenjamin Tissoires 	struct usb_device *dev = interface_to_usbdev(intf);
1991c58ac3a8SBenjamin Tissoires 	struct wacom *wacom;
1992c58ac3a8SBenjamin Tissoires 	struct wacom_wac *wacom_wac;
1993c58ac3a8SBenjamin Tissoires 	struct wacom_features *features;
1994c58ac3a8SBenjamin Tissoires 	int error;
1995c58ac3a8SBenjamin Tissoires 
1996c58ac3a8SBenjamin Tissoires 	if (!id->driver_data)
1997c58ac3a8SBenjamin Tissoires 		return -EINVAL;
1998c58ac3a8SBenjamin Tissoires 
1999c58ac3a8SBenjamin Tissoires 	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
2000c58ac3a8SBenjamin Tissoires 
2001c58ac3a8SBenjamin Tissoires 	/* hid-core sets this quirk for the boot interface */
2002c58ac3a8SBenjamin Tissoires 	hdev->quirks &= ~HID_QUIRK_NOGET;
2003c58ac3a8SBenjamin Tissoires 
200419b64330SBenjamin Tissoires 	wacom = devm_kzalloc(&hdev->dev, sizeof(struct wacom), GFP_KERNEL);
2005c58ac3a8SBenjamin Tissoires 	if (!wacom)
2006c58ac3a8SBenjamin Tissoires 		return -ENOMEM;
2007c58ac3a8SBenjamin Tissoires 
2008c58ac3a8SBenjamin Tissoires 	hid_set_drvdata(hdev, wacom);
2009c58ac3a8SBenjamin Tissoires 	wacom->hdev = hdev;
2010c58ac3a8SBenjamin Tissoires 
2011c58ac3a8SBenjamin Tissoires 	wacom_wac = &wacom->wacom_wac;
2012c58ac3a8SBenjamin Tissoires 	wacom_wac->features = *((struct wacom_features *)id->driver_data);
2013c58ac3a8SBenjamin Tissoires 	features = &wacom_wac->features;
2014c58ac3a8SBenjamin Tissoires 
2015c58ac3a8SBenjamin Tissoires 	if (features->check_for_hid_type && features->hid_type != hdev->type) {
2016c58ac3a8SBenjamin Tissoires 		error = -ENODEV;
2017c58ac3a8SBenjamin Tissoires 		goto fail_type;
2018c58ac3a8SBenjamin Tissoires 	}
2019c58ac3a8SBenjamin Tissoires 
2020c6fa1aebSJason Gerecke 	wacom_wac->hid_data.inputmode = -1;
2021326ea2a9SJason Gerecke 	wacom_wac->mode_report = -1;
2022c6fa1aebSJason Gerecke 
2023c58ac3a8SBenjamin Tissoires 	wacom->usbdev = dev;
2024c58ac3a8SBenjamin Tissoires 	wacom->intf = intf;
2025c58ac3a8SBenjamin Tissoires 	mutex_init(&wacom->lock);
2026d17d1f17SBenjamin Tissoires 	INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
2027d17d1f17SBenjamin Tissoires 	INIT_WORK(&wacom->battery_work, wacom_battery_work);
2028e6f2813aSBenjamin Tissoires 	INIT_WORK(&wacom->remote_work, wacom_remote_work);
2029c58ac3a8SBenjamin Tissoires 
2030c58ac3a8SBenjamin Tissoires 	/* ask for the report descriptor to be loaded by HID */
2031c58ac3a8SBenjamin Tissoires 	error = hid_parse(hdev);
2032c58ac3a8SBenjamin Tissoires 	if (error) {
2033c58ac3a8SBenjamin Tissoires 		hid_err(hdev, "parse failed\n");
2034c58ac3a8SBenjamin Tissoires 		goto fail_parse;
2035c58ac3a8SBenjamin Tissoires 	}
2036c58ac3a8SBenjamin Tissoires 
2037fd5f92b6SBenjamin Tissoires 	error = wacom_parse_and_register(wacom, false);
2038c58ac3a8SBenjamin Tissoires 	if (error)
2039c58ac3a8SBenjamin Tissoires 		goto fail_parse;
2040c58ac3a8SBenjamin Tissoires 
2041c58ac3a8SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
2042c58ac3a8SBenjamin Tissoires 		error = device_create_file(&hdev->dev, &dev_attr_speed);
2043c58ac3a8SBenjamin Tissoires 		if (error)
2044c58ac3a8SBenjamin Tissoires 			hid_warn(hdev,
2045c58ac3a8SBenjamin Tissoires 				 "can't create sysfs speed attribute err: %d\n",
2046c58ac3a8SBenjamin Tissoires 				 error);
2047c58ac3a8SBenjamin Tissoires 	}
2048c58ac3a8SBenjamin Tissoires 
2049c58ac3a8SBenjamin Tissoires 	return 0;
2050c58ac3a8SBenjamin Tissoires 
20517fefeec5SBenjamin Tissoires fail_parse:
2052e6f2813aSBenjamin Tissoires fail_type:
2053471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
2054471d1714SBenjamin Tissoires 	return error;
2055471d1714SBenjamin Tissoires }
2056471d1714SBenjamin Tissoires 
2057471d1714SBenjamin Tissoires static void wacom_remove(struct hid_device *hdev)
2058471d1714SBenjamin Tissoires {
2059471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
2060f6205161SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
2061f6205161SBenjamin Tissoires 	struct wacom_features *features = &wacom_wac->features;
2062f6205161SBenjamin Tissoires 
2063f6205161SBenjamin Tissoires 	if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
2064f6205161SBenjamin Tissoires 		hid_hw_close(hdev);
2065471d1714SBenjamin Tissoires 
2066471d1714SBenjamin Tissoires 	hid_hw_stop(hdev);
2067471d1714SBenjamin Tissoires 
2068d17d1f17SBenjamin Tissoires 	cancel_work_sync(&wacom->wireless_work);
2069d17d1f17SBenjamin Tissoires 	cancel_work_sync(&wacom->battery_work);
2070e6f2813aSBenjamin Tissoires 	cancel_work_sync(&wacom->remote_work);
2071f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
2072f81a1295SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
2073471d1714SBenjamin Tissoires 
2074471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
2075471d1714SBenjamin Tissoires }
2076471d1714SBenjamin Tissoires 
207741a74581SGeert Uytterhoeven #ifdef CONFIG_PM
2078471d1714SBenjamin Tissoires static int wacom_resume(struct hid_device *hdev)
2079471d1714SBenjamin Tissoires {
2080471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
2081471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
2082471d1714SBenjamin Tissoires 
2083471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
2084471d1714SBenjamin Tissoires 
2085471d1714SBenjamin Tissoires 	/* switch to wacom mode first */
2086471d1714SBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
2087471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
2088471d1714SBenjamin Tissoires 
2089471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
2090471d1714SBenjamin Tissoires 
2091471d1714SBenjamin Tissoires 	return 0;
2092471d1714SBenjamin Tissoires }
2093471d1714SBenjamin Tissoires 
2094471d1714SBenjamin Tissoires static int wacom_reset_resume(struct hid_device *hdev)
2095471d1714SBenjamin Tissoires {
2096471d1714SBenjamin Tissoires 	return wacom_resume(hdev);
2097471d1714SBenjamin Tissoires }
209841a74581SGeert Uytterhoeven #endif /* CONFIG_PM */
2099471d1714SBenjamin Tissoires 
2100471d1714SBenjamin Tissoires static struct hid_driver wacom_driver = {
2101471d1714SBenjamin Tissoires 	.name =		"wacom",
2102471d1714SBenjamin Tissoires 	.id_table =	wacom_ids,
2103471d1714SBenjamin Tissoires 	.probe =	wacom_probe,
2104471d1714SBenjamin Tissoires 	.remove =	wacom_remove,
21057704ac93SBenjamin Tissoires 	.report =	wacom_wac_report,
2106471d1714SBenjamin Tissoires #ifdef CONFIG_PM
2107471d1714SBenjamin Tissoires 	.resume =	wacom_resume,
2108471d1714SBenjamin Tissoires 	.reset_resume =	wacom_reset_resume,
2109471d1714SBenjamin Tissoires #endif
2110471d1714SBenjamin Tissoires 	.raw_event =	wacom_raw_event,
2111471d1714SBenjamin Tissoires };
2112471d1714SBenjamin Tissoires module_hid_driver(wacom_driver);
2113f2e0a7d4SBenjamin Tissoires 
2114f2e0a7d4SBenjamin Tissoires MODULE_VERSION(DRIVER_VERSION);
2115f2e0a7d4SBenjamin Tissoires MODULE_AUTHOR(DRIVER_AUTHOR);
2116f2e0a7d4SBenjamin Tissoires MODULE_DESCRIPTION(DRIVER_DESC);
2117f2e0a7d4SBenjamin Tissoires MODULE_LICENSE(DRIVER_LICENSE);
2118