xref: /openbmc/linux/drivers/hid/wacom_sys.c (revision d97a552210320d3bec8ee22b8ccdb1d6d189482a)
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;
176*d97a5522SBenjamin Tissoires 	bool finger = WACOM_FINGER_FIELD(field);
177*d97a5522SBenjamin 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 		}
406471d1714SBenjamin Tissoires 	} else if (features->device_type == BTN_TOOL_PEN) {
407471d1714SBenjamin Tissoires 		if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
408471d1714SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 2, 2, 2);
409471d1714SBenjamin Tissoires 		}
410471d1714SBenjamin Tissoires 	}
411471d1714SBenjamin Tissoires 
412471d1714SBenjamin Tissoires 	return 0;
413471d1714SBenjamin Tissoires }
414471d1714SBenjamin Tissoires 
415471d1714SBenjamin Tissoires static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
416471d1714SBenjamin Tissoires 					 struct wacom_features *features)
417471d1714SBenjamin Tissoires {
418471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
419471d1714SBenjamin Tissoires 	struct usb_interface *intf = wacom->intf;
420471d1714SBenjamin Tissoires 
421471d1714SBenjamin Tissoires 	/* default features */
422471d1714SBenjamin Tissoires 	features->device_type = BTN_TOOL_PEN;
423471d1714SBenjamin Tissoires 	features->x_fuzz = 4;
424471d1714SBenjamin Tissoires 	features->y_fuzz = 4;
425471d1714SBenjamin Tissoires 	features->pressure_fuzz = 0;
426471d1714SBenjamin Tissoires 	features->distance_fuzz = 0;
427471d1714SBenjamin Tissoires 
428471d1714SBenjamin Tissoires 	/*
429471d1714SBenjamin Tissoires 	 * The wireless device HID is basic and layout conflicts with
430471d1714SBenjamin Tissoires 	 * other tablets (monitor and touch interface can look like pen).
431471d1714SBenjamin Tissoires 	 * Skip the query for this type and modify defaults based on
432471d1714SBenjamin Tissoires 	 * interface number.
433471d1714SBenjamin Tissoires 	 */
434471d1714SBenjamin Tissoires 	if (features->type == WIRELESS) {
435471d1714SBenjamin Tissoires 		if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
436471d1714SBenjamin Tissoires 			features->device_type = 0;
437471d1714SBenjamin Tissoires 		} else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
438471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_FINGER;
439471d1714SBenjamin Tissoires 			features->pktlen = WACOM_PKGLEN_BBTOUCH3;
440471d1714SBenjamin Tissoires 		}
441471d1714SBenjamin Tissoires 	}
442471d1714SBenjamin Tissoires 
443471d1714SBenjamin Tissoires 	/* only devices that support touch need to retrieve the info */
444471d1714SBenjamin Tissoires 	if (features->type < BAMBOO_PT)
445471d1714SBenjamin Tissoires 		return;
446471d1714SBenjamin Tissoires 
447471d1714SBenjamin Tissoires 	wacom_parse_hid(hdev, features);
448471d1714SBenjamin Tissoires }
449471d1714SBenjamin Tissoires 
450471d1714SBenjamin Tissoires struct wacom_hdev_data {
451471d1714SBenjamin Tissoires 	struct list_head list;
452471d1714SBenjamin Tissoires 	struct kref kref;
453471d1714SBenjamin Tissoires 	struct hid_device *dev;
454471d1714SBenjamin Tissoires 	struct wacom_shared shared;
455471d1714SBenjamin Tissoires };
456471d1714SBenjamin Tissoires 
457471d1714SBenjamin Tissoires static LIST_HEAD(wacom_udev_list);
458471d1714SBenjamin Tissoires static DEFINE_MUTEX(wacom_udev_list_lock);
459471d1714SBenjamin Tissoires 
460471d1714SBenjamin Tissoires static bool wacom_are_sibling(struct hid_device *hdev,
461471d1714SBenjamin Tissoires 		struct hid_device *sibling)
462471d1714SBenjamin Tissoires {
463471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
464471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
465471d1714SBenjamin Tissoires 	int vid = features->oVid;
466471d1714SBenjamin Tissoires 	int pid = features->oPid;
467471d1714SBenjamin Tissoires 	int n1,n2;
468471d1714SBenjamin Tissoires 
469471d1714SBenjamin Tissoires 	if (vid == 0 && pid == 0) {
470471d1714SBenjamin Tissoires 		vid = hdev->vendor;
471471d1714SBenjamin Tissoires 		pid = hdev->product;
472471d1714SBenjamin Tissoires 	}
473471d1714SBenjamin Tissoires 
474471d1714SBenjamin Tissoires 	if (vid != sibling->vendor || pid != sibling->product)
475471d1714SBenjamin Tissoires 		return false;
476471d1714SBenjamin Tissoires 
477471d1714SBenjamin Tissoires 	/* Compare the physical path. */
478471d1714SBenjamin Tissoires 	n1 = strrchr(hdev->phys, '.') - hdev->phys;
479471d1714SBenjamin Tissoires 	n2 = strrchr(sibling->phys, '.') - sibling->phys;
480471d1714SBenjamin Tissoires 	if (n1 != n2 || n1 <= 0 || n2 <= 0)
481471d1714SBenjamin Tissoires 		return false;
482471d1714SBenjamin Tissoires 
483471d1714SBenjamin Tissoires 	return !strncmp(hdev->phys, sibling->phys, n1);
484471d1714SBenjamin Tissoires }
485471d1714SBenjamin Tissoires 
486471d1714SBenjamin Tissoires static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
487471d1714SBenjamin Tissoires {
488471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
489471d1714SBenjamin Tissoires 
490471d1714SBenjamin Tissoires 	list_for_each_entry(data, &wacom_udev_list, list) {
491471d1714SBenjamin Tissoires 		if (wacom_are_sibling(hdev, data->dev)) {
492471d1714SBenjamin Tissoires 			kref_get(&data->kref);
493471d1714SBenjamin Tissoires 			return data;
494471d1714SBenjamin Tissoires 		}
495471d1714SBenjamin Tissoires 	}
496471d1714SBenjamin Tissoires 
497471d1714SBenjamin Tissoires 	return NULL;
498471d1714SBenjamin Tissoires }
499471d1714SBenjamin Tissoires 
500471d1714SBenjamin Tissoires static int wacom_add_shared_data(struct hid_device *hdev)
501471d1714SBenjamin Tissoires {
502471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
503471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
504471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
505471d1714SBenjamin Tissoires 	int retval = 0;
506471d1714SBenjamin Tissoires 
507471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
508471d1714SBenjamin Tissoires 
509471d1714SBenjamin Tissoires 	data = wacom_get_hdev_data(hdev);
510471d1714SBenjamin Tissoires 	if (!data) {
511471d1714SBenjamin Tissoires 		data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
512471d1714SBenjamin Tissoires 		if (!data) {
513471d1714SBenjamin Tissoires 			retval = -ENOMEM;
514471d1714SBenjamin Tissoires 			goto out;
515471d1714SBenjamin Tissoires 		}
516471d1714SBenjamin Tissoires 
517471d1714SBenjamin Tissoires 		kref_init(&data->kref);
518471d1714SBenjamin Tissoires 		data->dev = hdev;
519471d1714SBenjamin Tissoires 		list_add_tail(&data->list, &wacom_udev_list);
520471d1714SBenjamin Tissoires 	}
521471d1714SBenjamin Tissoires 
522471d1714SBenjamin Tissoires 	wacom_wac->shared = &data->shared;
523471d1714SBenjamin Tissoires 
524471d1714SBenjamin Tissoires out:
525471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
526471d1714SBenjamin Tissoires 	return retval;
527471d1714SBenjamin Tissoires }
528471d1714SBenjamin Tissoires 
529471d1714SBenjamin Tissoires static void wacom_release_shared_data(struct kref *kref)
530471d1714SBenjamin Tissoires {
531471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data =
532471d1714SBenjamin Tissoires 		container_of(kref, struct wacom_hdev_data, kref);
533471d1714SBenjamin Tissoires 
534471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
535471d1714SBenjamin Tissoires 	list_del(&data->list);
536471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
537471d1714SBenjamin Tissoires 
538471d1714SBenjamin Tissoires 	kfree(data);
539471d1714SBenjamin Tissoires }
540471d1714SBenjamin Tissoires 
541471d1714SBenjamin Tissoires static void wacom_remove_shared_data(struct wacom_wac *wacom)
542471d1714SBenjamin Tissoires {
543471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
544471d1714SBenjamin Tissoires 
545471d1714SBenjamin Tissoires 	if (wacom->shared) {
546471d1714SBenjamin Tissoires 		data = container_of(wacom->shared, struct wacom_hdev_data, shared);
547471d1714SBenjamin Tissoires 		kref_put(&data->kref, wacom_release_shared_data);
548471d1714SBenjamin Tissoires 		wacom->shared = NULL;
549471d1714SBenjamin Tissoires 	}
550471d1714SBenjamin Tissoires }
551471d1714SBenjamin Tissoires 
552471d1714SBenjamin Tissoires static int wacom_led_control(struct wacom *wacom)
553471d1714SBenjamin Tissoires {
554471d1714SBenjamin Tissoires 	unsigned char *buf;
555471d1714SBenjamin Tissoires 	int retval;
556912ca216SPing Cheng 	unsigned char report_id = WAC_CMD_LED_CONTROL;
557912ca216SPing Cheng 	int buf_size = 9;
558471d1714SBenjamin Tissoires 
559912ca216SPing Cheng 	if (wacom->wacom_wac.pid) { /* wireless connected */
560912ca216SPing Cheng 		report_id = WAC_CMD_WL_LED_CONTROL;
561912ca216SPing Cheng 		buf_size = 13;
562912ca216SPing Cheng 	}
563912ca216SPing Cheng 	buf = kzalloc(buf_size, GFP_KERNEL);
564471d1714SBenjamin Tissoires 	if (!buf)
565471d1714SBenjamin Tissoires 		return -ENOMEM;
566471d1714SBenjamin Tissoires 
567471d1714SBenjamin Tissoires 	if (wacom->wacom_wac.features.type >= INTUOS5S &&
568471d1714SBenjamin Tissoires 	    wacom->wacom_wac.features.type <= INTUOSPL) {
569471d1714SBenjamin Tissoires 		/*
570471d1714SBenjamin Tissoires 		 * Touch Ring and crop mark LED luminance may take on
571471d1714SBenjamin Tissoires 		 * one of four values:
572471d1714SBenjamin Tissoires 		 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
573471d1714SBenjamin Tissoires 		 */
574471d1714SBenjamin Tissoires 		int ring_led = wacom->led.select[0] & 0x03;
575471d1714SBenjamin Tissoires 		int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
576471d1714SBenjamin Tissoires 		int crop_lum = 0;
577912ca216SPing Cheng 		unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
578471d1714SBenjamin Tissoires 
579912ca216SPing Cheng 		buf[0] = report_id;
580912ca216SPing Cheng 		if (wacom->wacom_wac.pid) {
581912ca216SPing Cheng 			wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
582912ca216SPing Cheng 					 buf, buf_size, WAC_CMD_RETRIES);
583912ca216SPing Cheng 			buf[0] = report_id;
584912ca216SPing Cheng 			buf[4] = led_bits;
585912ca216SPing Cheng 		} else
586912ca216SPing Cheng 			buf[1] = led_bits;
587471d1714SBenjamin Tissoires 	}
588471d1714SBenjamin Tissoires 	else {
589471d1714SBenjamin Tissoires 		int led = wacom->led.select[0] | 0x4;
590471d1714SBenjamin Tissoires 
591471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
592471d1714SBenjamin Tissoires 		    wacom->wacom_wac.features.type == WACOM_24HD)
593471d1714SBenjamin Tissoires 			led |= (wacom->led.select[1] << 4) | 0x40;
594471d1714SBenjamin Tissoires 
595912ca216SPing Cheng 		buf[0] = report_id;
596471d1714SBenjamin Tissoires 		buf[1] = led;
597471d1714SBenjamin Tissoires 		buf[2] = wacom->led.llv;
598471d1714SBenjamin Tissoires 		buf[3] = wacom->led.hlv;
599471d1714SBenjamin Tissoires 		buf[4] = wacom->led.img_lum;
600471d1714SBenjamin Tissoires 	}
601471d1714SBenjamin Tissoires 
602912ca216SPing Cheng 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
603296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
604471d1714SBenjamin Tissoires 	kfree(buf);
605471d1714SBenjamin Tissoires 
606471d1714SBenjamin Tissoires 	return retval;
607471d1714SBenjamin Tissoires }
608471d1714SBenjamin Tissoires 
609849e2f06SBenjamin Tissoires static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
610849e2f06SBenjamin Tissoires 		const unsigned len, const void *img)
611471d1714SBenjamin Tissoires {
612471d1714SBenjamin Tissoires 	unsigned char *buf;
613471d1714SBenjamin Tissoires 	int i, retval;
614849e2f06SBenjamin Tissoires 	const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
615471d1714SBenjamin Tissoires 
616849e2f06SBenjamin Tissoires 	buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
617471d1714SBenjamin Tissoires 	if (!buf)
618471d1714SBenjamin Tissoires 		return -ENOMEM;
619471d1714SBenjamin Tissoires 
620471d1714SBenjamin Tissoires 	/* Send 'start' command */
621471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
622471d1714SBenjamin Tissoires 	buf[1] = 1;
623296b7378SPrzemo Firszt 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
624296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
625471d1714SBenjamin Tissoires 	if (retval < 0)
626471d1714SBenjamin Tissoires 		goto out;
627471d1714SBenjamin Tissoires 
628849e2f06SBenjamin Tissoires 	buf[0] = xfer_id;
629471d1714SBenjamin Tissoires 	buf[1] = button_id & 0x07;
630471d1714SBenjamin Tissoires 	for (i = 0; i < 4; i++) {
631471d1714SBenjamin Tissoires 		buf[2] = i;
632849e2f06SBenjamin Tissoires 		memcpy(buf + 3, img + i * chunk_len, chunk_len);
633471d1714SBenjamin Tissoires 
634471d1714SBenjamin Tissoires 		retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
635296b7378SPrzemo Firszt 					  buf, chunk_len + 3, WAC_CMD_RETRIES);
636471d1714SBenjamin Tissoires 		if (retval < 0)
637471d1714SBenjamin Tissoires 			break;
638471d1714SBenjamin Tissoires 	}
639471d1714SBenjamin Tissoires 
640471d1714SBenjamin Tissoires 	/* Send 'stop' */
641471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
642471d1714SBenjamin Tissoires 	buf[1] = 0;
643296b7378SPrzemo Firszt 	wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
644296b7378SPrzemo Firszt 			 WAC_CMD_RETRIES);
645471d1714SBenjamin Tissoires 
646471d1714SBenjamin Tissoires out:
647471d1714SBenjamin Tissoires 	kfree(buf);
648471d1714SBenjamin Tissoires 	return retval;
649471d1714SBenjamin Tissoires }
650471d1714SBenjamin Tissoires 
651471d1714SBenjamin Tissoires static ssize_t wacom_led_select_store(struct device *dev, int set_id,
652471d1714SBenjamin Tissoires 				      const char *buf, size_t count)
653471d1714SBenjamin Tissoires {
654471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
655471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
656471d1714SBenjamin Tissoires 	unsigned int id;
657471d1714SBenjamin Tissoires 	int err;
658471d1714SBenjamin Tissoires 
659471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &id);
660471d1714SBenjamin Tissoires 	if (err)
661471d1714SBenjamin Tissoires 		return err;
662471d1714SBenjamin Tissoires 
663471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
664471d1714SBenjamin Tissoires 
665471d1714SBenjamin Tissoires 	wacom->led.select[set_id] = id & 0x3;
666471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
667471d1714SBenjamin Tissoires 
668471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
669471d1714SBenjamin Tissoires 
670471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
671471d1714SBenjamin Tissoires }
672471d1714SBenjamin Tissoires 
673471d1714SBenjamin Tissoires #define DEVICE_LED_SELECT_ATTR(SET_ID)					\
674471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
675471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
676471d1714SBenjamin Tissoires {									\
677471d1714SBenjamin Tissoires 	return wacom_led_select_store(dev, SET_ID, buf, count);		\
678471d1714SBenjamin Tissoires }									\
679471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
680471d1714SBenjamin Tissoires 	struct device_attribute *attr, char *buf)			\
681471d1714SBenjamin Tissoires {									\
682471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
683471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
68437449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n",			\
68537449adcSPing Cheng 			 wacom->led.select[SET_ID]);			\
686471d1714SBenjamin Tissoires }									\
687e0984bc3SPing Cheng static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,	\
688471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_show,			\
689471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_store)
690471d1714SBenjamin Tissoires 
691471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(0);
692471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(1);
693471d1714SBenjamin Tissoires 
694471d1714SBenjamin Tissoires static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
695471d1714SBenjamin Tissoires 				     const char *buf, size_t count)
696471d1714SBenjamin Tissoires {
697471d1714SBenjamin Tissoires 	unsigned int value;
698471d1714SBenjamin Tissoires 	int err;
699471d1714SBenjamin Tissoires 
700471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &value);
701471d1714SBenjamin Tissoires 	if (err)
702471d1714SBenjamin Tissoires 		return err;
703471d1714SBenjamin Tissoires 
704471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
705471d1714SBenjamin Tissoires 
706471d1714SBenjamin Tissoires 	*dest = value & 0x7f;
707471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
708471d1714SBenjamin Tissoires 
709471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
710471d1714SBenjamin Tissoires 
711471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
712471d1714SBenjamin Tissoires }
713471d1714SBenjamin Tissoires 
714471d1714SBenjamin Tissoires #define DEVICE_LUMINANCE_ATTR(name, field)				\
715471d1714SBenjamin Tissoires static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
716471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
717471d1714SBenjamin Tissoires {									\
718471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
719471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
720471d1714SBenjamin Tissoires 									\
721471d1714SBenjamin Tissoires 	return wacom_luminance_store(wacom, &wacom->led.field,		\
722471d1714SBenjamin Tissoires 				     buf, count);			\
723471d1714SBenjamin Tissoires }									\
72437449adcSPing Cheng static ssize_t wacom_##name##_luminance_show(struct device *dev,	\
72537449adcSPing Cheng 	struct device_attribute *attr, char *buf)			\
72637449adcSPing Cheng {									\
72737449adcSPing Cheng 	struct wacom *wacom = dev_get_drvdata(dev);			\
72837449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);	\
72937449adcSPing Cheng }									\
730e0984bc3SPing Cheng static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,			\
73137449adcSPing Cheng 		   wacom_##name##_luminance_show,			\
73237449adcSPing Cheng 		   wacom_##name##_luminance_store)
733471d1714SBenjamin Tissoires 
734471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status0, llv);
735471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status1, hlv);
736471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(buttons, img_lum);
737471d1714SBenjamin Tissoires 
738471d1714SBenjamin Tissoires static ssize_t wacom_button_image_store(struct device *dev, int button_id,
739471d1714SBenjamin Tissoires 					const char *buf, size_t count)
740471d1714SBenjamin Tissoires {
741471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
742471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
743471d1714SBenjamin Tissoires 	int err;
744849e2f06SBenjamin Tissoires 	unsigned len;
745849e2f06SBenjamin Tissoires 	u8 xfer_id;
746471d1714SBenjamin Tissoires 
747849e2f06SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
748849e2f06SBenjamin Tissoires 		len = 256;
749849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_BT_XFER;
750849e2f06SBenjamin Tissoires 	} else {
751849e2f06SBenjamin Tissoires 		len = 1024;
752849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_XFER;
753849e2f06SBenjamin Tissoires 	}
754849e2f06SBenjamin Tissoires 
755849e2f06SBenjamin Tissoires 	if (count != len)
756471d1714SBenjamin Tissoires 		return -EINVAL;
757471d1714SBenjamin Tissoires 
758471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
759471d1714SBenjamin Tissoires 
760849e2f06SBenjamin Tissoires 	err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
761471d1714SBenjamin Tissoires 
762471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
763471d1714SBenjamin Tissoires 
764471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
765471d1714SBenjamin Tissoires }
766471d1714SBenjamin Tissoires 
767471d1714SBenjamin Tissoires #define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
768471d1714SBenjamin Tissoires static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
769471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
770471d1714SBenjamin Tissoires {									\
771471d1714SBenjamin Tissoires 	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
772471d1714SBenjamin Tissoires }									\
773e0984bc3SPing Cheng static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,	\
774471d1714SBenjamin Tissoires 		   NULL, wacom_btnimg##BUTTON_ID##_store)
775471d1714SBenjamin Tissoires 
776471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(0);
777471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(1);
778471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(2);
779471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(3);
780471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(4);
781471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(5);
782471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(6);
783471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(7);
784471d1714SBenjamin Tissoires 
785471d1714SBenjamin Tissoires static struct attribute *cintiq_led_attrs[] = {
786471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
787471d1714SBenjamin Tissoires 	&dev_attr_status_led1_select.attr,
788471d1714SBenjamin Tissoires 	NULL
789471d1714SBenjamin Tissoires };
790471d1714SBenjamin Tissoires 
791471d1714SBenjamin Tissoires static struct attribute_group cintiq_led_attr_group = {
792471d1714SBenjamin Tissoires 	.name = "wacom_led",
793471d1714SBenjamin Tissoires 	.attrs = cintiq_led_attrs,
794471d1714SBenjamin Tissoires };
795471d1714SBenjamin Tissoires 
796471d1714SBenjamin Tissoires static struct attribute *intuos4_led_attrs[] = {
797471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
798471d1714SBenjamin Tissoires 	&dev_attr_status1_luminance.attr,
799471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
800471d1714SBenjamin Tissoires 	&dev_attr_buttons_luminance.attr,
801471d1714SBenjamin Tissoires 	&dev_attr_button0_rawimg.attr,
802471d1714SBenjamin Tissoires 	&dev_attr_button1_rawimg.attr,
803471d1714SBenjamin Tissoires 	&dev_attr_button2_rawimg.attr,
804471d1714SBenjamin Tissoires 	&dev_attr_button3_rawimg.attr,
805471d1714SBenjamin Tissoires 	&dev_attr_button4_rawimg.attr,
806471d1714SBenjamin Tissoires 	&dev_attr_button5_rawimg.attr,
807471d1714SBenjamin Tissoires 	&dev_attr_button6_rawimg.attr,
808471d1714SBenjamin Tissoires 	&dev_attr_button7_rawimg.attr,
809471d1714SBenjamin Tissoires 	NULL
810471d1714SBenjamin Tissoires };
811471d1714SBenjamin Tissoires 
812471d1714SBenjamin Tissoires static struct attribute_group intuos4_led_attr_group = {
813471d1714SBenjamin Tissoires 	.name = "wacom_led",
814471d1714SBenjamin Tissoires 	.attrs = intuos4_led_attrs,
815471d1714SBenjamin Tissoires };
816471d1714SBenjamin Tissoires 
817471d1714SBenjamin Tissoires static struct attribute *intuos5_led_attrs[] = {
818471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
819471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
820471d1714SBenjamin Tissoires 	NULL
821471d1714SBenjamin Tissoires };
822471d1714SBenjamin Tissoires 
823471d1714SBenjamin Tissoires static struct attribute_group intuos5_led_attr_group = {
824471d1714SBenjamin Tissoires 	.name = "wacom_led",
825471d1714SBenjamin Tissoires 	.attrs = intuos5_led_attrs,
826471d1714SBenjamin Tissoires };
827471d1714SBenjamin Tissoires 
828471d1714SBenjamin Tissoires static int wacom_initialize_leds(struct wacom *wacom)
829471d1714SBenjamin Tissoires {
830471d1714SBenjamin Tissoires 	int error;
831471d1714SBenjamin Tissoires 
832471d1714SBenjamin Tissoires 	/* Initialize default values */
833471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
834471d1714SBenjamin Tissoires 	case INTUOS4S:
835471d1714SBenjamin Tissoires 	case INTUOS4:
83681af7e61SBenjamin Tissoires 	case INTUOS4WL:
837471d1714SBenjamin Tissoires 	case INTUOS4L:
838471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
839471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
840471d1714SBenjamin Tissoires 		wacom->led.llv = 10;
841471d1714SBenjamin Tissoires 		wacom->led.hlv = 20;
842471d1714SBenjamin Tissoires 		wacom->led.img_lum = 10;
843471d1714SBenjamin Tissoires 		error = sysfs_create_group(&wacom->hdev->dev.kobj,
844471d1714SBenjamin Tissoires 					   &intuos4_led_attr_group);
845471d1714SBenjamin Tissoires 		break;
846471d1714SBenjamin Tissoires 
847471d1714SBenjamin Tissoires 	case WACOM_24HD:
848471d1714SBenjamin Tissoires 	case WACOM_21UX2:
849471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
850471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
851471d1714SBenjamin Tissoires 		wacom->led.llv = 0;
852471d1714SBenjamin Tissoires 		wacom->led.hlv = 0;
853471d1714SBenjamin Tissoires 		wacom->led.img_lum = 0;
854471d1714SBenjamin Tissoires 
855471d1714SBenjamin Tissoires 		error = sysfs_create_group(&wacom->hdev->dev.kobj,
856471d1714SBenjamin Tissoires 					   &cintiq_led_attr_group);
857471d1714SBenjamin Tissoires 		break;
858471d1714SBenjamin Tissoires 
859471d1714SBenjamin Tissoires 	case INTUOS5S:
860471d1714SBenjamin Tissoires 	case INTUOS5:
861471d1714SBenjamin Tissoires 	case INTUOS5L:
862471d1714SBenjamin Tissoires 	case INTUOSPS:
863471d1714SBenjamin Tissoires 	case INTUOSPM:
864471d1714SBenjamin Tissoires 	case INTUOSPL:
865471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
866471d1714SBenjamin Tissoires 			wacom->led.select[0] = 0;
867471d1714SBenjamin Tissoires 			wacom->led.select[1] = 0;
868471d1714SBenjamin Tissoires 			wacom->led.llv = 32;
869471d1714SBenjamin Tissoires 			wacom->led.hlv = 0;
870471d1714SBenjamin Tissoires 			wacom->led.img_lum = 0;
871471d1714SBenjamin Tissoires 
872471d1714SBenjamin Tissoires 			error = sysfs_create_group(&wacom->hdev->dev.kobj,
873471d1714SBenjamin Tissoires 						  &intuos5_led_attr_group);
874471d1714SBenjamin Tissoires 		} else
875471d1714SBenjamin Tissoires 			return 0;
876471d1714SBenjamin Tissoires 		break;
877471d1714SBenjamin Tissoires 
878471d1714SBenjamin Tissoires 	default:
879471d1714SBenjamin Tissoires 		return 0;
880471d1714SBenjamin Tissoires 	}
881471d1714SBenjamin Tissoires 
882471d1714SBenjamin Tissoires 	if (error) {
883471d1714SBenjamin Tissoires 		hid_err(wacom->hdev,
884471d1714SBenjamin Tissoires 			"cannot create sysfs group err: %d\n", error);
885471d1714SBenjamin Tissoires 		return error;
886471d1714SBenjamin Tissoires 	}
887471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
888c757cbafSBenjamin Tissoires 	wacom->led_initialized = true;
889471d1714SBenjamin Tissoires 
890471d1714SBenjamin Tissoires 	return 0;
891471d1714SBenjamin Tissoires }
892471d1714SBenjamin Tissoires 
893471d1714SBenjamin Tissoires static void wacom_destroy_leds(struct wacom *wacom)
894471d1714SBenjamin Tissoires {
895c757cbafSBenjamin Tissoires 	if (!wacom->led_initialized)
896c757cbafSBenjamin Tissoires 		return;
897c757cbafSBenjamin Tissoires 
898c757cbafSBenjamin Tissoires 	wacom->led_initialized = false;
899c757cbafSBenjamin Tissoires 
900471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
901471d1714SBenjamin Tissoires 	case INTUOS4S:
902471d1714SBenjamin Tissoires 	case INTUOS4:
90381af7e61SBenjamin Tissoires 	case INTUOS4WL:
904471d1714SBenjamin Tissoires 	case INTUOS4L:
905471d1714SBenjamin Tissoires 		sysfs_remove_group(&wacom->hdev->dev.kobj,
906471d1714SBenjamin Tissoires 				   &intuos4_led_attr_group);
907471d1714SBenjamin Tissoires 		break;
908471d1714SBenjamin Tissoires 
909471d1714SBenjamin Tissoires 	case WACOM_24HD:
910471d1714SBenjamin Tissoires 	case WACOM_21UX2:
911471d1714SBenjamin Tissoires 		sysfs_remove_group(&wacom->hdev->dev.kobj,
912471d1714SBenjamin Tissoires 				   &cintiq_led_attr_group);
913471d1714SBenjamin Tissoires 		break;
914471d1714SBenjamin Tissoires 
915471d1714SBenjamin Tissoires 	case INTUOS5S:
916471d1714SBenjamin Tissoires 	case INTUOS5:
917471d1714SBenjamin Tissoires 	case INTUOS5L:
918471d1714SBenjamin Tissoires 	case INTUOSPS:
919471d1714SBenjamin Tissoires 	case INTUOSPM:
920471d1714SBenjamin Tissoires 	case INTUOSPL:
921471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
922471d1714SBenjamin Tissoires 			sysfs_remove_group(&wacom->hdev->dev.kobj,
923471d1714SBenjamin Tissoires 					   &intuos5_led_attr_group);
924471d1714SBenjamin Tissoires 		break;
925471d1714SBenjamin Tissoires 	}
926471d1714SBenjamin Tissoires }
927471d1714SBenjamin Tissoires 
928471d1714SBenjamin Tissoires static enum power_supply_property wacom_battery_props[] = {
929ac8d1010SBenjamin Tissoires 	POWER_SUPPLY_PROP_STATUS,
930471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
931471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_CAPACITY
932471d1714SBenjamin Tissoires };
933471d1714SBenjamin Tissoires 
9347dbd229eSBenjamin Tissoires static enum power_supply_property wacom_ac_props[] = {
9357dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_PRESENT,
9367dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_ONLINE,
9377dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
9387dbd229eSBenjamin Tissoires };
9397dbd229eSBenjamin Tissoires 
940471d1714SBenjamin Tissoires static int wacom_battery_get_property(struct power_supply *psy,
941471d1714SBenjamin Tissoires 				      enum power_supply_property psp,
942471d1714SBenjamin Tissoires 				      union power_supply_propval *val)
943471d1714SBenjamin Tissoires {
944471d1714SBenjamin Tissoires 	struct wacom *wacom = container_of(psy, struct wacom, battery);
945471d1714SBenjamin Tissoires 	int ret = 0;
946471d1714SBenjamin Tissoires 
947471d1714SBenjamin Tissoires 	switch (psp) {
948471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_SCOPE:
949471d1714SBenjamin Tissoires 			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
950471d1714SBenjamin Tissoires 			break;
951471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_CAPACITY:
952471d1714SBenjamin Tissoires 			val->intval =
953ac8d1010SBenjamin Tissoires 				wacom->wacom_wac.battery_capacity;
954ac8d1010SBenjamin Tissoires 			break;
955ac8d1010SBenjamin Tissoires 		case POWER_SUPPLY_PROP_STATUS:
956ac8d1010SBenjamin Tissoires 			if (wacom->wacom_wac.bat_charging)
957ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_CHARGING;
958ac8d1010SBenjamin Tissoires 			else if (wacom->wacom_wac.battery_capacity == 100 &&
959ac8d1010SBenjamin Tissoires 				    wacom->wacom_wac.ps_connected)
960ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_FULL;
961ac8d1010SBenjamin Tissoires 			else
962ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
963471d1714SBenjamin Tissoires 			break;
964471d1714SBenjamin Tissoires 		default:
965471d1714SBenjamin Tissoires 			ret = -EINVAL;
966471d1714SBenjamin Tissoires 			break;
967471d1714SBenjamin Tissoires 	}
968471d1714SBenjamin Tissoires 
969471d1714SBenjamin Tissoires 	return ret;
970471d1714SBenjamin Tissoires }
971471d1714SBenjamin Tissoires 
9727dbd229eSBenjamin Tissoires static int wacom_ac_get_property(struct power_supply *psy,
9737dbd229eSBenjamin Tissoires 				enum power_supply_property psp,
9747dbd229eSBenjamin Tissoires 				union power_supply_propval *val)
9757dbd229eSBenjamin Tissoires {
9767dbd229eSBenjamin Tissoires 	struct wacom *wacom = container_of(psy, struct wacom, ac);
9777dbd229eSBenjamin Tissoires 	int ret = 0;
9787dbd229eSBenjamin Tissoires 
9797dbd229eSBenjamin Tissoires 	switch (psp) {
9807dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_PRESENT:
9817dbd229eSBenjamin Tissoires 		/* fall through */
9827dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_ONLINE:
9837dbd229eSBenjamin Tissoires 		val->intval = wacom->wacom_wac.ps_connected;
9847dbd229eSBenjamin Tissoires 		break;
9857dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_SCOPE:
9867dbd229eSBenjamin Tissoires 		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
9877dbd229eSBenjamin Tissoires 		break;
9887dbd229eSBenjamin Tissoires 	default:
9897dbd229eSBenjamin Tissoires 		ret = -EINVAL;
9907dbd229eSBenjamin Tissoires 		break;
9917dbd229eSBenjamin Tissoires 	}
9927dbd229eSBenjamin Tissoires 	return ret;
9937dbd229eSBenjamin Tissoires }
9947dbd229eSBenjamin Tissoires 
995471d1714SBenjamin Tissoires static int wacom_initialize_battery(struct wacom *wacom)
996471d1714SBenjamin Tissoires {
997d70420b9SBenjamin Tissoires 	static atomic_t battery_no = ATOMIC_INIT(0);
9987dbd229eSBenjamin Tissoires 	int error;
999d70420b9SBenjamin Tissoires 	unsigned long n;
1000471d1714SBenjamin Tissoires 
1001ac8d1010SBenjamin Tissoires 	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
1002d70420b9SBenjamin Tissoires 		n = atomic_inc_return(&battery_no) - 1;
10037dbd229eSBenjamin Tissoires 
1004471d1714SBenjamin Tissoires 		wacom->battery.properties = wacom_battery_props;
1005471d1714SBenjamin Tissoires 		wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
1006471d1714SBenjamin Tissoires 		wacom->battery.get_property = wacom_battery_get_property;
1007d70420b9SBenjamin Tissoires 		sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
1008d70420b9SBenjamin Tissoires 		wacom->battery.name = wacom->wacom_wac.bat_name;
1009471d1714SBenjamin Tissoires 		wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1010471d1714SBenjamin Tissoires 		wacom->battery.use_for_apm = 0;
1011471d1714SBenjamin Tissoires 
10127dbd229eSBenjamin Tissoires 		wacom->ac.properties = wacom_ac_props;
10137dbd229eSBenjamin Tissoires 		wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
10147dbd229eSBenjamin Tissoires 		wacom->ac.get_property = wacom_ac_get_property;
10157dbd229eSBenjamin Tissoires 		sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
10167dbd229eSBenjamin Tissoires 		wacom->ac.name = wacom->wacom_wac.ac_name;
10177dbd229eSBenjamin Tissoires 		wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
10187dbd229eSBenjamin Tissoires 		wacom->ac.use_for_apm = 0;
10197dbd229eSBenjamin Tissoires 
1020471d1714SBenjamin Tissoires 		error = power_supply_register(&wacom->hdev->dev,
1021471d1714SBenjamin Tissoires 					      &wacom->battery);
10227dbd229eSBenjamin Tissoires 		if (error)
10237dbd229eSBenjamin Tissoires 			return error;
1024471d1714SBenjamin Tissoires 
10257dbd229eSBenjamin Tissoires 		power_supply_powers(&wacom->battery, &wacom->hdev->dev);
10267dbd229eSBenjamin Tissoires 
10277dbd229eSBenjamin Tissoires 		error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
10287dbd229eSBenjamin Tissoires 		if (error) {
10297dbd229eSBenjamin Tissoires 			power_supply_unregister(&wacom->battery);
10307dbd229eSBenjamin Tissoires 			return error;
1031471d1714SBenjamin Tissoires 		}
1032471d1714SBenjamin Tissoires 
10337dbd229eSBenjamin Tissoires 		power_supply_powers(&wacom->ac, &wacom->hdev->dev);
10347dbd229eSBenjamin Tissoires 	}
10357dbd229eSBenjamin Tissoires 
10367dbd229eSBenjamin Tissoires 	return 0;
1037471d1714SBenjamin Tissoires }
1038471d1714SBenjamin Tissoires 
1039471d1714SBenjamin Tissoires static void wacom_destroy_battery(struct wacom *wacom)
1040471d1714SBenjamin Tissoires {
1041ac8d1010SBenjamin Tissoires 	if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1042471d1714SBenjamin Tissoires 	     wacom->battery.dev) {
1043471d1714SBenjamin Tissoires 		power_supply_unregister(&wacom->battery);
1044471d1714SBenjamin Tissoires 		wacom->battery.dev = NULL;
10457dbd229eSBenjamin Tissoires 		power_supply_unregister(&wacom->ac);
10467dbd229eSBenjamin Tissoires 		wacom->ac.dev = NULL;
1047471d1714SBenjamin Tissoires 	}
1048471d1714SBenjamin Tissoires }
1049471d1714SBenjamin Tissoires 
1050f81a1295SBenjamin Tissoires static ssize_t wacom_show_speed(struct device *dev,
1051f81a1295SBenjamin Tissoires 				struct device_attribute
1052f81a1295SBenjamin Tissoires 				*attr, char *buf)
1053f81a1295SBenjamin Tissoires {
1054f81a1295SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1055f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1056f81a1295SBenjamin Tissoires 
1057f81a1295SBenjamin Tissoires 	return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1058f81a1295SBenjamin Tissoires }
1059f81a1295SBenjamin Tissoires 
1060f81a1295SBenjamin Tissoires static ssize_t wacom_store_speed(struct device *dev,
1061f81a1295SBenjamin Tissoires 				struct device_attribute *attr,
1062f81a1295SBenjamin Tissoires 				const char *buf, size_t count)
1063f81a1295SBenjamin Tissoires {
1064f81a1295SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1065f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1066f81a1295SBenjamin Tissoires 	u8 new_speed;
1067f81a1295SBenjamin Tissoires 
1068f81a1295SBenjamin Tissoires 	if (kstrtou8(buf, 0, &new_speed))
1069f81a1295SBenjamin Tissoires 		return -EINVAL;
1070f81a1295SBenjamin Tissoires 
1071f81a1295SBenjamin Tissoires 	if (new_speed != 0 && new_speed != 1)
1072f81a1295SBenjamin Tissoires 		return -EINVAL;
1073f81a1295SBenjamin Tissoires 
1074f81a1295SBenjamin Tissoires 	wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1075f81a1295SBenjamin Tissoires 
1076f81a1295SBenjamin Tissoires 	return count;
1077f81a1295SBenjamin Tissoires }
1078f81a1295SBenjamin Tissoires 
1079e0984bc3SPing Cheng static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1080f81a1295SBenjamin Tissoires 		wacom_show_speed, wacom_store_speed);
1081f81a1295SBenjamin Tissoires 
1082471d1714SBenjamin Tissoires static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1083471d1714SBenjamin Tissoires {
1084471d1714SBenjamin Tissoires 	struct input_dev *input_dev;
1085471d1714SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
1086471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1087471d1714SBenjamin Tissoires 
1088471d1714SBenjamin Tissoires 	input_dev = input_allocate_device();
1089471d1714SBenjamin Tissoires 	if (!input_dev)
1090471d1714SBenjamin Tissoires 		return NULL;
1091471d1714SBenjamin Tissoires 
1092471d1714SBenjamin Tissoires 	input_dev->name = wacom_wac->name;
1093471d1714SBenjamin Tissoires 	input_dev->phys = hdev->phys;
1094471d1714SBenjamin Tissoires 	input_dev->dev.parent = &hdev->dev;
1095471d1714SBenjamin Tissoires 	input_dev->open = wacom_open;
1096471d1714SBenjamin Tissoires 	input_dev->close = wacom_close;
1097471d1714SBenjamin Tissoires 	input_dev->uniq = hdev->uniq;
1098471d1714SBenjamin Tissoires 	input_dev->id.bustype = hdev->bus;
1099471d1714SBenjamin Tissoires 	input_dev->id.vendor  = hdev->vendor;
110012969e3bSBenjamin Tissoires 	input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1101471d1714SBenjamin Tissoires 	input_dev->id.version = hdev->version;
1102471d1714SBenjamin Tissoires 	input_set_drvdata(input_dev, wacom);
1103471d1714SBenjamin Tissoires 
1104471d1714SBenjamin Tissoires 	return input_dev;
1105471d1714SBenjamin Tissoires }
1106471d1714SBenjamin Tissoires 
11072546dacdSBenjamin Tissoires static void wacom_free_inputs(struct wacom *wacom)
1108471d1714SBenjamin Tissoires {
11092546dacdSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
11102546dacdSBenjamin Tissoires 
11112546dacdSBenjamin Tissoires 	if (wacom_wac->input)
11122546dacdSBenjamin Tissoires 		input_free_device(wacom_wac->input);
11132546dacdSBenjamin Tissoires 	if (wacom_wac->pad_input)
11142546dacdSBenjamin Tissoires 		input_free_device(wacom_wac->pad_input);
11152546dacdSBenjamin Tissoires 	wacom_wac->input = NULL;
11162546dacdSBenjamin Tissoires 	wacom_wac->pad_input = NULL;
11172546dacdSBenjamin Tissoires }
11182546dacdSBenjamin Tissoires 
11192546dacdSBenjamin Tissoires static int wacom_allocate_inputs(struct wacom *wacom)
11202546dacdSBenjamin Tissoires {
11212546dacdSBenjamin Tissoires 	struct input_dev *input_dev, *pad_input_dev;
11222546dacdSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
11232546dacdSBenjamin Tissoires 
11242546dacdSBenjamin Tissoires 	input_dev = wacom_allocate_input(wacom);
11252546dacdSBenjamin Tissoires 	pad_input_dev = wacom_allocate_input(wacom);
11262546dacdSBenjamin Tissoires 	if (!input_dev || !pad_input_dev) {
11272546dacdSBenjamin Tissoires 		wacom_free_inputs(wacom);
11282546dacdSBenjamin Tissoires 		return -ENOMEM;
11292546dacdSBenjamin Tissoires 	}
11302546dacdSBenjamin Tissoires 
11312546dacdSBenjamin Tissoires 	wacom_wac->input = input_dev;
11322546dacdSBenjamin Tissoires 	wacom_wac->pad_input = pad_input_dev;
11332546dacdSBenjamin Tissoires 	wacom_wac->pad_input->name = wacom_wac->pad_name;
11342546dacdSBenjamin Tissoires 
11352546dacdSBenjamin Tissoires 	return 0;
11362546dacdSBenjamin Tissoires }
11372546dacdSBenjamin Tissoires 
11382546dacdSBenjamin Tissoires static void wacom_clean_inputs(struct wacom *wacom)
11392546dacdSBenjamin Tissoires {
11402546dacdSBenjamin Tissoires 	if (wacom->wacom_wac.input) {
11412546dacdSBenjamin Tissoires 		if (wacom->wacom_wac.input_registered)
1142471d1714SBenjamin Tissoires 			input_unregister_device(wacom->wacom_wac.input);
11432546dacdSBenjamin Tissoires 		else
11442546dacdSBenjamin Tissoires 			input_free_device(wacom->wacom_wac.input);
11452546dacdSBenjamin Tissoires 	}
11462546dacdSBenjamin Tissoires 	if (wacom->wacom_wac.pad_input) {
1147954df6adSPing Cheng 		if (wacom->wacom_wac.pad_registered)
1148471d1714SBenjamin Tissoires 			input_unregister_device(wacom->wacom_wac.pad_input);
11492546dacdSBenjamin Tissoires 		else
11502546dacdSBenjamin Tissoires 			input_free_device(wacom->wacom_wac.pad_input);
11512546dacdSBenjamin Tissoires 	}
1152471d1714SBenjamin Tissoires 	wacom->wacom_wac.input = NULL;
1153471d1714SBenjamin Tissoires 	wacom->wacom_wac.pad_input = NULL;
1154912ca216SPing Cheng 	wacom_destroy_leds(wacom);
1155471d1714SBenjamin Tissoires }
1156471d1714SBenjamin Tissoires 
1157471d1714SBenjamin Tissoires static int wacom_register_inputs(struct wacom *wacom)
1158471d1714SBenjamin Tissoires {
1159471d1714SBenjamin Tissoires 	struct input_dev *input_dev, *pad_input_dev;
1160471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1161471d1714SBenjamin Tissoires 	int error;
1162471d1714SBenjamin Tissoires 
11632546dacdSBenjamin Tissoires 	input_dev = wacom_wac->input;
11642546dacdSBenjamin Tissoires 	pad_input_dev = wacom_wac->pad_input;
1165471d1714SBenjamin Tissoires 
11662546dacdSBenjamin Tissoires 	if (!input_dev || !pad_input_dev)
11672546dacdSBenjamin Tissoires 		return -EINVAL;
1168471d1714SBenjamin Tissoires 
116930ebc1aeSPing Cheng 	error = wacom_setup_pentouch_input_capabilities(input_dev, wacom_wac);
117030ebc1aeSPing Cheng 	if (!error) {
1171471d1714SBenjamin Tissoires 		error = input_register_device(input_dev);
1172471d1714SBenjamin Tissoires 		if (error)
11732546dacdSBenjamin Tissoires 			return error;
1174954df6adSPing Cheng 		wacom_wac->input_registered = true;
117530ebc1aeSPing Cheng 	}
1176471d1714SBenjamin Tissoires 
1177471d1714SBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1178471d1714SBenjamin Tissoires 	if (error) {
1179471d1714SBenjamin Tissoires 		/* no pad in use on this interface */
1180471d1714SBenjamin Tissoires 		input_free_device(pad_input_dev);
1181471d1714SBenjamin Tissoires 		wacom_wac->pad_input = NULL;
1182471d1714SBenjamin Tissoires 		pad_input_dev = NULL;
1183471d1714SBenjamin Tissoires 	} else {
1184471d1714SBenjamin Tissoires 		error = input_register_device(pad_input_dev);
1185471d1714SBenjamin Tissoires 		if (error)
11867fefeec5SBenjamin Tissoires 			goto fail_register_pad_input;
1187954df6adSPing Cheng 		wacom_wac->pad_registered = true;
1188912ca216SPing Cheng 
1189912ca216SPing Cheng 		error = wacom_initialize_leds(wacom);
1190912ca216SPing Cheng 		if (error)
11917fefeec5SBenjamin Tissoires 			goto fail_leds;
1192471d1714SBenjamin Tissoires 	}
1193471d1714SBenjamin Tissoires 
1194471d1714SBenjamin Tissoires 	return 0;
1195471d1714SBenjamin Tissoires 
11967fefeec5SBenjamin Tissoires fail_leds:
1197912ca216SPing Cheng 	input_unregister_device(pad_input_dev);
1198912ca216SPing Cheng 	pad_input_dev = NULL;
1199954df6adSPing Cheng 	wacom_wac->pad_registered = false;
12007fefeec5SBenjamin Tissoires fail_register_pad_input:
1201471d1714SBenjamin Tissoires 	input_unregister_device(input_dev);
1202471d1714SBenjamin Tissoires 	wacom_wac->input = NULL;
1203954df6adSPing Cheng 	wacom_wac->input_registered = false;
1204471d1714SBenjamin Tissoires 	return error;
1205471d1714SBenjamin Tissoires }
1206471d1714SBenjamin Tissoires 
1207471d1714SBenjamin Tissoires static void wacom_wireless_work(struct work_struct *work)
1208471d1714SBenjamin Tissoires {
1209471d1714SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, work);
1210471d1714SBenjamin Tissoires 	struct usb_device *usbdev = wacom->usbdev;
1211471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1212471d1714SBenjamin Tissoires 	struct hid_device *hdev1, *hdev2;
1213471d1714SBenjamin Tissoires 	struct wacom *wacom1, *wacom2;
1214471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac1, *wacom_wac2;
1215471d1714SBenjamin Tissoires 	int error;
1216471d1714SBenjamin Tissoires 
1217471d1714SBenjamin Tissoires 	/*
1218471d1714SBenjamin Tissoires 	 * Regardless if this is a disconnect or a new tablet,
1219471d1714SBenjamin Tissoires 	 * remove any existing input and battery devices.
1220471d1714SBenjamin Tissoires 	 */
1221471d1714SBenjamin Tissoires 
1222471d1714SBenjamin Tissoires 	wacom_destroy_battery(wacom);
1223471d1714SBenjamin Tissoires 
1224471d1714SBenjamin Tissoires 	/* Stylus interface */
1225471d1714SBenjamin Tissoires 	hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1226471d1714SBenjamin Tissoires 	wacom1 = hid_get_drvdata(hdev1);
1227471d1714SBenjamin Tissoires 	wacom_wac1 = &(wacom1->wacom_wac);
12282546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom1);
1229471d1714SBenjamin Tissoires 
1230471d1714SBenjamin Tissoires 	/* Touch interface */
1231471d1714SBenjamin Tissoires 	hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1232471d1714SBenjamin Tissoires 	wacom2 = hid_get_drvdata(hdev2);
1233471d1714SBenjamin Tissoires 	wacom_wac2 = &(wacom2->wacom_wac);
12342546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom2);
1235471d1714SBenjamin Tissoires 
1236471d1714SBenjamin Tissoires 	if (wacom_wac->pid == 0) {
1237471d1714SBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet disconnected\n");
1238ac8d1010SBenjamin Tissoires 		wacom_wac1->shared->type = 0;
1239471d1714SBenjamin Tissoires 	} else {
1240471d1714SBenjamin Tissoires 		const struct hid_device_id *id = wacom_ids;
1241471d1714SBenjamin Tissoires 
1242471d1714SBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1243471d1714SBenjamin Tissoires 			 wacom_wac->pid);
1244471d1714SBenjamin Tissoires 
1245471d1714SBenjamin Tissoires 		while (id->bus) {
1246471d1714SBenjamin Tissoires 			if (id->vendor == USB_VENDOR_ID_WACOM &&
1247471d1714SBenjamin Tissoires 			    id->product == wacom_wac->pid)
1248471d1714SBenjamin Tissoires 				break;
1249471d1714SBenjamin Tissoires 			id++;
1250471d1714SBenjamin Tissoires 		}
1251471d1714SBenjamin Tissoires 
1252471d1714SBenjamin Tissoires 		if (!id->bus) {
1253471d1714SBenjamin Tissoires 			hid_info(wacom->hdev, "ignoring unknown PID.\n");
1254471d1714SBenjamin Tissoires 			return;
1255471d1714SBenjamin Tissoires 		}
1256471d1714SBenjamin Tissoires 
1257471d1714SBenjamin Tissoires 		/* Stylus interface */
1258471d1714SBenjamin Tissoires 		wacom_wac1->features =
1259471d1714SBenjamin Tissoires 			*((struct wacom_features *)id->driver_data);
1260471d1714SBenjamin Tissoires 		wacom_wac1->features.device_type = BTN_TOOL_PEN;
1261471d1714SBenjamin Tissoires 		snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1262471d1714SBenjamin Tissoires 			 wacom_wac1->features.name);
1263471d1714SBenjamin Tissoires 		snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1264471d1714SBenjamin Tissoires 			 wacom_wac1->features.name);
1265471d1714SBenjamin Tissoires 		wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1266471d1714SBenjamin Tissoires 		wacom_wac1->shared->type = wacom_wac1->features.type;
1267912ca216SPing Cheng 		wacom_wac1->pid = wacom_wac->pid;
12682546dacdSBenjamin Tissoires 		error = wacom_allocate_inputs(wacom1) ||
12692546dacdSBenjamin Tissoires 			wacom_register_inputs(wacom1);
1270471d1714SBenjamin Tissoires 		if (error)
1271471d1714SBenjamin Tissoires 			goto fail;
1272471d1714SBenjamin Tissoires 
1273471d1714SBenjamin Tissoires 		/* Touch interface */
1274471d1714SBenjamin Tissoires 		if (wacom_wac1->features.touch_max ||
1275471d1714SBenjamin Tissoires 		    wacom_wac1->features.type == INTUOSHT) {
1276471d1714SBenjamin Tissoires 			wacom_wac2->features =
1277471d1714SBenjamin Tissoires 				*((struct wacom_features *)id->driver_data);
1278471d1714SBenjamin Tissoires 			wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1279471d1714SBenjamin Tissoires 			wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1280471d1714SBenjamin Tissoires 			wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1281471d1714SBenjamin Tissoires 			if (wacom_wac2->features.touch_max)
1282471d1714SBenjamin Tissoires 				snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1283471d1714SBenjamin Tissoires 					 "%s (WL) Finger",wacom_wac2->features.name);
1284471d1714SBenjamin Tissoires 			else
1285471d1714SBenjamin Tissoires 				snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1286471d1714SBenjamin Tissoires 					 "%s (WL) Pad",wacom_wac2->features.name);
1287471d1714SBenjamin Tissoires 			snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1288471d1714SBenjamin Tissoires 				 "%s (WL) Pad", wacom_wac2->features.name);
1289912ca216SPing Cheng 			wacom_wac2->pid = wacom_wac->pid;
12902546dacdSBenjamin Tissoires 			error = wacom_allocate_inputs(wacom2) ||
12912546dacdSBenjamin Tissoires 				wacom_register_inputs(wacom2);
1292471d1714SBenjamin Tissoires 			if (error)
1293471d1714SBenjamin Tissoires 				goto fail;
1294471d1714SBenjamin Tissoires 
1295471d1714SBenjamin Tissoires 			if (wacom_wac1->features.type == INTUOSHT &&
1296471d1714SBenjamin Tissoires 			    wacom_wac1->features.touch_max)
1297471d1714SBenjamin Tissoires 				wacom_wac->shared->touch_input = wacom_wac2->input;
1298471d1714SBenjamin Tissoires 		}
1299471d1714SBenjamin Tissoires 
1300471d1714SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1301471d1714SBenjamin Tissoires 		if (error)
1302471d1714SBenjamin Tissoires 			goto fail;
1303471d1714SBenjamin Tissoires 	}
1304471d1714SBenjamin Tissoires 
1305471d1714SBenjamin Tissoires 	return;
1306471d1714SBenjamin Tissoires 
1307471d1714SBenjamin Tissoires fail:
13082546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom1);
13092546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom2);
1310471d1714SBenjamin Tissoires 	return;
1311471d1714SBenjamin Tissoires }
1312471d1714SBenjamin Tissoires 
1313471d1714SBenjamin Tissoires /*
1314471d1714SBenjamin Tissoires  * Not all devices report physical dimensions from HID.
1315471d1714SBenjamin Tissoires  * Compute the default from hardcoded logical dimension
1316471d1714SBenjamin Tissoires  * and resolution before driver overwrites them.
1317471d1714SBenjamin Tissoires  */
1318471d1714SBenjamin Tissoires static void wacom_set_default_phy(struct wacom_features *features)
1319471d1714SBenjamin Tissoires {
1320471d1714SBenjamin Tissoires 	if (features->x_resolution) {
1321471d1714SBenjamin Tissoires 		features->x_phy = (features->x_max * 100) /
1322471d1714SBenjamin Tissoires 					features->x_resolution;
1323471d1714SBenjamin Tissoires 		features->y_phy = (features->y_max * 100) /
1324471d1714SBenjamin Tissoires 					features->y_resolution;
1325471d1714SBenjamin Tissoires 	}
1326471d1714SBenjamin Tissoires }
1327471d1714SBenjamin Tissoires 
1328471d1714SBenjamin Tissoires static void wacom_calculate_res(struct wacom_features *features)
1329471d1714SBenjamin Tissoires {
1330471d1714SBenjamin Tissoires 	features->x_resolution = wacom_calc_hid_res(features->x_max,
1331471d1714SBenjamin Tissoires 						    features->x_phy,
1332471d1714SBenjamin Tissoires 						    features->unit,
1333471d1714SBenjamin Tissoires 						    features->unitExpo);
1334471d1714SBenjamin Tissoires 	features->y_resolution = wacom_calc_hid_res(features->y_max,
1335471d1714SBenjamin Tissoires 						    features->y_phy,
1336471d1714SBenjamin Tissoires 						    features->unit,
1337471d1714SBenjamin Tissoires 						    features->unitExpo);
1338471d1714SBenjamin Tissoires }
1339471d1714SBenjamin Tissoires 
1340471d1714SBenjamin Tissoires static size_t wacom_compute_pktlen(struct hid_device *hdev)
1341471d1714SBenjamin Tissoires {
1342471d1714SBenjamin Tissoires 	struct hid_report_enum *report_enum;
1343471d1714SBenjamin Tissoires 	struct hid_report *report;
1344471d1714SBenjamin Tissoires 	size_t size = 0;
1345471d1714SBenjamin Tissoires 
1346471d1714SBenjamin Tissoires 	report_enum = hdev->report_enum + HID_INPUT_REPORT;
1347471d1714SBenjamin Tissoires 
1348471d1714SBenjamin Tissoires 	list_for_each_entry(report, &report_enum->report_list, list) {
1349dabb05c6SMathieu Magnaudet 		size_t report_size = hid_report_len(report);
1350471d1714SBenjamin Tissoires 		if (report_size > size)
1351471d1714SBenjamin Tissoires 			size = report_size;
1352471d1714SBenjamin Tissoires 	}
1353471d1714SBenjamin Tissoires 
1354471d1714SBenjamin Tissoires 	return size;
1355471d1714SBenjamin Tissoires }
1356471d1714SBenjamin Tissoires 
1357471d1714SBenjamin Tissoires static int wacom_probe(struct hid_device *hdev,
1358471d1714SBenjamin Tissoires 		const struct hid_device_id *id)
1359471d1714SBenjamin Tissoires {
1360471d1714SBenjamin Tissoires 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1361471d1714SBenjamin Tissoires 	struct usb_device *dev = interface_to_usbdev(intf);
1362471d1714SBenjamin Tissoires 	struct wacom *wacom;
1363471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac;
1364471d1714SBenjamin Tissoires 	struct wacom_features *features;
1365471d1714SBenjamin Tissoires 	int error;
13667704ac93SBenjamin Tissoires 	unsigned int connect_mask = HID_CONNECT_HIDRAW;
1367471d1714SBenjamin Tissoires 
1368471d1714SBenjamin Tissoires 	if (!id->driver_data)
1369471d1714SBenjamin Tissoires 		return -EINVAL;
1370471d1714SBenjamin Tissoires 
13718ffffd52SBenjamin Tissoires 	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
13728ffffd52SBenjamin Tissoires 
1373471d1714SBenjamin Tissoires 	wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1374471d1714SBenjamin Tissoires 	if (!wacom)
1375471d1714SBenjamin Tissoires 		return -ENOMEM;
1376471d1714SBenjamin Tissoires 
1377471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, wacom);
1378471d1714SBenjamin Tissoires 	wacom->hdev = hdev;
1379471d1714SBenjamin Tissoires 
1380471d1714SBenjamin Tissoires 	/* ask for the report descriptor to be loaded by HID */
1381471d1714SBenjamin Tissoires 	error = hid_parse(hdev);
1382471d1714SBenjamin Tissoires 	if (error) {
1383471d1714SBenjamin Tissoires 		hid_err(hdev, "parse failed\n");
13847fefeec5SBenjamin Tissoires 		goto fail_parse;
1385471d1714SBenjamin Tissoires 	}
1386471d1714SBenjamin Tissoires 
1387471d1714SBenjamin Tissoires 	wacom_wac = &wacom->wacom_wac;
1388471d1714SBenjamin Tissoires 	wacom_wac->features = *((struct wacom_features *)id->driver_data);
1389471d1714SBenjamin Tissoires 	features = &wacom_wac->features;
1390471d1714SBenjamin Tissoires 	features->pktlen = wacom_compute_pktlen(hdev);
1391471d1714SBenjamin Tissoires 	if (features->pktlen > WACOM_PKGLEN_MAX) {
1392471d1714SBenjamin Tissoires 		error = -EINVAL;
13937fefeec5SBenjamin Tissoires 		goto fail_pktlen;
1394471d1714SBenjamin Tissoires 	}
1395471d1714SBenjamin Tissoires 
1396471d1714SBenjamin Tissoires 	if (features->check_for_hid_type && features->hid_type != hdev->type) {
1397471d1714SBenjamin Tissoires 		error = -ENODEV;
13987fefeec5SBenjamin Tissoires 		goto fail_type;
1399471d1714SBenjamin Tissoires 	}
1400471d1714SBenjamin Tissoires 
1401471d1714SBenjamin Tissoires 	wacom->usbdev = dev;
1402471d1714SBenjamin Tissoires 	wacom->intf = intf;
1403471d1714SBenjamin Tissoires 	mutex_init(&wacom->lock);
1404471d1714SBenjamin Tissoires 	INIT_WORK(&wacom->work, wacom_wireless_work);
1405471d1714SBenjamin Tissoires 
1406494078b0SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1407494078b0SBenjamin Tissoires 		error = wacom_allocate_inputs(wacom);
1408494078b0SBenjamin Tissoires 		if (error)
1409494078b0SBenjamin Tissoires 			goto fail_allocate_inputs;
1410494078b0SBenjamin Tissoires 	}
1411494078b0SBenjamin Tissoires 
1412471d1714SBenjamin Tissoires 	/* set the default size in case we do not get them from hid */
1413471d1714SBenjamin Tissoires 	wacom_set_default_phy(features);
1414471d1714SBenjamin Tissoires 
1415471d1714SBenjamin Tissoires 	/* Retrieve the physical and logical size for touch devices */
1416471d1714SBenjamin Tissoires 	wacom_retrieve_hid_descriptor(hdev, features);
1417471d1714SBenjamin Tissoires 
1418471d1714SBenjamin Tissoires 	/*
1419471d1714SBenjamin Tissoires 	 * Intuos5 has no useful data about its touch interface in its
1420471d1714SBenjamin Tissoires 	 * HID descriptor. If this is the touch interface (PacketSize
1421471d1714SBenjamin Tissoires 	 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1422471d1714SBenjamin Tissoires 	 */
1423471d1714SBenjamin Tissoires 	if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
1424471d1714SBenjamin Tissoires 		if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
1425471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_FINGER;
1426471d1714SBenjamin Tissoires 
1427471d1714SBenjamin Tissoires 			features->x_max = 4096;
1428471d1714SBenjamin Tissoires 			features->y_max = 4096;
1429471d1714SBenjamin Tissoires 		} else {
1430471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_PEN;
1431471d1714SBenjamin Tissoires 		}
1432471d1714SBenjamin Tissoires 	}
1433471d1714SBenjamin Tissoires 
1434471d1714SBenjamin Tissoires 	/*
1435471d1714SBenjamin Tissoires 	 * Same thing for Bamboo 3rd gen.
1436471d1714SBenjamin Tissoires 	 */
1437471d1714SBenjamin Tissoires 	if ((features->type == BAMBOO_PT) &&
1438471d1714SBenjamin Tissoires 	    (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1439471d1714SBenjamin Tissoires 	    (features->device_type == BTN_TOOL_PEN)) {
1440471d1714SBenjamin Tissoires 		features->device_type = BTN_TOOL_FINGER;
1441471d1714SBenjamin Tissoires 
1442471d1714SBenjamin Tissoires 		features->x_max = 4096;
1443471d1714SBenjamin Tissoires 		features->y_max = 4096;
1444471d1714SBenjamin Tissoires 	}
1445471d1714SBenjamin Tissoires 
1446f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
1447f81a1295SBenjamin Tissoires 		features->quirks |= WACOM_QUIRK_BATTERY;
1448f81a1295SBenjamin Tissoires 
1449471d1714SBenjamin Tissoires 	wacom_setup_device_quirks(features);
1450471d1714SBenjamin Tissoires 
1451471d1714SBenjamin Tissoires 	/* set unit to "100th of a mm" for devices not reported by HID */
1452471d1714SBenjamin Tissoires 	if (!features->unit) {
1453471d1714SBenjamin Tissoires 		features->unit = 0x11;
1454471d1714SBenjamin Tissoires 		features->unitExpo = -3;
1455471d1714SBenjamin Tissoires 	}
1456471d1714SBenjamin Tissoires 	wacom_calculate_res(features);
1457471d1714SBenjamin Tissoires 
1458471d1714SBenjamin Tissoires 	strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1459471d1714SBenjamin Tissoires 	snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1460471d1714SBenjamin Tissoires 		"%s Pad", features->name);
1461471d1714SBenjamin Tissoires 
1462471d1714SBenjamin Tissoires 	if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
1463471d1714SBenjamin Tissoires 		/* Append the device type to the name */
1464471d1714SBenjamin Tissoires 		if (features->device_type != BTN_TOOL_FINGER)
1465471d1714SBenjamin Tissoires 			strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1466471d1714SBenjamin Tissoires 		else if (features->touch_max)
1467471d1714SBenjamin Tissoires 			strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1468471d1714SBenjamin Tissoires 		else
1469471d1714SBenjamin Tissoires 			strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
1470471d1714SBenjamin Tissoires 
1471471d1714SBenjamin Tissoires 		error = wacom_add_shared_data(hdev);
1472471d1714SBenjamin Tissoires 		if (error)
14737fefeec5SBenjamin Tissoires 			goto fail_shared_data;
1474471d1714SBenjamin Tissoires 	}
1475471d1714SBenjamin Tissoires 
1476f81a1295SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1477f81a1295SBenjamin Tissoires 	     (features->quirks & WACOM_QUIRK_BATTERY)) {
1478f81a1295SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1479f81a1295SBenjamin Tissoires 		if (error)
14807fefeec5SBenjamin Tissoires 			goto fail_battery;
1481f81a1295SBenjamin Tissoires 	}
1482f81a1295SBenjamin Tissoires 
1483471d1714SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1484494078b0SBenjamin Tissoires 		error = wacom_register_inputs(wacom);
1485471d1714SBenjamin Tissoires 		if (error)
14867fefeec5SBenjamin Tissoires 			goto fail_register_inputs;
1487f81a1295SBenjamin Tissoires 	}
1488f81a1295SBenjamin Tissoires 
1489f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
1490f81a1295SBenjamin Tissoires 		error = device_create_file(&hdev->dev, &dev_attr_speed);
1491f81a1295SBenjamin Tissoires 		if (error)
1492f81a1295SBenjamin Tissoires 			hid_warn(hdev,
1493f81a1295SBenjamin Tissoires 				 "can't create sysfs speed attribute err: %d\n",
1494f81a1295SBenjamin Tissoires 				 error);
1495471d1714SBenjamin Tissoires 	}
1496471d1714SBenjamin Tissoires 
14977704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
14987704ac93SBenjamin Tissoires 		connect_mask |= HID_CONNECT_DRIVER;
14997704ac93SBenjamin Tissoires 
1500471d1714SBenjamin Tissoires 	/* Regular HID work starts now */
15017704ac93SBenjamin Tissoires 	error = hid_hw_start(hdev, connect_mask);
1502471d1714SBenjamin Tissoires 	if (error) {
1503471d1714SBenjamin Tissoires 		hid_err(hdev, "hw start failed\n");
15047fefeec5SBenjamin Tissoires 		goto fail_hw_start;
1505471d1714SBenjamin Tissoires 	}
1506471d1714SBenjamin Tissoires 
15075ae6e89fSBenjamin Tissoires 	/* Note that if query fails it is not a hard failure */
15085ae6e89fSBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
15095ae6e89fSBenjamin Tissoires 
1510471d1714SBenjamin Tissoires 	if (features->quirks & WACOM_QUIRK_MONITOR)
1511471d1714SBenjamin Tissoires 		error = hid_hw_open(hdev);
1512471d1714SBenjamin Tissoires 
1513471d1714SBenjamin Tissoires 	if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1514471d1714SBenjamin Tissoires 		if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1515471d1714SBenjamin Tissoires 			wacom_wac->shared->touch_input = wacom_wac->input;
1516471d1714SBenjamin Tissoires 	}
1517471d1714SBenjamin Tissoires 
1518471d1714SBenjamin Tissoires 	return 0;
1519471d1714SBenjamin Tissoires 
15207fefeec5SBenjamin Tissoires fail_hw_start:
15217fefeec5SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
15227fefeec5SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
15237fefeec5SBenjamin Tissoires fail_register_inputs:
15242546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom);
15257fefeec5SBenjamin Tissoires 	wacom_destroy_battery(wacom);
15267fefeec5SBenjamin Tissoires fail_battery:
15277fefeec5SBenjamin Tissoires 	wacom_remove_shared_data(wacom_wac);
15287fefeec5SBenjamin Tissoires fail_shared_data:
1529494078b0SBenjamin Tissoires 	wacom_clean_inputs(wacom);
1530494078b0SBenjamin Tissoires fail_allocate_inputs:
15317fefeec5SBenjamin Tissoires fail_type:
15327fefeec5SBenjamin Tissoires fail_pktlen:
15337fefeec5SBenjamin Tissoires fail_parse:
15347fefeec5SBenjamin Tissoires 	kfree(wacom);
1535471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
1536471d1714SBenjamin Tissoires 	return error;
1537471d1714SBenjamin Tissoires }
1538471d1714SBenjamin Tissoires 
1539471d1714SBenjamin Tissoires static void wacom_remove(struct hid_device *hdev)
1540471d1714SBenjamin Tissoires {
1541471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1542471d1714SBenjamin Tissoires 
1543471d1714SBenjamin Tissoires 	hid_hw_stop(hdev);
1544471d1714SBenjamin Tissoires 
1545471d1714SBenjamin Tissoires 	cancel_work_sync(&wacom->work);
15462546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom);
1547f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
1548f81a1295SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
1549471d1714SBenjamin Tissoires 	wacom_destroy_battery(wacom);
1550471d1714SBenjamin Tissoires 	wacom_remove_shared_data(&wacom->wacom_wac);
1551471d1714SBenjamin Tissoires 
1552471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
1553471d1714SBenjamin Tissoires 	kfree(wacom);
1554471d1714SBenjamin Tissoires }
1555471d1714SBenjamin Tissoires 
155641a74581SGeert Uytterhoeven #ifdef CONFIG_PM
1557471d1714SBenjamin Tissoires static int wacom_resume(struct hid_device *hdev)
1558471d1714SBenjamin Tissoires {
1559471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1560471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
1561471d1714SBenjamin Tissoires 
1562471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
1563471d1714SBenjamin Tissoires 
1564471d1714SBenjamin Tissoires 	/* switch to wacom mode first */
1565471d1714SBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
1566471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
1567471d1714SBenjamin Tissoires 
1568471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
1569471d1714SBenjamin Tissoires 
1570471d1714SBenjamin Tissoires 	return 0;
1571471d1714SBenjamin Tissoires }
1572471d1714SBenjamin Tissoires 
1573471d1714SBenjamin Tissoires static int wacom_reset_resume(struct hid_device *hdev)
1574471d1714SBenjamin Tissoires {
1575471d1714SBenjamin Tissoires 	return wacom_resume(hdev);
1576471d1714SBenjamin Tissoires }
157741a74581SGeert Uytterhoeven #endif /* CONFIG_PM */
1578471d1714SBenjamin Tissoires 
1579471d1714SBenjamin Tissoires static struct hid_driver wacom_driver = {
1580471d1714SBenjamin Tissoires 	.name =		"wacom",
1581471d1714SBenjamin Tissoires 	.id_table =	wacom_ids,
1582471d1714SBenjamin Tissoires 	.probe =	wacom_probe,
1583471d1714SBenjamin Tissoires 	.remove =	wacom_remove,
15847704ac93SBenjamin Tissoires 	.event =	wacom_wac_event,
15857704ac93SBenjamin Tissoires 	.report =	wacom_wac_report,
1586471d1714SBenjamin Tissoires #ifdef CONFIG_PM
1587471d1714SBenjamin Tissoires 	.resume =	wacom_resume,
1588471d1714SBenjamin Tissoires 	.reset_resume =	wacom_reset_resume,
1589471d1714SBenjamin Tissoires #endif
1590471d1714SBenjamin Tissoires 	.raw_event =	wacom_raw_event,
1591471d1714SBenjamin Tissoires };
1592471d1714SBenjamin Tissoires module_hid_driver(wacom_driver);
1593f2e0a7d4SBenjamin Tissoires 
1594f2e0a7d4SBenjamin Tissoires MODULE_VERSION(DRIVER_VERSION);
1595f2e0a7d4SBenjamin Tissoires MODULE_AUTHOR(DRIVER_AUTHOR);
1596f2e0a7d4SBenjamin Tissoires MODULE_DESCRIPTION(DRIVER_DESC);
1597f2e0a7d4SBenjamin Tissoires MODULE_LICENSE(DRIVER_LICENSE);
1598