xref: /openbmc/linux/drivers/hid/wacom_sys.c (revision 297d716f6260cc9421d971b124ca196b957ee458)
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
26471d1714SBenjamin Tissoires 
27e0984bc3SPing Cheng #define DEV_ATTR_RW_PERM (S_IRUGO | S_IWUSR | S_IWGRP)
28e0984bc3SPing Cheng #define DEV_ATTR_WO_PERM (S_IWUSR | S_IWGRP)
29e0984bc3SPing Cheng 
30c64d8834SPing Cheng static int wacom_get_report(struct hid_device *hdev, u8 type, u8 *buf,
31c64d8834SPing Cheng 			    size_t size, unsigned int retries)
32471d1714SBenjamin Tissoires {
33471d1714SBenjamin Tissoires 	int retval;
34471d1714SBenjamin Tissoires 
35471d1714SBenjamin Tissoires 	do {
36c64d8834SPing Cheng 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
37471d1714SBenjamin Tissoires 				HID_REQ_GET_REPORT);
38471d1714SBenjamin Tissoires 	} while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
39471d1714SBenjamin Tissoires 
40471d1714SBenjamin Tissoires 	return retval;
41471d1714SBenjamin Tissoires }
42471d1714SBenjamin Tissoires 
43296b7378SPrzemo Firszt static int wacom_set_report(struct hid_device *hdev, u8 type, u8 *buf,
44296b7378SPrzemo Firszt 			    size_t size, unsigned int retries)
45471d1714SBenjamin Tissoires {
46471d1714SBenjamin Tissoires 	int retval;
47471d1714SBenjamin Tissoires 
48471d1714SBenjamin Tissoires 	do {
49296b7378SPrzemo Firszt 		retval = hid_hw_raw_request(hdev, buf[0], buf, size, type,
50471d1714SBenjamin Tissoires 				HID_REQ_SET_REPORT);
51471d1714SBenjamin Tissoires 	} while ((retval == -ETIMEDOUT || retval == -EPIPE) && --retries);
52471d1714SBenjamin Tissoires 
53471d1714SBenjamin Tissoires 	return retval;
54471d1714SBenjamin Tissoires }
55471d1714SBenjamin Tissoires 
56471d1714SBenjamin Tissoires static int wacom_raw_event(struct hid_device *hdev, struct hid_report *report,
57471d1714SBenjamin Tissoires 		u8 *raw_data, int size)
58471d1714SBenjamin Tissoires {
59471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
60471d1714SBenjamin Tissoires 
61471d1714SBenjamin Tissoires 	if (size > WACOM_PKGLEN_MAX)
62471d1714SBenjamin Tissoires 		return 1;
63471d1714SBenjamin Tissoires 
64471d1714SBenjamin Tissoires 	memcpy(wacom->wacom_wac.data, raw_data, size);
65471d1714SBenjamin Tissoires 
66471d1714SBenjamin Tissoires 	wacom_wac_irq(&wacom->wacom_wac, size);
67471d1714SBenjamin Tissoires 
68471d1714SBenjamin Tissoires 	return 0;
69471d1714SBenjamin Tissoires }
70471d1714SBenjamin Tissoires 
71471d1714SBenjamin Tissoires static int wacom_open(struct input_dev *dev)
72471d1714SBenjamin Tissoires {
73471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
74471d1714SBenjamin Tissoires 
75dff67416SBenjamin Tissoires 	return hid_hw_open(wacom->hdev);
76471d1714SBenjamin Tissoires }
77471d1714SBenjamin Tissoires 
78471d1714SBenjamin Tissoires static void wacom_close(struct input_dev *dev)
79471d1714SBenjamin Tissoires {
80471d1714SBenjamin Tissoires 	struct wacom *wacom = input_get_drvdata(dev);
81471d1714SBenjamin Tissoires 
82471d1714SBenjamin Tissoires 	hid_hw_close(wacom->hdev);
83471d1714SBenjamin Tissoires }
84471d1714SBenjamin Tissoires 
85471d1714SBenjamin Tissoires /*
86471d1714SBenjamin Tissoires  * Calculate the resolution of the X or Y axis using hidinput_calc_abs_res.
87471d1714SBenjamin Tissoires  */
88471d1714SBenjamin Tissoires static int wacom_calc_hid_res(int logical_extents, int physical_extents,
89471d1714SBenjamin Tissoires 			       unsigned unit, int exponent)
90471d1714SBenjamin Tissoires {
91471d1714SBenjamin Tissoires 	struct hid_field field = {
92471d1714SBenjamin Tissoires 		.logical_maximum = logical_extents,
93471d1714SBenjamin Tissoires 		.physical_maximum = physical_extents,
94471d1714SBenjamin Tissoires 		.unit = unit,
95471d1714SBenjamin Tissoires 		.unit_exponent = exponent,
96471d1714SBenjamin Tissoires 	};
97471d1714SBenjamin Tissoires 
98471d1714SBenjamin Tissoires 	return hidinput_calc_abs_res(&field, ABS_X);
99471d1714SBenjamin Tissoires }
100471d1714SBenjamin Tissoires 
101471d1714SBenjamin Tissoires static void wacom_feature_mapping(struct hid_device *hdev,
102471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
103471d1714SBenjamin Tissoires {
104471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
105471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
1065ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
1078ffffd52SBenjamin Tissoires 	u8 *data;
1088ffffd52SBenjamin Tissoires 	int ret;
109471d1714SBenjamin Tissoires 
110471d1714SBenjamin Tissoires 	switch (usage->hid) {
111471d1714SBenjamin Tissoires 	case HID_DG_CONTACTMAX:
112471d1714SBenjamin Tissoires 		/* leave touch_max as is if predefined */
1138ffffd52SBenjamin Tissoires 		if (!features->touch_max) {
1148ffffd52SBenjamin Tissoires 			/* read manually */
1158ffffd52SBenjamin Tissoires 			data = kzalloc(2, GFP_KERNEL);
1168ffffd52SBenjamin Tissoires 			if (!data)
1178ffffd52SBenjamin Tissoires 				break;
1188ffffd52SBenjamin Tissoires 			data[0] = field->report->id;
1198ffffd52SBenjamin Tissoires 			ret = wacom_get_report(hdev, HID_FEATURE_REPORT,
1208ffffd52SBenjamin Tissoires 						data, 2, 0);
1218ffffd52SBenjamin Tissoires 			if (ret == 2)
1228ffffd52SBenjamin Tissoires 				features->touch_max = data[1];
1238ffffd52SBenjamin Tissoires 			kfree(data);
1248ffffd52SBenjamin Tissoires 		}
125471d1714SBenjamin Tissoires 		break;
1265ae6e89fSBenjamin Tissoires 	case HID_DG_INPUTMODE:
1275ae6e89fSBenjamin Tissoires 		/* Ignore if value index is out of bounds. */
1285ae6e89fSBenjamin Tissoires 		if (usage->usage_index >= field->report_count) {
1295ae6e89fSBenjamin Tissoires 			dev_err(&hdev->dev, "HID_DG_INPUTMODE out of range\n");
1305ae6e89fSBenjamin Tissoires 			break;
1315ae6e89fSBenjamin Tissoires 		}
1325ae6e89fSBenjamin Tissoires 
1335ae6e89fSBenjamin Tissoires 		hid_data->inputmode = field->report->id;
1345ae6e89fSBenjamin Tissoires 		hid_data->inputmode_index = usage->usage_index;
1355ae6e89fSBenjamin Tissoires 		break;
136471d1714SBenjamin Tissoires 	}
137471d1714SBenjamin Tissoires }
138471d1714SBenjamin Tissoires 
139471d1714SBenjamin Tissoires /*
140471d1714SBenjamin Tissoires  * Interface Descriptor of wacom devices can be incomplete and
141471d1714SBenjamin Tissoires  * inconsistent so wacom_features table is used to store stylus
142471d1714SBenjamin Tissoires  * device's packet lengths, various maximum values, and tablet
143471d1714SBenjamin Tissoires  * resolution based on product ID's.
144471d1714SBenjamin Tissoires  *
145471d1714SBenjamin Tissoires  * For devices that contain 2 interfaces, wacom_features table is
146471d1714SBenjamin Tissoires  * inaccurate for the touch interface.  Since the Interface Descriptor
147471d1714SBenjamin Tissoires  * for touch interfaces has pretty complete data, this function exists
148471d1714SBenjamin Tissoires  * to query tablet for this missing information instead of hard coding in
149471d1714SBenjamin Tissoires  * an additional table.
150471d1714SBenjamin Tissoires  *
151471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a stylus will contain a
152471d1714SBenjamin Tissoires  * boot mouse application collection that is not of interest and this
153471d1714SBenjamin Tissoires  * function will ignore it.
154471d1714SBenjamin Tissoires  *
155471d1714SBenjamin Tissoires  * It also contains a digitizer application collection that also is not
156471d1714SBenjamin Tissoires  * of interest since any information it contains would be duplicate
157471d1714SBenjamin Tissoires  * of what is in wacom_features. Usually it defines a report of an array
158471d1714SBenjamin Tissoires  * of bytes that could be used as max length of the stylus packet returned.
159471d1714SBenjamin Tissoires  * If it happens to define a Digitizer-Stylus Physical Collection then
160471d1714SBenjamin Tissoires  * the X and Y logical values contain valid data but it is ignored.
161471d1714SBenjamin Tissoires  *
162471d1714SBenjamin Tissoires  * A typical Interface Descriptor for a touch interface will contain a
163471d1714SBenjamin Tissoires  * Digitizer-Finger Physical Collection which will define both logical
164471d1714SBenjamin Tissoires  * X/Y maximum as well as the physical size of tablet. Since touch
165471d1714SBenjamin Tissoires  * interfaces haven't supported pressure or distance, this is enough
166471d1714SBenjamin Tissoires  * information to override invalid values in the wacom_features table.
167471d1714SBenjamin Tissoires  *
168471d1714SBenjamin Tissoires  * Intuos5 touch interface and 3rd gen Bamboo Touch do not contain useful
169471d1714SBenjamin Tissoires  * data. We deal with them after returning from this function.
170471d1714SBenjamin Tissoires  */
171471d1714SBenjamin Tissoires static void wacom_usage_mapping(struct hid_device *hdev,
172471d1714SBenjamin Tissoires 		struct hid_field *field, struct hid_usage *usage)
173471d1714SBenjamin Tissoires {
174471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
175471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
176d97a5522SBenjamin Tissoires 	bool finger = WACOM_FINGER_FIELD(field);
177d97a5522SBenjamin Tissoires 	bool pen = WACOM_PEN_FIELD(field);
178471d1714SBenjamin Tissoires 
179471d1714SBenjamin Tissoires 	/*
180471d1714SBenjamin Tissoires 	* Requiring Stylus Usage will ignore boot mouse
181471d1714SBenjamin Tissoires 	* X/Y values and some cases of invalid Digitizer X/Y
182471d1714SBenjamin Tissoires 	* values commonly reported.
183471d1714SBenjamin Tissoires 	*/
184471d1714SBenjamin Tissoires 	if (!pen && !finger)
185471d1714SBenjamin Tissoires 		return;
186471d1714SBenjamin Tissoires 
18730ebc1aeSPing Cheng 	/*
18830ebc1aeSPing Cheng 	 * Bamboo models do not support HID_DG_CONTACTMAX.
18930ebc1aeSPing Cheng 	 * And, Bamboo Pen only descriptor contains touch.
19030ebc1aeSPing Cheng 	 */
19130ebc1aeSPing Cheng 	if (features->type != BAMBOO_PT) {
19230ebc1aeSPing Cheng 		/* ISDv4 touch devices at least supports one touch point */
193471d1714SBenjamin Tissoires 		if (finger && !features->touch_max)
194471d1714SBenjamin Tissoires 			features->touch_max = 1;
19530ebc1aeSPing Cheng 	}
196471d1714SBenjamin Tissoires 
197471d1714SBenjamin Tissoires 	switch (usage->hid) {
198471d1714SBenjamin Tissoires 	case HID_GD_X:
199471d1714SBenjamin Tissoires 		features->x_max = field->logical_maximum;
200471d1714SBenjamin Tissoires 		if (finger) {
201471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_FINGER;
202471d1714SBenjamin Tissoires 			features->x_phy = field->physical_maximum;
203471d1714SBenjamin Tissoires 			if (features->type != BAMBOO_PT) {
204471d1714SBenjamin Tissoires 				features->unit = field->unit;
205471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
206471d1714SBenjamin Tissoires 			}
207471d1714SBenjamin Tissoires 		} else {
208471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_PEN;
209471d1714SBenjamin Tissoires 		}
210471d1714SBenjamin Tissoires 		break;
211471d1714SBenjamin Tissoires 	case HID_GD_Y:
212471d1714SBenjamin Tissoires 		features->y_max = field->logical_maximum;
213471d1714SBenjamin Tissoires 		if (finger) {
214471d1714SBenjamin Tissoires 			features->y_phy = field->physical_maximum;
215471d1714SBenjamin Tissoires 			if (features->type != BAMBOO_PT) {
216471d1714SBenjamin Tissoires 				features->unit = field->unit;
217471d1714SBenjamin Tissoires 				features->unitExpo = field->unit_exponent;
218471d1714SBenjamin Tissoires 			}
219471d1714SBenjamin Tissoires 		}
220471d1714SBenjamin Tissoires 		break;
221471d1714SBenjamin Tissoires 	case HID_DG_TIPPRESSURE:
222471d1714SBenjamin Tissoires 		if (pen)
223471d1714SBenjamin Tissoires 			features->pressure_max = field->logical_maximum;
224471d1714SBenjamin Tissoires 		break;
225471d1714SBenjamin Tissoires 	}
2267704ac93SBenjamin Tissoires 
2277704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
2287704ac93SBenjamin Tissoires 		wacom_wac_usage_mapping(hdev, field, usage);
229471d1714SBenjamin Tissoires }
230471d1714SBenjamin Tissoires 
231b58ba1baSJason Gerecke static void wacom_post_parse_hid(struct hid_device *hdev,
232b58ba1baSJason Gerecke 				 struct wacom_features *features)
233b58ba1baSJason Gerecke {
234b58ba1baSJason Gerecke 	struct wacom *wacom = hid_get_drvdata(hdev);
235b58ba1baSJason Gerecke 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
236b58ba1baSJason Gerecke 
237b58ba1baSJason Gerecke 	if (features->type == HID_GENERIC) {
238b58ba1baSJason Gerecke 		/* Any last-minute generic device setup */
239b58ba1baSJason Gerecke 		if (features->touch_max > 1) {
240b58ba1baSJason Gerecke 			input_mt_init_slots(wacom_wac->input, wacom_wac->features.touch_max,
241b58ba1baSJason Gerecke 				    INPUT_MT_DIRECT);
242b58ba1baSJason Gerecke 		}
243b58ba1baSJason Gerecke 	}
244b58ba1baSJason Gerecke }
245b58ba1baSJason Gerecke 
246471d1714SBenjamin Tissoires static void wacom_parse_hid(struct hid_device *hdev,
247471d1714SBenjamin Tissoires 			   struct wacom_features *features)
248471d1714SBenjamin Tissoires {
249471d1714SBenjamin Tissoires 	struct hid_report_enum *rep_enum;
250471d1714SBenjamin Tissoires 	struct hid_report *hreport;
251471d1714SBenjamin Tissoires 	int i, j;
252471d1714SBenjamin Tissoires 
253471d1714SBenjamin Tissoires 	/* check features first */
254471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_FEATURE_REPORT];
255471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
256471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++) {
257471d1714SBenjamin Tissoires 			/* Ignore if report count is out of bounds. */
258471d1714SBenjamin Tissoires 			if (hreport->field[i]->report_count < 1)
259471d1714SBenjamin Tissoires 				continue;
260471d1714SBenjamin Tissoires 
261471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++) {
262471d1714SBenjamin Tissoires 				wacom_feature_mapping(hdev, hreport->field[i],
263471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
264471d1714SBenjamin Tissoires 			}
265471d1714SBenjamin Tissoires 		}
266471d1714SBenjamin Tissoires 	}
267471d1714SBenjamin Tissoires 
268471d1714SBenjamin Tissoires 	/* now check the input usages */
269471d1714SBenjamin Tissoires 	rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
270471d1714SBenjamin Tissoires 	list_for_each_entry(hreport, &rep_enum->report_list, list) {
271471d1714SBenjamin Tissoires 
272471d1714SBenjamin Tissoires 		if (!hreport->maxfield)
273471d1714SBenjamin Tissoires 			continue;
274471d1714SBenjamin Tissoires 
275471d1714SBenjamin Tissoires 		for (i = 0; i < hreport->maxfield; i++)
276471d1714SBenjamin Tissoires 			for (j = 0; j < hreport->field[i]->maxusage; j++)
277471d1714SBenjamin Tissoires 				wacom_usage_mapping(hdev, hreport->field[i],
278471d1714SBenjamin Tissoires 						hreport->field[i]->usage + j);
279471d1714SBenjamin Tissoires 	}
280b58ba1baSJason Gerecke 
281b58ba1baSJason Gerecke 	wacom_post_parse_hid(hdev, features);
282471d1714SBenjamin Tissoires }
283471d1714SBenjamin Tissoires 
2845ae6e89fSBenjamin Tissoires static int wacom_hid_set_device_mode(struct hid_device *hdev)
2855ae6e89fSBenjamin Tissoires {
2865ae6e89fSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
2875ae6e89fSBenjamin Tissoires 	struct hid_data *hid_data = &wacom->wacom_wac.hid_data;
2885ae6e89fSBenjamin Tissoires 	struct hid_report *r;
2895ae6e89fSBenjamin Tissoires 	struct hid_report_enum *re;
2905ae6e89fSBenjamin Tissoires 
2915ae6e89fSBenjamin Tissoires 	if (hid_data->inputmode < 0)
2925ae6e89fSBenjamin Tissoires 		return 0;
2935ae6e89fSBenjamin Tissoires 
2945ae6e89fSBenjamin Tissoires 	re = &(hdev->report_enum[HID_FEATURE_REPORT]);
2955ae6e89fSBenjamin Tissoires 	r = re->report_id_hash[hid_data->inputmode];
2965ae6e89fSBenjamin Tissoires 	if (r) {
2975ae6e89fSBenjamin Tissoires 		r->field[0]->value[hid_data->inputmode_index] = 2;
2985ae6e89fSBenjamin Tissoires 		hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
2995ae6e89fSBenjamin Tissoires 	}
3005ae6e89fSBenjamin Tissoires 	return 0;
3015ae6e89fSBenjamin Tissoires }
3025ae6e89fSBenjamin Tissoires 
303471d1714SBenjamin Tissoires static int wacom_set_device_mode(struct hid_device *hdev, int report_id,
304471d1714SBenjamin Tissoires 		int length, int mode)
305471d1714SBenjamin Tissoires {
306471d1714SBenjamin Tissoires 	unsigned char *rep_data;
307471d1714SBenjamin Tissoires 	int error = -ENOMEM, limit = 0;
308471d1714SBenjamin Tissoires 
309471d1714SBenjamin Tissoires 	rep_data = kzalloc(length, GFP_KERNEL);
310471d1714SBenjamin Tissoires 	if (!rep_data)
311471d1714SBenjamin Tissoires 		return error;
312471d1714SBenjamin Tissoires 
313471d1714SBenjamin Tissoires 	do {
314471d1714SBenjamin Tissoires 		rep_data[0] = report_id;
315471d1714SBenjamin Tissoires 		rep_data[1] = mode;
316471d1714SBenjamin Tissoires 
317296b7378SPrzemo Firszt 		error = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data,
318296b7378SPrzemo Firszt 					 length, 1);
319471d1714SBenjamin Tissoires 		if (error >= 0)
320471d1714SBenjamin Tissoires 			error = wacom_get_report(hdev, HID_FEATURE_REPORT,
321c64d8834SPing Cheng 			                         rep_data, length, 1);
322471d1714SBenjamin Tissoires 	} while ((error < 0 || rep_data[1] != mode) && limit++ < WAC_MSG_RETRIES);
323471d1714SBenjamin Tissoires 
324471d1714SBenjamin Tissoires 	kfree(rep_data);
325471d1714SBenjamin Tissoires 
326471d1714SBenjamin Tissoires 	return error < 0 ? error : 0;
327471d1714SBenjamin Tissoires }
328471d1714SBenjamin Tissoires 
329f81a1295SBenjamin Tissoires static int wacom_bt_query_tablet_data(struct hid_device *hdev, u8 speed,
330f81a1295SBenjamin Tissoires 		struct wacom_features *features)
331f81a1295SBenjamin Tissoires {
332387142bbSBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
333387142bbSBenjamin Tissoires 	int ret;
334387142bbSBenjamin Tissoires 	u8 rep_data[2];
335387142bbSBenjamin Tissoires 
336387142bbSBenjamin Tissoires 	switch (features->type) {
337387142bbSBenjamin Tissoires 	case GRAPHIRE_BT:
338387142bbSBenjamin Tissoires 		rep_data[0] = 0x03;
339387142bbSBenjamin Tissoires 		rep_data[1] = 0x00;
340296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
341296b7378SPrzemo Firszt 					3);
342387142bbSBenjamin Tissoires 
343387142bbSBenjamin Tissoires 		if (ret >= 0) {
344387142bbSBenjamin Tissoires 			rep_data[0] = speed == 0 ? 0x05 : 0x06;
345387142bbSBenjamin Tissoires 			rep_data[1] = 0x00;
346387142bbSBenjamin Tissoires 
347387142bbSBenjamin Tissoires 			ret = wacom_set_report(hdev, HID_FEATURE_REPORT,
348296b7378SPrzemo Firszt 						rep_data, 2, 3);
349387142bbSBenjamin Tissoires 
350387142bbSBenjamin Tissoires 			if (ret >= 0) {
351387142bbSBenjamin Tissoires 				wacom->wacom_wac.bt_high_speed = speed;
352387142bbSBenjamin Tissoires 				return 0;
353387142bbSBenjamin Tissoires 			}
354387142bbSBenjamin Tissoires 		}
355387142bbSBenjamin Tissoires 
356387142bbSBenjamin Tissoires 		/*
357387142bbSBenjamin Tissoires 		 * Note that if the raw queries fail, it's not a hard failure
358387142bbSBenjamin Tissoires 		 * and it is safe to continue
359387142bbSBenjamin Tissoires 		 */
360387142bbSBenjamin Tissoires 		hid_warn(hdev, "failed to poke device, command %d, err %d\n",
361387142bbSBenjamin Tissoires 			 rep_data[0], ret);
362387142bbSBenjamin Tissoires 		break;
36381af7e61SBenjamin Tissoires 	case INTUOS4WL:
36481af7e61SBenjamin Tissoires 		if (speed == 1)
36581af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features &= ~0x20;
36681af7e61SBenjamin Tissoires 		else
36781af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_features |= 0x20;
36881af7e61SBenjamin Tissoires 
36981af7e61SBenjamin Tissoires 		rep_data[0] = 0x03;
37081af7e61SBenjamin Tissoires 		rep_data[1] = wacom->wacom_wac.bt_features;
37181af7e61SBenjamin Tissoires 
372296b7378SPrzemo Firszt 		ret = wacom_set_report(hdev, HID_FEATURE_REPORT, rep_data, 2,
373296b7378SPrzemo Firszt 					1);
37481af7e61SBenjamin Tissoires 		if (ret >= 0)
37581af7e61SBenjamin Tissoires 			wacom->wacom_wac.bt_high_speed = speed;
37681af7e61SBenjamin Tissoires 		break;
377387142bbSBenjamin Tissoires 	}
378387142bbSBenjamin Tissoires 
379f81a1295SBenjamin Tissoires 	return 0;
380f81a1295SBenjamin Tissoires }
381f81a1295SBenjamin Tissoires 
382471d1714SBenjamin Tissoires /*
383471d1714SBenjamin Tissoires  * Switch the tablet into its most-capable mode. Wacom tablets are
384471d1714SBenjamin Tissoires  * typically configured to power-up in a mode which sends mouse-like
385471d1714SBenjamin Tissoires  * reports to the OS. To get absolute position, pressure data, etc.
386471d1714SBenjamin Tissoires  * from the tablet, it is necessary to switch the tablet out of this
387471d1714SBenjamin Tissoires  * mode and into one which sends the full range of tablet data.
388471d1714SBenjamin Tissoires  */
389471d1714SBenjamin Tissoires static int wacom_query_tablet_data(struct hid_device *hdev,
390471d1714SBenjamin Tissoires 		struct wacom_features *features)
391471d1714SBenjamin Tissoires {
392f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
393f81a1295SBenjamin Tissoires 		return wacom_bt_query_tablet_data(hdev, 1, features);
394f81a1295SBenjamin Tissoires 
3955ae6e89fSBenjamin Tissoires 	if (features->type == HID_GENERIC)
3965ae6e89fSBenjamin Tissoires 		return wacom_hid_set_device_mode(hdev);
3975ae6e89fSBenjamin Tissoires 
398471d1714SBenjamin Tissoires 	if (features->device_type == BTN_TOOL_FINGER) {
399471d1714SBenjamin Tissoires 		if (features->type > TABLETPC) {
400471d1714SBenjamin Tissoires 			/* MT Tablet PC touch */
401471d1714SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 3, 4, 4);
402471d1714SBenjamin Tissoires 		}
403471d1714SBenjamin Tissoires 		else if (features->type == WACOM_24HDT || features->type == CINTIQ_HYBRID) {
404471d1714SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 18, 3, 2);
405471d1714SBenjamin Tissoires 		}
406500d4160SPing Cheng 		else if (features->type == WACOM_27QHDT) {
407500d4160SPing Cheng 			return wacom_set_device_mode(hdev, 131, 3, 2);
408500d4160SPing Cheng 		}
409471d1714SBenjamin Tissoires 	} else if (features->device_type == BTN_TOOL_PEN) {
410471d1714SBenjamin Tissoires 		if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
411471d1714SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 2, 2, 2);
412471d1714SBenjamin Tissoires 		}
413471d1714SBenjamin Tissoires 	}
414471d1714SBenjamin Tissoires 
415471d1714SBenjamin Tissoires 	return 0;
416471d1714SBenjamin Tissoires }
417471d1714SBenjamin Tissoires 
418471d1714SBenjamin Tissoires static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
419471d1714SBenjamin Tissoires 					 struct wacom_features *features)
420471d1714SBenjamin Tissoires {
421471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
422471d1714SBenjamin Tissoires 	struct usb_interface *intf = wacom->intf;
423471d1714SBenjamin Tissoires 
424471d1714SBenjamin Tissoires 	/* default features */
425471d1714SBenjamin Tissoires 	features->device_type = BTN_TOOL_PEN;
426471d1714SBenjamin Tissoires 	features->x_fuzz = 4;
427471d1714SBenjamin Tissoires 	features->y_fuzz = 4;
428471d1714SBenjamin Tissoires 	features->pressure_fuzz = 0;
429471d1714SBenjamin Tissoires 	features->distance_fuzz = 0;
430471d1714SBenjamin Tissoires 
431471d1714SBenjamin Tissoires 	/*
432471d1714SBenjamin Tissoires 	 * The wireless device HID is basic and layout conflicts with
433471d1714SBenjamin Tissoires 	 * other tablets (monitor and touch interface can look like pen).
434471d1714SBenjamin Tissoires 	 * Skip the query for this type and modify defaults based on
435471d1714SBenjamin Tissoires 	 * interface number.
436471d1714SBenjamin Tissoires 	 */
437471d1714SBenjamin Tissoires 	if (features->type == WIRELESS) {
438471d1714SBenjamin Tissoires 		if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
439471d1714SBenjamin Tissoires 			features->device_type = 0;
440471d1714SBenjamin Tissoires 		} else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
441471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_FINGER;
442471d1714SBenjamin Tissoires 			features->pktlen = WACOM_PKGLEN_BBTOUCH3;
443471d1714SBenjamin Tissoires 		}
444471d1714SBenjamin Tissoires 	}
445471d1714SBenjamin Tissoires 
446471d1714SBenjamin Tissoires 	/* only devices that support touch need to retrieve the info */
447471d1714SBenjamin Tissoires 	if (features->type < BAMBOO_PT)
448471d1714SBenjamin Tissoires 		return;
449471d1714SBenjamin Tissoires 
450471d1714SBenjamin Tissoires 	wacom_parse_hid(hdev, features);
451471d1714SBenjamin Tissoires }
452471d1714SBenjamin Tissoires 
453471d1714SBenjamin Tissoires struct wacom_hdev_data {
454471d1714SBenjamin Tissoires 	struct list_head list;
455471d1714SBenjamin Tissoires 	struct kref kref;
456471d1714SBenjamin Tissoires 	struct hid_device *dev;
457471d1714SBenjamin Tissoires 	struct wacom_shared shared;
458471d1714SBenjamin Tissoires };
459471d1714SBenjamin Tissoires 
460471d1714SBenjamin Tissoires static LIST_HEAD(wacom_udev_list);
461471d1714SBenjamin Tissoires static DEFINE_MUTEX(wacom_udev_list_lock);
462471d1714SBenjamin Tissoires 
463471d1714SBenjamin Tissoires static bool wacom_are_sibling(struct hid_device *hdev,
464471d1714SBenjamin Tissoires 		struct hid_device *sibling)
465471d1714SBenjamin Tissoires {
466471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
467471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
468471d1714SBenjamin Tissoires 	int vid = features->oVid;
469471d1714SBenjamin Tissoires 	int pid = features->oPid;
470471d1714SBenjamin Tissoires 	int n1,n2;
471471d1714SBenjamin Tissoires 
472471d1714SBenjamin Tissoires 	if (vid == 0 && pid == 0) {
473471d1714SBenjamin Tissoires 		vid = hdev->vendor;
474471d1714SBenjamin Tissoires 		pid = hdev->product;
475471d1714SBenjamin Tissoires 	}
476471d1714SBenjamin Tissoires 
477471d1714SBenjamin Tissoires 	if (vid != sibling->vendor || pid != sibling->product)
478471d1714SBenjamin Tissoires 		return false;
479471d1714SBenjamin Tissoires 
480471d1714SBenjamin Tissoires 	/* Compare the physical path. */
481471d1714SBenjamin Tissoires 	n1 = strrchr(hdev->phys, '.') - hdev->phys;
482471d1714SBenjamin Tissoires 	n2 = strrchr(sibling->phys, '.') - sibling->phys;
483471d1714SBenjamin Tissoires 	if (n1 != n2 || n1 <= 0 || n2 <= 0)
484471d1714SBenjamin Tissoires 		return false;
485471d1714SBenjamin Tissoires 
486471d1714SBenjamin Tissoires 	return !strncmp(hdev->phys, sibling->phys, n1);
487471d1714SBenjamin Tissoires }
488471d1714SBenjamin Tissoires 
489471d1714SBenjamin Tissoires static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
490471d1714SBenjamin Tissoires {
491471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
492471d1714SBenjamin Tissoires 
493471d1714SBenjamin Tissoires 	list_for_each_entry(data, &wacom_udev_list, list) {
494471d1714SBenjamin Tissoires 		if (wacom_are_sibling(hdev, data->dev)) {
495471d1714SBenjamin Tissoires 			kref_get(&data->kref);
496471d1714SBenjamin Tissoires 			return data;
497471d1714SBenjamin Tissoires 		}
498471d1714SBenjamin Tissoires 	}
499471d1714SBenjamin Tissoires 
500471d1714SBenjamin Tissoires 	return NULL;
501471d1714SBenjamin Tissoires }
502471d1714SBenjamin Tissoires 
503471d1714SBenjamin Tissoires static int wacom_add_shared_data(struct hid_device *hdev)
504471d1714SBenjamin Tissoires {
505471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
506471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
507471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
508471d1714SBenjamin Tissoires 	int retval = 0;
509471d1714SBenjamin Tissoires 
510471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
511471d1714SBenjamin Tissoires 
512471d1714SBenjamin Tissoires 	data = wacom_get_hdev_data(hdev);
513471d1714SBenjamin Tissoires 	if (!data) {
514471d1714SBenjamin Tissoires 		data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
515471d1714SBenjamin Tissoires 		if (!data) {
516471d1714SBenjamin Tissoires 			retval = -ENOMEM;
517471d1714SBenjamin Tissoires 			goto out;
518471d1714SBenjamin Tissoires 		}
519471d1714SBenjamin Tissoires 
520471d1714SBenjamin Tissoires 		kref_init(&data->kref);
521471d1714SBenjamin Tissoires 		data->dev = hdev;
522471d1714SBenjamin Tissoires 		list_add_tail(&data->list, &wacom_udev_list);
523471d1714SBenjamin Tissoires 	}
524471d1714SBenjamin Tissoires 
525471d1714SBenjamin Tissoires 	wacom_wac->shared = &data->shared;
526471d1714SBenjamin Tissoires 
527471d1714SBenjamin Tissoires out:
528471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
529471d1714SBenjamin Tissoires 	return retval;
530471d1714SBenjamin Tissoires }
531471d1714SBenjamin Tissoires 
532471d1714SBenjamin Tissoires static void wacom_release_shared_data(struct kref *kref)
533471d1714SBenjamin Tissoires {
534471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data =
535471d1714SBenjamin Tissoires 		container_of(kref, struct wacom_hdev_data, kref);
536471d1714SBenjamin Tissoires 
537471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
538471d1714SBenjamin Tissoires 	list_del(&data->list);
539471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
540471d1714SBenjamin Tissoires 
541471d1714SBenjamin Tissoires 	kfree(data);
542471d1714SBenjamin Tissoires }
543471d1714SBenjamin Tissoires 
544471d1714SBenjamin Tissoires static void wacom_remove_shared_data(struct wacom_wac *wacom)
545471d1714SBenjamin Tissoires {
546471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
547471d1714SBenjamin Tissoires 
548471d1714SBenjamin Tissoires 	if (wacom->shared) {
549471d1714SBenjamin Tissoires 		data = container_of(wacom->shared, struct wacom_hdev_data, shared);
550471d1714SBenjamin Tissoires 		kref_put(&data->kref, wacom_release_shared_data);
551471d1714SBenjamin Tissoires 		wacom->shared = NULL;
552471d1714SBenjamin Tissoires 	}
553471d1714SBenjamin Tissoires }
554471d1714SBenjamin Tissoires 
555471d1714SBenjamin Tissoires static int wacom_led_control(struct wacom *wacom)
556471d1714SBenjamin Tissoires {
557471d1714SBenjamin Tissoires 	unsigned char *buf;
558471d1714SBenjamin Tissoires 	int retval;
559912ca216SPing Cheng 	unsigned char report_id = WAC_CMD_LED_CONTROL;
560912ca216SPing Cheng 	int buf_size = 9;
561471d1714SBenjamin Tissoires 
562912ca216SPing Cheng 	if (wacom->wacom_wac.pid) { /* wireless connected */
563912ca216SPing Cheng 		report_id = WAC_CMD_WL_LED_CONTROL;
564912ca216SPing Cheng 		buf_size = 13;
565912ca216SPing Cheng 	}
566912ca216SPing Cheng 	buf = kzalloc(buf_size, GFP_KERNEL);
567471d1714SBenjamin Tissoires 	if (!buf)
568471d1714SBenjamin Tissoires 		return -ENOMEM;
569471d1714SBenjamin Tissoires 
570471d1714SBenjamin Tissoires 	if (wacom->wacom_wac.features.type >= INTUOS5S &&
571471d1714SBenjamin Tissoires 	    wacom->wacom_wac.features.type <= INTUOSPL) {
572471d1714SBenjamin Tissoires 		/*
573471d1714SBenjamin Tissoires 		 * Touch Ring and crop mark LED luminance may take on
574471d1714SBenjamin Tissoires 		 * one of four values:
575471d1714SBenjamin Tissoires 		 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
576471d1714SBenjamin Tissoires 		 */
577471d1714SBenjamin Tissoires 		int ring_led = wacom->led.select[0] & 0x03;
578471d1714SBenjamin Tissoires 		int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
579471d1714SBenjamin Tissoires 		int crop_lum = 0;
580912ca216SPing Cheng 		unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
581471d1714SBenjamin Tissoires 
582912ca216SPing Cheng 		buf[0] = report_id;
583912ca216SPing Cheng 		if (wacom->wacom_wac.pid) {
584912ca216SPing Cheng 			wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
585912ca216SPing Cheng 					 buf, buf_size, WAC_CMD_RETRIES);
586912ca216SPing Cheng 			buf[0] = report_id;
587912ca216SPing Cheng 			buf[4] = led_bits;
588912ca216SPing Cheng 		} else
589912ca216SPing Cheng 			buf[1] = led_bits;
590471d1714SBenjamin Tissoires 	}
591471d1714SBenjamin Tissoires 	else {
592471d1714SBenjamin Tissoires 		int led = wacom->led.select[0] | 0x4;
593471d1714SBenjamin Tissoires 
594471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
595471d1714SBenjamin Tissoires 		    wacom->wacom_wac.features.type == WACOM_24HD)
596471d1714SBenjamin Tissoires 			led |= (wacom->led.select[1] << 4) | 0x40;
597471d1714SBenjamin Tissoires 
598912ca216SPing Cheng 		buf[0] = report_id;
599471d1714SBenjamin Tissoires 		buf[1] = led;
600471d1714SBenjamin Tissoires 		buf[2] = wacom->led.llv;
601471d1714SBenjamin Tissoires 		buf[3] = wacom->led.hlv;
602471d1714SBenjamin Tissoires 		buf[4] = wacom->led.img_lum;
603471d1714SBenjamin Tissoires 	}
604471d1714SBenjamin Tissoires 
605912ca216SPing Cheng 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
606296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
607471d1714SBenjamin Tissoires 	kfree(buf);
608471d1714SBenjamin Tissoires 
609471d1714SBenjamin Tissoires 	return retval;
610471d1714SBenjamin Tissoires }
611471d1714SBenjamin Tissoires 
612849e2f06SBenjamin Tissoires static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
613849e2f06SBenjamin Tissoires 		const unsigned len, const void *img)
614471d1714SBenjamin Tissoires {
615471d1714SBenjamin Tissoires 	unsigned char *buf;
616471d1714SBenjamin Tissoires 	int i, retval;
617849e2f06SBenjamin Tissoires 	const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
618471d1714SBenjamin Tissoires 
619849e2f06SBenjamin Tissoires 	buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
620471d1714SBenjamin Tissoires 	if (!buf)
621471d1714SBenjamin Tissoires 		return -ENOMEM;
622471d1714SBenjamin Tissoires 
623471d1714SBenjamin Tissoires 	/* Send 'start' command */
624471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
625471d1714SBenjamin Tissoires 	buf[1] = 1;
626296b7378SPrzemo Firszt 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
627296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
628471d1714SBenjamin Tissoires 	if (retval < 0)
629471d1714SBenjamin Tissoires 		goto out;
630471d1714SBenjamin Tissoires 
631849e2f06SBenjamin Tissoires 	buf[0] = xfer_id;
632471d1714SBenjamin Tissoires 	buf[1] = button_id & 0x07;
633471d1714SBenjamin Tissoires 	for (i = 0; i < 4; i++) {
634471d1714SBenjamin Tissoires 		buf[2] = i;
635849e2f06SBenjamin Tissoires 		memcpy(buf + 3, img + i * chunk_len, chunk_len);
636471d1714SBenjamin Tissoires 
637471d1714SBenjamin Tissoires 		retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
638296b7378SPrzemo Firszt 					  buf, chunk_len + 3, WAC_CMD_RETRIES);
639471d1714SBenjamin Tissoires 		if (retval < 0)
640471d1714SBenjamin Tissoires 			break;
641471d1714SBenjamin Tissoires 	}
642471d1714SBenjamin Tissoires 
643471d1714SBenjamin Tissoires 	/* Send 'stop' */
644471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
645471d1714SBenjamin Tissoires 	buf[1] = 0;
646296b7378SPrzemo Firszt 	wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
647296b7378SPrzemo Firszt 			 WAC_CMD_RETRIES);
648471d1714SBenjamin Tissoires 
649471d1714SBenjamin Tissoires out:
650471d1714SBenjamin Tissoires 	kfree(buf);
651471d1714SBenjamin Tissoires 	return retval;
652471d1714SBenjamin Tissoires }
653471d1714SBenjamin Tissoires 
654471d1714SBenjamin Tissoires static ssize_t wacom_led_select_store(struct device *dev, int set_id,
655471d1714SBenjamin Tissoires 				      const char *buf, size_t count)
656471d1714SBenjamin Tissoires {
657471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
658471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
659471d1714SBenjamin Tissoires 	unsigned int id;
660471d1714SBenjamin Tissoires 	int err;
661471d1714SBenjamin Tissoires 
662471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &id);
663471d1714SBenjamin Tissoires 	if (err)
664471d1714SBenjamin Tissoires 		return err;
665471d1714SBenjamin Tissoires 
666471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
667471d1714SBenjamin Tissoires 
668471d1714SBenjamin Tissoires 	wacom->led.select[set_id] = id & 0x3;
669471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
670471d1714SBenjamin Tissoires 
671471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
672471d1714SBenjamin Tissoires 
673471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
674471d1714SBenjamin Tissoires }
675471d1714SBenjamin Tissoires 
676471d1714SBenjamin Tissoires #define DEVICE_LED_SELECT_ATTR(SET_ID)					\
677471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
678471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
679471d1714SBenjamin Tissoires {									\
680471d1714SBenjamin Tissoires 	return wacom_led_select_store(dev, SET_ID, buf, count);		\
681471d1714SBenjamin Tissoires }									\
682471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
683471d1714SBenjamin Tissoires 	struct device_attribute *attr, char *buf)			\
684471d1714SBenjamin Tissoires {									\
685471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
686471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
68737449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n",			\
68837449adcSPing Cheng 			 wacom->led.select[SET_ID]);			\
689471d1714SBenjamin Tissoires }									\
690e0984bc3SPing Cheng static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,	\
691471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_show,			\
692471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_store)
693471d1714SBenjamin Tissoires 
694471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(0);
695471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(1);
696471d1714SBenjamin Tissoires 
697471d1714SBenjamin Tissoires static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
698471d1714SBenjamin Tissoires 				     const char *buf, size_t count)
699471d1714SBenjamin Tissoires {
700471d1714SBenjamin Tissoires 	unsigned int value;
701471d1714SBenjamin Tissoires 	int err;
702471d1714SBenjamin Tissoires 
703471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &value);
704471d1714SBenjamin Tissoires 	if (err)
705471d1714SBenjamin Tissoires 		return err;
706471d1714SBenjamin Tissoires 
707471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
708471d1714SBenjamin Tissoires 
709471d1714SBenjamin Tissoires 	*dest = value & 0x7f;
710471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
711471d1714SBenjamin Tissoires 
712471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
713471d1714SBenjamin Tissoires 
714471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
715471d1714SBenjamin Tissoires }
716471d1714SBenjamin Tissoires 
717471d1714SBenjamin Tissoires #define DEVICE_LUMINANCE_ATTR(name, field)				\
718471d1714SBenjamin Tissoires static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
719471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
720471d1714SBenjamin Tissoires {									\
721471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
722471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
723471d1714SBenjamin Tissoires 									\
724471d1714SBenjamin Tissoires 	return wacom_luminance_store(wacom, &wacom->led.field,		\
725471d1714SBenjamin Tissoires 				     buf, count);			\
726471d1714SBenjamin Tissoires }									\
72737449adcSPing Cheng static ssize_t wacom_##name##_luminance_show(struct device *dev,	\
72837449adcSPing Cheng 	struct device_attribute *attr, char *buf)			\
72937449adcSPing Cheng {									\
73037449adcSPing Cheng 	struct wacom *wacom = dev_get_drvdata(dev);			\
73137449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);	\
73237449adcSPing Cheng }									\
733e0984bc3SPing Cheng static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,			\
73437449adcSPing Cheng 		   wacom_##name##_luminance_show,			\
73537449adcSPing Cheng 		   wacom_##name##_luminance_store)
736471d1714SBenjamin Tissoires 
737471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status0, llv);
738471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status1, hlv);
739471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(buttons, img_lum);
740471d1714SBenjamin Tissoires 
741471d1714SBenjamin Tissoires static ssize_t wacom_button_image_store(struct device *dev, int button_id,
742471d1714SBenjamin Tissoires 					const char *buf, size_t count)
743471d1714SBenjamin Tissoires {
744471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
745471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
746471d1714SBenjamin Tissoires 	int err;
747849e2f06SBenjamin Tissoires 	unsigned len;
748849e2f06SBenjamin Tissoires 	u8 xfer_id;
749471d1714SBenjamin Tissoires 
750849e2f06SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
751849e2f06SBenjamin Tissoires 		len = 256;
752849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_BT_XFER;
753849e2f06SBenjamin Tissoires 	} else {
754849e2f06SBenjamin Tissoires 		len = 1024;
755849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_XFER;
756849e2f06SBenjamin Tissoires 	}
757849e2f06SBenjamin Tissoires 
758849e2f06SBenjamin Tissoires 	if (count != len)
759471d1714SBenjamin Tissoires 		return -EINVAL;
760471d1714SBenjamin Tissoires 
761471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
762471d1714SBenjamin Tissoires 
763849e2f06SBenjamin Tissoires 	err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
764471d1714SBenjamin Tissoires 
765471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
766471d1714SBenjamin Tissoires 
767471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
768471d1714SBenjamin Tissoires }
769471d1714SBenjamin Tissoires 
770471d1714SBenjamin Tissoires #define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
771471d1714SBenjamin Tissoires static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
772471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
773471d1714SBenjamin Tissoires {									\
774471d1714SBenjamin Tissoires 	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
775471d1714SBenjamin Tissoires }									\
776e0984bc3SPing Cheng static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,	\
777471d1714SBenjamin Tissoires 		   NULL, wacom_btnimg##BUTTON_ID##_store)
778471d1714SBenjamin Tissoires 
779471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(0);
780471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(1);
781471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(2);
782471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(3);
783471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(4);
784471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(5);
785471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(6);
786471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(7);
787471d1714SBenjamin Tissoires 
788471d1714SBenjamin Tissoires static struct attribute *cintiq_led_attrs[] = {
789471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
790471d1714SBenjamin Tissoires 	&dev_attr_status_led1_select.attr,
791471d1714SBenjamin Tissoires 	NULL
792471d1714SBenjamin Tissoires };
793471d1714SBenjamin Tissoires 
794471d1714SBenjamin Tissoires static struct attribute_group cintiq_led_attr_group = {
795471d1714SBenjamin Tissoires 	.name = "wacom_led",
796471d1714SBenjamin Tissoires 	.attrs = cintiq_led_attrs,
797471d1714SBenjamin Tissoires };
798471d1714SBenjamin Tissoires 
799471d1714SBenjamin Tissoires static struct attribute *intuos4_led_attrs[] = {
800471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
801471d1714SBenjamin Tissoires 	&dev_attr_status1_luminance.attr,
802471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
803471d1714SBenjamin Tissoires 	&dev_attr_buttons_luminance.attr,
804471d1714SBenjamin Tissoires 	&dev_attr_button0_rawimg.attr,
805471d1714SBenjamin Tissoires 	&dev_attr_button1_rawimg.attr,
806471d1714SBenjamin Tissoires 	&dev_attr_button2_rawimg.attr,
807471d1714SBenjamin Tissoires 	&dev_attr_button3_rawimg.attr,
808471d1714SBenjamin Tissoires 	&dev_attr_button4_rawimg.attr,
809471d1714SBenjamin Tissoires 	&dev_attr_button5_rawimg.attr,
810471d1714SBenjamin Tissoires 	&dev_attr_button6_rawimg.attr,
811471d1714SBenjamin Tissoires 	&dev_attr_button7_rawimg.attr,
812471d1714SBenjamin Tissoires 	NULL
813471d1714SBenjamin Tissoires };
814471d1714SBenjamin Tissoires 
815471d1714SBenjamin Tissoires static struct attribute_group intuos4_led_attr_group = {
816471d1714SBenjamin Tissoires 	.name = "wacom_led",
817471d1714SBenjamin Tissoires 	.attrs = intuos4_led_attrs,
818471d1714SBenjamin Tissoires };
819471d1714SBenjamin Tissoires 
820471d1714SBenjamin Tissoires static struct attribute *intuos5_led_attrs[] = {
821471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
822471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
823471d1714SBenjamin Tissoires 	NULL
824471d1714SBenjamin Tissoires };
825471d1714SBenjamin Tissoires 
826471d1714SBenjamin Tissoires static struct attribute_group intuos5_led_attr_group = {
827471d1714SBenjamin Tissoires 	.name = "wacom_led",
828471d1714SBenjamin Tissoires 	.attrs = intuos5_led_attrs,
829471d1714SBenjamin Tissoires };
830471d1714SBenjamin Tissoires 
831471d1714SBenjamin Tissoires static int wacom_initialize_leds(struct wacom *wacom)
832471d1714SBenjamin Tissoires {
833471d1714SBenjamin Tissoires 	int error;
834471d1714SBenjamin Tissoires 
835471d1714SBenjamin Tissoires 	/* Initialize default values */
836471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
837471d1714SBenjamin Tissoires 	case INTUOS4S:
838471d1714SBenjamin Tissoires 	case INTUOS4:
83981af7e61SBenjamin Tissoires 	case INTUOS4WL:
840471d1714SBenjamin Tissoires 	case INTUOS4L:
841471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
842471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
843471d1714SBenjamin Tissoires 		wacom->led.llv = 10;
844471d1714SBenjamin Tissoires 		wacom->led.hlv = 20;
845471d1714SBenjamin Tissoires 		wacom->led.img_lum = 10;
846471d1714SBenjamin Tissoires 		error = sysfs_create_group(&wacom->hdev->dev.kobj,
847471d1714SBenjamin Tissoires 					   &intuos4_led_attr_group);
848471d1714SBenjamin Tissoires 		break;
849471d1714SBenjamin Tissoires 
850471d1714SBenjamin Tissoires 	case WACOM_24HD:
851471d1714SBenjamin Tissoires 	case WACOM_21UX2:
852471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
853471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
854471d1714SBenjamin Tissoires 		wacom->led.llv = 0;
855471d1714SBenjamin Tissoires 		wacom->led.hlv = 0;
856471d1714SBenjamin Tissoires 		wacom->led.img_lum = 0;
857471d1714SBenjamin Tissoires 
858471d1714SBenjamin Tissoires 		error = sysfs_create_group(&wacom->hdev->dev.kobj,
859471d1714SBenjamin Tissoires 					   &cintiq_led_attr_group);
860471d1714SBenjamin Tissoires 		break;
861471d1714SBenjamin Tissoires 
862471d1714SBenjamin Tissoires 	case INTUOS5S:
863471d1714SBenjamin Tissoires 	case INTUOS5:
864471d1714SBenjamin Tissoires 	case INTUOS5L:
865471d1714SBenjamin Tissoires 	case INTUOSPS:
866471d1714SBenjamin Tissoires 	case INTUOSPM:
867471d1714SBenjamin Tissoires 	case INTUOSPL:
868471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
869471d1714SBenjamin Tissoires 			wacom->led.select[0] = 0;
870471d1714SBenjamin Tissoires 			wacom->led.select[1] = 0;
871471d1714SBenjamin Tissoires 			wacom->led.llv = 32;
872471d1714SBenjamin Tissoires 			wacom->led.hlv = 0;
873471d1714SBenjamin Tissoires 			wacom->led.img_lum = 0;
874471d1714SBenjamin Tissoires 
875471d1714SBenjamin Tissoires 			error = sysfs_create_group(&wacom->hdev->dev.kobj,
876471d1714SBenjamin Tissoires 						  &intuos5_led_attr_group);
877471d1714SBenjamin Tissoires 		} else
878471d1714SBenjamin Tissoires 			return 0;
879471d1714SBenjamin Tissoires 		break;
880471d1714SBenjamin Tissoires 
881471d1714SBenjamin Tissoires 	default:
882471d1714SBenjamin Tissoires 		return 0;
883471d1714SBenjamin Tissoires 	}
884471d1714SBenjamin Tissoires 
885471d1714SBenjamin Tissoires 	if (error) {
886471d1714SBenjamin Tissoires 		hid_err(wacom->hdev,
887471d1714SBenjamin Tissoires 			"cannot create sysfs group err: %d\n", error);
888471d1714SBenjamin Tissoires 		return error;
889471d1714SBenjamin Tissoires 	}
890471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
891c757cbafSBenjamin Tissoires 	wacom->led_initialized = true;
892471d1714SBenjamin Tissoires 
893471d1714SBenjamin Tissoires 	return 0;
894471d1714SBenjamin Tissoires }
895471d1714SBenjamin Tissoires 
896471d1714SBenjamin Tissoires static void wacom_destroy_leds(struct wacom *wacom)
897471d1714SBenjamin Tissoires {
898c757cbafSBenjamin Tissoires 	if (!wacom->led_initialized)
899c757cbafSBenjamin Tissoires 		return;
900c757cbafSBenjamin Tissoires 
901c757cbafSBenjamin Tissoires 	wacom->led_initialized = false;
902c757cbafSBenjamin Tissoires 
903471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
904471d1714SBenjamin Tissoires 	case INTUOS4S:
905471d1714SBenjamin Tissoires 	case INTUOS4:
90681af7e61SBenjamin Tissoires 	case INTUOS4WL:
907471d1714SBenjamin Tissoires 	case INTUOS4L:
908471d1714SBenjamin Tissoires 		sysfs_remove_group(&wacom->hdev->dev.kobj,
909471d1714SBenjamin Tissoires 				   &intuos4_led_attr_group);
910471d1714SBenjamin Tissoires 		break;
911471d1714SBenjamin Tissoires 
912471d1714SBenjamin Tissoires 	case WACOM_24HD:
913471d1714SBenjamin Tissoires 	case WACOM_21UX2:
914471d1714SBenjamin Tissoires 		sysfs_remove_group(&wacom->hdev->dev.kobj,
915471d1714SBenjamin Tissoires 				   &cintiq_led_attr_group);
916471d1714SBenjamin Tissoires 		break;
917471d1714SBenjamin Tissoires 
918471d1714SBenjamin Tissoires 	case INTUOS5S:
919471d1714SBenjamin Tissoires 	case INTUOS5:
920471d1714SBenjamin Tissoires 	case INTUOS5L:
921471d1714SBenjamin Tissoires 	case INTUOSPS:
922471d1714SBenjamin Tissoires 	case INTUOSPM:
923471d1714SBenjamin Tissoires 	case INTUOSPL:
924471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
925471d1714SBenjamin Tissoires 			sysfs_remove_group(&wacom->hdev->dev.kobj,
926471d1714SBenjamin Tissoires 					   &intuos5_led_attr_group);
927471d1714SBenjamin Tissoires 		break;
928471d1714SBenjamin Tissoires 	}
929471d1714SBenjamin Tissoires }
930471d1714SBenjamin Tissoires 
931471d1714SBenjamin Tissoires static enum power_supply_property wacom_battery_props[] = {
932ac8d1010SBenjamin Tissoires 	POWER_SUPPLY_PROP_STATUS,
933471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
934471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_CAPACITY
935471d1714SBenjamin Tissoires };
936471d1714SBenjamin Tissoires 
9377dbd229eSBenjamin Tissoires static enum power_supply_property wacom_ac_props[] = {
9387dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_PRESENT,
9397dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_ONLINE,
9407dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
9417dbd229eSBenjamin Tissoires };
9427dbd229eSBenjamin Tissoires 
943471d1714SBenjamin Tissoires static int wacom_battery_get_property(struct power_supply *psy,
944471d1714SBenjamin Tissoires 				      enum power_supply_property psp,
945471d1714SBenjamin Tissoires 				      union power_supply_propval *val)
946471d1714SBenjamin Tissoires {
947*297d716fSKrzysztof Kozlowski 	struct wacom *wacom = power_supply_get_drvdata(psy);
948471d1714SBenjamin Tissoires 	int ret = 0;
949471d1714SBenjamin Tissoires 
950471d1714SBenjamin Tissoires 	switch (psp) {
951471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_SCOPE:
952471d1714SBenjamin Tissoires 			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
953471d1714SBenjamin Tissoires 			break;
954471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_CAPACITY:
955471d1714SBenjamin Tissoires 			val->intval =
956ac8d1010SBenjamin Tissoires 				wacom->wacom_wac.battery_capacity;
957ac8d1010SBenjamin Tissoires 			break;
958ac8d1010SBenjamin Tissoires 		case POWER_SUPPLY_PROP_STATUS:
959ac8d1010SBenjamin Tissoires 			if (wacom->wacom_wac.bat_charging)
960ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_CHARGING;
961ac8d1010SBenjamin Tissoires 			else if (wacom->wacom_wac.battery_capacity == 100 &&
962ac8d1010SBenjamin Tissoires 				    wacom->wacom_wac.ps_connected)
963ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_FULL;
964ac8d1010SBenjamin Tissoires 			else
965ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
966471d1714SBenjamin Tissoires 			break;
967471d1714SBenjamin Tissoires 		default:
968471d1714SBenjamin Tissoires 			ret = -EINVAL;
969471d1714SBenjamin Tissoires 			break;
970471d1714SBenjamin Tissoires 	}
971471d1714SBenjamin Tissoires 
972471d1714SBenjamin Tissoires 	return ret;
973471d1714SBenjamin Tissoires }
974471d1714SBenjamin Tissoires 
9757dbd229eSBenjamin Tissoires static int wacom_ac_get_property(struct power_supply *psy,
9767dbd229eSBenjamin Tissoires 				enum power_supply_property psp,
9777dbd229eSBenjamin Tissoires 				union power_supply_propval *val)
9787dbd229eSBenjamin Tissoires {
979*297d716fSKrzysztof Kozlowski 	struct wacom *wacom = power_supply_get_drvdata(psy);
9807dbd229eSBenjamin Tissoires 	int ret = 0;
9817dbd229eSBenjamin Tissoires 
9827dbd229eSBenjamin Tissoires 	switch (psp) {
9837dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_PRESENT:
9847dbd229eSBenjamin Tissoires 		/* fall through */
9857dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_ONLINE:
9867dbd229eSBenjamin Tissoires 		val->intval = wacom->wacom_wac.ps_connected;
9877dbd229eSBenjamin Tissoires 		break;
9887dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_SCOPE:
9897dbd229eSBenjamin Tissoires 		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
9907dbd229eSBenjamin Tissoires 		break;
9917dbd229eSBenjamin Tissoires 	default:
9927dbd229eSBenjamin Tissoires 		ret = -EINVAL;
9937dbd229eSBenjamin Tissoires 		break;
9947dbd229eSBenjamin Tissoires 	}
9957dbd229eSBenjamin Tissoires 	return ret;
9967dbd229eSBenjamin Tissoires }
9977dbd229eSBenjamin Tissoires 
998471d1714SBenjamin Tissoires static int wacom_initialize_battery(struct wacom *wacom)
999471d1714SBenjamin Tissoires {
1000d70420b9SBenjamin Tissoires 	static atomic_t battery_no = ATOMIC_INIT(0);
1001*297d716fSKrzysztof Kozlowski 	struct power_supply_config psy_cfg = { .drv_data = wacom, };
1002d70420b9SBenjamin Tissoires 	unsigned long n;
1003471d1714SBenjamin Tissoires 
1004ac8d1010SBenjamin Tissoires 	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
1005*297d716fSKrzysztof Kozlowski 		struct power_supply_desc *bat_desc = &wacom->battery_desc;
1006*297d716fSKrzysztof Kozlowski 		struct power_supply_desc *ac_desc = &wacom->ac_desc;
1007d70420b9SBenjamin Tissoires 		n = atomic_inc_return(&battery_no) - 1;
10087dbd229eSBenjamin Tissoires 
1009*297d716fSKrzysztof Kozlowski 		bat_desc->properties = wacom_battery_props;
1010*297d716fSKrzysztof Kozlowski 		bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1011*297d716fSKrzysztof Kozlowski 		bat_desc->get_property = wacom_battery_get_property;
1012d70420b9SBenjamin Tissoires 		sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
1013*297d716fSKrzysztof Kozlowski 		bat_desc->name = wacom->wacom_wac.bat_name;
1014*297d716fSKrzysztof Kozlowski 		bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1015*297d716fSKrzysztof Kozlowski 		bat_desc->use_for_apm = 0;
1016471d1714SBenjamin Tissoires 
1017*297d716fSKrzysztof Kozlowski 		ac_desc->properties = wacom_ac_props;
1018*297d716fSKrzysztof Kozlowski 		ac_desc->num_properties = ARRAY_SIZE(wacom_ac_props);
1019*297d716fSKrzysztof Kozlowski 		ac_desc->get_property = wacom_ac_get_property;
10207dbd229eSBenjamin Tissoires 		sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
1021*297d716fSKrzysztof Kozlowski 		ac_desc->name = wacom->wacom_wac.ac_name;
1022*297d716fSKrzysztof Kozlowski 		ac_desc->type = POWER_SUPPLY_TYPE_MAINS;
1023*297d716fSKrzysztof Kozlowski 		ac_desc->use_for_apm = 0;
10247dbd229eSBenjamin Tissoires 
1025*297d716fSKrzysztof Kozlowski 		wacom->battery = power_supply_register(&wacom->hdev->dev,
1026*297d716fSKrzysztof Kozlowski 					      &wacom->battery_desc, &psy_cfg);
1027*297d716fSKrzysztof Kozlowski 		if (IS_ERR(wacom->battery))
1028*297d716fSKrzysztof Kozlowski 			return PTR_ERR(wacom->battery);
1029471d1714SBenjamin Tissoires 
1030*297d716fSKrzysztof Kozlowski 		power_supply_powers(wacom->battery, &wacom->hdev->dev);
10317dbd229eSBenjamin Tissoires 
1032*297d716fSKrzysztof Kozlowski 		wacom->ac = power_supply_register(&wacom->hdev->dev,
1033*297d716fSKrzysztof Kozlowski 						  &wacom->ac_desc,
1034*297d716fSKrzysztof Kozlowski 						  &psy_cfg);
1035*297d716fSKrzysztof Kozlowski 		if (IS_ERR(wacom->ac)) {
1036*297d716fSKrzysztof Kozlowski 			power_supply_unregister(wacom->battery);
1037*297d716fSKrzysztof Kozlowski 			return PTR_ERR(wacom->ac);
1038471d1714SBenjamin Tissoires 		}
1039471d1714SBenjamin Tissoires 
1040*297d716fSKrzysztof Kozlowski 		power_supply_powers(wacom->ac, &wacom->hdev->dev);
10417dbd229eSBenjamin Tissoires 	}
10427dbd229eSBenjamin Tissoires 
10437dbd229eSBenjamin Tissoires 	return 0;
1044471d1714SBenjamin Tissoires }
1045471d1714SBenjamin Tissoires 
1046471d1714SBenjamin Tissoires static void wacom_destroy_battery(struct wacom *wacom)
1047471d1714SBenjamin Tissoires {
1048ac8d1010SBenjamin Tissoires 	if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1049*297d716fSKrzysztof Kozlowski 	     wacom->battery) {
1050*297d716fSKrzysztof Kozlowski 		power_supply_unregister(wacom->battery);
1051*297d716fSKrzysztof Kozlowski 		wacom->battery = NULL;
1052*297d716fSKrzysztof Kozlowski 		power_supply_unregister(wacom->ac);
1053*297d716fSKrzysztof Kozlowski 		wacom->ac = NULL;
1054471d1714SBenjamin Tissoires 	}
1055471d1714SBenjamin Tissoires }
1056471d1714SBenjamin Tissoires 
1057f81a1295SBenjamin Tissoires static ssize_t wacom_show_speed(struct device *dev,
1058f81a1295SBenjamin Tissoires 				struct device_attribute
1059f81a1295SBenjamin Tissoires 				*attr, char *buf)
1060f81a1295SBenjamin Tissoires {
1061f81a1295SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1062f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1063f81a1295SBenjamin Tissoires 
1064f81a1295SBenjamin Tissoires 	return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1065f81a1295SBenjamin Tissoires }
1066f81a1295SBenjamin Tissoires 
1067f81a1295SBenjamin Tissoires static ssize_t wacom_store_speed(struct device *dev,
1068f81a1295SBenjamin Tissoires 				struct device_attribute *attr,
1069f81a1295SBenjamin Tissoires 				const char *buf, size_t count)
1070f81a1295SBenjamin Tissoires {
1071f81a1295SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1072f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1073f81a1295SBenjamin Tissoires 	u8 new_speed;
1074f81a1295SBenjamin Tissoires 
1075f81a1295SBenjamin Tissoires 	if (kstrtou8(buf, 0, &new_speed))
1076f81a1295SBenjamin Tissoires 		return -EINVAL;
1077f81a1295SBenjamin Tissoires 
1078f81a1295SBenjamin Tissoires 	if (new_speed != 0 && new_speed != 1)
1079f81a1295SBenjamin Tissoires 		return -EINVAL;
1080f81a1295SBenjamin Tissoires 
1081f81a1295SBenjamin Tissoires 	wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1082f81a1295SBenjamin Tissoires 
1083f81a1295SBenjamin Tissoires 	return count;
1084f81a1295SBenjamin Tissoires }
1085f81a1295SBenjamin Tissoires 
1086e0984bc3SPing Cheng static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1087f81a1295SBenjamin Tissoires 		wacom_show_speed, wacom_store_speed);
1088f81a1295SBenjamin Tissoires 
1089471d1714SBenjamin Tissoires static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1090471d1714SBenjamin Tissoires {
1091471d1714SBenjamin Tissoires 	struct input_dev *input_dev;
1092471d1714SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
1093471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1094471d1714SBenjamin Tissoires 
1095471d1714SBenjamin Tissoires 	input_dev = input_allocate_device();
1096471d1714SBenjamin Tissoires 	if (!input_dev)
1097471d1714SBenjamin Tissoires 		return NULL;
1098471d1714SBenjamin Tissoires 
1099471d1714SBenjamin Tissoires 	input_dev->name = wacom_wac->name;
1100471d1714SBenjamin Tissoires 	input_dev->phys = hdev->phys;
1101471d1714SBenjamin Tissoires 	input_dev->dev.parent = &hdev->dev;
1102471d1714SBenjamin Tissoires 	input_dev->open = wacom_open;
1103471d1714SBenjamin Tissoires 	input_dev->close = wacom_close;
1104471d1714SBenjamin Tissoires 	input_dev->uniq = hdev->uniq;
1105471d1714SBenjamin Tissoires 	input_dev->id.bustype = hdev->bus;
1106471d1714SBenjamin Tissoires 	input_dev->id.vendor  = hdev->vendor;
110712969e3bSBenjamin Tissoires 	input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1108471d1714SBenjamin Tissoires 	input_dev->id.version = hdev->version;
1109471d1714SBenjamin Tissoires 	input_set_drvdata(input_dev, wacom);
1110471d1714SBenjamin Tissoires 
1111471d1714SBenjamin Tissoires 	return input_dev;
1112471d1714SBenjamin Tissoires }
1113471d1714SBenjamin Tissoires 
11142546dacdSBenjamin Tissoires static void wacom_free_inputs(struct wacom *wacom)
1115471d1714SBenjamin Tissoires {
11162546dacdSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
11172546dacdSBenjamin Tissoires 
11182546dacdSBenjamin Tissoires 	if (wacom_wac->input)
11192546dacdSBenjamin Tissoires 		input_free_device(wacom_wac->input);
11202546dacdSBenjamin Tissoires 	if (wacom_wac->pad_input)
11212546dacdSBenjamin Tissoires 		input_free_device(wacom_wac->pad_input);
11222546dacdSBenjamin Tissoires 	wacom_wac->input = NULL;
11232546dacdSBenjamin Tissoires 	wacom_wac->pad_input = NULL;
11242546dacdSBenjamin Tissoires }
11252546dacdSBenjamin Tissoires 
11262546dacdSBenjamin Tissoires static int wacom_allocate_inputs(struct wacom *wacom)
11272546dacdSBenjamin Tissoires {
11282546dacdSBenjamin Tissoires 	struct input_dev *input_dev, *pad_input_dev;
11292546dacdSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
11302546dacdSBenjamin Tissoires 
11312546dacdSBenjamin Tissoires 	input_dev = wacom_allocate_input(wacom);
11322546dacdSBenjamin Tissoires 	pad_input_dev = wacom_allocate_input(wacom);
11332546dacdSBenjamin Tissoires 	if (!input_dev || !pad_input_dev) {
11342546dacdSBenjamin Tissoires 		wacom_free_inputs(wacom);
11352546dacdSBenjamin Tissoires 		return -ENOMEM;
11362546dacdSBenjamin Tissoires 	}
11372546dacdSBenjamin Tissoires 
11382546dacdSBenjamin Tissoires 	wacom_wac->input = input_dev;
11392546dacdSBenjamin Tissoires 	wacom_wac->pad_input = pad_input_dev;
11402546dacdSBenjamin Tissoires 	wacom_wac->pad_input->name = wacom_wac->pad_name;
11412546dacdSBenjamin Tissoires 
11422546dacdSBenjamin Tissoires 	return 0;
11432546dacdSBenjamin Tissoires }
11442546dacdSBenjamin Tissoires 
11452546dacdSBenjamin Tissoires static void wacom_clean_inputs(struct wacom *wacom)
11462546dacdSBenjamin Tissoires {
11472546dacdSBenjamin Tissoires 	if (wacom->wacom_wac.input) {
11482546dacdSBenjamin Tissoires 		if (wacom->wacom_wac.input_registered)
1149471d1714SBenjamin Tissoires 			input_unregister_device(wacom->wacom_wac.input);
11502546dacdSBenjamin Tissoires 		else
11512546dacdSBenjamin Tissoires 			input_free_device(wacom->wacom_wac.input);
11522546dacdSBenjamin Tissoires 	}
11532546dacdSBenjamin Tissoires 	if (wacom->wacom_wac.pad_input) {
1154954df6adSPing Cheng 		if (wacom->wacom_wac.pad_registered)
1155471d1714SBenjamin Tissoires 			input_unregister_device(wacom->wacom_wac.pad_input);
11562546dacdSBenjamin Tissoires 		else
11572546dacdSBenjamin Tissoires 			input_free_device(wacom->wacom_wac.pad_input);
11582546dacdSBenjamin Tissoires 	}
1159471d1714SBenjamin Tissoires 	wacom->wacom_wac.input = NULL;
1160471d1714SBenjamin Tissoires 	wacom->wacom_wac.pad_input = NULL;
1161912ca216SPing Cheng 	wacom_destroy_leds(wacom);
1162471d1714SBenjamin Tissoires }
1163471d1714SBenjamin Tissoires 
1164471d1714SBenjamin Tissoires static int wacom_register_inputs(struct wacom *wacom)
1165471d1714SBenjamin Tissoires {
1166471d1714SBenjamin Tissoires 	struct input_dev *input_dev, *pad_input_dev;
1167471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1168471d1714SBenjamin Tissoires 	int error;
1169471d1714SBenjamin Tissoires 
11702546dacdSBenjamin Tissoires 	input_dev = wacom_wac->input;
11712546dacdSBenjamin Tissoires 	pad_input_dev = wacom_wac->pad_input;
1172471d1714SBenjamin Tissoires 
11732546dacdSBenjamin Tissoires 	if (!input_dev || !pad_input_dev)
11742546dacdSBenjamin Tissoires 		return -EINVAL;
1175471d1714SBenjamin Tissoires 
117630ebc1aeSPing Cheng 	error = wacom_setup_pentouch_input_capabilities(input_dev, wacom_wac);
117730ebc1aeSPing Cheng 	if (!error) {
1178471d1714SBenjamin Tissoires 		error = input_register_device(input_dev);
1179471d1714SBenjamin Tissoires 		if (error)
11802546dacdSBenjamin Tissoires 			return error;
1181954df6adSPing Cheng 		wacom_wac->input_registered = true;
118230ebc1aeSPing Cheng 	}
1183471d1714SBenjamin Tissoires 
1184471d1714SBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1185471d1714SBenjamin Tissoires 	if (error) {
1186471d1714SBenjamin Tissoires 		/* no pad in use on this interface */
1187471d1714SBenjamin Tissoires 		input_free_device(pad_input_dev);
1188471d1714SBenjamin Tissoires 		wacom_wac->pad_input = NULL;
1189471d1714SBenjamin Tissoires 		pad_input_dev = NULL;
1190471d1714SBenjamin Tissoires 	} else {
1191471d1714SBenjamin Tissoires 		error = input_register_device(pad_input_dev);
1192471d1714SBenjamin Tissoires 		if (error)
11937fefeec5SBenjamin Tissoires 			goto fail_register_pad_input;
1194954df6adSPing Cheng 		wacom_wac->pad_registered = true;
1195912ca216SPing Cheng 
1196912ca216SPing Cheng 		error = wacom_initialize_leds(wacom);
1197912ca216SPing Cheng 		if (error)
11987fefeec5SBenjamin Tissoires 			goto fail_leds;
1199471d1714SBenjamin Tissoires 	}
1200471d1714SBenjamin Tissoires 
1201471d1714SBenjamin Tissoires 	return 0;
1202471d1714SBenjamin Tissoires 
12037fefeec5SBenjamin Tissoires fail_leds:
1204912ca216SPing Cheng 	input_unregister_device(pad_input_dev);
1205912ca216SPing Cheng 	pad_input_dev = NULL;
1206954df6adSPing Cheng 	wacom_wac->pad_registered = false;
12077fefeec5SBenjamin Tissoires fail_register_pad_input:
1208471d1714SBenjamin Tissoires 	input_unregister_device(input_dev);
1209471d1714SBenjamin Tissoires 	wacom_wac->input = NULL;
1210954df6adSPing Cheng 	wacom_wac->input_registered = false;
1211471d1714SBenjamin Tissoires 	return error;
1212471d1714SBenjamin Tissoires }
1213471d1714SBenjamin Tissoires 
1214471d1714SBenjamin Tissoires static void wacom_wireless_work(struct work_struct *work)
1215471d1714SBenjamin Tissoires {
1216471d1714SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, work);
1217471d1714SBenjamin Tissoires 	struct usb_device *usbdev = wacom->usbdev;
1218471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1219471d1714SBenjamin Tissoires 	struct hid_device *hdev1, *hdev2;
1220471d1714SBenjamin Tissoires 	struct wacom *wacom1, *wacom2;
1221471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac1, *wacom_wac2;
1222471d1714SBenjamin Tissoires 	int error;
1223471d1714SBenjamin Tissoires 
1224471d1714SBenjamin Tissoires 	/*
1225471d1714SBenjamin Tissoires 	 * Regardless if this is a disconnect or a new tablet,
1226471d1714SBenjamin Tissoires 	 * remove any existing input and battery devices.
1227471d1714SBenjamin Tissoires 	 */
1228471d1714SBenjamin Tissoires 
1229471d1714SBenjamin Tissoires 	wacom_destroy_battery(wacom);
1230471d1714SBenjamin Tissoires 
1231471d1714SBenjamin Tissoires 	/* Stylus interface */
1232471d1714SBenjamin Tissoires 	hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1233471d1714SBenjamin Tissoires 	wacom1 = hid_get_drvdata(hdev1);
1234471d1714SBenjamin Tissoires 	wacom_wac1 = &(wacom1->wacom_wac);
12352546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom1);
1236471d1714SBenjamin Tissoires 
1237471d1714SBenjamin Tissoires 	/* Touch interface */
1238471d1714SBenjamin Tissoires 	hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1239471d1714SBenjamin Tissoires 	wacom2 = hid_get_drvdata(hdev2);
1240471d1714SBenjamin Tissoires 	wacom_wac2 = &(wacom2->wacom_wac);
12412546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom2);
1242471d1714SBenjamin Tissoires 
1243471d1714SBenjamin Tissoires 	if (wacom_wac->pid == 0) {
1244471d1714SBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet disconnected\n");
1245ac8d1010SBenjamin Tissoires 		wacom_wac1->shared->type = 0;
1246471d1714SBenjamin Tissoires 	} else {
1247471d1714SBenjamin Tissoires 		const struct hid_device_id *id = wacom_ids;
1248471d1714SBenjamin Tissoires 
1249471d1714SBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1250471d1714SBenjamin Tissoires 			 wacom_wac->pid);
1251471d1714SBenjamin Tissoires 
1252471d1714SBenjamin Tissoires 		while (id->bus) {
1253471d1714SBenjamin Tissoires 			if (id->vendor == USB_VENDOR_ID_WACOM &&
1254471d1714SBenjamin Tissoires 			    id->product == wacom_wac->pid)
1255471d1714SBenjamin Tissoires 				break;
1256471d1714SBenjamin Tissoires 			id++;
1257471d1714SBenjamin Tissoires 		}
1258471d1714SBenjamin Tissoires 
1259471d1714SBenjamin Tissoires 		if (!id->bus) {
1260471d1714SBenjamin Tissoires 			hid_info(wacom->hdev, "ignoring unknown PID.\n");
1261471d1714SBenjamin Tissoires 			return;
1262471d1714SBenjamin Tissoires 		}
1263471d1714SBenjamin Tissoires 
1264471d1714SBenjamin Tissoires 		/* Stylus interface */
1265471d1714SBenjamin Tissoires 		wacom_wac1->features =
1266471d1714SBenjamin Tissoires 			*((struct wacom_features *)id->driver_data);
1267471d1714SBenjamin Tissoires 		wacom_wac1->features.device_type = BTN_TOOL_PEN;
1268471d1714SBenjamin Tissoires 		snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1269471d1714SBenjamin Tissoires 			 wacom_wac1->features.name);
1270471d1714SBenjamin Tissoires 		snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1271471d1714SBenjamin Tissoires 			 wacom_wac1->features.name);
1272471d1714SBenjamin Tissoires 		wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1273471d1714SBenjamin Tissoires 		wacom_wac1->shared->type = wacom_wac1->features.type;
1274912ca216SPing Cheng 		wacom_wac1->pid = wacom_wac->pid;
12752546dacdSBenjamin Tissoires 		error = wacom_allocate_inputs(wacom1) ||
12762546dacdSBenjamin Tissoires 			wacom_register_inputs(wacom1);
1277471d1714SBenjamin Tissoires 		if (error)
1278471d1714SBenjamin Tissoires 			goto fail;
1279471d1714SBenjamin Tissoires 
1280471d1714SBenjamin Tissoires 		/* Touch interface */
1281471d1714SBenjamin Tissoires 		if (wacom_wac1->features.touch_max ||
1282471d1714SBenjamin Tissoires 		    wacom_wac1->features.type == INTUOSHT) {
1283471d1714SBenjamin Tissoires 			wacom_wac2->features =
1284471d1714SBenjamin Tissoires 				*((struct wacom_features *)id->driver_data);
1285471d1714SBenjamin Tissoires 			wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1286471d1714SBenjamin Tissoires 			wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1287471d1714SBenjamin Tissoires 			wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1288471d1714SBenjamin Tissoires 			if (wacom_wac2->features.touch_max)
1289471d1714SBenjamin Tissoires 				snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1290471d1714SBenjamin Tissoires 					 "%s (WL) Finger",wacom_wac2->features.name);
1291471d1714SBenjamin Tissoires 			else
1292471d1714SBenjamin Tissoires 				snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1293471d1714SBenjamin Tissoires 					 "%s (WL) Pad",wacom_wac2->features.name);
1294471d1714SBenjamin Tissoires 			snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1295471d1714SBenjamin Tissoires 				 "%s (WL) Pad", wacom_wac2->features.name);
1296912ca216SPing Cheng 			wacom_wac2->pid = wacom_wac->pid;
12972546dacdSBenjamin Tissoires 			error = wacom_allocate_inputs(wacom2) ||
12982546dacdSBenjamin Tissoires 				wacom_register_inputs(wacom2);
1299471d1714SBenjamin Tissoires 			if (error)
1300471d1714SBenjamin Tissoires 				goto fail;
1301471d1714SBenjamin Tissoires 
1302471d1714SBenjamin Tissoires 			if (wacom_wac1->features.type == INTUOSHT &&
1303471d1714SBenjamin Tissoires 			    wacom_wac1->features.touch_max)
1304471d1714SBenjamin Tissoires 				wacom_wac->shared->touch_input = wacom_wac2->input;
1305471d1714SBenjamin Tissoires 		}
1306471d1714SBenjamin Tissoires 
1307471d1714SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1308471d1714SBenjamin Tissoires 		if (error)
1309471d1714SBenjamin Tissoires 			goto fail;
1310471d1714SBenjamin Tissoires 	}
1311471d1714SBenjamin Tissoires 
1312471d1714SBenjamin Tissoires 	return;
1313471d1714SBenjamin Tissoires 
1314471d1714SBenjamin Tissoires fail:
13152546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom1);
13162546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom2);
1317471d1714SBenjamin Tissoires 	return;
1318471d1714SBenjamin Tissoires }
1319471d1714SBenjamin Tissoires 
1320471d1714SBenjamin Tissoires /*
1321471d1714SBenjamin Tissoires  * Not all devices report physical dimensions from HID.
1322471d1714SBenjamin Tissoires  * Compute the default from hardcoded logical dimension
1323471d1714SBenjamin Tissoires  * and resolution before driver overwrites them.
1324471d1714SBenjamin Tissoires  */
1325471d1714SBenjamin Tissoires static void wacom_set_default_phy(struct wacom_features *features)
1326471d1714SBenjamin Tissoires {
1327471d1714SBenjamin Tissoires 	if (features->x_resolution) {
1328471d1714SBenjamin Tissoires 		features->x_phy = (features->x_max * 100) /
1329471d1714SBenjamin Tissoires 					features->x_resolution;
1330471d1714SBenjamin Tissoires 		features->y_phy = (features->y_max * 100) /
1331471d1714SBenjamin Tissoires 					features->y_resolution;
1332471d1714SBenjamin Tissoires 	}
1333471d1714SBenjamin Tissoires }
1334471d1714SBenjamin Tissoires 
1335471d1714SBenjamin Tissoires static void wacom_calculate_res(struct wacom_features *features)
1336471d1714SBenjamin Tissoires {
1337471d1714SBenjamin Tissoires 	features->x_resolution = wacom_calc_hid_res(features->x_max,
1338471d1714SBenjamin Tissoires 						    features->x_phy,
1339471d1714SBenjamin Tissoires 						    features->unit,
1340471d1714SBenjamin Tissoires 						    features->unitExpo);
1341471d1714SBenjamin Tissoires 	features->y_resolution = wacom_calc_hid_res(features->y_max,
1342471d1714SBenjamin Tissoires 						    features->y_phy,
1343471d1714SBenjamin Tissoires 						    features->unit,
1344471d1714SBenjamin Tissoires 						    features->unitExpo);
1345471d1714SBenjamin Tissoires }
1346471d1714SBenjamin Tissoires 
1347471d1714SBenjamin Tissoires static size_t wacom_compute_pktlen(struct hid_device *hdev)
1348471d1714SBenjamin Tissoires {
1349471d1714SBenjamin Tissoires 	struct hid_report_enum *report_enum;
1350471d1714SBenjamin Tissoires 	struct hid_report *report;
1351471d1714SBenjamin Tissoires 	size_t size = 0;
1352471d1714SBenjamin Tissoires 
1353471d1714SBenjamin Tissoires 	report_enum = hdev->report_enum + HID_INPUT_REPORT;
1354471d1714SBenjamin Tissoires 
1355471d1714SBenjamin Tissoires 	list_for_each_entry(report, &report_enum->report_list, list) {
1356dabb05c6SMathieu Magnaudet 		size_t report_size = hid_report_len(report);
1357471d1714SBenjamin Tissoires 		if (report_size > size)
1358471d1714SBenjamin Tissoires 			size = report_size;
1359471d1714SBenjamin Tissoires 	}
1360471d1714SBenjamin Tissoires 
1361471d1714SBenjamin Tissoires 	return size;
1362471d1714SBenjamin Tissoires }
1363471d1714SBenjamin Tissoires 
1364471d1714SBenjamin Tissoires static int wacom_probe(struct hid_device *hdev,
1365471d1714SBenjamin Tissoires 		const struct hid_device_id *id)
1366471d1714SBenjamin Tissoires {
1367471d1714SBenjamin Tissoires 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1368471d1714SBenjamin Tissoires 	struct usb_device *dev = interface_to_usbdev(intf);
1369471d1714SBenjamin Tissoires 	struct wacom *wacom;
1370471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac;
1371471d1714SBenjamin Tissoires 	struct wacom_features *features;
1372471d1714SBenjamin Tissoires 	int error;
13737704ac93SBenjamin Tissoires 	unsigned int connect_mask = HID_CONNECT_HIDRAW;
1374471d1714SBenjamin Tissoires 
1375471d1714SBenjamin Tissoires 	if (!id->driver_data)
1376471d1714SBenjamin Tissoires 		return -EINVAL;
1377471d1714SBenjamin Tissoires 
13788ffffd52SBenjamin Tissoires 	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
13798ffffd52SBenjamin Tissoires 
1380471d1714SBenjamin Tissoires 	wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1381471d1714SBenjamin Tissoires 	if (!wacom)
1382471d1714SBenjamin Tissoires 		return -ENOMEM;
1383471d1714SBenjamin Tissoires 
1384471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, wacom);
1385471d1714SBenjamin Tissoires 	wacom->hdev = hdev;
1386471d1714SBenjamin Tissoires 
1387471d1714SBenjamin Tissoires 	/* ask for the report descriptor to be loaded by HID */
1388471d1714SBenjamin Tissoires 	error = hid_parse(hdev);
1389471d1714SBenjamin Tissoires 	if (error) {
1390471d1714SBenjamin Tissoires 		hid_err(hdev, "parse failed\n");
13917fefeec5SBenjamin Tissoires 		goto fail_parse;
1392471d1714SBenjamin Tissoires 	}
1393471d1714SBenjamin Tissoires 
1394471d1714SBenjamin Tissoires 	wacom_wac = &wacom->wacom_wac;
1395471d1714SBenjamin Tissoires 	wacom_wac->features = *((struct wacom_features *)id->driver_data);
1396471d1714SBenjamin Tissoires 	features = &wacom_wac->features;
1397471d1714SBenjamin Tissoires 	features->pktlen = wacom_compute_pktlen(hdev);
1398471d1714SBenjamin Tissoires 	if (features->pktlen > WACOM_PKGLEN_MAX) {
1399471d1714SBenjamin Tissoires 		error = -EINVAL;
14007fefeec5SBenjamin Tissoires 		goto fail_pktlen;
1401471d1714SBenjamin Tissoires 	}
1402471d1714SBenjamin Tissoires 
1403471d1714SBenjamin Tissoires 	if (features->check_for_hid_type && features->hid_type != hdev->type) {
1404471d1714SBenjamin Tissoires 		error = -ENODEV;
14057fefeec5SBenjamin Tissoires 		goto fail_type;
1406471d1714SBenjamin Tissoires 	}
1407471d1714SBenjamin Tissoires 
1408471d1714SBenjamin Tissoires 	wacom->usbdev = dev;
1409471d1714SBenjamin Tissoires 	wacom->intf = intf;
1410471d1714SBenjamin Tissoires 	mutex_init(&wacom->lock);
1411471d1714SBenjamin Tissoires 	INIT_WORK(&wacom->work, wacom_wireless_work);
1412471d1714SBenjamin Tissoires 
1413494078b0SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1414494078b0SBenjamin Tissoires 		error = wacom_allocate_inputs(wacom);
1415494078b0SBenjamin Tissoires 		if (error)
1416494078b0SBenjamin Tissoires 			goto fail_allocate_inputs;
1417494078b0SBenjamin Tissoires 	}
1418494078b0SBenjamin Tissoires 
1419471d1714SBenjamin Tissoires 	/* set the default size in case we do not get them from hid */
1420471d1714SBenjamin Tissoires 	wacom_set_default_phy(features);
1421471d1714SBenjamin Tissoires 
1422471d1714SBenjamin Tissoires 	/* Retrieve the physical and logical size for touch devices */
1423471d1714SBenjamin Tissoires 	wacom_retrieve_hid_descriptor(hdev, features);
1424471d1714SBenjamin Tissoires 
1425471d1714SBenjamin Tissoires 	/*
1426471d1714SBenjamin Tissoires 	 * Intuos5 has no useful data about its touch interface in its
1427471d1714SBenjamin Tissoires 	 * HID descriptor. If this is the touch interface (PacketSize
1428471d1714SBenjamin Tissoires 	 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1429471d1714SBenjamin Tissoires 	 */
1430471d1714SBenjamin Tissoires 	if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
1431471d1714SBenjamin Tissoires 		if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
1432471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_FINGER;
1433471d1714SBenjamin Tissoires 
1434471d1714SBenjamin Tissoires 			features->x_max = 4096;
1435471d1714SBenjamin Tissoires 			features->y_max = 4096;
1436471d1714SBenjamin Tissoires 		} else {
1437471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_PEN;
1438471d1714SBenjamin Tissoires 		}
1439471d1714SBenjamin Tissoires 	}
1440471d1714SBenjamin Tissoires 
1441471d1714SBenjamin Tissoires 	/*
1442471d1714SBenjamin Tissoires 	 * Same thing for Bamboo 3rd gen.
1443471d1714SBenjamin Tissoires 	 */
1444471d1714SBenjamin Tissoires 	if ((features->type == BAMBOO_PT) &&
1445471d1714SBenjamin Tissoires 	    (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1446471d1714SBenjamin Tissoires 	    (features->device_type == BTN_TOOL_PEN)) {
1447471d1714SBenjamin Tissoires 		features->device_type = BTN_TOOL_FINGER;
1448471d1714SBenjamin Tissoires 
1449471d1714SBenjamin Tissoires 		features->x_max = 4096;
1450471d1714SBenjamin Tissoires 		features->y_max = 4096;
1451471d1714SBenjamin Tissoires 	}
1452471d1714SBenjamin Tissoires 
1453f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
1454f81a1295SBenjamin Tissoires 		features->quirks |= WACOM_QUIRK_BATTERY;
1455f81a1295SBenjamin Tissoires 
1456471d1714SBenjamin Tissoires 	wacom_setup_device_quirks(features);
1457471d1714SBenjamin Tissoires 
1458471d1714SBenjamin Tissoires 	/* set unit to "100th of a mm" for devices not reported by HID */
1459471d1714SBenjamin Tissoires 	if (!features->unit) {
1460471d1714SBenjamin Tissoires 		features->unit = 0x11;
1461471d1714SBenjamin Tissoires 		features->unitExpo = -3;
1462471d1714SBenjamin Tissoires 	}
1463471d1714SBenjamin Tissoires 	wacom_calculate_res(features);
1464471d1714SBenjamin Tissoires 
1465471d1714SBenjamin Tissoires 	strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1466471d1714SBenjamin Tissoires 	snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1467471d1714SBenjamin Tissoires 		"%s Pad", features->name);
1468471d1714SBenjamin Tissoires 
1469471d1714SBenjamin Tissoires 	if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
1470471d1714SBenjamin Tissoires 		/* Append the device type to the name */
1471471d1714SBenjamin Tissoires 		if (features->device_type != BTN_TOOL_FINGER)
1472471d1714SBenjamin Tissoires 			strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1473471d1714SBenjamin Tissoires 		else if (features->touch_max)
1474471d1714SBenjamin Tissoires 			strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1475471d1714SBenjamin Tissoires 		else
1476471d1714SBenjamin Tissoires 			strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
1477471d1714SBenjamin Tissoires 
1478471d1714SBenjamin Tissoires 		error = wacom_add_shared_data(hdev);
1479471d1714SBenjamin Tissoires 		if (error)
14807fefeec5SBenjamin Tissoires 			goto fail_shared_data;
1481471d1714SBenjamin Tissoires 	}
1482471d1714SBenjamin Tissoires 
1483f81a1295SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1484f81a1295SBenjamin Tissoires 	     (features->quirks & WACOM_QUIRK_BATTERY)) {
1485f81a1295SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1486f81a1295SBenjamin Tissoires 		if (error)
14877fefeec5SBenjamin Tissoires 			goto fail_battery;
1488f81a1295SBenjamin Tissoires 	}
1489f81a1295SBenjamin Tissoires 
1490471d1714SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1491494078b0SBenjamin Tissoires 		error = wacom_register_inputs(wacom);
1492471d1714SBenjamin Tissoires 		if (error)
14937fefeec5SBenjamin Tissoires 			goto fail_register_inputs;
1494f81a1295SBenjamin Tissoires 	}
1495f81a1295SBenjamin Tissoires 
1496f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
1497f81a1295SBenjamin Tissoires 		error = device_create_file(&hdev->dev, &dev_attr_speed);
1498f81a1295SBenjamin Tissoires 		if (error)
1499f81a1295SBenjamin Tissoires 			hid_warn(hdev,
1500f81a1295SBenjamin Tissoires 				 "can't create sysfs speed attribute err: %d\n",
1501f81a1295SBenjamin Tissoires 				 error);
1502471d1714SBenjamin Tissoires 	}
1503471d1714SBenjamin Tissoires 
15047704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
15057704ac93SBenjamin Tissoires 		connect_mask |= HID_CONNECT_DRIVER;
15067704ac93SBenjamin Tissoires 
1507471d1714SBenjamin Tissoires 	/* Regular HID work starts now */
15087704ac93SBenjamin Tissoires 	error = hid_hw_start(hdev, connect_mask);
1509471d1714SBenjamin Tissoires 	if (error) {
1510471d1714SBenjamin Tissoires 		hid_err(hdev, "hw start failed\n");
15117fefeec5SBenjamin Tissoires 		goto fail_hw_start;
1512471d1714SBenjamin Tissoires 	}
1513471d1714SBenjamin Tissoires 
15145ae6e89fSBenjamin Tissoires 	/* Note that if query fails it is not a hard failure */
15155ae6e89fSBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
15165ae6e89fSBenjamin Tissoires 
1517471d1714SBenjamin Tissoires 	if (features->quirks & WACOM_QUIRK_MONITOR)
1518471d1714SBenjamin Tissoires 		error = hid_hw_open(hdev);
1519471d1714SBenjamin Tissoires 
1520471d1714SBenjamin Tissoires 	if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1521471d1714SBenjamin Tissoires 		if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1522471d1714SBenjamin Tissoires 			wacom_wac->shared->touch_input = wacom_wac->input;
1523471d1714SBenjamin Tissoires 	}
1524471d1714SBenjamin Tissoires 
1525471d1714SBenjamin Tissoires 	return 0;
1526471d1714SBenjamin Tissoires 
15277fefeec5SBenjamin Tissoires fail_hw_start:
15287fefeec5SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
15297fefeec5SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
15307fefeec5SBenjamin Tissoires fail_register_inputs:
15312546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom);
15327fefeec5SBenjamin Tissoires 	wacom_destroy_battery(wacom);
15337fefeec5SBenjamin Tissoires fail_battery:
15347fefeec5SBenjamin Tissoires 	wacom_remove_shared_data(wacom_wac);
15357fefeec5SBenjamin Tissoires fail_shared_data:
1536494078b0SBenjamin Tissoires 	wacom_clean_inputs(wacom);
1537494078b0SBenjamin Tissoires fail_allocate_inputs:
15387fefeec5SBenjamin Tissoires fail_type:
15397fefeec5SBenjamin Tissoires fail_pktlen:
15407fefeec5SBenjamin Tissoires fail_parse:
15417fefeec5SBenjamin Tissoires 	kfree(wacom);
1542471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
1543471d1714SBenjamin Tissoires 	return error;
1544471d1714SBenjamin Tissoires }
1545471d1714SBenjamin Tissoires 
1546471d1714SBenjamin Tissoires static void wacom_remove(struct hid_device *hdev)
1547471d1714SBenjamin Tissoires {
1548471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1549471d1714SBenjamin Tissoires 
1550471d1714SBenjamin Tissoires 	hid_hw_stop(hdev);
1551471d1714SBenjamin Tissoires 
1552471d1714SBenjamin Tissoires 	cancel_work_sync(&wacom->work);
15532546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom);
1554f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
1555f81a1295SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
1556471d1714SBenjamin Tissoires 	wacom_destroy_battery(wacom);
1557471d1714SBenjamin Tissoires 	wacom_remove_shared_data(&wacom->wacom_wac);
1558471d1714SBenjamin Tissoires 
1559471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
1560471d1714SBenjamin Tissoires 	kfree(wacom);
1561471d1714SBenjamin Tissoires }
1562471d1714SBenjamin Tissoires 
156341a74581SGeert Uytterhoeven #ifdef CONFIG_PM
1564471d1714SBenjamin Tissoires static int wacom_resume(struct hid_device *hdev)
1565471d1714SBenjamin Tissoires {
1566471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1567471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
1568471d1714SBenjamin Tissoires 
1569471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
1570471d1714SBenjamin Tissoires 
1571471d1714SBenjamin Tissoires 	/* switch to wacom mode first */
1572471d1714SBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
1573471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
1574471d1714SBenjamin Tissoires 
1575471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
1576471d1714SBenjamin Tissoires 
1577471d1714SBenjamin Tissoires 	return 0;
1578471d1714SBenjamin Tissoires }
1579471d1714SBenjamin Tissoires 
1580471d1714SBenjamin Tissoires static int wacom_reset_resume(struct hid_device *hdev)
1581471d1714SBenjamin Tissoires {
1582471d1714SBenjamin Tissoires 	return wacom_resume(hdev);
1583471d1714SBenjamin Tissoires }
158441a74581SGeert Uytterhoeven #endif /* CONFIG_PM */
1585471d1714SBenjamin Tissoires 
1586471d1714SBenjamin Tissoires static struct hid_driver wacom_driver = {
1587471d1714SBenjamin Tissoires 	.name =		"wacom",
1588471d1714SBenjamin Tissoires 	.id_table =	wacom_ids,
1589471d1714SBenjamin Tissoires 	.probe =	wacom_probe,
1590471d1714SBenjamin Tissoires 	.remove =	wacom_remove,
15917704ac93SBenjamin Tissoires 	.event =	wacom_wac_event,
15927704ac93SBenjamin Tissoires 	.report =	wacom_wac_report,
1593471d1714SBenjamin Tissoires #ifdef CONFIG_PM
1594471d1714SBenjamin Tissoires 	.resume =	wacom_resume,
1595471d1714SBenjamin Tissoires 	.reset_resume =	wacom_reset_resume,
1596471d1714SBenjamin Tissoires #endif
1597471d1714SBenjamin Tissoires 	.raw_event =	wacom_raw_event,
1598471d1714SBenjamin Tissoires };
1599471d1714SBenjamin Tissoires module_hid_driver(wacom_driver);
1600f2e0a7d4SBenjamin Tissoires 
1601f2e0a7d4SBenjamin Tissoires MODULE_VERSION(DRIVER_VERSION);
1602f2e0a7d4SBenjamin Tissoires MODULE_AUTHOR(DRIVER_AUTHOR);
1603f2e0a7d4SBenjamin Tissoires MODULE_DESCRIPTION(DRIVER_DESC);
1604f2e0a7d4SBenjamin Tissoires MODULE_LICENSE(DRIVER_LICENSE);
1605