xref: /openbmc/linux/drivers/hid/wacom_sys.c (revision 2df68a8864883ff006b76f50dfc32fd230247ef9)
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
2872b236d6SAaron Skomra #define WAC_REMOTE_SERIAL_MAX_STRLEN	9
29471d1714SBenjamin Tissoires 
30e0984bc3SPing Cheng #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
31e0984bc3SPing Cheng #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
3272b236d6SAaron Skomra #define DEV_ATTR_RO_PERM (S_IRUSR | S_IRGRP)
33e0984bc3SPing Cheng 
34c64d8834SPing Cheng static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
35c64d8834SPing Cheng 			    size_t size, unsigned int retries)
36471d1714SBenjamin Tissoires {
37471d1714SBenjamin Tissoires 	int retval;
38471d1714SBenjamin Tissoires 
39471d1714SBenjamin Tissoires 	do {
40c64d8834SPing Cheng 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
41471d1714SBenjamin Tissoires 				HID_REQ_GET_REPORT);
42aef3156dSJason Gerecke 	} while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
43aef3156dSJason Gerecke 
44aef3156dSJason Gerecke 	if (retval < 0)
45aef3156dSJason Gerecke 		hid_err(hdev, "wacom_get_report: ran out of retries "
46aef3156dSJason Gerecke 			"(last error = %d)\n", retval);
47471d1714SBenjamin Tissoires 
48471d1714SBenjamin Tissoires 	return retval;
49471d1714SBenjamin Tissoires }
50471d1714SBenjamin Tissoires 
51296b7378SPrzemo Firszt static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
52296b7378SPrzemo Firszt 			    size_t size, unsigned int retries)
53471d1714SBenjamin Tissoires {
54471d1714SBenjamin Tissoires 	int retval;
55471d1714SBenjamin Tissoires 
56471d1714SBenjamin Tissoires 	do {
57296b7378SPrzemo Firszt 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
58471d1714SBenjamin Tissoires 				HID_REQ_SET_REPORT);
59aef3156dSJason Gerecke 	} while ((retval == -ETIMEDOUT || retval == -EAGAIN) && --retries);
60aef3156dSJason Gerecke 
61aef3156dSJason Gerecke 	if (retval < 0)
62aef3156dSJason Gerecke 		hid_err(hdev, "wacom_set_report: ran out of retries "
63aef3156dSJason Gerecke 			"(last error = %d)\n", retval);
64471d1714SBenjamin Tissoires 
65471d1714SBenjamin Tissoires 	return retval;
66471d1714SBenjamin Tissoires }
67471d1714SBenjamin Tissoires 
68471d1714SBenjamin Tissoires static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
69471d1714SBenjamin Tissoires 		u8 *raw_data, int size)
70471d1714SBenjamin Tissoires {
71471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
72471d1714SBenjamin Tissoires 
73471d1714SBenjamin Tissoires 	if (size > WACOM_PKGLEN_MAX)
74471d1714SBenjamin Tissoires 		return 1;
75471d1714SBenjamin Tissoires 
76471d1714SBenjamin Tissoires 	memcpy(wacom->wacom_wac.data, raw_data, size);
77471d1714SBenjamin Tissoires 
78471d1714SBenjamin Tissoires 	wacom_wac_irq(&wacom->wacom_wac, size);
79471d1714SBenjamin Tissoires 
80471d1714SBenjamin Tissoires 	return 0;
81471d1714SBenjamin Tissoires }
82471d1714SBenjamin Tissoires 
83471d1714SBenjamin Tissoires static int wacom_open(struct input_dev *dev)
84471d1714SBenjamin Tissoires {
85471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
86471d1714SBenjamin Tissoires 
87dff67416SBenjamin Tissoires 	return hid_hw_open(wacom->hdev);
88471d1714SBenjamin Tissoires }
89471d1714SBenjamin Tissoires 
90471d1714SBenjamin Tissoires static void wacom_close(struct input_dev *dev)
91471d1714SBenjamin Tissoires {
92471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
93471d1714SBenjamin Tissoires 
943dad188eSBenjamin Tissoires 	/*
953dad188eSBenjamin Tissoires 	 * wacom->hdev should never be null, but surprisingly, I had the case
963dad188eSBenjamin Tissoires 	 * once while unplugging the Wacom Wireless Receiver.
973dad188eSBenjamin Tissoires 	 */
983dad188eSBenjamin Tissoires 	if (wacom->hdev)
99471d1714SBenjamin Tissoires 		hid_hw_close(wacom->hdev);
100471d1714SBenjamin Tissoires }
101471d1714SBenjamin Tissoires 
102471d1714SBenjamin Tissoires /*
103471d1714SBenjamin Tissoires  * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
104471d1714SBenjamin Tissoires  */
105471d1714SBenjamin Tissoires static int wacom_calc_hid_res(int logical_extents, int physical_extents,
106471d1714SBenjamin Tissoires 			       unsigned unit, int exponent)
107471d1714SBenjamin Tissoires {
108471d1714SBenjamin Tissoires 	struct hid_field field = {
109471d1714SBenjamin Tissoires 		.logical_maximum = logical_extents,
110471d1714SBenjamin Tissoires 		.physical_maximum = physical_extents,
111471d1714SBenjamin Tissoires 		.unit = unit,
112471d1714SBenjamin Tissoires 		.unit_exponent = exponent,
113471d1714SBenjamin Tissoires 	};
114471d1714SBenjamin Tissoires 
115471d1714SBenjamin Tissoires 	return hidinput_calc_abs_res(&field, ABS_X);
116471d1714SBenjamin Tissoires }
117471d1714SBenjamin Tissoires 
118471d1714SBenjamin Tissoires static void wacom_feature_mapping(struct hid_device *hdev,
119471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
120471d1714SBenjamin Tissoires {
121471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
122471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
1235ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
1248ffffd52SBenjamin Tissoires 	u8 *data;
1258ffffd52SBenjamin Tissoires 	int ret;
126471d1714SBenjamin Tissoires 
127471d1714SBenjamin Tissoires 	switch (usage->hid) {
128471d1714SBenjamin Tissoires 	case HID_DG_CONTACTMAX:
129471d1714SBenjamin Tissoires 		/* leave touch_max as is if predefined */
1308ffffd52SBenjamin Tissoires 		if (!features->touch_max) {
1318ffffd52SBenjamin Tissoires 			/* read manually */
1328ffffd52SBenjamin Tissoires 			data = kzalloc(2, GFP_KERNEL);
1338ffffd52SBenjamin Tissoires 			if (!data)
1348ffffd52SBenjamin Tissoires 				break;
1358ffffd52SBenjamin Tissoires 			data[0] = field->report->id;
1368ffffd52SBenjamin Tissoires 			ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
13705e8fd92SJason Gerecke 						data, 2, WAC_CMD_RETRIES);
13805e8fd92SJason Gerecke 			if (ret == 2) {
1398ffffd52SBenjamin Tissoires 				features->touch_max = data[1];
14005e8fd92SJason Gerecke 			} else {
14105e8fd92SJason Gerecke 				features->touch_max = 16;
14205e8fd92SJason Gerecke 				hid_warn(hdev, "wacom_feature_mapping: "
14305e8fd92SJason Gerecke 					 "could not get HID_DG_CONTACTMAX, "
14405e8fd92SJason Gerecke 					 "defaulting to %d\n",
14505e8fd92SJason Gerecke 					  features->touch_max);
14605e8fd92SJason Gerecke 			}
1478ffffd52SBenjamin Tissoires 			kfree(data);
1488ffffd52SBenjamin Tissoires 		}
149471d1714SBenjamin Tissoires 		break;
1505ae6e89fSBenjamin Tissoires 	case HID_DG_INPUTMODE:
1515ae6e89fSBenjamin Tissoires 		/* Ignore if value index is out of bounds. */
1525ae6e89fSBenjamin Tissoires 		if (usage->usage_index >= field->report_count) {
1535ae6e89fSBenjamin Tissoires 			dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
1545ae6e89fSBenjamin Tissoires 			break;
1555ae6e89fSBenjamin Tissoires 		}
1565ae6e89fSBenjamin Tissoires 
1575ae6e89fSBenjamin Tissoires 		hid_data->inputmode = field->report->id;
1585ae6e89fSBenjamin Tissoires 		hid_data->inputmode_index = usage->usage_index;
1595ae6e89fSBenjamin Tissoires 		break;
160326ea2a9SJason Gerecke 
161326ea2a9SJason Gerecke 	case HID_UP_DIGITIZER:
162326ea2a9SJason Gerecke 		if (field->report->id == 0x0B &&
163326ea2a9SJason Gerecke 		    (field->application == WACOM_G9_DIGITIZER ||
164326ea2a9SJason Gerecke 		     field->application == WACOM_G11_DIGITIZER)) {
165326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_report = field->report->id;
166326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_value = 0;
167326ea2a9SJason Gerecke 		}
168326ea2a9SJason Gerecke 		break;
169326ea2a9SJason Gerecke 
170326ea2a9SJason Gerecke 	case WACOM_G9_PAGE:
171326ea2a9SJason Gerecke 	case WACOM_G11_PAGE:
172326ea2a9SJason Gerecke 		if (field->report->id == 0x03 &&
173326ea2a9SJason Gerecke 		    (field->application == WACOM_G9_TOUCHSCREEN ||
174326ea2a9SJason Gerecke 		     field->application == WACOM_G11_TOUCHSCREEN)) {
175326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_report = field->report->id;
176326ea2a9SJason Gerecke 			wacom->wacom_wac.mode_value = 0;
177326ea2a9SJason Gerecke 		}
178326ea2a9SJason Gerecke 		break;
179471d1714SBenjamin Tissoires 	}
180471d1714SBenjamin Tissoires }
181471d1714SBenjamin Tissoires 
182471d1714SBenjamin Tissoires /*
183471d1714SBenjamin Tissoires  * Interface Descriptor of wacom devices can be incomplete and
184471d1714SBenjamin Tissoires  * inconsistent so wacom_features table is used to store stylus
185471d1714SBenjamin Tissoires  * device's packet lengths, various maximum values, and tablet
186471d1714SBenjamin Tissoires  * resolution based on product ID's.
187471d1714SBenjamin Tissoires  *
188471d1714SBenjamin Tissoires  * For devices that contain 2 interfaces, wacom_features table is
189471d1714SBenjamin Tissoires  * inaccurate for the touch interface.  Since the Interface Descriptor
190471d1714SBenjamin Tissoires  * for touch interfaces has pretty complete data, this function exists
191471d1714SBenjamin Tissoires  * to query tablet for this missing information instead of hard coding in
192471d1714SBenjamin Tissoires  * an additional table.
193471d1714SBenjamin Tissoires  *
194471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a stylus will contain a
195471d1714SBenjamin Tissoires  * boot mouse application collection that is not of interest and this
196471d1714SBenjamin Tissoires  * function will ignore it.
197471d1714SBenjamin Tissoires  *
198471d1714SBenjamin Tissoires  * It also contains a digitizer application collection that also is not
199471d1714SBenjamin Tissoires  * of interest since any information it contains would be duplicate
200471d1714SBenjamin Tissoires  * of what is in wacom_features. Usually it defines a report of an array
201471d1714SBenjamin Tissoires  * of bytes that could be used as max length of the stylus packet returned.
202471d1714SBenjamin Tissoires  * If it happens to define a Digitizer-Stylus Physical Collection then
203471d1714SBenjamin Tissoires  * the X and Y logical values contain valid data but it is ignored.
204471d1714SBenjamin Tissoires  *
205471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a touch interface will contain a
206471d1714SBenjamin Tissoires  * Digitizer-Finger Physical Collection which will define both logical
207471d1714SBenjamin Tissoires  * X/Y maximum as well as the physical size of tablet. Since touch
208471d1714SBenjamin Tissoires  * interfaces haven't supported pressure or distance, this is enough
209471d1714SBenjamin Tissoires  * information to override invalid values in the wacom_features table.
210471d1714SBenjamin Tissoires  *
211471d1714SBenjamin Tissoires  * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
212471d1714SBenjamin Tissoires  * data. We deal with them after returning from this function.
213471d1714SBenjamin Tissoires  */
214471d1714SBenjamin Tissoires static void wacom_usage_mapping(struct hid_device *hdev,
215471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
216471d1714SBenjamin Tissoires {
217471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
218471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
219d97a5522SBenjamin Tissoires 	bool finger = WACOM_FINGER_FIELD(field);
220d97a5522SBenjamin Tissoires 	bool pen = WACOM_PEN_FIELD(field);
221471d1714SBenjamin Tissoires 
222471d1714SBenjamin Tissoires 	/*
223471d1714SBenjamin Tissoires 	* Requiring Stylus Usage will ignore boot mouse
224471d1714SBenjamin Tissoires 	* X/Y values and some cases of invalid Digitizer X/Y
225471d1714SBenjamin Tissoires 	* values commonly reported.
226471d1714SBenjamin Tissoires 	*/
227042628abSJason Gerecke 	if (pen)
228aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_PEN;
229042628abSJason Gerecke 	else if (finger)
230aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_TOUCH;
231042628abSJason Gerecke 	else
232471d1714SBenjamin Tissoires 		return;
233471d1714SBenjamin Tissoires 
23430ebc1aeSPing Cheng 	/*
23530ebc1aeSPing Cheng 	 * Bamboo models do not support HID_DG_CONTACTMAX.
23630ebc1aeSPing Cheng 	 * And, Bamboo Pen only descriptor contains touch.
23730ebc1aeSPing Cheng 	 */
2383b164a00SPing Cheng 	if (features->type > BAMBOO_PT) {
23930ebc1aeSPing Cheng 		/* ISDv4 touch devices at least supports one touch point */
240471d1714SBenjamin Tissoires 		if (finger && !features->touch_max)
241471d1714SBenjamin Tissoires 			features->touch_max = 1;
24230ebc1aeSPing Cheng 	}
243471d1714SBenjamin Tissoires 
244471d1714SBenjamin Tissoires 	switch (usage->hid) {
245471d1714SBenjamin Tissoires 	case HID_GD_X:
246471d1714SBenjamin Tissoires 		features->x_max = field->logical_maximum;
247471d1714SBenjamin Tissoires 		if (finger) {
248471d1714SBenjamin Tissoires 			features->x_phy = field->physical_maximum;
2493b164a00SPing Cheng 			if ((features->type != BAMBOO_PT) &&
2503b164a00SPing Cheng 			    (features->type != BAMBOO_TOUCH)) {
251471d1714SBenjamin Tissoires 				features->unit = field->unit;
252471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
253471d1714SBenjamin Tissoires 			}
254471d1714SBenjamin Tissoires 		}
255471d1714SBenjamin Tissoires 		break;
256471d1714SBenjamin Tissoires 	case HID_GD_Y:
257471d1714SBenjamin Tissoires 		features->y_max = field->logical_maximum;
258471d1714SBenjamin Tissoires 		if (finger) {
259471d1714SBenjamin Tissoires 			features->y_phy = field->physical_maximum;
2603b164a00SPing Cheng 			if ((features->type != BAMBOO_PT) &&
2613b164a00SPing Cheng 			    (features->type != BAMBOO_TOUCH)) {
262471d1714SBenjamin Tissoires 				features->unit = field->unit;
263471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
264471d1714SBenjamin Tissoires 			}
265471d1714SBenjamin Tissoires 		}
266471d1714SBenjamin Tissoires 		break;
267471d1714SBenjamin Tissoires 	case HID_DG_TIPPRESSURE:
268471d1714SBenjamin Tissoires 		if (pen)
269471d1714SBenjamin Tissoires 			features->pressure_max = field->logical_maximum;
270471d1714SBenjamin Tissoires 		break;
271471d1714SBenjamin Tissoires 	}
2727704ac93SBenjamin Tissoires 
2737704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
2747704ac93SBenjamin Tissoires 		wacom_wac_usage_mapping(hdev, field, usage);
275471d1714SBenjamin Tissoires }
276471d1714SBenjamin Tissoires 
277b58ba1baSJason Gerecke static void wacom_post_parse_hid(struct hid_device *hdev,
278b58ba1baSJason Gerecke 				 struct wacom_features *features)
279b58ba1baSJason Gerecke {
280b58ba1baSJason Gerecke 	struct wacom *wacom = hid_get_drvdata(hdev);
281b58ba1baSJason Gerecke 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
282b58ba1baSJason Gerecke 
283b58ba1baSJason Gerecke 	if (features->type == HID_GENERIC) {
284b58ba1baSJason Gerecke 		/* Any last-minute generic device setup */
285b58ba1baSJason Gerecke 		if (features->touch_max > 1) {
2862a6cdbddSJason Gerecke 			input_mt_init_slots(wacom_wac->touch_input, wacom_wac->features.touch_max,
287b58ba1baSJason Gerecke 				    INPUT_MT_DIRECT);
288b58ba1baSJason Gerecke 		}
289b58ba1baSJason Gerecke 	}
290b58ba1baSJason Gerecke }
291b58ba1baSJason Gerecke 
292471d1714SBenjamin Tissoires static void wacom_parse_hid(struct hid_device *hdev,
293471d1714SBenjamin Tissoires 			   struct wacom_features *features)
294471d1714SBenjamin Tissoires {
295471d1714SBenjamin Tissoires 	struct hid_report_enum *rep_enum;
296471d1714SBenjamin Tissoires 	struct hid_report *hreport;
297471d1714SBenjamin Tissoires 	int i, j;
298471d1714SBenjamin Tissoires 
299471d1714SBenjamin Tissoires 	/* check features first */
300471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
301471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
302471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++) {
303471d1714SBenjamin Tissoires 			/* Ignore if report count is out of bounds. */
304471d1714SBenjamin Tissoires 			if (hreport->field[i]->report_count < 1)
305471d1714SBenjamin Tissoires 				continue;
306471d1714SBenjamin Tissoires 
307471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++) {
308471d1714SBenjamin Tissoires 				wacom_feature_mapping(hdev, hreport->field[i],
309471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
310471d1714SBenjamin Tissoires 			}
311471d1714SBenjamin Tissoires 		}
312471d1714SBenjamin Tissoires 	}
313471d1714SBenjamin Tissoires 
314471d1714SBenjamin Tissoires 	/* now check the input usages */
315471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
316471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
317471d1714SBenjamin Tissoires 
318471d1714SBenjamin Tissoires 		if (!hreport->maxfield)
319471d1714SBenjamin Tissoires 			continue;
320471d1714SBenjamin Tissoires 
321471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++)
322471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++)
323471d1714SBenjamin Tissoires 				wacom_usage_mapping(hdev, hreport->field[i],
324471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
325471d1714SBenjamin Tissoires 	}
326b58ba1baSJason Gerecke 
327b58ba1baSJason Gerecke 	wacom_post_parse_hid(hdev, features);
328471d1714SBenjamin Tissoires }
329471d1714SBenjamin Tissoires 
3305ae6e89fSBenjamin Tissoires static int wacom_hid_set_device_mode(struct hid_device *hdev)
3315ae6e89fSBenjamin Tissoires {
3325ae6e89fSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
3335ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
3345ae6e89fSBenjamin Tissoires 	struct hid_report *r;
3355ae6e89fSBenjamin Tissoires 	struct hid_report_enum *re;
3365ae6e89fSBenjamin Tissoires 
3375ae6e89fSBenjamin Tissoires 	if (hid_data->inputmode < 0)
3385ae6e89fSBenjamin Tissoires 		return 0;
3395ae6e89fSBenjamin Tissoires 
3405ae6e89fSBenjamin Tissoires 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
3415ae6e89fSBenjamin Tissoires 	r = re->report_id_hash[hid_data->inputmode];
3425ae6e89fSBenjamin Tissoires 	if (r) {
3435ae6e89fSBenjamin Tissoires 		r->field[0]->value[hid_data->inputmode_index] = 2;
3445ae6e89fSBenjamin Tissoires 		hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
3455ae6e89fSBenjamin Tissoires 	}
3465ae6e89fSBenjamin Tissoires 	return 0;
3475ae6e89fSBenjamin Tissoires }
3485ae6e89fSBenjamin Tissoires 
349326ea2a9SJason Gerecke static int wacom_set_device_mode(struct hid_device *hdev,
350326ea2a9SJason Gerecke 				 struct wacom_wac *wacom_wac)
351471d1714SBenjamin Tissoires {
352326ea2a9SJason Gerecke 	u8 *rep_data;
353326ea2a9SJason Gerecke 	struct hid_report *r;
354326ea2a9SJason Gerecke 	struct hid_report_enum *re;
355326ea2a9SJason Gerecke 	int length;
356471d1714SBenjamin Tissoires 	int error = -ENOMEM, limit = 0;
357471d1714SBenjamin Tissoires 
358326ea2a9SJason Gerecke 	if (wacom_wac->mode_report < 0)
359326ea2a9SJason Gerecke 		return 0;
360326ea2a9SJason Gerecke 
361326ea2a9SJason Gerecke 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
362326ea2a9SJason Gerecke 	r = re->report_id_hash[wacom_wac->mode_report];
363326ea2a9SJason Gerecke 	if (!r)
364326ea2a9SJason Gerecke 		return -EINVAL;
365326ea2a9SJason Gerecke 
366326ea2a9SJason Gerecke 	rep_data = hid_alloc_report_buf(r, GFP_KERNEL);
367471d1714SBenjamin Tissoires 	if (!rep_data)
368326ea2a9SJason Gerecke 		return -ENOMEM;
369326ea2a9SJason Gerecke 
370326ea2a9SJason Gerecke 	length = hid_report_len(r);
371471d1714SBenjamin Tissoires 
372471d1714SBenjamin Tissoires 	do {
373326ea2a9SJason Gerecke 		rep_data[0] = wacom_wac->mode_report;
374326ea2a9SJason Gerecke 		rep_data[1] = wacom_wac->mode_value;
375471d1714SBenjamin Tissoires 
376296b7378SPrzemo Firszt 		error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
377296b7378SPrzemo Firszt 					 length, 1);
378471d1714SBenjamin Tissoires 		if (error >= 0)
379471d1714SBenjamin Tissoires 			error = wacom_get_report(hdev, HID_FEATURE_REPORT,
380c64d8834SPing Cheng 			                         rep_data, length, 1);
381326ea2a9SJason Gerecke 	} while (error >= 0 &&
382326ea2a9SJason Gerecke 		 rep_data[1] != wacom_wac->mode_report &&
383326ea2a9SJason Gerecke 		 limit++ < WAC_MSG_RETRIES);
384471d1714SBenjamin Tissoires 
385471d1714SBenjamin Tissoires 	kfree(rep_data);
386471d1714SBenjamin Tissoires 
387471d1714SBenjamin Tissoires 	return error < 0 ? error : 0;
388471d1714SBenjamin Tissoires }
389471d1714SBenjamin Tissoires 
390f81a1295SBenjamin Tissoires static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
391f81a1295SBenjamin Tissoires 		struct wacom_features *features)
392f81a1295SBenjamin Tissoires {
393387142bbSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
394387142bbSBenjamin Tissoires 	int ret;
395387142bbSBenjamin Tissoires 	u8 rep_data[2];
396387142bbSBenjamin Tissoires 
397387142bbSBenjamin Tissoires 	switch (features->type) {
398387142bbSBenjamin Tissoires 	case GRAPHIRE_BT:
399387142bbSBenjamin Tissoires 		rep_data[0] = 0x03;
400387142bbSBenjamin Tissoires 		rep_data[1] = 0x00;
401296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
402296b7378SPrzemo Firszt 					3);
403387142bbSBenjamin Tissoires 
404387142bbSBenjamin Tissoires 		if (ret >= 0) {
405387142bbSBenjamin Tissoires 			rep_data[0] = speed == 0 ? 0x05 : 0x06;
406387142bbSBenjamin Tissoires 			rep_data[1] = 0x00;
407387142bbSBenjamin Tissoires 
408387142bbSBenjamin Tissoires 			ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
409296b7378SPrzemo Firszt 						rep_data, 2, 3);
410387142bbSBenjamin Tissoires 
411387142bbSBenjamin Tissoires 			if (ret >= 0) {
412387142bbSBenjamin Tissoires 				wacom->wacom_wac.bt_high_speed = speed;
413387142bbSBenjamin Tissoires 				return 0;
414387142bbSBenjamin Tissoires 			}
415387142bbSBenjamin Tissoires 		}
416387142bbSBenjamin Tissoires 
417387142bbSBenjamin Tissoires 		/*
418387142bbSBenjamin Tissoires 		 * Note that if the raw queries fail, it's not a hard failure
419387142bbSBenjamin Tissoires 		 * and it is safe to continue
420387142bbSBenjamin Tissoires 		 */
421387142bbSBenjamin Tissoires 		hid_warn(hdev, "failed to poke device, command %d, err %d\n",
422387142bbSBenjamin Tissoires 			 rep_data[0], ret);
423387142bbSBenjamin Tissoires 		break;
42481af7e61SBenjamin Tissoires 	case INTUOS4WL:
42581af7e61SBenjamin Tissoires 		if (speed == 1)
42681af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features &= ~0x20;
42781af7e61SBenjamin Tissoires 		else
42881af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features |= 0x20;
42981af7e61SBenjamin Tissoires 
43081af7e61SBenjamin Tissoires 		rep_data[0] = 0x03;
43181af7e61SBenjamin Tissoires 		rep_data[1] = wacom->wacom_wac.bt_features;
43281af7e61SBenjamin Tissoires 
433296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
434296b7378SPrzemo Firszt 					1);
43581af7e61SBenjamin Tissoires 		if (ret >= 0)
43681af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_high_speed = speed;
43781af7e61SBenjamin Tissoires 		break;
438387142bbSBenjamin Tissoires 	}
439387142bbSBenjamin Tissoires 
440f81a1295SBenjamin Tissoires 	return 0;
441f81a1295SBenjamin Tissoires }
442f81a1295SBenjamin Tissoires 
443471d1714SBenjamin Tissoires /*
444471d1714SBenjamin Tissoires  * Switch the tablet into its most-capable mode. Wacom tablets are
445471d1714SBenjamin Tissoires  * typically configured to power-up in a mode which sends mouse-like
446471d1714SBenjamin Tissoires  * reports to the OS. To get absolute position, pressure data, etc.
447471d1714SBenjamin Tissoires  * from the tablet, it is necessary to switch the tablet out of this
448471d1714SBenjamin Tissoires  * mode and into one which sends the full range of tablet data.
449471d1714SBenjamin Tissoires  */
450471d1714SBenjamin Tissoires static int wacom_query_tablet_data(struct hid_device *hdev,
451471d1714SBenjamin Tissoires 		struct wacom_features *features)
452471d1714SBenjamin Tissoires {
453326ea2a9SJason Gerecke 	struct wacom *wacom = hid_get_drvdata(hdev);
454326ea2a9SJason Gerecke 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
455326ea2a9SJason Gerecke 
456f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
457f81a1295SBenjamin Tissoires 		return wacom_bt_query_tablet_data(hdev, 1, features);
458f81a1295SBenjamin Tissoires 
459326ea2a9SJason Gerecke 	if (features->type != HID_GENERIC) {
460aa86b18cSJason Gerecke 		if (features->device_type & WACOM_DEVICETYPE_TOUCH) {
461471d1714SBenjamin Tissoires 			if (features->type > TABLETPC) {
462471d1714SBenjamin Tissoires 				/* MT Tablet PC touch */
463326ea2a9SJason Gerecke 				wacom_wac->mode_report = 3;
464326ea2a9SJason Gerecke 				wacom_wac->mode_value = 4;
465326ea2a9SJason Gerecke 			} else if (features->type == WACOM_24HDT) {
466326ea2a9SJason Gerecke 				wacom_wac->mode_report = 18;
467326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
468326ea2a9SJason Gerecke 			} else if (features->type == WACOM_27QHDT) {
469326ea2a9SJason Gerecke 				wacom_wac->mode_report = 131;
470326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
471326ea2a9SJason Gerecke 			} else if (features->type == BAMBOO_PAD) {
472326ea2a9SJason Gerecke 				wacom_wac->mode_report = 2;
473326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
4748c97a765SBenjamin Tissoires 			}
475aa86b18cSJason Gerecke 		} else if (features->device_type & WACOM_DEVICETYPE_PEN) {
4767adb91bdSPing Cheng 			if (features->type <= BAMBOO_PT) {
477326ea2a9SJason Gerecke 				wacom_wac->mode_report = 2;
478326ea2a9SJason Gerecke 				wacom_wac->mode_value = 2;
479471d1714SBenjamin Tissoires 			}
480471d1714SBenjamin Tissoires 		}
481326ea2a9SJason Gerecke 	}
482326ea2a9SJason Gerecke 
483326ea2a9SJason Gerecke 	wacom_set_device_mode(hdev, wacom_wac);
484326ea2a9SJason Gerecke 
485326ea2a9SJason Gerecke 	if (features->type == HID_GENERIC)
486326ea2a9SJason Gerecke 		return wacom_hid_set_device_mode(hdev);
487471d1714SBenjamin Tissoires 
488471d1714SBenjamin Tissoires 	return 0;
489471d1714SBenjamin Tissoires }
490471d1714SBenjamin Tissoires 
491471d1714SBenjamin Tissoires static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
492471d1714SBenjamin Tissoires 					 struct wacom_features *features)
493471d1714SBenjamin Tissoires {
494471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
495471d1714SBenjamin Tissoires 	struct usb_interface *intf = wacom->intf;
496471d1714SBenjamin Tissoires 
497471d1714SBenjamin Tissoires 	/* default features */
498471d1714SBenjamin Tissoires 	features->x_fuzz = 4;
499471d1714SBenjamin Tissoires 	features->y_fuzz = 4;
500471d1714SBenjamin Tissoires 	features->pressure_fuzz = 0;
501bef7e200SJason Gerecke 	features->distance_fuzz = 1;
502bef7e200SJason Gerecke 	features->tilt_fuzz = 1;
503471d1714SBenjamin Tissoires 
504471d1714SBenjamin Tissoires 	/*
505471d1714SBenjamin Tissoires 	 * The wireless device HID is basic and layout conflicts with
506471d1714SBenjamin Tissoires 	 * other tablets (monitor and touch interface can look like pen).
507471d1714SBenjamin Tissoires 	 * Skip the query for this type and modify defaults based on
508471d1714SBenjamin Tissoires 	 * interface number.
509471d1714SBenjamin Tissoires 	 */
510471d1714SBenjamin Tissoires 	if (features->type == WIRELESS) {
5113f14a63aSJason Gerecke 		if (intf->cur_altsetting->desc.bInterfaceNumber == 0)
512ccad85ccSJason Gerecke 			features->device_type = WACOM_DEVICETYPE_WL_MONITOR;
5133f14a63aSJason Gerecke 		else
514aa86b18cSJason Gerecke 			features->device_type = WACOM_DEVICETYPE_NONE;
5153f14a63aSJason Gerecke 		return;
516471d1714SBenjamin Tissoires 	}
517471d1714SBenjamin Tissoires 
518471d1714SBenjamin Tissoires 	wacom_parse_hid(hdev, features);
519471d1714SBenjamin Tissoires }
520471d1714SBenjamin Tissoires 
521471d1714SBenjamin Tissoires struct wacom_hdev_data {
522471d1714SBenjamin Tissoires 	struct list_head list;
523471d1714SBenjamin Tissoires 	struct kref kref;
524471d1714SBenjamin Tissoires 	struct hid_device *dev;
525471d1714SBenjamin Tissoires 	struct wacom_shared shared;
526471d1714SBenjamin Tissoires };
527471d1714SBenjamin Tissoires 
528471d1714SBenjamin Tissoires static LIST_HEAD(wacom_udev_list);
529471d1714SBenjamin Tissoires static DEFINE_MUTEX(wacom_udev_list_lock);
530471d1714SBenjamin Tissoires 
531471d1714SBenjamin Tissoires static bool wacom_are_sibling(struct hid_device *hdev,
532471d1714SBenjamin Tissoires 		struct hid_device *sibling)
533471d1714SBenjamin Tissoires {
534471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
535471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
536471d1714SBenjamin Tissoires 	int vid = features->oVid;
537471d1714SBenjamin Tissoires 	int pid = features->oPid;
538471d1714SBenjamin Tissoires 	int n1,n2;
539471d1714SBenjamin Tissoires 
540471d1714SBenjamin Tissoires 	if (vid == 0 && pid == 0) {
541471d1714SBenjamin Tissoires 		vid = hdev->vendor;
542471d1714SBenjamin Tissoires 		pid = hdev->product;
543471d1714SBenjamin Tissoires 	}
544471d1714SBenjamin Tissoires 
545471d1714SBenjamin Tissoires 	if (vid != sibling->vendor || pid != sibling->product)
546471d1714SBenjamin Tissoires 		return false;
547471d1714SBenjamin Tissoires 
548471d1714SBenjamin Tissoires 	/* Compare the physical path. */
549471d1714SBenjamin Tissoires 	n1 = strrchr(hdev->phys, '.') - hdev->phys;
550471d1714SBenjamin Tissoires 	n2 = strrchr(sibling->phys, '.') - sibling->phys;
551471d1714SBenjamin Tissoires 	if (n1 != n2 || n1 <= 0 || n2 <= 0)
552471d1714SBenjamin Tissoires 		return false;
553471d1714SBenjamin Tissoires 
554471d1714SBenjamin Tissoires 	return !strncmp(hdev->phys, sibling->phys, n1);
555471d1714SBenjamin Tissoires }
556471d1714SBenjamin Tissoires 
557471d1714SBenjamin Tissoires static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
558471d1714SBenjamin Tissoires {
559471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
560471d1714SBenjamin Tissoires 
561471d1714SBenjamin Tissoires 	list_for_each_entry(data, &wacom_udev_list, list) {
562471d1714SBenjamin Tissoires 		if (wacom_are_sibling(hdev, data->dev)) {
563471d1714SBenjamin Tissoires 			kref_get(&data->kref);
564471d1714SBenjamin Tissoires 			return data;
565471d1714SBenjamin Tissoires 		}
566471d1714SBenjamin Tissoires 	}
567471d1714SBenjamin Tissoires 
568471d1714SBenjamin Tissoires 	return NULL;
569471d1714SBenjamin Tissoires }
570471d1714SBenjamin Tissoires 
571471d1714SBenjamin Tissoires static int wacom_add_shared_data(struct hid_device *hdev)
572471d1714SBenjamin Tissoires {
573471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
574471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
575471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
576471d1714SBenjamin Tissoires 	int retval = 0;
577471d1714SBenjamin Tissoires 
578471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
579471d1714SBenjamin Tissoires 
580471d1714SBenjamin Tissoires 	data = wacom_get_hdev_data(hdev);
581471d1714SBenjamin Tissoires 	if (!data) {
582471d1714SBenjamin Tissoires 		data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
583471d1714SBenjamin Tissoires 		if (!data) {
584471d1714SBenjamin Tissoires 			retval = -ENOMEM;
585471d1714SBenjamin Tissoires 			goto out;
586471d1714SBenjamin Tissoires 		}
587471d1714SBenjamin Tissoires 
588471d1714SBenjamin Tissoires 		kref_init(&data->kref);
589471d1714SBenjamin Tissoires 		data->dev = hdev;
590471d1714SBenjamin Tissoires 		list_add_tail(&data->list, &wacom_udev_list);
591471d1714SBenjamin Tissoires 	}
592471d1714SBenjamin Tissoires 
593471d1714SBenjamin Tissoires 	wacom_wac->shared = &data->shared;
594471d1714SBenjamin Tissoires 
595aa86b18cSJason Gerecke 	if (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)
596a97ac104SBenjamin Tissoires 		wacom_wac->shared->touch = hdev;
597aa86b18cSJason Gerecke 	else if (wacom_wac->features.device_type & WACOM_DEVICETYPE_PEN)
598a97ac104SBenjamin Tissoires 		wacom_wac->shared->pen = hdev;
599a97ac104SBenjamin Tissoires 
600471d1714SBenjamin Tissoires out:
601471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
602471d1714SBenjamin Tissoires 	return retval;
603471d1714SBenjamin Tissoires }
604471d1714SBenjamin Tissoires 
605471d1714SBenjamin Tissoires static void wacom_release_shared_data(struct kref *kref)
606471d1714SBenjamin Tissoires {
607471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data =
608471d1714SBenjamin Tissoires 		container_of(kref, struct wacom_hdev_data, kref);
609471d1714SBenjamin Tissoires 
610471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
611471d1714SBenjamin Tissoires 	list_del(&data->list);
612471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
613471d1714SBenjamin Tissoires 
614471d1714SBenjamin Tissoires 	kfree(data);
615471d1714SBenjamin Tissoires }
616471d1714SBenjamin Tissoires 
617a97ac104SBenjamin Tissoires static void wacom_remove_shared_data(struct wacom *wacom)
618471d1714SBenjamin Tissoires {
619471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
620a97ac104SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
621471d1714SBenjamin Tissoires 
622a97ac104SBenjamin Tissoires 	if (wacom_wac->shared) {
623a97ac104SBenjamin Tissoires 		data = container_of(wacom_wac->shared, struct wacom_hdev_data,
624a97ac104SBenjamin Tissoires 				    shared);
625a97ac104SBenjamin Tissoires 
626a97ac104SBenjamin Tissoires 		if (wacom_wac->shared->touch == wacom->hdev)
627a97ac104SBenjamin Tissoires 			wacom_wac->shared->touch = NULL;
628a97ac104SBenjamin Tissoires 		else if (wacom_wac->shared->pen == wacom->hdev)
629a97ac104SBenjamin Tissoires 			wacom_wac->shared->pen = NULL;
630a97ac104SBenjamin Tissoires 
631471d1714SBenjamin Tissoires 		kref_put(&data->kref, wacom_release_shared_data);
632a97ac104SBenjamin Tissoires 		wacom_wac->shared = NULL;
633471d1714SBenjamin Tissoires 	}
634471d1714SBenjamin Tissoires }
635471d1714SBenjamin Tissoires 
636471d1714SBenjamin Tissoires static int wacom_led_control(struct wacom *wacom)
637471d1714SBenjamin Tissoires {
638471d1714SBenjamin Tissoires 	unsigned char *buf;
639471d1714SBenjamin Tissoires 	int retval;
640912ca216SPing Cheng 	unsigned char report_id = WAC_CMD_LED_CONTROL;
641912ca216SPing Cheng 	int buf_size = 9;
642471d1714SBenjamin Tissoires 
643912ca216SPing Cheng 	if (wacom->wacom_wac.pid) { /* wireless connected */
644912ca216SPing Cheng 		report_id = WAC_CMD_WL_LED_CONTROL;
645912ca216SPing Cheng 		buf_size = 13;
646912ca216SPing Cheng 	}
647912ca216SPing Cheng 	buf = kzalloc(buf_size, GFP_KERNEL);
648471d1714SBenjamin Tissoires 	if (!buf)
649471d1714SBenjamin Tissoires 		return -ENOMEM;
650471d1714SBenjamin Tissoires 
651471d1714SBenjamin Tissoires 	if (wacom->wacom_wac.features.type >= INTUOS5S &&
652471d1714SBenjamin Tissoires 	    wacom->wacom_wac.features.type <= INTUOSPL) {
653471d1714SBenjamin Tissoires 		/*
654471d1714SBenjamin Tissoires 		 * Touch Ring and crop mark LED luminance may take on
655471d1714SBenjamin Tissoires 		 * one of four values:
656471d1714SBenjamin Tissoires 		 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
657471d1714SBenjamin Tissoires 		 */
658471d1714SBenjamin Tissoires 		int ring_led = wacom->led.select[0] & 0x03;
659471d1714SBenjamin Tissoires 		int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
660471d1714SBenjamin Tissoires 		int crop_lum = 0;
661912ca216SPing Cheng 		unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
662471d1714SBenjamin Tissoires 
663912ca216SPing Cheng 		buf[0] = report_id;
664912ca216SPing Cheng 		if (wacom->wacom_wac.pid) {
665912ca216SPing Cheng 			wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
666912ca216SPing Cheng 					 buf, buf_size, WAC_CMD_RETRIES);
667912ca216SPing Cheng 			buf[0] = report_id;
668912ca216SPing Cheng 			buf[4] = led_bits;
669912ca216SPing Cheng 		} else
670912ca216SPing Cheng 			buf[1] = led_bits;
671471d1714SBenjamin Tissoires 	}
672471d1714SBenjamin Tissoires 	else {
673471d1714SBenjamin Tissoires 		int led = wacom->led.select[0] | 0x4;
674471d1714SBenjamin Tissoires 
675471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
676471d1714SBenjamin Tissoires 		    wacom->wacom_wac.features.type == WACOM_24HD)
677471d1714SBenjamin Tissoires 			led |= (wacom->led.select[1] << 4) | 0x40;
678471d1714SBenjamin Tissoires 
679912ca216SPing Cheng 		buf[0] = report_id;
680471d1714SBenjamin Tissoires 		buf[1] = led;
681471d1714SBenjamin Tissoires 		buf[2] = wacom->led.llv;
682471d1714SBenjamin Tissoires 		buf[3] = wacom->led.hlv;
683471d1714SBenjamin Tissoires 		buf[4] = wacom->led.img_lum;
684471d1714SBenjamin Tissoires 	}
685471d1714SBenjamin Tissoires 
686912ca216SPing Cheng 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
687296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
688471d1714SBenjamin Tissoires 	kfree(buf);
689471d1714SBenjamin Tissoires 
690471d1714SBenjamin Tissoires 	return retval;
691471d1714SBenjamin Tissoires }
692471d1714SBenjamin Tissoires 
693849e2f06SBenjamin Tissoires static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
694849e2f06SBenjamin Tissoires 		const unsigned len, const void *img)
695471d1714SBenjamin Tissoires {
696471d1714SBenjamin Tissoires 	unsigned char *buf;
697471d1714SBenjamin Tissoires 	int i, retval;
698849e2f06SBenjamin Tissoires 	const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
699471d1714SBenjamin Tissoires 
700849e2f06SBenjamin Tissoires 	buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
701471d1714SBenjamin Tissoires 	if (!buf)
702471d1714SBenjamin Tissoires 		return -ENOMEM;
703471d1714SBenjamin Tissoires 
704471d1714SBenjamin Tissoires 	/* Send 'start' command */
705471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
706471d1714SBenjamin Tissoires 	buf[1] = 1;
707296b7378SPrzemo Firszt 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
708296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
709471d1714SBenjamin Tissoires 	if (retval < 0)
710471d1714SBenjamin Tissoires 		goto out;
711471d1714SBenjamin Tissoires 
712849e2f06SBenjamin Tissoires 	buf[0] = xfer_id;
713471d1714SBenjamin Tissoires 	buf[1] = button_id & 0x07;
714471d1714SBenjamin Tissoires 	for (i = 0; i < 4; i++) {
715471d1714SBenjamin Tissoires 		buf[2] = i;
716849e2f06SBenjamin Tissoires 		memcpy(buf + 3, img + i * chunk_len, chunk_len);
717471d1714SBenjamin Tissoires 
718471d1714SBenjamin Tissoires 		retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
719296b7378SPrzemo Firszt 					  buf, chunk_len + 3, WAC_CMD_RETRIES);
720471d1714SBenjamin Tissoires 		if (retval < 0)
721471d1714SBenjamin Tissoires 			break;
722471d1714SBenjamin Tissoires 	}
723471d1714SBenjamin Tissoires 
724471d1714SBenjamin Tissoires 	/* Send 'stop' */
725471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
726471d1714SBenjamin Tissoires 	buf[1] = 0;
727296b7378SPrzemo Firszt 	wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
728296b7378SPrzemo Firszt 			 WAC_CMD_RETRIES);
729471d1714SBenjamin Tissoires 
730471d1714SBenjamin Tissoires out:
731471d1714SBenjamin Tissoires 	kfree(buf);
732471d1714SBenjamin Tissoires 	return retval;
733471d1714SBenjamin Tissoires }
734471d1714SBenjamin Tissoires 
735471d1714SBenjamin Tissoires static ssize_t wacom_led_select_store(struct device *dev, int set_id,
736471d1714SBenjamin Tissoires 				      const char *buf, size_t count)
737471d1714SBenjamin Tissoires {
738ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
739471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
740471d1714SBenjamin Tissoires 	unsigned int id;
741471d1714SBenjamin Tissoires 	int err;
742471d1714SBenjamin Tissoires 
743471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &id);
744471d1714SBenjamin Tissoires 	if (err)
745471d1714SBenjamin Tissoires 		return err;
746471d1714SBenjamin Tissoires 
747471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
748471d1714SBenjamin Tissoires 
749471d1714SBenjamin Tissoires 	wacom->led.select[set_id] = id & 0x3;
750471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
751471d1714SBenjamin Tissoires 
752471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
753471d1714SBenjamin Tissoires 
754471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
755471d1714SBenjamin Tissoires }
756471d1714SBenjamin Tissoires 
757471d1714SBenjamin Tissoires #define DEVICE_LED_SELECT_ATTR(SET_ID)					\
758471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
759471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
760471d1714SBenjamin Tissoires {									\
761471d1714SBenjamin Tissoires 	return wacom_led_select_store(dev, SET_ID, buf, count);		\
762471d1714SBenjamin Tissoires }									\
763471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
764471d1714SBenjamin Tissoires 	struct device_attribute *attr, char *buf)			\
765471d1714SBenjamin Tissoires {									\
766ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);\
767471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
76837449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n",			\
76937449adcSPing Cheng 			 wacom->led.select[SET_ID]);			\
770471d1714SBenjamin Tissoires }									\
771e0984bc3SPing Cheng static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,	\
772471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_show,			\
773471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_store)
774471d1714SBenjamin Tissoires 
775471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(0);
776471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(1);
777471d1714SBenjamin Tissoires 
778471d1714SBenjamin Tissoires static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
779471d1714SBenjamin Tissoires 				     const char *buf, size_t count)
780471d1714SBenjamin Tissoires {
781471d1714SBenjamin Tissoires 	unsigned int value;
782471d1714SBenjamin Tissoires 	int err;
783471d1714SBenjamin Tissoires 
784471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &value);
785471d1714SBenjamin Tissoires 	if (err)
786471d1714SBenjamin Tissoires 		return err;
787471d1714SBenjamin Tissoires 
788471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
789471d1714SBenjamin Tissoires 
790471d1714SBenjamin Tissoires 	*dest = value & 0x7f;
791471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
792471d1714SBenjamin Tissoires 
793471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
794471d1714SBenjamin Tissoires 
795471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
796471d1714SBenjamin Tissoires }
797471d1714SBenjamin Tissoires 
798471d1714SBenjamin Tissoires #define DEVICE_LUMINANCE_ATTR(name, field)				\
799471d1714SBenjamin Tissoires static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
800471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
801471d1714SBenjamin Tissoires {									\
802ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);\
803471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
804471d1714SBenjamin Tissoires 									\
805471d1714SBenjamin Tissoires 	return wacom_luminance_store(wacom, &wacom->led.field,		\
806471d1714SBenjamin Tissoires 				     buf, count);			\
807471d1714SBenjamin Tissoires }									\
80837449adcSPing Cheng static ssize_t wacom_##name##_luminance_show(struct device *dev,	\
80937449adcSPing Cheng 	struct device_attribute *attr, char *buf)			\
81037449adcSPing Cheng {									\
81137449adcSPing Cheng 	struct wacom *wacom = dev_get_drvdata(dev);			\
81237449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);	\
81337449adcSPing Cheng }									\
814e0984bc3SPing Cheng static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,			\
81537449adcSPing Cheng 		   wacom_##name##_luminance_show,			\
81637449adcSPing Cheng 		   wacom_##name##_luminance_store)
817471d1714SBenjamin Tissoires 
818471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status0, llv);
819471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status1, hlv);
820471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(buttons, img_lum);
821471d1714SBenjamin Tissoires 
822471d1714SBenjamin Tissoires static ssize_t wacom_button_image_store(struct device *dev, int button_id,
823471d1714SBenjamin Tissoires 					const char *buf, size_t count)
824471d1714SBenjamin Tissoires {
825ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
826471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
827471d1714SBenjamin Tissoires 	int err;
828849e2f06SBenjamin Tissoires 	unsigned len;
829849e2f06SBenjamin Tissoires 	u8 xfer_id;
830471d1714SBenjamin Tissoires 
831849e2f06SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
832849e2f06SBenjamin Tissoires 		len = 256;
833849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_BT_XFER;
834849e2f06SBenjamin Tissoires 	} else {
835849e2f06SBenjamin Tissoires 		len = 1024;
836849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_XFER;
837849e2f06SBenjamin Tissoires 	}
838849e2f06SBenjamin Tissoires 
839849e2f06SBenjamin Tissoires 	if (count != len)
840471d1714SBenjamin Tissoires 		return -EINVAL;
841471d1714SBenjamin Tissoires 
842471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
843471d1714SBenjamin Tissoires 
844849e2f06SBenjamin Tissoires 	err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
845471d1714SBenjamin Tissoires 
846471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
847471d1714SBenjamin Tissoires 
848471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
849471d1714SBenjamin Tissoires }
850471d1714SBenjamin Tissoires 
851471d1714SBenjamin Tissoires #define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
852471d1714SBenjamin Tissoires static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
853471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
854471d1714SBenjamin Tissoires {									\
855471d1714SBenjamin Tissoires 	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
856471d1714SBenjamin Tissoires }									\
857e0984bc3SPing Cheng static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,	\
858471d1714SBenjamin Tissoires 		   NULL, wacom_btnimg##BUTTON_ID##_store)
859471d1714SBenjamin Tissoires 
860471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(0);
861471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(1);
862471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(2);
863471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(3);
864471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(4);
865471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(5);
866471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(6);
867471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(7);
868471d1714SBenjamin Tissoires 
869471d1714SBenjamin Tissoires static struct attribute *cintiq_led_attrs[] = {
870471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
871471d1714SBenjamin Tissoires 	&dev_attr_status_led1_select.attr,
872471d1714SBenjamin Tissoires 	NULL
873471d1714SBenjamin Tissoires };
874471d1714SBenjamin Tissoires 
875471d1714SBenjamin Tissoires static struct attribute_group cintiq_led_attr_group = {
876471d1714SBenjamin Tissoires 	.name = "wacom_led",
877471d1714SBenjamin Tissoires 	.attrs = cintiq_led_attrs,
878471d1714SBenjamin Tissoires };
879471d1714SBenjamin Tissoires 
880471d1714SBenjamin Tissoires static struct attribute *intuos4_led_attrs[] = {
881471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
882471d1714SBenjamin Tissoires 	&dev_attr_status1_luminance.attr,
883471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
884471d1714SBenjamin Tissoires 	&dev_attr_buttons_luminance.attr,
885471d1714SBenjamin Tissoires 	&dev_attr_button0_rawimg.attr,
886471d1714SBenjamin Tissoires 	&dev_attr_button1_rawimg.attr,
887471d1714SBenjamin Tissoires 	&dev_attr_button2_rawimg.attr,
888471d1714SBenjamin Tissoires 	&dev_attr_button3_rawimg.attr,
889471d1714SBenjamin Tissoires 	&dev_attr_button4_rawimg.attr,
890471d1714SBenjamin Tissoires 	&dev_attr_button5_rawimg.attr,
891471d1714SBenjamin Tissoires 	&dev_attr_button6_rawimg.attr,
892471d1714SBenjamin Tissoires 	&dev_attr_button7_rawimg.attr,
893471d1714SBenjamin Tissoires 	NULL
894471d1714SBenjamin Tissoires };
895471d1714SBenjamin Tissoires 
896471d1714SBenjamin Tissoires static struct attribute_group intuos4_led_attr_group = {
897471d1714SBenjamin Tissoires 	.name = "wacom_led",
898471d1714SBenjamin Tissoires 	.attrs = intuos4_led_attrs,
899471d1714SBenjamin Tissoires };
900471d1714SBenjamin Tissoires 
901471d1714SBenjamin Tissoires static struct attribute *intuos5_led_attrs[] = {
902471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
903471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
904471d1714SBenjamin Tissoires 	NULL
905471d1714SBenjamin Tissoires };
906471d1714SBenjamin Tissoires 
907471d1714SBenjamin Tissoires static struct attribute_group intuos5_led_attr_group = {
908471d1714SBenjamin Tissoires 	.name = "wacom_led",
909471d1714SBenjamin Tissoires 	.attrs = intuos5_led_attrs,
910471d1714SBenjamin Tissoires };
911471d1714SBenjamin Tissoires 
912*2df68a88SBenjamin Tissoires struct wacom_sysfs_group_devres {
913*2df68a88SBenjamin Tissoires 	struct attribute_group *group;
914*2df68a88SBenjamin Tissoires 	struct kobject *root;
915*2df68a88SBenjamin Tissoires };
916*2df68a88SBenjamin Tissoires 
917*2df68a88SBenjamin Tissoires static void wacom_devm_sysfs_group_release(struct device *dev, void *res)
918*2df68a88SBenjamin Tissoires {
919*2df68a88SBenjamin Tissoires 	struct wacom_sysfs_group_devres *devres = res;
920*2df68a88SBenjamin Tissoires 	struct kobject *kobj = devres->root;
921*2df68a88SBenjamin Tissoires 
922*2df68a88SBenjamin Tissoires 	dev_dbg(dev, "%s: dropping reference to %s\n",
923*2df68a88SBenjamin Tissoires 		__func__, devres->group->name);
924*2df68a88SBenjamin Tissoires 	sysfs_remove_group(kobj, devres->group);
925*2df68a88SBenjamin Tissoires }
926*2df68a88SBenjamin Tissoires 
927*2df68a88SBenjamin Tissoires static int wacom_devm_sysfs_create_group(struct wacom *wacom,
928*2df68a88SBenjamin Tissoires 					 struct attribute_group *group)
929*2df68a88SBenjamin Tissoires {
930*2df68a88SBenjamin Tissoires 	struct wacom_sysfs_group_devres *devres;
931*2df68a88SBenjamin Tissoires 	int error;
932*2df68a88SBenjamin Tissoires 
933*2df68a88SBenjamin Tissoires 	devres = devres_alloc(wacom_devm_sysfs_group_release,
934*2df68a88SBenjamin Tissoires 			      sizeof(struct wacom_sysfs_group_devres),
935*2df68a88SBenjamin Tissoires 			      GFP_KERNEL);
936*2df68a88SBenjamin Tissoires 	if (!devres)
937*2df68a88SBenjamin Tissoires 		return -ENOMEM;
938*2df68a88SBenjamin Tissoires 
939*2df68a88SBenjamin Tissoires 	devres->group = group;
940*2df68a88SBenjamin Tissoires 	devres->root = &wacom->hdev->dev.kobj;
941*2df68a88SBenjamin Tissoires 
942*2df68a88SBenjamin Tissoires 	error = sysfs_create_group(devres->root, group);
943*2df68a88SBenjamin Tissoires 	if (error)
944*2df68a88SBenjamin Tissoires 		return error;
945*2df68a88SBenjamin Tissoires 
946*2df68a88SBenjamin Tissoires 	devres_add(&wacom->hdev->dev, devres);
947*2df68a88SBenjamin Tissoires 
948*2df68a88SBenjamin Tissoires 	return 0;
949*2df68a88SBenjamin Tissoires }
950*2df68a88SBenjamin Tissoires 
951471d1714SBenjamin Tissoires static int wacom_initialize_leds(struct wacom *wacom)
952471d1714SBenjamin Tissoires {
953471d1714SBenjamin Tissoires 	int error;
954471d1714SBenjamin Tissoires 
955862cf553SJason Gerecke 	if (!(wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD))
956862cf553SJason Gerecke 		return 0;
957862cf553SJason Gerecke 
958471d1714SBenjamin Tissoires 	/* Initialize default values */
959471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
960471d1714SBenjamin Tissoires 	case INTUOS4S:
961471d1714SBenjamin Tissoires 	case INTUOS4:
96281af7e61SBenjamin Tissoires 	case INTUOS4WL:
963471d1714SBenjamin Tissoires 	case INTUOS4L:
964471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
965471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
966471d1714SBenjamin Tissoires 		wacom->led.llv = 10;
967471d1714SBenjamin Tissoires 		wacom->led.hlv = 20;
968471d1714SBenjamin Tissoires 		wacom->led.img_lum = 10;
969*2df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
970471d1714SBenjamin Tissoires 						      &intuos4_led_attr_group);
971471d1714SBenjamin Tissoires 		break;
972471d1714SBenjamin Tissoires 
973471d1714SBenjamin Tissoires 	case WACOM_24HD:
974471d1714SBenjamin Tissoires 	case WACOM_21UX2:
975471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
976471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
977471d1714SBenjamin Tissoires 		wacom->led.llv = 0;
978471d1714SBenjamin Tissoires 		wacom->led.hlv = 0;
979471d1714SBenjamin Tissoires 		wacom->led.img_lum = 0;
980471d1714SBenjamin Tissoires 
981*2df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
982471d1714SBenjamin Tissoires 						      &cintiq_led_attr_group);
983471d1714SBenjamin Tissoires 		break;
984471d1714SBenjamin Tissoires 
985471d1714SBenjamin Tissoires 	case INTUOS5S:
986471d1714SBenjamin Tissoires 	case INTUOS5:
987471d1714SBenjamin Tissoires 	case INTUOS5L:
988471d1714SBenjamin Tissoires 	case INTUOSPS:
989471d1714SBenjamin Tissoires 	case INTUOSPM:
990471d1714SBenjamin Tissoires 	case INTUOSPL:
991471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
992471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
993471d1714SBenjamin Tissoires 		wacom->led.llv = 32;
994471d1714SBenjamin Tissoires 		wacom->led.hlv = 0;
995471d1714SBenjamin Tissoires 		wacom->led.img_lum = 0;
996471d1714SBenjamin Tissoires 
997*2df68a88SBenjamin Tissoires 		error = wacom_devm_sysfs_create_group(wacom,
998471d1714SBenjamin Tissoires 						      &intuos5_led_attr_group);
999471d1714SBenjamin Tissoires 		break;
1000471d1714SBenjamin Tissoires 
1001471d1714SBenjamin Tissoires 	default:
1002471d1714SBenjamin Tissoires 		return 0;
1003471d1714SBenjamin Tissoires 	}
1004471d1714SBenjamin Tissoires 
1005471d1714SBenjamin Tissoires 	if (error) {
1006471d1714SBenjamin Tissoires 		hid_err(wacom->hdev,
1007471d1714SBenjamin Tissoires 			"cannot create sysfs group err: %d\n", error);
1008471d1714SBenjamin Tissoires 		return error;
1009471d1714SBenjamin Tissoires 	}
1010471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
1011471d1714SBenjamin Tissoires 
1012471d1714SBenjamin Tissoires 	return 0;
1013471d1714SBenjamin Tissoires }
1014471d1714SBenjamin Tissoires 
1015471d1714SBenjamin Tissoires static enum power_supply_property wacom_battery_props[] = {
101671fa641eSJason Gerecke 	POWER_SUPPLY_PROP_PRESENT,
1017ac8d1010SBenjamin Tissoires 	POWER_SUPPLY_PROP_STATUS,
1018471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
1019471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_CAPACITY
1020471d1714SBenjamin Tissoires };
1021471d1714SBenjamin Tissoires 
10227dbd229eSBenjamin Tissoires static enum power_supply_property wacom_ac_props[] = {
10237dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_PRESENT,
10247dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_ONLINE,
10257dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
10267dbd229eSBenjamin Tissoires };
10277dbd229eSBenjamin Tissoires 
1028471d1714SBenjamin Tissoires static int wacom_battery_get_property(struct power_supply *psy,
1029471d1714SBenjamin Tissoires 				      enum power_supply_property psp,
1030471d1714SBenjamin Tissoires 				      union power_supply_propval *val)
1031471d1714SBenjamin Tissoires {
1032297d716fSKrzysztof Kozlowski 	struct wacom *wacom = power_supply_get_drvdata(psy);
1033471d1714SBenjamin Tissoires 	int ret = 0;
1034471d1714SBenjamin Tissoires 
1035471d1714SBenjamin Tissoires 	switch (psp) {
103671fa641eSJason Gerecke 		case POWER_SUPPLY_PROP_PRESENT:
103771fa641eSJason Gerecke 			val->intval = wacom->wacom_wac.bat_connected;
103871fa641eSJason Gerecke 			break;
1039471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_SCOPE:
1040471d1714SBenjamin Tissoires 			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1041471d1714SBenjamin Tissoires 			break;
1042471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_CAPACITY:
1043471d1714SBenjamin Tissoires 			val->intval =
1044ac8d1010SBenjamin Tissoires 				wacom->wacom_wac.battery_capacity;
1045ac8d1010SBenjamin Tissoires 			break;
1046ac8d1010SBenjamin Tissoires 		case POWER_SUPPLY_PROP_STATUS:
1047ac8d1010SBenjamin Tissoires 			if (wacom->wacom_wac.bat_charging)
1048ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_CHARGING;
1049ac8d1010SBenjamin Tissoires 			else if (wacom->wacom_wac.battery_capacity == 100 &&
1050ac8d1010SBenjamin Tissoires 				    wacom->wacom_wac.ps_connected)
1051ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_FULL;
1052b0882cb7SJason Gerecke 			else if (wacom->wacom_wac.ps_connected)
1053b0882cb7SJason Gerecke 				val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1054ac8d1010SBenjamin Tissoires 			else
1055ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1056471d1714SBenjamin Tissoires 			break;
1057471d1714SBenjamin Tissoires 		default:
1058471d1714SBenjamin Tissoires 			ret = -EINVAL;
1059471d1714SBenjamin Tissoires 			break;
1060471d1714SBenjamin Tissoires 	}
1061471d1714SBenjamin Tissoires 
1062471d1714SBenjamin Tissoires 	return ret;
1063471d1714SBenjamin Tissoires }
1064471d1714SBenjamin Tissoires 
10657dbd229eSBenjamin Tissoires static int wacom_ac_get_property(struct power_supply *psy,
10667dbd229eSBenjamin Tissoires 				enum power_supply_property psp,
10677dbd229eSBenjamin Tissoires 				union power_supply_propval *val)
10687dbd229eSBenjamin Tissoires {
1069297d716fSKrzysztof Kozlowski 	struct wacom *wacom = power_supply_get_drvdata(psy);
10707dbd229eSBenjamin Tissoires 	int ret = 0;
10717dbd229eSBenjamin Tissoires 
10727dbd229eSBenjamin Tissoires 	switch (psp) {
10737dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_PRESENT:
10747dbd229eSBenjamin Tissoires 		/* fall through */
10757dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_ONLINE:
10767dbd229eSBenjamin Tissoires 		val->intval = wacom->wacom_wac.ps_connected;
10777dbd229eSBenjamin Tissoires 		break;
10787dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_SCOPE:
10797dbd229eSBenjamin Tissoires 		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
10807dbd229eSBenjamin Tissoires 		break;
10817dbd229eSBenjamin Tissoires 	default:
10827dbd229eSBenjamin Tissoires 		ret = -EINVAL;
10837dbd229eSBenjamin Tissoires 		break;
10847dbd229eSBenjamin Tissoires 	}
10857dbd229eSBenjamin Tissoires 	return ret;
10867dbd229eSBenjamin Tissoires }
10877dbd229eSBenjamin Tissoires 
1088471d1714SBenjamin Tissoires static int wacom_initialize_battery(struct wacom *wacom)
1089471d1714SBenjamin Tissoires {
1090d70420b9SBenjamin Tissoires 	static atomic_t battery_no = ATOMIC_INIT(0);
1091b189da90SBenjamin Tissoires 	struct device *dev = &wacom->hdev->dev;
1092297d716fSKrzysztof Kozlowski 	struct power_supply_config psy_cfg = { .drv_data = wacom, };
1093b189da90SBenjamin Tissoires 	struct power_supply_desc *bat_desc = &wacom->battery_desc;
1094d70420b9SBenjamin Tissoires 	unsigned long n;
1095b189da90SBenjamin Tissoires 	int error;
1096b189da90SBenjamin Tissoires 
1097b189da90SBenjamin Tissoires 	if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1098b189da90SBenjamin Tissoires 		return -ENOMEM;
1099471d1714SBenjamin Tissoires 
1100ac8d1010SBenjamin Tissoires 	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
1101297d716fSKrzysztof Kozlowski 		struct power_supply_desc *ac_desc = &wacom->ac_desc;
1102d70420b9SBenjamin Tissoires 		n = atomic_inc_return(&battery_no) - 1;
11037dbd229eSBenjamin Tissoires 
1104297d716fSKrzysztof Kozlowski 		bat_desc->properties = wacom_battery_props;
1105297d716fSKrzysztof Kozlowski 		bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1106297d716fSKrzysztof Kozlowski 		bat_desc->get_property = wacom_battery_get_property;
1107d70420b9SBenjamin Tissoires 		sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
1108297d716fSKrzysztof Kozlowski 		bat_desc->name = wacom->wacom_wac.bat_name;
1109297d716fSKrzysztof Kozlowski 		bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1110297d716fSKrzysztof Kozlowski 		bat_desc->use_for_apm = 0;
1111471d1714SBenjamin Tissoires 
1112297d716fSKrzysztof Kozlowski 		ac_desc->properties = wacom_ac_props;
1113297d716fSKrzysztof Kozlowski 		ac_desc->num_properties = ARRAY_SIZE(wacom_ac_props);
1114297d716fSKrzysztof Kozlowski 		ac_desc->get_property = wacom_ac_get_property;
11157dbd229eSBenjamin Tissoires 		sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
1116297d716fSKrzysztof Kozlowski 		ac_desc->name = wacom->wacom_wac.ac_name;
1117297d716fSKrzysztof Kozlowski 		ac_desc->type = POWER_SUPPLY_TYPE_MAINS;
1118297d716fSKrzysztof Kozlowski 		ac_desc->use_for_apm = 0;
11197dbd229eSBenjamin Tissoires 
1120b189da90SBenjamin Tissoires 		wacom->battery = devm_power_supply_register(dev,
1121b189da90SBenjamin Tissoires 							   &wacom->battery_desc,
1122b189da90SBenjamin Tissoires 							   &psy_cfg);
1123b189da90SBenjamin Tissoires 		if (IS_ERR(wacom->battery)) {
1124b189da90SBenjamin Tissoires 			error = PTR_ERR(wacom->battery);
1125b189da90SBenjamin Tissoires 			goto err;
1126b189da90SBenjamin Tissoires 		}
1127471d1714SBenjamin Tissoires 
1128297d716fSKrzysztof Kozlowski 		power_supply_powers(wacom->battery, &wacom->hdev->dev);
11297dbd229eSBenjamin Tissoires 
1130b189da90SBenjamin Tissoires 		wacom->ac = devm_power_supply_register(dev,
1131297d716fSKrzysztof Kozlowski 						       &wacom->ac_desc,
1132297d716fSKrzysztof Kozlowski 						       &psy_cfg);
1133297d716fSKrzysztof Kozlowski 		if (IS_ERR(wacom->ac)) {
1134b189da90SBenjamin Tissoires 			error = PTR_ERR(wacom->ac);
1135b189da90SBenjamin Tissoires 			goto err;
1136471d1714SBenjamin Tissoires 		}
1137471d1714SBenjamin Tissoires 
1138297d716fSKrzysztof Kozlowski 		power_supply_powers(wacom->ac, &wacom->hdev->dev);
11397dbd229eSBenjamin Tissoires 	}
11407dbd229eSBenjamin Tissoires 
1141b189da90SBenjamin Tissoires 	devres_close_group(dev, bat_desc);
11427dbd229eSBenjamin Tissoires 	return 0;
1143b189da90SBenjamin Tissoires 
1144b189da90SBenjamin Tissoires err:
1145b189da90SBenjamin Tissoires 	devres_release_group(dev, bat_desc);
1146b189da90SBenjamin Tissoires 	return error;
1147471d1714SBenjamin Tissoires }
1148471d1714SBenjamin Tissoires 
1149471d1714SBenjamin Tissoires static void wacom_destroy_battery(struct wacom *wacom)
1150471d1714SBenjamin Tissoires {
11518de29a35SLinus Torvalds 	if (wacom->battery) {
1152b189da90SBenjamin Tissoires 		devres_release_group(&wacom->hdev->dev, &wacom->battery_desc);
1153297d716fSKrzysztof Kozlowski 		wacom->battery = NULL;
1154297d716fSKrzysztof Kozlowski 		wacom->ac = NULL;
1155471d1714SBenjamin Tissoires 	}
1156471d1714SBenjamin Tissoires }
1157471d1714SBenjamin Tissoires 
1158f81a1295SBenjamin Tissoires static ssize_t wacom_show_speed(struct device *dev,
1159f81a1295SBenjamin Tissoires 				struct device_attribute
1160f81a1295SBenjamin Tissoires 				*attr, char *buf)
1161f81a1295SBenjamin Tissoires {
1162ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1163f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1164f81a1295SBenjamin Tissoires 
1165f81a1295SBenjamin Tissoires 	return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1166f81a1295SBenjamin Tissoires }
1167f81a1295SBenjamin Tissoires 
1168f81a1295SBenjamin Tissoires static ssize_t wacom_store_speed(struct device *dev,
1169f81a1295SBenjamin Tissoires 				struct device_attribute *attr,
1170f81a1295SBenjamin Tissoires 				const char *buf, size_t count)
1171f81a1295SBenjamin Tissoires {
1172ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
1173f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1174f81a1295SBenjamin Tissoires 	u8 new_speed;
1175f81a1295SBenjamin Tissoires 
1176f81a1295SBenjamin Tissoires 	if (kstrtou8(buf, 0, &new_speed))
1177f81a1295SBenjamin Tissoires 		return -EINVAL;
1178f81a1295SBenjamin Tissoires 
1179f81a1295SBenjamin Tissoires 	if (new_speed != 0 && new_speed != 1)
1180f81a1295SBenjamin Tissoires 		return -EINVAL;
1181f81a1295SBenjamin Tissoires 
1182f81a1295SBenjamin Tissoires 	wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1183f81a1295SBenjamin Tissoires 
1184f81a1295SBenjamin Tissoires 	return count;
1185f81a1295SBenjamin Tissoires }
1186f81a1295SBenjamin Tissoires 
1187e0984bc3SPing Cheng static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1188f81a1295SBenjamin Tissoires 		wacom_show_speed, wacom_store_speed);
1189f81a1295SBenjamin Tissoires 
119072b236d6SAaron Skomra 
119172b236d6SAaron Skomra static ssize_t wacom_show_remote_mode(struct kobject *kobj,
119272b236d6SAaron Skomra 				      struct kobj_attribute *kattr,
119372b236d6SAaron Skomra 				      char *buf, int index)
119472b236d6SAaron Skomra {
11952cf83833SGeliang Tang 	struct device *dev = kobj_to_dev(kobj->parent);
1196ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
119772b236d6SAaron Skomra 	struct wacom *wacom = hid_get_drvdata(hdev);
119872b236d6SAaron Skomra 	u8 mode;
119972b236d6SAaron Skomra 
120072b236d6SAaron Skomra 	mode = wacom->led.select[index];
120172b236d6SAaron Skomra 	if (mode >= 0 && mode < 3)
120272b236d6SAaron Skomra 		return snprintf(buf, PAGE_SIZE, "%d\n", mode);
120372b236d6SAaron Skomra 	else
120472b236d6SAaron Skomra 		return snprintf(buf, PAGE_SIZE, "%d\n", -1);
120572b236d6SAaron Skomra }
120672b236d6SAaron Skomra 
120772b236d6SAaron Skomra #define DEVICE_EKR_ATTR_GROUP(SET_ID)					\
120872b236d6SAaron Skomra static ssize_t wacom_show_remote##SET_ID##_mode(struct kobject *kobj,	\
120972b236d6SAaron Skomra 			       struct kobj_attribute *kattr, char *buf)	\
121072b236d6SAaron Skomra {									\
121172b236d6SAaron Skomra 	return wacom_show_remote_mode(kobj, kattr, buf, SET_ID);	\
121272b236d6SAaron Skomra }									\
121372b236d6SAaron Skomra static struct kobj_attribute remote##SET_ID##_mode_attr = {		\
121472b236d6SAaron Skomra 	.attr = {.name = "remote_mode",					\
121572b236d6SAaron Skomra 		.mode = DEV_ATTR_RO_PERM},				\
121672b236d6SAaron Skomra 	.show = wacom_show_remote##SET_ID##_mode,			\
121772b236d6SAaron Skomra };									\
121872b236d6SAaron Skomra static struct attribute *remote##SET_ID##_serial_attrs[] = {		\
121972b236d6SAaron Skomra 	&remote##SET_ID##_mode_attr.attr,				\
122072b236d6SAaron Skomra 	NULL								\
122172b236d6SAaron Skomra };									\
122272b236d6SAaron Skomra static struct attribute_group remote##SET_ID##_serial_group = {		\
122372b236d6SAaron Skomra 	.name = NULL,							\
122472b236d6SAaron Skomra 	.attrs = remote##SET_ID##_serial_attrs,				\
122572b236d6SAaron Skomra }
122672b236d6SAaron Skomra 
122772b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(0);
122872b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(1);
122972b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(2);
123072b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(3);
123172b236d6SAaron Skomra DEVICE_EKR_ATTR_GROUP(4);
123272b236d6SAaron Skomra 
123372b236d6SAaron Skomra int wacom_remote_create_attr_group(struct wacom *wacom, __u32 serial, int index)
123472b236d6SAaron Skomra {
123572b236d6SAaron Skomra 	int error = 0;
123672b236d6SAaron Skomra 	char *buf;
123772b236d6SAaron Skomra 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
123872b236d6SAaron Skomra 
123972b236d6SAaron Skomra 	wacom_wac->serial[index] = serial;
124072b236d6SAaron Skomra 
124172b236d6SAaron Skomra 	buf = kzalloc(WAC_REMOTE_SERIAL_MAX_STRLEN, GFP_KERNEL);
124272b236d6SAaron Skomra 	if (!buf)
124372b236d6SAaron Skomra 		return -ENOMEM;
124472b236d6SAaron Skomra 	snprintf(buf, WAC_REMOTE_SERIAL_MAX_STRLEN, "%d", serial);
124572b236d6SAaron Skomra 	wacom->remote_group[index].name = buf;
124672b236d6SAaron Skomra 
124772b236d6SAaron Skomra 	error = sysfs_create_group(wacom->remote_dir,
124872b236d6SAaron Skomra 				   &wacom->remote_group[index]);
124972b236d6SAaron Skomra 	if (error) {
125072b236d6SAaron Skomra 		hid_err(wacom->hdev,
125172b236d6SAaron Skomra 			"cannot create sysfs group err: %d\n", error);
125272b236d6SAaron Skomra 		kobject_put(wacom->remote_dir);
125372b236d6SAaron Skomra 		return error;
125472b236d6SAaron Skomra 	}
125572b236d6SAaron Skomra 
125672b236d6SAaron Skomra 	return 0;
125772b236d6SAaron Skomra }
125872b236d6SAaron Skomra 
125972b236d6SAaron Skomra void wacom_remote_destroy_attr_group(struct wacom *wacom, __u32 serial)
126072b236d6SAaron Skomra {
126172b236d6SAaron Skomra 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
126272b236d6SAaron Skomra 	int i;
126372b236d6SAaron Skomra 
126472b236d6SAaron Skomra 	if (!serial)
126572b236d6SAaron Skomra 		return;
126672b236d6SAaron Skomra 
126772b236d6SAaron Skomra 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
126872b236d6SAaron Skomra 		if (wacom_wac->serial[i] == serial) {
126972b236d6SAaron Skomra 			wacom_wac->serial[i] = 0;
127072b236d6SAaron Skomra 			wacom->led.select[i] = WACOM_STATUS_UNKNOWN;
127172b236d6SAaron Skomra 			if (wacom->remote_group[i].name) {
127272b236d6SAaron Skomra 				sysfs_remove_group(wacom->remote_dir,
127372b236d6SAaron Skomra 						   &wacom->remote_group[i]);
127472b236d6SAaron Skomra 				kfree(wacom->remote_group[i].name);
127572b236d6SAaron Skomra 				wacom->remote_group[i].name = NULL;
127672b236d6SAaron Skomra 			}
127772b236d6SAaron Skomra 		}
127872b236d6SAaron Skomra 	}
127972b236d6SAaron Skomra }
128072b236d6SAaron Skomra 
128172b236d6SAaron Skomra static int wacom_cmd_unpair_remote(struct wacom *wacom, unsigned char selector)
128272b236d6SAaron Skomra {
128372b236d6SAaron Skomra 	const size_t buf_size = 2;
128472b236d6SAaron Skomra 	unsigned char *buf;
128572b236d6SAaron Skomra 	int retval;
128672b236d6SAaron Skomra 
128772b236d6SAaron Skomra 	buf = kzalloc(buf_size, GFP_KERNEL);
128872b236d6SAaron Skomra 	if (!buf)
128972b236d6SAaron Skomra 		return -ENOMEM;
129072b236d6SAaron Skomra 
129172b236d6SAaron Skomra 	buf[0] = WAC_CMD_DELETE_PAIRING;
129272b236d6SAaron Skomra 	buf[1] = selector;
129372b236d6SAaron Skomra 
129472b236d6SAaron Skomra 	retval = wacom_set_report(wacom->hdev, HID_OUTPUT_REPORT, buf,
129572b236d6SAaron Skomra 				  buf_size, WAC_CMD_RETRIES);
129672b236d6SAaron Skomra 	kfree(buf);
129772b236d6SAaron Skomra 
129872b236d6SAaron Skomra 	return retval;
129972b236d6SAaron Skomra }
130072b236d6SAaron Skomra 
130172b236d6SAaron Skomra static ssize_t wacom_store_unpair_remote(struct kobject *kobj,
130272b236d6SAaron Skomra 					 struct kobj_attribute *attr,
130372b236d6SAaron Skomra 					 const char *buf, size_t count)
130472b236d6SAaron Skomra {
130572b236d6SAaron Skomra 	unsigned char selector = 0;
13062cf83833SGeliang Tang 	struct device *dev = kobj_to_dev(kobj->parent);
1307ee79a8f8SGeliang Tang 	struct hid_device *hdev = to_hid_device(dev);
130872b236d6SAaron Skomra 	struct wacom *wacom = hid_get_drvdata(hdev);
130972b236d6SAaron Skomra 	int err;
131072b236d6SAaron Skomra 
131172b236d6SAaron Skomra 	if (!strncmp(buf, "*\n", 2)) {
131272b236d6SAaron Skomra 		selector = WAC_CMD_UNPAIR_ALL;
131372b236d6SAaron Skomra 	} else {
131472b236d6SAaron Skomra 		hid_info(wacom->hdev, "remote: unrecognized unpair code: %s\n",
131572b236d6SAaron Skomra 			 buf);
131672b236d6SAaron Skomra 		return -1;
131772b236d6SAaron Skomra 	}
131872b236d6SAaron Skomra 
131972b236d6SAaron Skomra 	mutex_lock(&wacom->lock);
132072b236d6SAaron Skomra 
132172b236d6SAaron Skomra 	err = wacom_cmd_unpair_remote(wacom, selector);
132272b236d6SAaron Skomra 	mutex_unlock(&wacom->lock);
132372b236d6SAaron Skomra 
132472b236d6SAaron Skomra 	return err < 0 ? err : count;
132572b236d6SAaron Skomra }
132672b236d6SAaron Skomra 
132772b236d6SAaron Skomra static struct kobj_attribute unpair_remote_attr = {
132872b236d6SAaron Skomra 	.attr = {.name = "unpair_remote", .mode = 0200},
132972b236d6SAaron Skomra 	.store = wacom_store_unpair_remote,
133072b236d6SAaron Skomra };
133172b236d6SAaron Skomra 
133272b236d6SAaron Skomra static const struct attribute *remote_unpair_attrs[] = {
133372b236d6SAaron Skomra 	&unpair_remote_attr.attr,
133472b236d6SAaron Skomra 	NULL
133572b236d6SAaron Skomra };
133672b236d6SAaron Skomra 
133772b236d6SAaron Skomra static int wacom_initialize_remote(struct wacom *wacom)
133872b236d6SAaron Skomra {
133972b236d6SAaron Skomra 	int error = 0;
134072b236d6SAaron Skomra 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
134172b236d6SAaron Skomra 	int i;
134272b236d6SAaron Skomra 
134372b236d6SAaron Skomra 	if (wacom->wacom_wac.features.type != REMOTE)
134472b236d6SAaron Skomra 		return 0;
134572b236d6SAaron Skomra 
134672b236d6SAaron Skomra 	wacom->remote_group[0] = remote0_serial_group;
134772b236d6SAaron Skomra 	wacom->remote_group[1] = remote1_serial_group;
134872b236d6SAaron Skomra 	wacom->remote_group[2] = remote2_serial_group;
134972b236d6SAaron Skomra 	wacom->remote_group[3] = remote3_serial_group;
135072b236d6SAaron Skomra 	wacom->remote_group[4] = remote4_serial_group;
135172b236d6SAaron Skomra 
135272b236d6SAaron Skomra 	wacom->remote_dir = kobject_create_and_add("wacom_remote",
135372b236d6SAaron Skomra 						   &wacom->hdev->dev.kobj);
135472b236d6SAaron Skomra 	if (!wacom->remote_dir)
135572b236d6SAaron Skomra 		return -ENOMEM;
135672b236d6SAaron Skomra 
135772b236d6SAaron Skomra 	error = sysfs_create_files(wacom->remote_dir, remote_unpair_attrs);
135872b236d6SAaron Skomra 
135972b236d6SAaron Skomra 	if (error) {
136072b236d6SAaron Skomra 		hid_err(wacom->hdev,
136172b236d6SAaron Skomra 			"cannot create sysfs group err: %d\n", error);
136272b236d6SAaron Skomra 		return error;
136372b236d6SAaron Skomra 	}
136472b236d6SAaron Skomra 
136572b236d6SAaron Skomra 	for (i = 0; i < WACOM_MAX_REMOTES; i++) {
136672b236d6SAaron Skomra 		wacom->led.select[i] = WACOM_STATUS_UNKNOWN;
136772b236d6SAaron Skomra 		wacom_wac->serial[i] = 0;
136872b236d6SAaron Skomra 	}
136972b236d6SAaron Skomra 
137072b236d6SAaron Skomra 	return 0;
137172b236d6SAaron Skomra }
137272b236d6SAaron Skomra 
1373471d1714SBenjamin Tissoires static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1374471d1714SBenjamin Tissoires {
1375471d1714SBenjamin Tissoires 	struct input_dev *input_dev;
1376471d1714SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
1377471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1378471d1714SBenjamin Tissoires 
13793dad188eSBenjamin Tissoires 	input_dev = devm_input_allocate_device(&hdev->dev);
1380471d1714SBenjamin Tissoires 	if (!input_dev)
1381471d1714SBenjamin Tissoires 		return NULL;
1382471d1714SBenjamin Tissoires 
13832bdd163cSJason Gerecke 	input_dev->name = wacom_wac->features.name;
1384471d1714SBenjamin Tissoires 	input_dev->phys = hdev->phys;
1385471d1714SBenjamin Tissoires 	input_dev->dev.parent = &hdev->dev;
1386471d1714SBenjamin Tissoires 	input_dev->open = wacom_open;
1387471d1714SBenjamin Tissoires 	input_dev->close = wacom_close;
1388471d1714SBenjamin Tissoires 	input_dev->uniq = hdev->uniq;
1389471d1714SBenjamin Tissoires 	input_dev->id.bustype = hdev->bus;
1390471d1714SBenjamin Tissoires 	input_dev->id.vendor  = hdev->vendor;
139112969e3bSBenjamin Tissoires 	input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1392471d1714SBenjamin Tissoires 	input_dev->id.version = hdev->version;
1393471d1714SBenjamin Tissoires 	input_set_drvdata(input_dev, wacom);
1394471d1714SBenjamin Tissoires 
1395471d1714SBenjamin Tissoires 	return input_dev;
1396471d1714SBenjamin Tissoires }
1397471d1714SBenjamin Tissoires 
1398d9f2d203SJason Gerecke static int wacom_allocate_inputs(struct wacom *wacom)
1399d9f2d203SJason Gerecke {
1400d9f2d203SJason Gerecke 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1401d9f2d203SJason Gerecke 
1402d9f2d203SJason Gerecke 	wacom_wac->pen_input = wacom_allocate_input(wacom);
1403d9f2d203SJason Gerecke 	wacom_wac->touch_input = wacom_allocate_input(wacom);
1404d9f2d203SJason Gerecke 	wacom_wac->pad_input = wacom_allocate_input(wacom);
14053dad188eSBenjamin Tissoires 	if (!wacom_wac->pen_input ||
14063dad188eSBenjamin Tissoires 	    !wacom_wac->touch_input ||
14073dad188eSBenjamin Tissoires 	    !wacom_wac->pad_input)
1408d9f2d203SJason Gerecke 		return -ENOMEM;
1409d9f2d203SJason Gerecke 
14102bdd163cSJason Gerecke 	wacom_wac->pen_input->name = wacom_wac->pen_name;
1411d9f2d203SJason Gerecke 	wacom_wac->touch_input->name = wacom_wac->touch_name;
1412d9f2d203SJason Gerecke 	wacom_wac->pad_input->name = wacom_wac->pad_name;
1413d9f2d203SJason Gerecke 
1414d9f2d203SJason Gerecke 	return 0;
1415d9f2d203SJason Gerecke }
1416d9f2d203SJason Gerecke 
1417471d1714SBenjamin Tissoires static int wacom_register_inputs(struct wacom *wacom)
1418471d1714SBenjamin Tissoires {
14192a6cdbddSJason Gerecke 	struct input_dev *pen_input_dev, *touch_input_dev, *pad_input_dev;
1420471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
14212636a3f2SJason Gerecke 	int error = 0;
1422471d1714SBenjamin Tissoires 
14232a6cdbddSJason Gerecke 	pen_input_dev = wacom_wac->pen_input;
14242a6cdbddSJason Gerecke 	touch_input_dev = wacom_wac->touch_input;
14252546dacdSBenjamin Tissoires 	pad_input_dev = wacom_wac->pad_input;
1426471d1714SBenjamin Tissoires 
14272a6cdbddSJason Gerecke 	if (!pen_input_dev || !touch_input_dev || !pad_input_dev)
14282546dacdSBenjamin Tissoires 		return -EINVAL;
1429471d1714SBenjamin Tissoires 
14302a6cdbddSJason Gerecke 	error = wacom_setup_pen_input_capabilities(pen_input_dev, wacom_wac);
14312a6cdbddSJason Gerecke 	if (error) {
14322a6cdbddSJason Gerecke 		/* no pen in use on this interface */
14332a6cdbddSJason Gerecke 		input_free_device(pen_input_dev);
14342a6cdbddSJason Gerecke 		wacom_wac->pen_input = NULL;
14352a6cdbddSJason Gerecke 		pen_input_dev = NULL;
14362a6cdbddSJason Gerecke 	} else {
14372a6cdbddSJason Gerecke 		error = input_register_device(pen_input_dev);
1438471d1714SBenjamin Tissoires 		if (error)
14393dad188eSBenjamin Tissoires 			goto fail;
14402a6cdbddSJason Gerecke 	}
14412a6cdbddSJason Gerecke 
14422a6cdbddSJason Gerecke 	error = wacom_setup_touch_input_capabilities(touch_input_dev, wacom_wac);
14432a6cdbddSJason Gerecke 	if (error) {
14442a6cdbddSJason Gerecke 		/* no touch in use on this interface */
14452a6cdbddSJason Gerecke 		input_free_device(touch_input_dev);
14462a6cdbddSJason Gerecke 		wacom_wac->touch_input = NULL;
14472a6cdbddSJason Gerecke 		touch_input_dev = NULL;
14482a6cdbddSJason Gerecke 	} else {
14492a6cdbddSJason Gerecke 		error = input_register_device(touch_input_dev);
14502a6cdbddSJason Gerecke 		if (error)
14513dad188eSBenjamin Tissoires 			goto fail;
145230ebc1aeSPing Cheng 	}
1453471d1714SBenjamin Tissoires 
1454471d1714SBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1455471d1714SBenjamin Tissoires 	if (error) {
1456471d1714SBenjamin Tissoires 		/* no pad in use on this interface */
1457471d1714SBenjamin Tissoires 		input_free_device(pad_input_dev);
1458471d1714SBenjamin Tissoires 		wacom_wac->pad_input = NULL;
1459471d1714SBenjamin Tissoires 		pad_input_dev = NULL;
1460471d1714SBenjamin Tissoires 	} else {
1461471d1714SBenjamin Tissoires 		error = input_register_device(pad_input_dev);
1462471d1714SBenjamin Tissoires 		if (error)
14633dad188eSBenjamin Tissoires 			goto fail;
1464471d1714SBenjamin Tissoires 	}
1465471d1714SBenjamin Tissoires 
1466471d1714SBenjamin Tissoires 	return 0;
1467471d1714SBenjamin Tissoires 
14683dad188eSBenjamin Tissoires fail:
14693dad188eSBenjamin Tissoires 	wacom_wac->pad_input = NULL;
14702a6cdbddSJason Gerecke 	wacom_wac->touch_input = NULL;
14712a6cdbddSJason Gerecke 	wacom_wac->pen_input = NULL;
1472471d1714SBenjamin Tissoires 	return error;
1473471d1714SBenjamin Tissoires }
1474471d1714SBenjamin Tissoires 
14750be01712SJason Gerecke /*
14760be01712SJason Gerecke  * Not all devices report physical dimensions from HID.
14770be01712SJason Gerecke  * Compute the default from hardcoded logical dimension
14780be01712SJason Gerecke  * and resolution before driver overwrites them.
14790be01712SJason Gerecke  */
14800be01712SJason Gerecke static void wacom_set_default_phy(struct wacom_features *features)
14810be01712SJason Gerecke {
14820be01712SJason Gerecke 	if (features->x_resolution) {
14830be01712SJason Gerecke 		features->x_phy = (features->x_max * 100) /
14840be01712SJason Gerecke 					features->x_resolution;
14850be01712SJason Gerecke 		features->y_phy = (features->y_max * 100) /
14860be01712SJason Gerecke 					features->y_resolution;
14870be01712SJason Gerecke 	}
14880be01712SJason Gerecke }
14890be01712SJason Gerecke 
14900be01712SJason Gerecke static void wacom_calculate_res(struct wacom_features *features)
14910be01712SJason Gerecke {
14920be01712SJason Gerecke 	/* set unit to "100th of a mm" for devices not reported by HID */
14930be01712SJason Gerecke 	if (!features->unit) {
14940be01712SJason Gerecke 		features->unit = 0x11;
14950be01712SJason Gerecke 		features->unitExpo = -3;
14960be01712SJason Gerecke 	}
14970be01712SJason Gerecke 
14980be01712SJason Gerecke 	features->x_resolution = wacom_calc_hid_res(features->x_max,
14990be01712SJason Gerecke 						    features->x_phy,
15000be01712SJason Gerecke 						    features->unit,
15010be01712SJason Gerecke 						    features->unitExpo);
15020be01712SJason Gerecke 	features->y_resolution = wacom_calc_hid_res(features->y_max,
15030be01712SJason Gerecke 						    features->y_phy,
15040be01712SJason Gerecke 						    features->unit,
15050be01712SJason Gerecke 						    features->unitExpo);
15060be01712SJason Gerecke }
15070be01712SJason Gerecke 
1508fce9957dSJason Gerecke void wacom_battery_work(struct work_struct *work)
1509fce9957dSJason Gerecke {
1510d17d1f17SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, battery_work);
1511fce9957dSJason Gerecke 
1512fce9957dSJason Gerecke 	if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
15138de29a35SLinus Torvalds 	     !wacom->battery) {
1514fce9957dSJason Gerecke 		wacom_initialize_battery(wacom);
1515fce9957dSJason Gerecke 	}
1516fce9957dSJason Gerecke 	else if (!(wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
15178de29a35SLinus Torvalds 		 wacom->battery) {
1518fce9957dSJason Gerecke 		wacom_destroy_battery(wacom);
1519fce9957dSJason Gerecke 	}
1520fce9957dSJason Gerecke }
1521fce9957dSJason Gerecke 
1522471d1714SBenjamin Tissoires static size_t wacom_compute_pktlen(struct hid_device *hdev)
1523471d1714SBenjamin Tissoires {
1524471d1714SBenjamin Tissoires 	struct hid_report_enum *report_enum;
1525471d1714SBenjamin Tissoires 	struct hid_report *report;
1526471d1714SBenjamin Tissoires 	size_t size = 0;
1527471d1714SBenjamin Tissoires 
1528471d1714SBenjamin Tissoires 	report_enum = hdev->report_enum + HID_INPUT_REPORT;
1529471d1714SBenjamin Tissoires 
1530471d1714SBenjamin Tissoires 	list_for_each_entry(report, &report_enum->report_list, list) {
1531dabb05c6SMathieu Magnaudet 		size_t report_size = hid_report_len(report);
1532471d1714SBenjamin Tissoires 		if (report_size > size)
1533471d1714SBenjamin Tissoires 			size = report_size;
1534471d1714SBenjamin Tissoires 	}
1535471d1714SBenjamin Tissoires 
1536471d1714SBenjamin Tissoires 	return size;
1537471d1714SBenjamin Tissoires }
1538471d1714SBenjamin Tissoires 
1539fd5f92b6SBenjamin Tissoires static void wacom_update_name(struct wacom *wacom, const char *suffix)
1540c24eab4eSPing Cheng {
1541c24eab4eSPing Cheng 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1542c24eab4eSPing Cheng 	struct wacom_features *features = &wacom_wac->features;
154344b5250bSJason Gerecke 	char name[WACOM_NAME_MAX];
1544c24eab4eSPing Cheng 
1545c24eab4eSPing Cheng 	/* Generic devices name unspecified */
1546c24eab4eSPing Cheng 	if ((features->type == HID_GENERIC) && !strcmp("Wacom HID", features->name)) {
1547c24eab4eSPing Cheng 		if (strstr(wacom->hdev->name, "Wacom") ||
1548c24eab4eSPing Cheng 		    strstr(wacom->hdev->name, "wacom") ||
1549c24eab4eSPing Cheng 		    strstr(wacom->hdev->name, "WACOM")) {
1550c24eab4eSPing Cheng 			/* name is in HID descriptor, use it */
155144b5250bSJason Gerecke 			strlcpy(name, wacom->hdev->name, sizeof(name));
1552c24eab4eSPing Cheng 
1553c24eab4eSPing Cheng 			/* strip out excess whitespaces */
1554c24eab4eSPing Cheng 			while (1) {
155544b5250bSJason Gerecke 				char *gap = strstr(name, "  ");
1556c24eab4eSPing Cheng 				if (gap == NULL)
1557c24eab4eSPing Cheng 					break;
1558c24eab4eSPing Cheng 				/* shift everything including the terminator */
1559c24eab4eSPing Cheng 				memmove(gap, gap+1, strlen(gap));
1560c24eab4eSPing Cheng 			}
1561c24eab4eSPing Cheng 			/* get rid of trailing whitespace */
156244b5250bSJason Gerecke 			if (name[strlen(name)-1] == ' ')
156344b5250bSJason Gerecke 				name[strlen(name)-1] = '\0';
1564c24eab4eSPing Cheng 		} else {
1565c24eab4eSPing Cheng 			/* no meaningful name retrieved. use product ID */
156644b5250bSJason Gerecke 			snprintf(name, sizeof(name),
1567c24eab4eSPing Cheng 				 "%s %X", features->name, wacom->hdev->product);
1568c24eab4eSPing Cheng 		}
1569c24eab4eSPing Cheng 	} else {
157044b5250bSJason Gerecke 		strlcpy(name, features->name, sizeof(name));
1571c24eab4eSPing Cheng 	}
1572c24eab4eSPing Cheng 
1573c24eab4eSPing Cheng 	/* Append the device type to the name */
15742a6cdbddSJason Gerecke 	snprintf(wacom_wac->pen_name, sizeof(wacom_wac->pen_name),
1575fd5f92b6SBenjamin Tissoires 		"%s%s Pen", name, suffix);
15762a6cdbddSJason Gerecke 	snprintf(wacom_wac->touch_name, sizeof(wacom_wac->touch_name),
1577fd5f92b6SBenjamin Tissoires 		"%s%s Finger", name, suffix);
1578c24eab4eSPing Cheng 	snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1579fd5f92b6SBenjamin Tissoires 		"%s%s Pad", name, suffix);
15808d80f790SJason Gerecke }
1581c24eab4eSPing Cheng 
158284dfbd7fSBenjamin Tissoires static void wacom_release_resources(struct wacom *wacom)
158384dfbd7fSBenjamin Tissoires {
158484dfbd7fSBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
158584dfbd7fSBenjamin Tissoires 
158684dfbd7fSBenjamin Tissoires 	if (!wacom->resources)
158784dfbd7fSBenjamin Tissoires 		return;
158884dfbd7fSBenjamin Tissoires 
158984dfbd7fSBenjamin Tissoires 	devres_release_group(&hdev->dev, wacom);
159084dfbd7fSBenjamin Tissoires 
159184dfbd7fSBenjamin Tissoires 	wacom->resources = false;
159284dfbd7fSBenjamin Tissoires 
159384dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.pen_input = NULL;
159484dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.touch_input = NULL;
159584dfbd7fSBenjamin Tissoires 	wacom->wacom_wac.pad_input = NULL;
159684dfbd7fSBenjamin Tissoires }
159784dfbd7fSBenjamin Tissoires 
1598fd5f92b6SBenjamin Tissoires static int wacom_parse_and_register(struct wacom *wacom, bool wireless)
1599471d1714SBenjamin Tissoires {
1600c58ac3a8SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1601c58ac3a8SBenjamin Tissoires 	struct wacom_features *features = &wacom_wac->features;
1602c58ac3a8SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
1603471d1714SBenjamin Tissoires 	int error;
16047704ac93SBenjamin Tissoires 	unsigned int connect_mask = HID_CONNECT_HIDRAW;
1605471d1714SBenjamin Tissoires 
1606471d1714SBenjamin Tissoires 	features->pktlen = wacom_compute_pktlen(hdev);
1607c58ac3a8SBenjamin Tissoires 	if (features->pktlen > WACOM_PKGLEN_MAX)
1608c58ac3a8SBenjamin Tissoires 		return -EINVAL;
1609471d1714SBenjamin Tissoires 
161084dfbd7fSBenjamin Tissoires 	if (!devres_open_group(&hdev->dev, wacom, GFP_KERNEL))
161184dfbd7fSBenjamin Tissoires 		return -ENOMEM;
161284dfbd7fSBenjamin Tissoires 
161384dfbd7fSBenjamin Tissoires 	wacom->resources = true;
161484dfbd7fSBenjamin Tissoires 
1615494078b0SBenjamin Tissoires 	error = wacom_allocate_inputs(wacom);
1616494078b0SBenjamin Tissoires 	if (error)
161784dfbd7fSBenjamin Tissoires 		goto fail_open_group;
1618494078b0SBenjamin Tissoires 
16198c97a765SBenjamin Tissoires 	/*
16208c97a765SBenjamin Tissoires 	 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
16218c97a765SBenjamin Tissoires 	 * into debug mode for the touch part.
16228c97a765SBenjamin Tissoires 	 * We ignore the other interfaces.
16238c97a765SBenjamin Tissoires 	 */
16248c97a765SBenjamin Tissoires 	if (features->type == BAMBOO_PAD) {
16258c97a765SBenjamin Tissoires 		if (features->pktlen == WACOM_PKGLEN_PENABLED) {
16268c97a765SBenjamin Tissoires 			features->type = HID_GENERIC;
16278c97a765SBenjamin Tissoires 		} else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
16288c97a765SBenjamin Tissoires 			   (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
16298c97a765SBenjamin Tissoires 			error = -ENODEV;
1630c58ac3a8SBenjamin Tissoires 			goto fail_allocate_inputs;
16318c97a765SBenjamin Tissoires 		}
16328c97a765SBenjamin Tissoires 	}
16338c97a765SBenjamin Tissoires 
1634471d1714SBenjamin Tissoires 	/* set the default size in case we do not get them from hid */
1635471d1714SBenjamin Tissoires 	wacom_set_default_phy(features);
1636471d1714SBenjamin Tissoires 
1637471d1714SBenjamin Tissoires 	/* Retrieve the physical and logical size for touch devices */
1638471d1714SBenjamin Tissoires 	wacom_retrieve_hid_descriptor(hdev, features);
163942f4f272SPing Cheng 	wacom_setup_device_quirks(wacom);
1640042628abSJason Gerecke 
1641aa86b18cSJason Gerecke 	if (features->device_type == WACOM_DEVICETYPE_NONE &&
1642aa86b18cSJason Gerecke 	    features->type != WIRELESS) {
16438e116d31SJason Gerecke 		error = features->type == HID_GENERIC ? -ENODEV : 0;
16448e116d31SJason Gerecke 
1645042628abSJason Gerecke 		dev_warn(&hdev->dev, "Unknown device_type for '%s'. %s.",
16468e116d31SJason Gerecke 			 hdev->name,
16478e116d31SJason Gerecke 			 error ? "Ignoring" : "Assuming pen");
16488e116d31SJason Gerecke 
16498e116d31SJason Gerecke 		if (error)
1650c58ac3a8SBenjamin Tissoires 			goto fail_parsed;
1651042628abSJason Gerecke 
1652aa86b18cSJason Gerecke 		features->device_type |= WACOM_DEVICETYPE_PEN;
1653042628abSJason Gerecke 	}
1654042628abSJason Gerecke 
1655471d1714SBenjamin Tissoires 	wacom_calculate_res(features);
1656471d1714SBenjamin Tissoires 
1657fd5f92b6SBenjamin Tissoires 	wacom_update_name(wacom, wireless ? " (WL)" : "");
1658471d1714SBenjamin Tissoires 
1659471d1714SBenjamin Tissoires 	error = wacom_add_shared_data(hdev);
1660471d1714SBenjamin Tissoires 	if (error)
16617fefeec5SBenjamin Tissoires 		goto fail_shared_data;
1662471d1714SBenjamin Tissoires 
1663ccad85ccSJason Gerecke 	if (!(features->device_type & WACOM_DEVICETYPE_WL_MONITOR) &&
1664f81a1295SBenjamin Tissoires 	     (features->quirks & WACOM_QUIRK_BATTERY)) {
1665f81a1295SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1666f81a1295SBenjamin Tissoires 		if (error)
16677fefeec5SBenjamin Tissoires 			goto fail_battery;
1668f81a1295SBenjamin Tissoires 	}
1669f81a1295SBenjamin Tissoires 
1670494078b0SBenjamin Tissoires 	error = wacom_register_inputs(wacom);
1671471d1714SBenjamin Tissoires 	if (error)
16727fefeec5SBenjamin Tissoires 		goto fail_register_inputs;
1673f81a1295SBenjamin Tissoires 
1674b62f6465SBenjamin Tissoires 	if (wacom->wacom_wac.features.device_type & WACOM_DEVICETYPE_PAD) {
167585d2c77bSBenjamin Tissoires 		error = wacom_initialize_leds(wacom);
167685d2c77bSBenjamin Tissoires 		if (error)
167785d2c77bSBenjamin Tissoires 			goto fail_leds;
167885d2c77bSBenjamin Tissoires 
1679b62f6465SBenjamin Tissoires 		error = wacom_initialize_remote(wacom);
1680b62f6465SBenjamin Tissoires 		if (error)
1681b62f6465SBenjamin Tissoires 			goto fail_remote;
1682b62f6465SBenjamin Tissoires 	}
1683b62f6465SBenjamin Tissoires 
16847704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
16857704ac93SBenjamin Tissoires 		connect_mask |= HID_CONNECT_DRIVER;
16867704ac93SBenjamin Tissoires 
1687471d1714SBenjamin Tissoires 	/* Regular HID work starts now */
16887704ac93SBenjamin Tissoires 	error = hid_hw_start(hdev, connect_mask);
1689471d1714SBenjamin Tissoires 	if (error) {
1690471d1714SBenjamin Tissoires 		hid_err(hdev, "hw start failed\n");
16917fefeec5SBenjamin Tissoires 		goto fail_hw_start;
1692471d1714SBenjamin Tissoires 	}
1693471d1714SBenjamin Tissoires 
1694fd5f92b6SBenjamin Tissoires 	if (!wireless) {
169586e88f0eSJason Gerecke 		/* Note that if query fails it is not a hard failure */
169686e88f0eSJason Gerecke 		wacom_query_tablet_data(hdev, features);
1697fd5f92b6SBenjamin Tissoires 	}
169886e88f0eSJason Gerecke 
169986e88f0eSJason Gerecke 	/* touch only Bamboo doesn't support pen */
170086e88f0eSJason Gerecke 	if ((features->type == BAMBOO_TOUCH) &&
170186e88f0eSJason Gerecke 	    (features->device_type & WACOM_DEVICETYPE_PEN)) {
170286e88f0eSJason Gerecke 		error = -ENODEV;
1703b62f6465SBenjamin Tissoires 		goto fail_quirks;
170486e88f0eSJason Gerecke 	}
170586e88f0eSJason Gerecke 
170686e88f0eSJason Gerecke 	/* pen only Bamboo neither support touch nor pad */
170786e88f0eSJason Gerecke 	if ((features->type == BAMBOO_PEN) &&
170886e88f0eSJason Gerecke 	    ((features->device_type & WACOM_DEVICETYPE_TOUCH) ||
170986e88f0eSJason Gerecke 	    (features->device_type & WACOM_DEVICETYPE_PAD))) {
171086e88f0eSJason Gerecke 		error = -ENODEV;
1711b62f6465SBenjamin Tissoires 		goto fail_quirks;
171286e88f0eSJason Gerecke 	}
171386e88f0eSJason Gerecke 
1714ccad85ccSJason Gerecke 	if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
1715471d1714SBenjamin Tissoires 		error = hid_hw_open(hdev);
1716471d1714SBenjamin Tissoires 
1717eda01dabSPing Cheng 	if ((wacom_wac->features.type == INTUOSHT ||
1718eda01dabSPing Cheng 	     wacom_wac->features.type == INTUOSHT2) &&
1719eda01dabSPing Cheng 	    (wacom_wac->features.device_type & WACOM_DEVICETYPE_TOUCH)) {
172097f9afa4SBenjamin Tissoires 		wacom_wac->shared->type = wacom_wac->features.type;
17212a6cdbddSJason Gerecke 		wacom_wac->shared->touch_input = wacom_wac->touch_input;
1722471d1714SBenjamin Tissoires 	}
1723471d1714SBenjamin Tissoires 
172484dfbd7fSBenjamin Tissoires 	devres_close_group(&hdev->dev, wacom);
172584dfbd7fSBenjamin Tissoires 
1726471d1714SBenjamin Tissoires 	return 0;
1727471d1714SBenjamin Tissoires 
1728b62f6465SBenjamin Tissoires fail_quirks:
1729c58ac3a8SBenjamin Tissoires 	hid_hw_stop(hdev);
1730b62f6465SBenjamin Tissoires fail_hw_start:
1731b62f6465SBenjamin Tissoires 	kobject_put(wacom->remote_dir);
1732b62f6465SBenjamin Tissoires fail_remote:
173385d2c77bSBenjamin Tissoires fail_leds:
1734b62f6465SBenjamin Tissoires fail_register_inputs:
17357fefeec5SBenjamin Tissoires fail_battery:
1736a97ac104SBenjamin Tissoires 	wacom_remove_shared_data(wacom);
17377fefeec5SBenjamin Tissoires fail_shared_data:
1738c58ac3a8SBenjamin Tissoires fail_parsed:
1739494078b0SBenjamin Tissoires fail_allocate_inputs:
174084dfbd7fSBenjamin Tissoires fail_open_group:
174184dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom);
1742c58ac3a8SBenjamin Tissoires 	return error;
1743c58ac3a8SBenjamin Tissoires }
1744c58ac3a8SBenjamin Tissoires 
1745a2f091afSBenjamin Tissoires static void wacom_wireless_work(struct work_struct *work)
1746a2f091afSBenjamin Tissoires {
1747d17d1f17SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, wireless_work);
1748a2f091afSBenjamin Tissoires 	struct usb_device *usbdev = wacom->usbdev;
1749a2f091afSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1750a2f091afSBenjamin Tissoires 	struct hid_device *hdev1, *hdev2;
1751a2f091afSBenjamin Tissoires 	struct wacom *wacom1, *wacom2;
1752a2f091afSBenjamin Tissoires 	struct wacom_wac *wacom_wac1, *wacom_wac2;
1753a2f091afSBenjamin Tissoires 	int error;
1754a2f091afSBenjamin Tissoires 
1755a2f091afSBenjamin Tissoires 	/*
1756a2f091afSBenjamin Tissoires 	 * Regardless if this is a disconnect or a new tablet,
1757a2f091afSBenjamin Tissoires 	 * remove any existing input and battery devices.
1758a2f091afSBenjamin Tissoires 	 */
1759a2f091afSBenjamin Tissoires 
1760a2f091afSBenjamin Tissoires 	wacom_destroy_battery(wacom);
1761a2f091afSBenjamin Tissoires 
1762a2f091afSBenjamin Tissoires 	/* Stylus interface */
1763a2f091afSBenjamin Tissoires 	hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1764a2f091afSBenjamin Tissoires 	wacom1 = hid_get_drvdata(hdev1);
1765a2f091afSBenjamin Tissoires 	wacom_wac1 = &(wacom1->wacom_wac);
176684dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom1);
1767a2f091afSBenjamin Tissoires 
1768a2f091afSBenjamin Tissoires 	/* Touch interface */
1769a2f091afSBenjamin Tissoires 	hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1770a2f091afSBenjamin Tissoires 	wacom2 = hid_get_drvdata(hdev2);
1771a2f091afSBenjamin Tissoires 	wacom_wac2 = &(wacom2->wacom_wac);
177284dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom2);
1773a2f091afSBenjamin Tissoires 
1774a2f091afSBenjamin Tissoires 	if (wacom_wac->pid == 0) {
1775a2f091afSBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet disconnected\n");
1776a2f091afSBenjamin Tissoires 		wacom_wac1->shared->type = 0;
1777a2f091afSBenjamin Tissoires 	} else {
1778a2f091afSBenjamin Tissoires 		const struct hid_device_id *id = wacom_ids;
1779a2f091afSBenjamin Tissoires 
1780a2f091afSBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1781a2f091afSBenjamin Tissoires 			 wacom_wac->pid);
1782a2f091afSBenjamin Tissoires 
1783a2f091afSBenjamin Tissoires 		while (id->bus) {
1784a2f091afSBenjamin Tissoires 			if (id->vendor == USB_VENDOR_ID_WACOM &&
1785a2f091afSBenjamin Tissoires 			    id->product == wacom_wac->pid)
1786a2f091afSBenjamin Tissoires 				break;
1787a2f091afSBenjamin Tissoires 			id++;
1788a2f091afSBenjamin Tissoires 		}
1789a2f091afSBenjamin Tissoires 
1790a2f091afSBenjamin Tissoires 		if (!id->bus) {
1791a2f091afSBenjamin Tissoires 			hid_info(wacom->hdev, "ignoring unknown PID.\n");
1792a2f091afSBenjamin Tissoires 			return;
1793a2f091afSBenjamin Tissoires 		}
1794a2f091afSBenjamin Tissoires 
1795a2f091afSBenjamin Tissoires 		/* Stylus interface */
1796a2f091afSBenjamin Tissoires 		wacom_wac1->features =
1797a2f091afSBenjamin Tissoires 			*((struct wacom_features *)id->driver_data);
1798fd5f92b6SBenjamin Tissoires 
1799a2f091afSBenjamin Tissoires 		wacom_wac1->pid = wacom_wac->pid;
1800fd5f92b6SBenjamin Tissoires 		hid_hw_stop(hdev1);
1801fd5f92b6SBenjamin Tissoires 		error = wacom_parse_and_register(wacom1, true);
1802a2f091afSBenjamin Tissoires 		if (error)
1803a2f091afSBenjamin Tissoires 			goto fail;
1804a2f091afSBenjamin Tissoires 
1805a2f091afSBenjamin Tissoires 		/* Touch interface */
1806a2f091afSBenjamin Tissoires 		if (wacom_wac1->features.touch_max ||
1807a2f091afSBenjamin Tissoires 		    (wacom_wac1->features.type >= INTUOSHT &&
1808a2f091afSBenjamin Tissoires 		    wacom_wac1->features.type <= BAMBOO_PT)) {
1809a2f091afSBenjamin Tissoires 			wacom_wac2->features =
1810a2f091afSBenjamin Tissoires 				*((struct wacom_features *)id->driver_data);
1811a2f091afSBenjamin Tissoires 			wacom_wac2->pid = wacom_wac->pid;
1812fd5f92b6SBenjamin Tissoires 			hid_hw_stop(hdev2);
1813fd5f92b6SBenjamin Tissoires 			error = wacom_parse_and_register(wacom2, true);
1814a2f091afSBenjamin Tissoires 			if (error)
1815a2f091afSBenjamin Tissoires 				goto fail;
1816a2f091afSBenjamin Tissoires 		}
1817a2f091afSBenjamin Tissoires 
1818a2f091afSBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1819a2f091afSBenjamin Tissoires 		if (error)
1820a2f091afSBenjamin Tissoires 			goto fail;
1821a2f091afSBenjamin Tissoires 	}
1822a2f091afSBenjamin Tissoires 
1823a2f091afSBenjamin Tissoires 	return;
1824a2f091afSBenjamin Tissoires 
1825a2f091afSBenjamin Tissoires fail:
182684dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom1);
182784dfbd7fSBenjamin Tissoires 	wacom_release_resources(wacom2);
1828a2f091afSBenjamin Tissoires 	return;
1829a2f091afSBenjamin Tissoires }
1830a2f091afSBenjamin Tissoires 
1831c58ac3a8SBenjamin Tissoires static int wacom_probe(struct hid_device *hdev,
1832c58ac3a8SBenjamin Tissoires 		const struct hid_device_id *id)
1833c58ac3a8SBenjamin Tissoires {
1834c58ac3a8SBenjamin Tissoires 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1835c58ac3a8SBenjamin Tissoires 	struct usb_device *dev = interface_to_usbdev(intf);
1836c58ac3a8SBenjamin Tissoires 	struct wacom *wacom;
1837c58ac3a8SBenjamin Tissoires 	struct wacom_wac *wacom_wac;
1838c58ac3a8SBenjamin Tissoires 	struct wacom_features *features;
1839c58ac3a8SBenjamin Tissoires 	int error;
1840c58ac3a8SBenjamin Tissoires 
1841c58ac3a8SBenjamin Tissoires 	if (!id->driver_data)
1842c58ac3a8SBenjamin Tissoires 		return -EINVAL;
1843c58ac3a8SBenjamin Tissoires 
1844c58ac3a8SBenjamin Tissoires 	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
1845c58ac3a8SBenjamin Tissoires 
1846c58ac3a8SBenjamin Tissoires 	/* hid-core sets this quirk for the boot interface */
1847c58ac3a8SBenjamin Tissoires 	hdev->quirks &= ~HID_QUIRK_NOGET;
1848c58ac3a8SBenjamin Tissoires 
1849c58ac3a8SBenjamin Tissoires 	wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1850c58ac3a8SBenjamin Tissoires 	if (!wacom)
1851c58ac3a8SBenjamin Tissoires 		return -ENOMEM;
1852c58ac3a8SBenjamin Tissoires 
1853c58ac3a8SBenjamin Tissoires 	hid_set_drvdata(hdev, wacom);
1854c58ac3a8SBenjamin Tissoires 	wacom->hdev = hdev;
1855c58ac3a8SBenjamin Tissoires 
1856c58ac3a8SBenjamin Tissoires 	wacom_wac = &wacom->wacom_wac;
1857c58ac3a8SBenjamin Tissoires 	wacom_wac->features = *((struct wacom_features *)id->driver_data);
1858c58ac3a8SBenjamin Tissoires 	features = &wacom_wac->features;
1859c58ac3a8SBenjamin Tissoires 
1860c58ac3a8SBenjamin Tissoires 	if (features->check_for_hid_type && features->hid_type != hdev->type) {
1861c58ac3a8SBenjamin Tissoires 		error = -ENODEV;
1862c58ac3a8SBenjamin Tissoires 		goto fail_type;
1863c58ac3a8SBenjamin Tissoires 	}
1864c58ac3a8SBenjamin Tissoires 
1865c6fa1aebSJason Gerecke 	wacom_wac->hid_data.inputmode = -1;
1866326ea2a9SJason Gerecke 	wacom_wac->mode_report = -1;
1867c6fa1aebSJason Gerecke 
1868c58ac3a8SBenjamin Tissoires 	wacom->usbdev = dev;
1869c58ac3a8SBenjamin Tissoires 	wacom->intf = intf;
1870c58ac3a8SBenjamin Tissoires 	mutex_init(&wacom->lock);
1871d17d1f17SBenjamin Tissoires 	INIT_WORK(&wacom->wireless_work, wacom_wireless_work);
1872d17d1f17SBenjamin Tissoires 	INIT_WORK(&wacom->battery_work, wacom_battery_work);
1873c58ac3a8SBenjamin Tissoires 
1874c58ac3a8SBenjamin Tissoires 	/* ask for the report descriptor to be loaded by HID */
1875c58ac3a8SBenjamin Tissoires 	error = hid_parse(hdev);
1876c58ac3a8SBenjamin Tissoires 	if (error) {
1877c58ac3a8SBenjamin Tissoires 		hid_err(hdev, "parse failed\n");
1878c58ac3a8SBenjamin Tissoires 		goto fail_parse;
1879c58ac3a8SBenjamin Tissoires 	}
1880c58ac3a8SBenjamin Tissoires 
1881fd5f92b6SBenjamin Tissoires 	error = wacom_parse_and_register(wacom, false);
1882c58ac3a8SBenjamin Tissoires 	if (error)
1883c58ac3a8SBenjamin Tissoires 		goto fail_parse;
1884c58ac3a8SBenjamin Tissoires 
1885c58ac3a8SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
1886c58ac3a8SBenjamin Tissoires 		error = device_create_file(&hdev->dev, &dev_attr_speed);
1887c58ac3a8SBenjamin Tissoires 		if (error)
1888c58ac3a8SBenjamin Tissoires 			hid_warn(hdev,
1889c58ac3a8SBenjamin Tissoires 				 "can't create sysfs speed attribute err: %d\n",
1890c58ac3a8SBenjamin Tissoires 				 error);
1891c58ac3a8SBenjamin Tissoires 	}
1892c58ac3a8SBenjamin Tissoires 
1893c58ac3a8SBenjamin Tissoires 	return 0;
1894c58ac3a8SBenjamin Tissoires 
18957fefeec5SBenjamin Tissoires fail_type:
18967fefeec5SBenjamin Tissoires fail_parse:
18977fefeec5SBenjamin Tissoires 	kfree(wacom);
1898471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
1899471d1714SBenjamin Tissoires 	return error;
1900471d1714SBenjamin Tissoires }
1901471d1714SBenjamin Tissoires 
1902471d1714SBenjamin Tissoires static void wacom_remove(struct hid_device *hdev)
1903471d1714SBenjamin Tissoires {
1904471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1905f6205161SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1906f6205161SBenjamin Tissoires 	struct wacom_features *features = &wacom_wac->features;
1907f6205161SBenjamin Tissoires 
1908f6205161SBenjamin Tissoires 	if (features->device_type & WACOM_DEVICETYPE_WL_MONITOR)
1909f6205161SBenjamin Tissoires 		hid_hw_close(hdev);
1910471d1714SBenjamin Tissoires 
1911471d1714SBenjamin Tissoires 	hid_hw_stop(hdev);
1912471d1714SBenjamin Tissoires 
1913d17d1f17SBenjamin Tissoires 	cancel_work_sync(&wacom->wireless_work);
1914d17d1f17SBenjamin Tissoires 	cancel_work_sync(&wacom->battery_work);
1915b62f6465SBenjamin Tissoires 	kobject_put(wacom->remote_dir);
1916f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
1917f81a1295SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
1918a97ac104SBenjamin Tissoires 	wacom_remove_shared_data(wacom);
1919471d1714SBenjamin Tissoires 
1920471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
1921471d1714SBenjamin Tissoires 	kfree(wacom);
1922471d1714SBenjamin Tissoires }
1923471d1714SBenjamin Tissoires 
192441a74581SGeert Uytterhoeven #ifdef CONFIG_PM
1925471d1714SBenjamin Tissoires static int wacom_resume(struct hid_device *hdev)
1926471d1714SBenjamin Tissoires {
1927471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1928471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
1929471d1714SBenjamin Tissoires 
1930471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
1931471d1714SBenjamin Tissoires 
1932471d1714SBenjamin Tissoires 	/* switch to wacom mode first */
1933471d1714SBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
1934471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
1935471d1714SBenjamin Tissoires 
1936471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
1937471d1714SBenjamin Tissoires 
1938471d1714SBenjamin Tissoires 	return 0;
1939471d1714SBenjamin Tissoires }
1940471d1714SBenjamin Tissoires 
1941471d1714SBenjamin Tissoires static int wacom_reset_resume(struct hid_device *hdev)
1942471d1714SBenjamin Tissoires {
1943471d1714SBenjamin Tissoires 	return wacom_resume(hdev);
1944471d1714SBenjamin Tissoires }
194541a74581SGeert Uytterhoeven #endif /* CONFIG_PM */
1946471d1714SBenjamin Tissoires 
1947471d1714SBenjamin Tissoires static struct hid_driver wacom_driver = {
1948471d1714SBenjamin Tissoires 	.name =		"wacom",
1949471d1714SBenjamin Tissoires 	.id_table =	wacom_ids,
1950471d1714SBenjamin Tissoires 	.probe =	wacom_probe,
1951471d1714SBenjamin Tissoires 	.remove =	wacom_remove,
19527704ac93SBenjamin Tissoires 	.report =	wacom_wac_report,
1953471d1714SBenjamin Tissoires #ifdef CONFIG_PM
1954471d1714SBenjamin Tissoires 	.resume =	wacom_resume,
1955471d1714SBenjamin Tissoires 	.reset_resume =	wacom_reset_resume,
1956471d1714SBenjamin Tissoires #endif
1957471d1714SBenjamin Tissoires 	.raw_event =	wacom_raw_event,
1958471d1714SBenjamin Tissoires };
1959471d1714SBenjamin Tissoires module_hid_driver(wacom_driver);
1960f2e0a7d4SBenjamin Tissoires 
1961f2e0a7d4SBenjamin Tissoires MODULE_VERSION(DRIVER_VERSION);
1962f2e0a7d4SBenjamin Tissoires MODULE_AUTHOR(DRIVER_AUTHOR);
1963f2e0a7d4SBenjamin Tissoires MODULE_DESCRIPTION(DRIVER_DESC);
1964f2e0a7d4SBenjamin Tissoires MODULE_LICENSE(DRIVER_LICENSE);
1965