xref: /openbmc/linux/drivers/hid/wacom_sys.c (revision 8c97a765467c5d58682e85f103899ec2355fc393)
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 		}
409*8c97a765SBenjamin Tissoires 		else if (features->type == BAMBOO_PAD) {
410*8c97a765SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 2, 2, 2);
411*8c97a765SBenjamin Tissoires 		}
412471d1714SBenjamin Tissoires 	} else if (features->device_type == BTN_TOOL_PEN) {
413471d1714SBenjamin Tissoires 		if (features->type <= BAMBOO_PT && features->type != WIRELESS) {
414471d1714SBenjamin Tissoires 			return wacom_set_device_mode(hdev, 2, 2, 2);
415471d1714SBenjamin Tissoires 		}
416471d1714SBenjamin Tissoires 	}
417471d1714SBenjamin Tissoires 
418471d1714SBenjamin Tissoires 	return 0;
419471d1714SBenjamin Tissoires }
420471d1714SBenjamin Tissoires 
421471d1714SBenjamin Tissoires static void wacom_retrieve_hid_descriptor(struct hid_device *hdev,
422471d1714SBenjamin Tissoires 					 struct wacom_features *features)
423471d1714SBenjamin Tissoires {
424471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
425471d1714SBenjamin Tissoires 	struct usb_interface *intf = wacom->intf;
426471d1714SBenjamin Tissoires 
427471d1714SBenjamin Tissoires 	/* default features */
428471d1714SBenjamin Tissoires 	features->device_type = BTN_TOOL_PEN;
429471d1714SBenjamin Tissoires 	features->x_fuzz = 4;
430471d1714SBenjamin Tissoires 	features->y_fuzz = 4;
431471d1714SBenjamin Tissoires 	features->pressure_fuzz = 0;
432471d1714SBenjamin Tissoires 	features->distance_fuzz = 0;
433471d1714SBenjamin Tissoires 
434471d1714SBenjamin Tissoires 	/*
435471d1714SBenjamin Tissoires 	 * The wireless device HID is basic and layout conflicts with
436471d1714SBenjamin Tissoires 	 * other tablets (monitor and touch interface can look like pen).
437471d1714SBenjamin Tissoires 	 * Skip the query for this type and modify defaults based on
438471d1714SBenjamin Tissoires 	 * interface number.
439471d1714SBenjamin Tissoires 	 */
440471d1714SBenjamin Tissoires 	if (features->type == WIRELESS) {
441471d1714SBenjamin Tissoires 		if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
442471d1714SBenjamin Tissoires 			features->device_type = 0;
443471d1714SBenjamin Tissoires 		} else if (intf->cur_altsetting->desc.bInterfaceNumber == 2) {
444471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_FINGER;
445471d1714SBenjamin Tissoires 			features->pktlen = WACOM_PKGLEN_BBTOUCH3;
446471d1714SBenjamin Tissoires 		}
447471d1714SBenjamin Tissoires 	}
448471d1714SBenjamin Tissoires 
449471d1714SBenjamin Tissoires 	/* only devices that support touch need to retrieve the info */
450471d1714SBenjamin Tissoires 	if (features->type < BAMBOO_PT)
451471d1714SBenjamin Tissoires 		return;
452471d1714SBenjamin Tissoires 
453471d1714SBenjamin Tissoires 	wacom_parse_hid(hdev, features);
454471d1714SBenjamin Tissoires }
455471d1714SBenjamin Tissoires 
456471d1714SBenjamin Tissoires struct wacom_hdev_data {
457471d1714SBenjamin Tissoires 	struct list_head list;
458471d1714SBenjamin Tissoires 	struct kref kref;
459471d1714SBenjamin Tissoires 	struct hid_device *dev;
460471d1714SBenjamin Tissoires 	struct wacom_shared shared;
461471d1714SBenjamin Tissoires };
462471d1714SBenjamin Tissoires 
463471d1714SBenjamin Tissoires static LIST_HEAD(wacom_udev_list);
464471d1714SBenjamin Tissoires static DEFINE_MUTEX(wacom_udev_list_lock);
465471d1714SBenjamin Tissoires 
466471d1714SBenjamin Tissoires static bool wacom_are_sibling(struct hid_device *hdev,
467471d1714SBenjamin Tissoires 		struct hid_device *sibling)
468471d1714SBenjamin Tissoires {
469471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
470471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
471471d1714SBenjamin Tissoires 	int vid = features->oVid;
472471d1714SBenjamin Tissoires 	int pid = features->oPid;
473471d1714SBenjamin Tissoires 	int n1,n2;
474471d1714SBenjamin Tissoires 
475471d1714SBenjamin Tissoires 	if (vid == 0 && pid == 0) {
476471d1714SBenjamin Tissoires 		vid = hdev->vendor;
477471d1714SBenjamin Tissoires 		pid = hdev->product;
478471d1714SBenjamin Tissoires 	}
479471d1714SBenjamin Tissoires 
480471d1714SBenjamin Tissoires 	if (vid != sibling->vendor || pid != sibling->product)
481471d1714SBenjamin Tissoires 		return false;
482471d1714SBenjamin Tissoires 
483471d1714SBenjamin Tissoires 	/* Compare the physical path. */
484471d1714SBenjamin Tissoires 	n1 = strrchr(hdev->phys, '.') - hdev->phys;
485471d1714SBenjamin Tissoires 	n2 = strrchr(sibling->phys, '.') - sibling->phys;
486471d1714SBenjamin Tissoires 	if (n1 != n2 || n1 <= 0 || n2 <= 0)
487471d1714SBenjamin Tissoires 		return false;
488471d1714SBenjamin Tissoires 
489471d1714SBenjamin Tissoires 	return !strncmp(hdev->phys, sibling->phys, n1);
490471d1714SBenjamin Tissoires }
491471d1714SBenjamin Tissoires 
492471d1714SBenjamin Tissoires static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
493471d1714SBenjamin Tissoires {
494471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
495471d1714SBenjamin Tissoires 
496471d1714SBenjamin Tissoires 	list_for_each_entry(data, &wacom_udev_list, list) {
497471d1714SBenjamin Tissoires 		if (wacom_are_sibling(hdev, data->dev)) {
498471d1714SBenjamin Tissoires 			kref_get(&data->kref);
499471d1714SBenjamin Tissoires 			return data;
500471d1714SBenjamin Tissoires 		}
501471d1714SBenjamin Tissoires 	}
502471d1714SBenjamin Tissoires 
503471d1714SBenjamin Tissoires 	return NULL;
504471d1714SBenjamin Tissoires }
505471d1714SBenjamin Tissoires 
506471d1714SBenjamin Tissoires static int wacom_add_shared_data(struct hid_device *hdev)
507471d1714SBenjamin Tissoires {
508471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
509471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
510471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
511471d1714SBenjamin Tissoires 	int retval = 0;
512471d1714SBenjamin Tissoires 
513471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
514471d1714SBenjamin Tissoires 
515471d1714SBenjamin Tissoires 	data = wacom_get_hdev_data(hdev);
516471d1714SBenjamin Tissoires 	if (!data) {
517471d1714SBenjamin Tissoires 		data = kzalloc(sizeof(struct wacom_hdev_data), GFP_KERNEL);
518471d1714SBenjamin Tissoires 		if (!data) {
519471d1714SBenjamin Tissoires 			retval = -ENOMEM;
520471d1714SBenjamin Tissoires 			goto out;
521471d1714SBenjamin Tissoires 		}
522471d1714SBenjamin Tissoires 
523471d1714SBenjamin Tissoires 		kref_init(&data->kref);
524471d1714SBenjamin Tissoires 		data->dev = hdev;
525471d1714SBenjamin Tissoires 		list_add_tail(&data->list, &wacom_udev_list);
526471d1714SBenjamin Tissoires 	}
527471d1714SBenjamin Tissoires 
528471d1714SBenjamin Tissoires 	wacom_wac->shared = &data->shared;
529471d1714SBenjamin Tissoires 
530a97ac104SBenjamin Tissoires 	if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
531a97ac104SBenjamin Tissoires 		wacom_wac->shared->touch = hdev;
532a97ac104SBenjamin Tissoires 	else if (wacom_wac->features.device_type == BTN_TOOL_PEN)
533a97ac104SBenjamin Tissoires 		wacom_wac->shared->pen = hdev;
534a97ac104SBenjamin Tissoires 
535471d1714SBenjamin Tissoires out:
536471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
537471d1714SBenjamin Tissoires 	return retval;
538471d1714SBenjamin Tissoires }
539471d1714SBenjamin Tissoires 
540471d1714SBenjamin Tissoires static void wacom_release_shared_data(struct kref *kref)
541471d1714SBenjamin Tissoires {
542471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data =
543471d1714SBenjamin Tissoires 		container_of(kref, struct wacom_hdev_data, kref);
544471d1714SBenjamin Tissoires 
545471d1714SBenjamin Tissoires 	mutex_lock(&wacom_udev_list_lock);
546471d1714SBenjamin Tissoires 	list_del(&data->list);
547471d1714SBenjamin Tissoires 	mutex_unlock(&wacom_udev_list_lock);
548471d1714SBenjamin Tissoires 
549471d1714SBenjamin Tissoires 	kfree(data);
550471d1714SBenjamin Tissoires }
551471d1714SBenjamin Tissoires 
552a97ac104SBenjamin Tissoires static void wacom_remove_shared_data(struct wacom *wacom)
553471d1714SBenjamin Tissoires {
554471d1714SBenjamin Tissoires 	struct wacom_hdev_data *data;
555a97ac104SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
556471d1714SBenjamin Tissoires 
557a97ac104SBenjamin Tissoires 	if (wacom_wac->shared) {
558a97ac104SBenjamin Tissoires 		data = container_of(wacom_wac->shared, struct wacom_hdev_data,
559a97ac104SBenjamin Tissoires 				    shared);
560a97ac104SBenjamin Tissoires 
561a97ac104SBenjamin Tissoires 		if (wacom_wac->shared->touch == wacom->hdev)
562a97ac104SBenjamin Tissoires 			wacom_wac->shared->touch = NULL;
563a97ac104SBenjamin Tissoires 		else if (wacom_wac->shared->pen == wacom->hdev)
564a97ac104SBenjamin Tissoires 			wacom_wac->shared->pen = NULL;
565a97ac104SBenjamin Tissoires 
566471d1714SBenjamin Tissoires 		kref_put(&data->kref, wacom_release_shared_data);
567a97ac104SBenjamin Tissoires 		wacom_wac->shared = NULL;
568471d1714SBenjamin Tissoires 	}
569471d1714SBenjamin Tissoires }
570471d1714SBenjamin Tissoires 
571471d1714SBenjamin Tissoires static int wacom_led_control(struct wacom *wacom)
572471d1714SBenjamin Tissoires {
573471d1714SBenjamin Tissoires 	unsigned char *buf;
574471d1714SBenjamin Tissoires 	int retval;
575912ca216SPing Cheng 	unsigned char report_id = WAC_CMD_LED_CONTROL;
576912ca216SPing Cheng 	int buf_size = 9;
577471d1714SBenjamin Tissoires 
578912ca216SPing Cheng 	if (wacom->wacom_wac.pid) { /* wireless connected */
579912ca216SPing Cheng 		report_id = WAC_CMD_WL_LED_CONTROL;
580912ca216SPing Cheng 		buf_size = 13;
581912ca216SPing Cheng 	}
582912ca216SPing Cheng 	buf = kzalloc(buf_size, GFP_KERNEL);
583471d1714SBenjamin Tissoires 	if (!buf)
584471d1714SBenjamin Tissoires 		return -ENOMEM;
585471d1714SBenjamin Tissoires 
586471d1714SBenjamin Tissoires 	if (wacom->wacom_wac.features.type >= INTUOS5S &&
587471d1714SBenjamin Tissoires 	    wacom->wacom_wac.features.type <= INTUOSPL) {
588471d1714SBenjamin Tissoires 		/*
589471d1714SBenjamin Tissoires 		 * Touch Ring and crop mark LED luminance may take on
590471d1714SBenjamin Tissoires 		 * one of four values:
591471d1714SBenjamin Tissoires 		 *    0 = Low; 1 = Medium; 2 = High; 3 = Off
592471d1714SBenjamin Tissoires 		 */
593471d1714SBenjamin Tissoires 		int ring_led = wacom->led.select[0] & 0x03;
594471d1714SBenjamin Tissoires 		int ring_lum = (((wacom->led.llv & 0x60) >> 5) - 1) & 0x03;
595471d1714SBenjamin Tissoires 		int crop_lum = 0;
596912ca216SPing Cheng 		unsigned char led_bits = (crop_lum << 4) | (ring_lum << 2) | (ring_led);
597471d1714SBenjamin Tissoires 
598912ca216SPing Cheng 		buf[0] = report_id;
599912ca216SPing Cheng 		if (wacom->wacom_wac.pid) {
600912ca216SPing Cheng 			wacom_get_report(wacom->hdev, HID_FEATURE_REPORT,
601912ca216SPing Cheng 					 buf, buf_size, WAC_CMD_RETRIES);
602912ca216SPing Cheng 			buf[0] = report_id;
603912ca216SPing Cheng 			buf[4] = led_bits;
604912ca216SPing Cheng 		} else
605912ca216SPing Cheng 			buf[1] = led_bits;
606471d1714SBenjamin Tissoires 	}
607471d1714SBenjamin Tissoires 	else {
608471d1714SBenjamin Tissoires 		int led = wacom->led.select[0] | 0x4;
609471d1714SBenjamin Tissoires 
610471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.type == WACOM_21UX2 ||
611471d1714SBenjamin Tissoires 		    wacom->wacom_wac.features.type == WACOM_24HD)
612471d1714SBenjamin Tissoires 			led |= (wacom->led.select[1] << 4) | 0x40;
613471d1714SBenjamin Tissoires 
614912ca216SPing Cheng 		buf[0] = report_id;
615471d1714SBenjamin Tissoires 		buf[1] = led;
616471d1714SBenjamin Tissoires 		buf[2] = wacom->led.llv;
617471d1714SBenjamin Tissoires 		buf[3] = wacom->led.hlv;
618471d1714SBenjamin Tissoires 		buf[4] = wacom->led.img_lum;
619471d1714SBenjamin Tissoires 	}
620471d1714SBenjamin Tissoires 
621912ca216SPing Cheng 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, buf_size,
622296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
623471d1714SBenjamin Tissoires 	kfree(buf);
624471d1714SBenjamin Tissoires 
625471d1714SBenjamin Tissoires 	return retval;
626471d1714SBenjamin Tissoires }
627471d1714SBenjamin Tissoires 
628849e2f06SBenjamin Tissoires static int wacom_led_putimage(struct wacom *wacom, int button_id, u8 xfer_id,
629849e2f06SBenjamin Tissoires 		const unsigned len, const void *img)
630471d1714SBenjamin Tissoires {
631471d1714SBenjamin Tissoires 	unsigned char *buf;
632471d1714SBenjamin Tissoires 	int i, retval;
633849e2f06SBenjamin Tissoires 	const unsigned chunk_len = len / 4; /* 4 chunks are needed to be sent */
634471d1714SBenjamin Tissoires 
635849e2f06SBenjamin Tissoires 	buf = kzalloc(chunk_len + 3 , GFP_KERNEL);
636471d1714SBenjamin Tissoires 	if (!buf)
637471d1714SBenjamin Tissoires 		return -ENOMEM;
638471d1714SBenjamin Tissoires 
639471d1714SBenjamin Tissoires 	/* Send 'start' command */
640471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
641471d1714SBenjamin Tissoires 	buf[1] = 1;
642296b7378SPrzemo Firszt 	retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
643296b7378SPrzemo Firszt 				  WAC_CMD_RETRIES);
644471d1714SBenjamin Tissoires 	if (retval < 0)
645471d1714SBenjamin Tissoires 		goto out;
646471d1714SBenjamin Tissoires 
647849e2f06SBenjamin Tissoires 	buf[0] = xfer_id;
648471d1714SBenjamin Tissoires 	buf[1] = button_id & 0x07;
649471d1714SBenjamin Tissoires 	for (i = 0; i < 4; i++) {
650471d1714SBenjamin Tissoires 		buf[2] = i;
651849e2f06SBenjamin Tissoires 		memcpy(buf + 3, img + i * chunk_len, chunk_len);
652471d1714SBenjamin Tissoires 
653471d1714SBenjamin Tissoires 		retval = wacom_set_report(wacom->hdev, HID_FEATURE_REPORT,
654296b7378SPrzemo Firszt 					  buf, chunk_len + 3, WAC_CMD_RETRIES);
655471d1714SBenjamin Tissoires 		if (retval < 0)
656471d1714SBenjamin Tissoires 			break;
657471d1714SBenjamin Tissoires 	}
658471d1714SBenjamin Tissoires 
659471d1714SBenjamin Tissoires 	/* Send 'stop' */
660471d1714SBenjamin Tissoires 	buf[0] = WAC_CMD_ICON_START;
661471d1714SBenjamin Tissoires 	buf[1] = 0;
662296b7378SPrzemo Firszt 	wacom_set_report(wacom->hdev, HID_FEATURE_REPORT, buf, 2,
663296b7378SPrzemo Firszt 			 WAC_CMD_RETRIES);
664471d1714SBenjamin Tissoires 
665471d1714SBenjamin Tissoires out:
666471d1714SBenjamin Tissoires 	kfree(buf);
667471d1714SBenjamin Tissoires 	return retval;
668471d1714SBenjamin Tissoires }
669471d1714SBenjamin Tissoires 
670471d1714SBenjamin Tissoires static ssize_t wacom_led_select_store(struct device *dev, int set_id,
671471d1714SBenjamin Tissoires 				      const char *buf, size_t count)
672471d1714SBenjamin Tissoires {
673471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
674471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
675471d1714SBenjamin Tissoires 	unsigned int id;
676471d1714SBenjamin Tissoires 	int err;
677471d1714SBenjamin Tissoires 
678471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &id);
679471d1714SBenjamin Tissoires 	if (err)
680471d1714SBenjamin Tissoires 		return err;
681471d1714SBenjamin Tissoires 
682471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
683471d1714SBenjamin Tissoires 
684471d1714SBenjamin Tissoires 	wacom->led.select[set_id] = id & 0x3;
685471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
686471d1714SBenjamin Tissoires 
687471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
688471d1714SBenjamin Tissoires 
689471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
690471d1714SBenjamin Tissoires }
691471d1714SBenjamin Tissoires 
692471d1714SBenjamin Tissoires #define DEVICE_LED_SELECT_ATTR(SET_ID)					\
693471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_store(struct device *dev,	\
694471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
695471d1714SBenjamin Tissoires {									\
696471d1714SBenjamin Tissoires 	return wacom_led_select_store(dev, SET_ID, buf, count);		\
697471d1714SBenjamin Tissoires }									\
698471d1714SBenjamin Tissoires static ssize_t wacom_led##SET_ID##_select_show(struct device *dev,	\
699471d1714SBenjamin Tissoires 	struct device_attribute *attr, char *buf)			\
700471d1714SBenjamin Tissoires {									\
701471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
702471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
70337449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n",			\
70437449adcSPing Cheng 			 wacom->led.select[SET_ID]);			\
705471d1714SBenjamin Tissoires }									\
706e0984bc3SPing Cheng static DEVICE_ATTR(status_led##SET_ID##_select, DEV_ATTR_RW_PERM,	\
707471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_show,			\
708471d1714SBenjamin Tissoires 		    wacom_led##SET_ID##_select_store)
709471d1714SBenjamin Tissoires 
710471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(0);
711471d1714SBenjamin Tissoires DEVICE_LED_SELECT_ATTR(1);
712471d1714SBenjamin Tissoires 
713471d1714SBenjamin Tissoires static ssize_t wacom_luminance_store(struct wacom *wacom, u8 *dest,
714471d1714SBenjamin Tissoires 				     const char *buf, size_t count)
715471d1714SBenjamin Tissoires {
716471d1714SBenjamin Tissoires 	unsigned int value;
717471d1714SBenjamin Tissoires 	int err;
718471d1714SBenjamin Tissoires 
719471d1714SBenjamin Tissoires 	err = kstrtouint(buf, 10, &value);
720471d1714SBenjamin Tissoires 	if (err)
721471d1714SBenjamin Tissoires 		return err;
722471d1714SBenjamin Tissoires 
723471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
724471d1714SBenjamin Tissoires 
725471d1714SBenjamin Tissoires 	*dest = value & 0x7f;
726471d1714SBenjamin Tissoires 	err = wacom_led_control(wacom);
727471d1714SBenjamin Tissoires 
728471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
729471d1714SBenjamin Tissoires 
730471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
731471d1714SBenjamin Tissoires }
732471d1714SBenjamin Tissoires 
733471d1714SBenjamin Tissoires #define DEVICE_LUMINANCE_ATTR(name, field)				\
734471d1714SBenjamin Tissoires static ssize_t wacom_##name##_luminance_store(struct device *dev,	\
735471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
736471d1714SBenjamin Tissoires {									\
737471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);\
738471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);			\
739471d1714SBenjamin Tissoires 									\
740471d1714SBenjamin Tissoires 	return wacom_luminance_store(wacom, &wacom->led.field,		\
741471d1714SBenjamin Tissoires 				     buf, count);			\
742471d1714SBenjamin Tissoires }									\
74337449adcSPing Cheng static ssize_t wacom_##name##_luminance_show(struct device *dev,	\
74437449adcSPing Cheng 	struct device_attribute *attr, char *buf)			\
74537449adcSPing Cheng {									\
74637449adcSPing Cheng 	struct wacom *wacom = dev_get_drvdata(dev);			\
74737449adcSPing Cheng 	return scnprintf(buf, PAGE_SIZE, "%d\n", wacom->led.field);	\
74837449adcSPing Cheng }									\
749e0984bc3SPing Cheng static DEVICE_ATTR(name##_luminance, DEV_ATTR_RW_PERM,			\
75037449adcSPing Cheng 		   wacom_##name##_luminance_show,			\
75137449adcSPing Cheng 		   wacom_##name##_luminance_store)
752471d1714SBenjamin Tissoires 
753471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status0, llv);
754471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(status1, hlv);
755471d1714SBenjamin Tissoires DEVICE_LUMINANCE_ATTR(buttons, img_lum);
756471d1714SBenjamin Tissoires 
757471d1714SBenjamin Tissoires static ssize_t wacom_button_image_store(struct device *dev, int button_id,
758471d1714SBenjamin Tissoires 					const char *buf, size_t count)
759471d1714SBenjamin Tissoires {
760471d1714SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
761471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
762471d1714SBenjamin Tissoires 	int err;
763849e2f06SBenjamin Tissoires 	unsigned len;
764849e2f06SBenjamin Tissoires 	u8 xfer_id;
765471d1714SBenjamin Tissoires 
766849e2f06SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
767849e2f06SBenjamin Tissoires 		len = 256;
768849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_BT_XFER;
769849e2f06SBenjamin Tissoires 	} else {
770849e2f06SBenjamin Tissoires 		len = 1024;
771849e2f06SBenjamin Tissoires 		xfer_id = WAC_CMD_ICON_XFER;
772849e2f06SBenjamin Tissoires 	}
773849e2f06SBenjamin Tissoires 
774849e2f06SBenjamin Tissoires 	if (count != len)
775471d1714SBenjamin Tissoires 		return -EINVAL;
776471d1714SBenjamin Tissoires 
777471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
778471d1714SBenjamin Tissoires 
779849e2f06SBenjamin Tissoires 	err = wacom_led_putimage(wacom, button_id, xfer_id, len, buf);
780471d1714SBenjamin Tissoires 
781471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
782471d1714SBenjamin Tissoires 
783471d1714SBenjamin Tissoires 	return err < 0 ? err : count;
784471d1714SBenjamin Tissoires }
785471d1714SBenjamin Tissoires 
786471d1714SBenjamin Tissoires #define DEVICE_BTNIMG_ATTR(BUTTON_ID)					\
787471d1714SBenjamin Tissoires static ssize_t wacom_btnimg##BUTTON_ID##_store(struct device *dev,	\
788471d1714SBenjamin Tissoires 	struct device_attribute *attr, const char *buf, size_t count)	\
789471d1714SBenjamin Tissoires {									\
790471d1714SBenjamin Tissoires 	return wacom_button_image_store(dev, BUTTON_ID, buf, count);	\
791471d1714SBenjamin Tissoires }									\
792e0984bc3SPing Cheng static DEVICE_ATTR(button##BUTTON_ID##_rawimg, DEV_ATTR_WO_PERM,	\
793471d1714SBenjamin Tissoires 		   NULL, wacom_btnimg##BUTTON_ID##_store)
794471d1714SBenjamin Tissoires 
795471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(0);
796471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(1);
797471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(2);
798471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(3);
799471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(4);
800471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(5);
801471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(6);
802471d1714SBenjamin Tissoires DEVICE_BTNIMG_ATTR(7);
803471d1714SBenjamin Tissoires 
804471d1714SBenjamin Tissoires static struct attribute *cintiq_led_attrs[] = {
805471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
806471d1714SBenjamin Tissoires 	&dev_attr_status_led1_select.attr,
807471d1714SBenjamin Tissoires 	NULL
808471d1714SBenjamin Tissoires };
809471d1714SBenjamin Tissoires 
810471d1714SBenjamin Tissoires static struct attribute_group cintiq_led_attr_group = {
811471d1714SBenjamin Tissoires 	.name = "wacom_led",
812471d1714SBenjamin Tissoires 	.attrs = cintiq_led_attrs,
813471d1714SBenjamin Tissoires };
814471d1714SBenjamin Tissoires 
815471d1714SBenjamin Tissoires static struct attribute *intuos4_led_attrs[] = {
816471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
817471d1714SBenjamin Tissoires 	&dev_attr_status1_luminance.attr,
818471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
819471d1714SBenjamin Tissoires 	&dev_attr_buttons_luminance.attr,
820471d1714SBenjamin Tissoires 	&dev_attr_button0_rawimg.attr,
821471d1714SBenjamin Tissoires 	&dev_attr_button1_rawimg.attr,
822471d1714SBenjamin Tissoires 	&dev_attr_button2_rawimg.attr,
823471d1714SBenjamin Tissoires 	&dev_attr_button3_rawimg.attr,
824471d1714SBenjamin Tissoires 	&dev_attr_button4_rawimg.attr,
825471d1714SBenjamin Tissoires 	&dev_attr_button5_rawimg.attr,
826471d1714SBenjamin Tissoires 	&dev_attr_button6_rawimg.attr,
827471d1714SBenjamin Tissoires 	&dev_attr_button7_rawimg.attr,
828471d1714SBenjamin Tissoires 	NULL
829471d1714SBenjamin Tissoires };
830471d1714SBenjamin Tissoires 
831471d1714SBenjamin Tissoires static struct attribute_group intuos4_led_attr_group = {
832471d1714SBenjamin Tissoires 	.name = "wacom_led",
833471d1714SBenjamin Tissoires 	.attrs = intuos4_led_attrs,
834471d1714SBenjamin Tissoires };
835471d1714SBenjamin Tissoires 
836471d1714SBenjamin Tissoires static struct attribute *intuos5_led_attrs[] = {
837471d1714SBenjamin Tissoires 	&dev_attr_status0_luminance.attr,
838471d1714SBenjamin Tissoires 	&dev_attr_status_led0_select.attr,
839471d1714SBenjamin Tissoires 	NULL
840471d1714SBenjamin Tissoires };
841471d1714SBenjamin Tissoires 
842471d1714SBenjamin Tissoires static struct attribute_group intuos5_led_attr_group = {
843471d1714SBenjamin Tissoires 	.name = "wacom_led",
844471d1714SBenjamin Tissoires 	.attrs = intuos5_led_attrs,
845471d1714SBenjamin Tissoires };
846471d1714SBenjamin Tissoires 
847471d1714SBenjamin Tissoires static int wacom_initialize_leds(struct wacom *wacom)
848471d1714SBenjamin Tissoires {
849471d1714SBenjamin Tissoires 	int error;
850471d1714SBenjamin Tissoires 
851471d1714SBenjamin Tissoires 	/* Initialize default values */
852471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
853471d1714SBenjamin Tissoires 	case INTUOS4S:
854471d1714SBenjamin Tissoires 	case INTUOS4:
85581af7e61SBenjamin Tissoires 	case INTUOS4WL:
856471d1714SBenjamin Tissoires 	case INTUOS4L:
857471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
858471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
859471d1714SBenjamin Tissoires 		wacom->led.llv = 10;
860471d1714SBenjamin Tissoires 		wacom->led.hlv = 20;
861471d1714SBenjamin Tissoires 		wacom->led.img_lum = 10;
862471d1714SBenjamin Tissoires 		error = sysfs_create_group(&wacom->hdev->dev.kobj,
863471d1714SBenjamin Tissoires 					   &intuos4_led_attr_group);
864471d1714SBenjamin Tissoires 		break;
865471d1714SBenjamin Tissoires 
866471d1714SBenjamin Tissoires 	case WACOM_24HD:
867471d1714SBenjamin Tissoires 	case WACOM_21UX2:
868471d1714SBenjamin Tissoires 		wacom->led.select[0] = 0;
869471d1714SBenjamin Tissoires 		wacom->led.select[1] = 0;
870471d1714SBenjamin Tissoires 		wacom->led.llv = 0;
871471d1714SBenjamin Tissoires 		wacom->led.hlv = 0;
872471d1714SBenjamin Tissoires 		wacom->led.img_lum = 0;
873471d1714SBenjamin Tissoires 
874471d1714SBenjamin Tissoires 		error = sysfs_create_group(&wacom->hdev->dev.kobj,
875471d1714SBenjamin Tissoires 					   &cintiq_led_attr_group);
876471d1714SBenjamin Tissoires 		break;
877471d1714SBenjamin Tissoires 
878471d1714SBenjamin Tissoires 	case INTUOS5S:
879471d1714SBenjamin Tissoires 	case INTUOS5:
880471d1714SBenjamin Tissoires 	case INTUOS5L:
881471d1714SBenjamin Tissoires 	case INTUOSPS:
882471d1714SBenjamin Tissoires 	case INTUOSPM:
883471d1714SBenjamin Tissoires 	case INTUOSPL:
884471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN) {
885471d1714SBenjamin Tissoires 			wacom->led.select[0] = 0;
886471d1714SBenjamin Tissoires 			wacom->led.select[1] = 0;
887471d1714SBenjamin Tissoires 			wacom->led.llv = 32;
888471d1714SBenjamin Tissoires 			wacom->led.hlv = 0;
889471d1714SBenjamin Tissoires 			wacom->led.img_lum = 0;
890471d1714SBenjamin Tissoires 
891471d1714SBenjamin Tissoires 			error = sysfs_create_group(&wacom->hdev->dev.kobj,
892471d1714SBenjamin Tissoires 						  &intuos5_led_attr_group);
893471d1714SBenjamin Tissoires 		} else
894471d1714SBenjamin Tissoires 			return 0;
895471d1714SBenjamin Tissoires 		break;
896471d1714SBenjamin Tissoires 
897471d1714SBenjamin Tissoires 	default:
898471d1714SBenjamin Tissoires 		return 0;
899471d1714SBenjamin Tissoires 	}
900471d1714SBenjamin Tissoires 
901471d1714SBenjamin Tissoires 	if (error) {
902471d1714SBenjamin Tissoires 		hid_err(wacom->hdev,
903471d1714SBenjamin Tissoires 			"cannot create sysfs group err: %d\n", error);
904471d1714SBenjamin Tissoires 		return error;
905471d1714SBenjamin Tissoires 	}
906471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
907c757cbafSBenjamin Tissoires 	wacom->led_initialized = true;
908471d1714SBenjamin Tissoires 
909471d1714SBenjamin Tissoires 	return 0;
910471d1714SBenjamin Tissoires }
911471d1714SBenjamin Tissoires 
912471d1714SBenjamin Tissoires static void wacom_destroy_leds(struct wacom *wacom)
913471d1714SBenjamin Tissoires {
914c757cbafSBenjamin Tissoires 	if (!wacom->led_initialized)
915c757cbafSBenjamin Tissoires 		return;
916c757cbafSBenjamin Tissoires 
917c757cbafSBenjamin Tissoires 	wacom->led_initialized = false;
918c757cbafSBenjamin Tissoires 
919471d1714SBenjamin Tissoires 	switch (wacom->wacom_wac.features.type) {
920471d1714SBenjamin Tissoires 	case INTUOS4S:
921471d1714SBenjamin Tissoires 	case INTUOS4:
92281af7e61SBenjamin Tissoires 	case INTUOS4WL:
923471d1714SBenjamin Tissoires 	case INTUOS4L:
924471d1714SBenjamin Tissoires 		sysfs_remove_group(&wacom->hdev->dev.kobj,
925471d1714SBenjamin Tissoires 				   &intuos4_led_attr_group);
926471d1714SBenjamin Tissoires 		break;
927471d1714SBenjamin Tissoires 
928471d1714SBenjamin Tissoires 	case WACOM_24HD:
929471d1714SBenjamin Tissoires 	case WACOM_21UX2:
930471d1714SBenjamin Tissoires 		sysfs_remove_group(&wacom->hdev->dev.kobj,
931471d1714SBenjamin Tissoires 				   &cintiq_led_attr_group);
932471d1714SBenjamin Tissoires 		break;
933471d1714SBenjamin Tissoires 
934471d1714SBenjamin Tissoires 	case INTUOS5S:
935471d1714SBenjamin Tissoires 	case INTUOS5:
936471d1714SBenjamin Tissoires 	case INTUOS5L:
937471d1714SBenjamin Tissoires 	case INTUOSPS:
938471d1714SBenjamin Tissoires 	case INTUOSPM:
939471d1714SBenjamin Tissoires 	case INTUOSPL:
940471d1714SBenjamin Tissoires 		if (wacom->wacom_wac.features.device_type == BTN_TOOL_PEN)
941471d1714SBenjamin Tissoires 			sysfs_remove_group(&wacom->hdev->dev.kobj,
942471d1714SBenjamin Tissoires 					   &intuos5_led_attr_group);
943471d1714SBenjamin Tissoires 		break;
944471d1714SBenjamin Tissoires 	}
945471d1714SBenjamin Tissoires }
946471d1714SBenjamin Tissoires 
947471d1714SBenjamin Tissoires static enum power_supply_property wacom_battery_props[] = {
948ac8d1010SBenjamin Tissoires 	POWER_SUPPLY_PROP_STATUS,
949471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
950471d1714SBenjamin Tissoires 	POWER_SUPPLY_PROP_CAPACITY
951471d1714SBenjamin Tissoires };
952471d1714SBenjamin Tissoires 
9537dbd229eSBenjamin Tissoires static enum power_supply_property wacom_ac_props[] = {
9547dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_PRESENT,
9557dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_ONLINE,
9567dbd229eSBenjamin Tissoires 	POWER_SUPPLY_PROP_SCOPE,
9577dbd229eSBenjamin Tissoires };
9587dbd229eSBenjamin Tissoires 
959471d1714SBenjamin Tissoires static int wacom_battery_get_property(struct power_supply *psy,
960471d1714SBenjamin Tissoires 				      enum power_supply_property psp,
961471d1714SBenjamin Tissoires 				      union power_supply_propval *val)
962471d1714SBenjamin Tissoires {
963471d1714SBenjamin Tissoires 	struct wacom *wacom = container_of(psy, struct wacom, battery);
964471d1714SBenjamin Tissoires 	int ret = 0;
965471d1714SBenjamin Tissoires 
966471d1714SBenjamin Tissoires 	switch (psp) {
967471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_SCOPE:
968471d1714SBenjamin Tissoires 			val->intval = POWER_SUPPLY_SCOPE_DEVICE;
969471d1714SBenjamin Tissoires 			break;
970471d1714SBenjamin Tissoires 		case POWER_SUPPLY_PROP_CAPACITY:
971471d1714SBenjamin Tissoires 			val->intval =
972ac8d1010SBenjamin Tissoires 				wacom->wacom_wac.battery_capacity;
973ac8d1010SBenjamin Tissoires 			break;
974ac8d1010SBenjamin Tissoires 		case POWER_SUPPLY_PROP_STATUS:
975ac8d1010SBenjamin Tissoires 			if (wacom->wacom_wac.bat_charging)
976ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_CHARGING;
977ac8d1010SBenjamin Tissoires 			else if (wacom->wacom_wac.battery_capacity == 100 &&
978ac8d1010SBenjamin Tissoires 				    wacom->wacom_wac.ps_connected)
979ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_FULL;
980ac8d1010SBenjamin Tissoires 			else
981ac8d1010SBenjamin Tissoires 				val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
982471d1714SBenjamin Tissoires 			break;
983471d1714SBenjamin Tissoires 		default:
984471d1714SBenjamin Tissoires 			ret = -EINVAL;
985471d1714SBenjamin Tissoires 			break;
986471d1714SBenjamin Tissoires 	}
987471d1714SBenjamin Tissoires 
988471d1714SBenjamin Tissoires 	return ret;
989471d1714SBenjamin Tissoires }
990471d1714SBenjamin Tissoires 
9917dbd229eSBenjamin Tissoires static int wacom_ac_get_property(struct power_supply *psy,
9927dbd229eSBenjamin Tissoires 				enum power_supply_property psp,
9937dbd229eSBenjamin Tissoires 				union power_supply_propval *val)
9947dbd229eSBenjamin Tissoires {
9957dbd229eSBenjamin Tissoires 	struct wacom *wacom = container_of(psy, struct wacom, ac);
9967dbd229eSBenjamin Tissoires 	int ret = 0;
9977dbd229eSBenjamin Tissoires 
9987dbd229eSBenjamin Tissoires 	switch (psp) {
9997dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_PRESENT:
10007dbd229eSBenjamin Tissoires 		/* fall through */
10017dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_ONLINE:
10027dbd229eSBenjamin Tissoires 		val->intval = wacom->wacom_wac.ps_connected;
10037dbd229eSBenjamin Tissoires 		break;
10047dbd229eSBenjamin Tissoires 	case POWER_SUPPLY_PROP_SCOPE:
10057dbd229eSBenjamin Tissoires 		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
10067dbd229eSBenjamin Tissoires 		break;
10077dbd229eSBenjamin Tissoires 	default:
10087dbd229eSBenjamin Tissoires 		ret = -EINVAL;
10097dbd229eSBenjamin Tissoires 		break;
10107dbd229eSBenjamin Tissoires 	}
10117dbd229eSBenjamin Tissoires 	return ret;
10127dbd229eSBenjamin Tissoires }
10137dbd229eSBenjamin Tissoires 
1014471d1714SBenjamin Tissoires static int wacom_initialize_battery(struct wacom *wacom)
1015471d1714SBenjamin Tissoires {
1016d70420b9SBenjamin Tissoires 	static atomic_t battery_no = ATOMIC_INIT(0);
10177dbd229eSBenjamin Tissoires 	int error;
1018d70420b9SBenjamin Tissoires 	unsigned long n;
1019471d1714SBenjamin Tissoires 
1020ac8d1010SBenjamin Tissoires 	if (wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) {
1021d70420b9SBenjamin Tissoires 		n = atomic_inc_return(&battery_no) - 1;
10227dbd229eSBenjamin Tissoires 
1023471d1714SBenjamin Tissoires 		wacom->battery.properties = wacom_battery_props;
1024471d1714SBenjamin Tissoires 		wacom->battery.num_properties = ARRAY_SIZE(wacom_battery_props);
1025471d1714SBenjamin Tissoires 		wacom->battery.get_property = wacom_battery_get_property;
1026d70420b9SBenjamin Tissoires 		sprintf(wacom->wacom_wac.bat_name, "wacom_battery_%ld", n);
1027d70420b9SBenjamin Tissoires 		wacom->battery.name = wacom->wacom_wac.bat_name;
1028471d1714SBenjamin Tissoires 		wacom->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1029471d1714SBenjamin Tissoires 		wacom->battery.use_for_apm = 0;
1030471d1714SBenjamin Tissoires 
10317dbd229eSBenjamin Tissoires 		wacom->ac.properties = wacom_ac_props;
10327dbd229eSBenjamin Tissoires 		wacom->ac.num_properties = ARRAY_SIZE(wacom_ac_props);
10337dbd229eSBenjamin Tissoires 		wacom->ac.get_property = wacom_ac_get_property;
10347dbd229eSBenjamin Tissoires 		sprintf(wacom->wacom_wac.ac_name, "wacom_ac_%ld", n);
10357dbd229eSBenjamin Tissoires 		wacom->ac.name = wacom->wacom_wac.ac_name;
10367dbd229eSBenjamin Tissoires 		wacom->ac.type = POWER_SUPPLY_TYPE_MAINS;
10377dbd229eSBenjamin Tissoires 		wacom->ac.use_for_apm = 0;
10387dbd229eSBenjamin Tissoires 
1039471d1714SBenjamin Tissoires 		error = power_supply_register(&wacom->hdev->dev,
1040471d1714SBenjamin Tissoires 					      &wacom->battery);
10417dbd229eSBenjamin Tissoires 		if (error)
10427dbd229eSBenjamin Tissoires 			return error;
1043471d1714SBenjamin Tissoires 
10447dbd229eSBenjamin Tissoires 		power_supply_powers(&wacom->battery, &wacom->hdev->dev);
10457dbd229eSBenjamin Tissoires 
10467dbd229eSBenjamin Tissoires 		error = power_supply_register(&wacom->hdev->dev, &wacom->ac);
10477dbd229eSBenjamin Tissoires 		if (error) {
10487dbd229eSBenjamin Tissoires 			power_supply_unregister(&wacom->battery);
10497dbd229eSBenjamin Tissoires 			return error;
1050471d1714SBenjamin Tissoires 		}
1051471d1714SBenjamin Tissoires 
10527dbd229eSBenjamin Tissoires 		power_supply_powers(&wacom->ac, &wacom->hdev->dev);
10537dbd229eSBenjamin Tissoires 	}
10547dbd229eSBenjamin Tissoires 
10557dbd229eSBenjamin Tissoires 	return 0;
1056471d1714SBenjamin Tissoires }
1057471d1714SBenjamin Tissoires 
1058471d1714SBenjamin Tissoires static void wacom_destroy_battery(struct wacom *wacom)
1059471d1714SBenjamin Tissoires {
1060ac8d1010SBenjamin Tissoires 	if ((wacom->wacom_wac.features.quirks & WACOM_QUIRK_BATTERY) &&
1061471d1714SBenjamin Tissoires 	     wacom->battery.dev) {
1062471d1714SBenjamin Tissoires 		power_supply_unregister(&wacom->battery);
1063471d1714SBenjamin Tissoires 		wacom->battery.dev = NULL;
10647dbd229eSBenjamin Tissoires 		power_supply_unregister(&wacom->ac);
10657dbd229eSBenjamin Tissoires 		wacom->ac.dev = NULL;
1066471d1714SBenjamin Tissoires 	}
1067471d1714SBenjamin Tissoires }
1068471d1714SBenjamin Tissoires 
1069f81a1295SBenjamin Tissoires static ssize_t wacom_show_speed(struct device *dev,
1070f81a1295SBenjamin Tissoires 				struct device_attribute
1071f81a1295SBenjamin Tissoires 				*attr, char *buf)
1072f81a1295SBenjamin Tissoires {
1073f81a1295SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1074f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1075f81a1295SBenjamin Tissoires 
1076f81a1295SBenjamin Tissoires 	return snprintf(buf, PAGE_SIZE, "%i\n", wacom->wacom_wac.bt_high_speed);
1077f81a1295SBenjamin Tissoires }
1078f81a1295SBenjamin Tissoires 
1079f81a1295SBenjamin Tissoires static ssize_t wacom_store_speed(struct device *dev,
1080f81a1295SBenjamin Tissoires 				struct device_attribute *attr,
1081f81a1295SBenjamin Tissoires 				const char *buf, size_t count)
1082f81a1295SBenjamin Tissoires {
1083f81a1295SBenjamin Tissoires 	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1084f81a1295SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1085f81a1295SBenjamin Tissoires 	u8 new_speed;
1086f81a1295SBenjamin Tissoires 
1087f81a1295SBenjamin Tissoires 	if (kstrtou8(buf, 0, &new_speed))
1088f81a1295SBenjamin Tissoires 		return -EINVAL;
1089f81a1295SBenjamin Tissoires 
1090f81a1295SBenjamin Tissoires 	if (new_speed != 0 && new_speed != 1)
1091f81a1295SBenjamin Tissoires 		return -EINVAL;
1092f81a1295SBenjamin Tissoires 
1093f81a1295SBenjamin Tissoires 	wacom_bt_query_tablet_data(hdev, new_speed, &wacom->wacom_wac.features);
1094f81a1295SBenjamin Tissoires 
1095f81a1295SBenjamin Tissoires 	return count;
1096f81a1295SBenjamin Tissoires }
1097f81a1295SBenjamin Tissoires 
1098e0984bc3SPing Cheng static DEVICE_ATTR(speed, DEV_ATTR_RW_PERM,
1099f81a1295SBenjamin Tissoires 		wacom_show_speed, wacom_store_speed);
1100f81a1295SBenjamin Tissoires 
1101471d1714SBenjamin Tissoires static struct input_dev *wacom_allocate_input(struct wacom *wacom)
1102471d1714SBenjamin Tissoires {
1103471d1714SBenjamin Tissoires 	struct input_dev *input_dev;
1104471d1714SBenjamin Tissoires 	struct hid_device *hdev = wacom->hdev;
1105471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1106471d1714SBenjamin Tissoires 
1107471d1714SBenjamin Tissoires 	input_dev = input_allocate_device();
1108471d1714SBenjamin Tissoires 	if (!input_dev)
1109471d1714SBenjamin Tissoires 		return NULL;
1110471d1714SBenjamin Tissoires 
1111471d1714SBenjamin Tissoires 	input_dev->name = wacom_wac->name;
1112471d1714SBenjamin Tissoires 	input_dev->phys = hdev->phys;
1113471d1714SBenjamin Tissoires 	input_dev->dev.parent = &hdev->dev;
1114471d1714SBenjamin Tissoires 	input_dev->open = wacom_open;
1115471d1714SBenjamin Tissoires 	input_dev->close = wacom_close;
1116471d1714SBenjamin Tissoires 	input_dev->uniq = hdev->uniq;
1117471d1714SBenjamin Tissoires 	input_dev->id.bustype = hdev->bus;
1118471d1714SBenjamin Tissoires 	input_dev->id.vendor  = hdev->vendor;
111912969e3bSBenjamin Tissoires 	input_dev->id.product = wacom_wac->pid ? wacom_wac->pid : hdev->product;
1120471d1714SBenjamin Tissoires 	input_dev->id.version = hdev->version;
1121471d1714SBenjamin Tissoires 	input_set_drvdata(input_dev, wacom);
1122471d1714SBenjamin Tissoires 
1123471d1714SBenjamin Tissoires 	return input_dev;
1124471d1714SBenjamin Tissoires }
1125471d1714SBenjamin Tissoires 
11262546dacdSBenjamin Tissoires static void wacom_free_inputs(struct wacom *wacom)
1127471d1714SBenjamin Tissoires {
11282546dacdSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
11292546dacdSBenjamin Tissoires 
11302546dacdSBenjamin Tissoires 	if (wacom_wac->input)
11312546dacdSBenjamin Tissoires 		input_free_device(wacom_wac->input);
11322546dacdSBenjamin Tissoires 	if (wacom_wac->pad_input)
11332546dacdSBenjamin Tissoires 		input_free_device(wacom_wac->pad_input);
11342546dacdSBenjamin Tissoires 	wacom_wac->input = NULL;
11352546dacdSBenjamin Tissoires 	wacom_wac->pad_input = NULL;
11362546dacdSBenjamin Tissoires }
11372546dacdSBenjamin Tissoires 
11382546dacdSBenjamin Tissoires static int wacom_allocate_inputs(struct wacom *wacom)
11392546dacdSBenjamin Tissoires {
11402546dacdSBenjamin Tissoires 	struct input_dev *input_dev, *pad_input_dev;
11412546dacdSBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
11422546dacdSBenjamin Tissoires 
11432546dacdSBenjamin Tissoires 	input_dev = wacom_allocate_input(wacom);
11442546dacdSBenjamin Tissoires 	pad_input_dev = wacom_allocate_input(wacom);
11452546dacdSBenjamin Tissoires 	if (!input_dev || !pad_input_dev) {
11462546dacdSBenjamin Tissoires 		wacom_free_inputs(wacom);
11472546dacdSBenjamin Tissoires 		return -ENOMEM;
11482546dacdSBenjamin Tissoires 	}
11492546dacdSBenjamin Tissoires 
11502546dacdSBenjamin Tissoires 	wacom_wac->input = input_dev;
11512546dacdSBenjamin Tissoires 	wacom_wac->pad_input = pad_input_dev;
11522546dacdSBenjamin Tissoires 	wacom_wac->pad_input->name = wacom_wac->pad_name;
11532546dacdSBenjamin Tissoires 
11542546dacdSBenjamin Tissoires 	return 0;
11552546dacdSBenjamin Tissoires }
11562546dacdSBenjamin Tissoires 
11572546dacdSBenjamin Tissoires static void wacom_clean_inputs(struct wacom *wacom)
11582546dacdSBenjamin Tissoires {
11592546dacdSBenjamin Tissoires 	if (wacom->wacom_wac.input) {
11602546dacdSBenjamin Tissoires 		if (wacom->wacom_wac.input_registered)
1161471d1714SBenjamin Tissoires 			input_unregister_device(wacom->wacom_wac.input);
11622546dacdSBenjamin Tissoires 		else
11632546dacdSBenjamin Tissoires 			input_free_device(wacom->wacom_wac.input);
11642546dacdSBenjamin Tissoires 	}
11652546dacdSBenjamin Tissoires 	if (wacom->wacom_wac.pad_input) {
1166954df6adSPing Cheng 		if (wacom->wacom_wac.pad_registered)
1167471d1714SBenjamin Tissoires 			input_unregister_device(wacom->wacom_wac.pad_input);
11682546dacdSBenjamin Tissoires 		else
11692546dacdSBenjamin Tissoires 			input_free_device(wacom->wacom_wac.pad_input);
11702546dacdSBenjamin Tissoires 	}
1171471d1714SBenjamin Tissoires 	wacom->wacom_wac.input = NULL;
1172471d1714SBenjamin Tissoires 	wacom->wacom_wac.pad_input = NULL;
1173912ca216SPing Cheng 	wacom_destroy_leds(wacom);
1174471d1714SBenjamin Tissoires }
1175471d1714SBenjamin Tissoires 
1176471d1714SBenjamin Tissoires static int wacom_register_inputs(struct wacom *wacom)
1177471d1714SBenjamin Tissoires {
1178471d1714SBenjamin Tissoires 	struct input_dev *input_dev, *pad_input_dev;
1179471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &(wacom->wacom_wac);
1180471d1714SBenjamin Tissoires 	int error;
1181471d1714SBenjamin Tissoires 
11822546dacdSBenjamin Tissoires 	input_dev = wacom_wac->input;
11832546dacdSBenjamin Tissoires 	pad_input_dev = wacom_wac->pad_input;
1184471d1714SBenjamin Tissoires 
11852546dacdSBenjamin Tissoires 	if (!input_dev || !pad_input_dev)
11862546dacdSBenjamin Tissoires 		return -EINVAL;
1187471d1714SBenjamin Tissoires 
118830ebc1aeSPing Cheng 	error = wacom_setup_pentouch_input_capabilities(input_dev, wacom_wac);
118930ebc1aeSPing Cheng 	if (!error) {
1190471d1714SBenjamin Tissoires 		error = input_register_device(input_dev);
1191471d1714SBenjamin Tissoires 		if (error)
11922546dacdSBenjamin Tissoires 			return error;
1193954df6adSPing Cheng 		wacom_wac->input_registered = true;
119430ebc1aeSPing Cheng 	}
1195471d1714SBenjamin Tissoires 
1196471d1714SBenjamin Tissoires 	error = wacom_setup_pad_input_capabilities(pad_input_dev, wacom_wac);
1197471d1714SBenjamin Tissoires 	if (error) {
1198471d1714SBenjamin Tissoires 		/* no pad in use on this interface */
1199471d1714SBenjamin Tissoires 		input_free_device(pad_input_dev);
1200471d1714SBenjamin Tissoires 		wacom_wac->pad_input = NULL;
1201471d1714SBenjamin Tissoires 		pad_input_dev = NULL;
1202471d1714SBenjamin Tissoires 	} else {
1203471d1714SBenjamin Tissoires 		error = input_register_device(pad_input_dev);
1204471d1714SBenjamin Tissoires 		if (error)
12057fefeec5SBenjamin Tissoires 			goto fail_register_pad_input;
1206954df6adSPing Cheng 		wacom_wac->pad_registered = true;
1207912ca216SPing Cheng 
1208912ca216SPing Cheng 		error = wacom_initialize_leds(wacom);
1209912ca216SPing Cheng 		if (error)
12107fefeec5SBenjamin Tissoires 			goto fail_leds;
1211471d1714SBenjamin Tissoires 	}
1212471d1714SBenjamin Tissoires 
1213471d1714SBenjamin Tissoires 	return 0;
1214471d1714SBenjamin Tissoires 
12157fefeec5SBenjamin Tissoires fail_leds:
1216912ca216SPing Cheng 	input_unregister_device(pad_input_dev);
1217912ca216SPing Cheng 	pad_input_dev = NULL;
1218954df6adSPing Cheng 	wacom_wac->pad_registered = false;
12197fefeec5SBenjamin Tissoires fail_register_pad_input:
1220471d1714SBenjamin Tissoires 	input_unregister_device(input_dev);
1221471d1714SBenjamin Tissoires 	wacom_wac->input = NULL;
1222954df6adSPing Cheng 	wacom_wac->input_registered = false;
1223471d1714SBenjamin Tissoires 	return error;
1224471d1714SBenjamin Tissoires }
1225471d1714SBenjamin Tissoires 
1226471d1714SBenjamin Tissoires static void wacom_wireless_work(struct work_struct *work)
1227471d1714SBenjamin Tissoires {
1228471d1714SBenjamin Tissoires 	struct wacom *wacom = container_of(work, struct wacom, work);
1229471d1714SBenjamin Tissoires 	struct usb_device *usbdev = wacom->usbdev;
1230471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1231471d1714SBenjamin Tissoires 	struct hid_device *hdev1, *hdev2;
1232471d1714SBenjamin Tissoires 	struct wacom *wacom1, *wacom2;
1233471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac1, *wacom_wac2;
1234471d1714SBenjamin Tissoires 	int error;
1235471d1714SBenjamin Tissoires 
1236471d1714SBenjamin Tissoires 	/*
1237471d1714SBenjamin Tissoires 	 * Regardless if this is a disconnect or a new tablet,
1238471d1714SBenjamin Tissoires 	 * remove any existing input and battery devices.
1239471d1714SBenjamin Tissoires 	 */
1240471d1714SBenjamin Tissoires 
1241471d1714SBenjamin Tissoires 	wacom_destroy_battery(wacom);
1242471d1714SBenjamin Tissoires 
1243471d1714SBenjamin Tissoires 	/* Stylus interface */
1244471d1714SBenjamin Tissoires 	hdev1 = usb_get_intfdata(usbdev->config->interface[1]);
1245471d1714SBenjamin Tissoires 	wacom1 = hid_get_drvdata(hdev1);
1246471d1714SBenjamin Tissoires 	wacom_wac1 = &(wacom1->wacom_wac);
12472546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom1);
1248471d1714SBenjamin Tissoires 
1249471d1714SBenjamin Tissoires 	/* Touch interface */
1250471d1714SBenjamin Tissoires 	hdev2 = usb_get_intfdata(usbdev->config->interface[2]);
1251471d1714SBenjamin Tissoires 	wacom2 = hid_get_drvdata(hdev2);
1252471d1714SBenjamin Tissoires 	wacom_wac2 = &(wacom2->wacom_wac);
12532546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom2);
1254471d1714SBenjamin Tissoires 
1255471d1714SBenjamin Tissoires 	if (wacom_wac->pid == 0) {
1256471d1714SBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet disconnected\n");
1257ac8d1010SBenjamin Tissoires 		wacom_wac1->shared->type = 0;
1258471d1714SBenjamin Tissoires 	} else {
1259471d1714SBenjamin Tissoires 		const struct hid_device_id *id = wacom_ids;
1260471d1714SBenjamin Tissoires 
1261471d1714SBenjamin Tissoires 		hid_info(wacom->hdev, "wireless tablet connected with PID %x\n",
1262471d1714SBenjamin Tissoires 			 wacom_wac->pid);
1263471d1714SBenjamin Tissoires 
1264471d1714SBenjamin Tissoires 		while (id->bus) {
1265471d1714SBenjamin Tissoires 			if (id->vendor == USB_VENDOR_ID_WACOM &&
1266471d1714SBenjamin Tissoires 			    id->product == wacom_wac->pid)
1267471d1714SBenjamin Tissoires 				break;
1268471d1714SBenjamin Tissoires 			id++;
1269471d1714SBenjamin Tissoires 		}
1270471d1714SBenjamin Tissoires 
1271471d1714SBenjamin Tissoires 		if (!id->bus) {
1272471d1714SBenjamin Tissoires 			hid_info(wacom->hdev, "ignoring unknown PID.\n");
1273471d1714SBenjamin Tissoires 			return;
1274471d1714SBenjamin Tissoires 		}
1275471d1714SBenjamin Tissoires 
1276471d1714SBenjamin Tissoires 		/* Stylus interface */
1277471d1714SBenjamin Tissoires 		wacom_wac1->features =
1278471d1714SBenjamin Tissoires 			*((struct wacom_features *)id->driver_data);
1279471d1714SBenjamin Tissoires 		wacom_wac1->features.device_type = BTN_TOOL_PEN;
1280471d1714SBenjamin Tissoires 		snprintf(wacom_wac1->name, WACOM_NAME_MAX, "%s (WL) Pen",
1281471d1714SBenjamin Tissoires 			 wacom_wac1->features.name);
1282471d1714SBenjamin Tissoires 		snprintf(wacom_wac1->pad_name, WACOM_NAME_MAX, "%s (WL) Pad",
1283471d1714SBenjamin Tissoires 			 wacom_wac1->features.name);
1284471d1714SBenjamin Tissoires 		wacom_wac1->shared->touch_max = wacom_wac1->features.touch_max;
1285471d1714SBenjamin Tissoires 		wacom_wac1->shared->type = wacom_wac1->features.type;
1286912ca216SPing Cheng 		wacom_wac1->pid = wacom_wac->pid;
12872546dacdSBenjamin Tissoires 		error = wacom_allocate_inputs(wacom1) ||
12882546dacdSBenjamin Tissoires 			wacom_register_inputs(wacom1);
1289471d1714SBenjamin Tissoires 		if (error)
1290471d1714SBenjamin Tissoires 			goto fail;
1291471d1714SBenjamin Tissoires 
1292471d1714SBenjamin Tissoires 		/* Touch interface */
1293471d1714SBenjamin Tissoires 		if (wacom_wac1->features.touch_max ||
1294471d1714SBenjamin Tissoires 		    wacom_wac1->features.type == INTUOSHT) {
1295471d1714SBenjamin Tissoires 			wacom_wac2->features =
1296471d1714SBenjamin Tissoires 				*((struct wacom_features *)id->driver_data);
1297471d1714SBenjamin Tissoires 			wacom_wac2->features.pktlen = WACOM_PKGLEN_BBTOUCH3;
1298471d1714SBenjamin Tissoires 			wacom_wac2->features.device_type = BTN_TOOL_FINGER;
1299471d1714SBenjamin Tissoires 			wacom_wac2->features.x_max = wacom_wac2->features.y_max = 4096;
1300471d1714SBenjamin Tissoires 			if (wacom_wac2->features.touch_max)
1301471d1714SBenjamin Tissoires 				snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1302471d1714SBenjamin Tissoires 					 "%s (WL) Finger",wacom_wac2->features.name);
1303471d1714SBenjamin Tissoires 			else
1304471d1714SBenjamin Tissoires 				snprintf(wacom_wac2->name, WACOM_NAME_MAX,
1305471d1714SBenjamin Tissoires 					 "%s (WL) Pad",wacom_wac2->features.name);
1306471d1714SBenjamin Tissoires 			snprintf(wacom_wac2->pad_name, WACOM_NAME_MAX,
1307471d1714SBenjamin Tissoires 				 "%s (WL) Pad", wacom_wac2->features.name);
1308912ca216SPing Cheng 			wacom_wac2->pid = wacom_wac->pid;
13092546dacdSBenjamin Tissoires 			error = wacom_allocate_inputs(wacom2) ||
13102546dacdSBenjamin Tissoires 				wacom_register_inputs(wacom2);
1311471d1714SBenjamin Tissoires 			if (error)
1312471d1714SBenjamin Tissoires 				goto fail;
1313471d1714SBenjamin Tissoires 
1314471d1714SBenjamin Tissoires 			if (wacom_wac1->features.type == INTUOSHT &&
1315471d1714SBenjamin Tissoires 			    wacom_wac1->features.touch_max)
1316471d1714SBenjamin Tissoires 				wacom_wac->shared->touch_input = wacom_wac2->input;
1317471d1714SBenjamin Tissoires 		}
1318471d1714SBenjamin Tissoires 
1319471d1714SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1320471d1714SBenjamin Tissoires 		if (error)
1321471d1714SBenjamin Tissoires 			goto fail;
1322471d1714SBenjamin Tissoires 	}
1323471d1714SBenjamin Tissoires 
1324471d1714SBenjamin Tissoires 	return;
1325471d1714SBenjamin Tissoires 
1326471d1714SBenjamin Tissoires fail:
13272546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom1);
13282546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom2);
1329471d1714SBenjamin Tissoires 	return;
1330471d1714SBenjamin Tissoires }
1331471d1714SBenjamin Tissoires 
1332471d1714SBenjamin Tissoires /*
1333471d1714SBenjamin Tissoires  * Not all devices report physical dimensions from HID.
1334471d1714SBenjamin Tissoires  * Compute the default from hardcoded logical dimension
1335471d1714SBenjamin Tissoires  * and resolution before driver overwrites them.
1336471d1714SBenjamin Tissoires  */
1337471d1714SBenjamin Tissoires static void wacom_set_default_phy(struct wacom_features *features)
1338471d1714SBenjamin Tissoires {
1339471d1714SBenjamin Tissoires 	if (features->x_resolution) {
1340471d1714SBenjamin Tissoires 		features->x_phy = (features->x_max * 100) /
1341471d1714SBenjamin Tissoires 					features->x_resolution;
1342471d1714SBenjamin Tissoires 		features->y_phy = (features->y_max * 100) /
1343471d1714SBenjamin Tissoires 					features->y_resolution;
1344471d1714SBenjamin Tissoires 	}
1345471d1714SBenjamin Tissoires }
1346471d1714SBenjamin Tissoires 
1347471d1714SBenjamin Tissoires static void wacom_calculate_res(struct wacom_features *features)
1348471d1714SBenjamin Tissoires {
1349471d1714SBenjamin Tissoires 	features->x_resolution = wacom_calc_hid_res(features->x_max,
1350471d1714SBenjamin Tissoires 						    features->x_phy,
1351471d1714SBenjamin Tissoires 						    features->unit,
1352471d1714SBenjamin Tissoires 						    features->unitExpo);
1353471d1714SBenjamin Tissoires 	features->y_resolution = wacom_calc_hid_res(features->y_max,
1354471d1714SBenjamin Tissoires 						    features->y_phy,
1355471d1714SBenjamin Tissoires 						    features->unit,
1356471d1714SBenjamin Tissoires 						    features->unitExpo);
1357471d1714SBenjamin Tissoires }
1358471d1714SBenjamin Tissoires 
1359471d1714SBenjamin Tissoires static size_t wacom_compute_pktlen(struct hid_device *hdev)
1360471d1714SBenjamin Tissoires {
1361471d1714SBenjamin Tissoires 	struct hid_report_enum *report_enum;
1362471d1714SBenjamin Tissoires 	struct hid_report *report;
1363471d1714SBenjamin Tissoires 	size_t size = 0;
1364471d1714SBenjamin Tissoires 
1365471d1714SBenjamin Tissoires 	report_enum = hdev->report_enum + HID_INPUT_REPORT;
1366471d1714SBenjamin Tissoires 
1367471d1714SBenjamin Tissoires 	list_for_each_entry(report, &report_enum->report_list, list) {
1368dabb05c6SMathieu Magnaudet 		size_t report_size = hid_report_len(report);
1369471d1714SBenjamin Tissoires 		if (report_size > size)
1370471d1714SBenjamin Tissoires 			size = report_size;
1371471d1714SBenjamin Tissoires 	}
1372471d1714SBenjamin Tissoires 
1373471d1714SBenjamin Tissoires 	return size;
1374471d1714SBenjamin Tissoires }
1375471d1714SBenjamin Tissoires 
1376471d1714SBenjamin Tissoires static int wacom_probe(struct hid_device *hdev,
1377471d1714SBenjamin Tissoires 		const struct hid_device_id *id)
1378471d1714SBenjamin Tissoires {
1379471d1714SBenjamin Tissoires 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
1380471d1714SBenjamin Tissoires 	struct usb_device *dev = interface_to_usbdev(intf);
1381471d1714SBenjamin Tissoires 	struct wacom *wacom;
1382471d1714SBenjamin Tissoires 	struct wacom_wac *wacom_wac;
1383471d1714SBenjamin Tissoires 	struct wacom_features *features;
1384471d1714SBenjamin Tissoires 	int error;
13857704ac93SBenjamin Tissoires 	unsigned int connect_mask = HID_CONNECT_HIDRAW;
1386471d1714SBenjamin Tissoires 
1387471d1714SBenjamin Tissoires 	if (!id->driver_data)
1388471d1714SBenjamin Tissoires 		return -EINVAL;
1389471d1714SBenjamin Tissoires 
13908ffffd52SBenjamin Tissoires 	hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
13918ffffd52SBenjamin Tissoires 
1392471d1714SBenjamin Tissoires 	wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
1393471d1714SBenjamin Tissoires 	if (!wacom)
1394471d1714SBenjamin Tissoires 		return -ENOMEM;
1395471d1714SBenjamin Tissoires 
1396471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, wacom);
1397471d1714SBenjamin Tissoires 	wacom->hdev = hdev;
1398471d1714SBenjamin Tissoires 
1399471d1714SBenjamin Tissoires 	/* ask for the report descriptor to be loaded by HID */
1400471d1714SBenjamin Tissoires 	error = hid_parse(hdev);
1401471d1714SBenjamin Tissoires 	if (error) {
1402471d1714SBenjamin Tissoires 		hid_err(hdev, "parse failed\n");
14037fefeec5SBenjamin Tissoires 		goto fail_parse;
1404471d1714SBenjamin Tissoires 	}
1405471d1714SBenjamin Tissoires 
1406471d1714SBenjamin Tissoires 	wacom_wac = &wacom->wacom_wac;
1407471d1714SBenjamin Tissoires 	wacom_wac->features = *((struct wacom_features *)id->driver_data);
1408471d1714SBenjamin Tissoires 	features = &wacom_wac->features;
1409471d1714SBenjamin Tissoires 	features->pktlen = wacom_compute_pktlen(hdev);
1410471d1714SBenjamin Tissoires 	if (features->pktlen > WACOM_PKGLEN_MAX) {
1411471d1714SBenjamin Tissoires 		error = -EINVAL;
14127fefeec5SBenjamin Tissoires 		goto fail_pktlen;
1413471d1714SBenjamin Tissoires 	}
1414471d1714SBenjamin Tissoires 
1415471d1714SBenjamin Tissoires 	if (features->check_for_hid_type && features->hid_type != hdev->type) {
1416471d1714SBenjamin Tissoires 		error = -ENODEV;
14177fefeec5SBenjamin Tissoires 		goto fail_type;
1418471d1714SBenjamin Tissoires 	}
1419471d1714SBenjamin Tissoires 
1420471d1714SBenjamin Tissoires 	wacom->usbdev = dev;
1421471d1714SBenjamin Tissoires 	wacom->intf = intf;
1422471d1714SBenjamin Tissoires 	mutex_init(&wacom->lock);
1423471d1714SBenjamin Tissoires 	INIT_WORK(&wacom->work, wacom_wireless_work);
1424471d1714SBenjamin Tissoires 
1425494078b0SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1426494078b0SBenjamin Tissoires 		error = wacom_allocate_inputs(wacom);
1427494078b0SBenjamin Tissoires 		if (error)
1428494078b0SBenjamin Tissoires 			goto fail_allocate_inputs;
1429494078b0SBenjamin Tissoires 	}
1430494078b0SBenjamin Tissoires 
1431*8c97a765SBenjamin Tissoires 	/*
1432*8c97a765SBenjamin Tissoires 	 * Bamboo Pad has a generic hid handling for the Pen, and we switch it
1433*8c97a765SBenjamin Tissoires 	 * into debug mode for the touch part.
1434*8c97a765SBenjamin Tissoires 	 * We ignore the other interfaces.
1435*8c97a765SBenjamin Tissoires 	 */
1436*8c97a765SBenjamin Tissoires 	if (features->type == BAMBOO_PAD) {
1437*8c97a765SBenjamin Tissoires 		if (features->pktlen == WACOM_PKGLEN_PENABLED) {
1438*8c97a765SBenjamin Tissoires 			features->type = HID_GENERIC;
1439*8c97a765SBenjamin Tissoires 		} else if ((features->pktlen != WACOM_PKGLEN_BPAD_TOUCH) &&
1440*8c97a765SBenjamin Tissoires 			   (features->pktlen != WACOM_PKGLEN_BPAD_TOUCH_USB)) {
1441*8c97a765SBenjamin Tissoires 			error = -ENODEV;
1442*8c97a765SBenjamin Tissoires 			goto fail_shared_data;
1443*8c97a765SBenjamin Tissoires 		}
1444*8c97a765SBenjamin Tissoires 	}
1445*8c97a765SBenjamin Tissoires 
1446471d1714SBenjamin Tissoires 	/* set the default size in case we do not get them from hid */
1447471d1714SBenjamin Tissoires 	wacom_set_default_phy(features);
1448471d1714SBenjamin Tissoires 
1449471d1714SBenjamin Tissoires 	/* Retrieve the physical and logical size for touch devices */
1450471d1714SBenjamin Tissoires 	wacom_retrieve_hid_descriptor(hdev, features);
1451471d1714SBenjamin Tissoires 
1452471d1714SBenjamin Tissoires 	/*
1453471d1714SBenjamin Tissoires 	 * Intuos5 has no useful data about its touch interface in its
1454471d1714SBenjamin Tissoires 	 * HID descriptor. If this is the touch interface (PacketSize
1455471d1714SBenjamin Tissoires 	 * of WACOM_PKGLEN_BBTOUCH3), override the table values.
1456471d1714SBenjamin Tissoires 	 */
1457471d1714SBenjamin Tissoires 	if (features->type >= INTUOS5S && features->type <= INTUOSHT) {
1458471d1714SBenjamin Tissoires 		if (features->pktlen == WACOM_PKGLEN_BBTOUCH3) {
1459471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_FINGER;
1460471d1714SBenjamin Tissoires 
1461471d1714SBenjamin Tissoires 			features->x_max = 4096;
1462471d1714SBenjamin Tissoires 			features->y_max = 4096;
1463471d1714SBenjamin Tissoires 		} else {
1464471d1714SBenjamin Tissoires 			features->device_type = BTN_TOOL_PEN;
1465471d1714SBenjamin Tissoires 		}
1466471d1714SBenjamin Tissoires 	}
1467471d1714SBenjamin Tissoires 
1468471d1714SBenjamin Tissoires 	/*
1469471d1714SBenjamin Tissoires 	 * Same thing for Bamboo 3rd gen.
1470471d1714SBenjamin Tissoires 	 */
1471471d1714SBenjamin Tissoires 	if ((features->type == BAMBOO_PT) &&
1472471d1714SBenjamin Tissoires 	    (features->pktlen == WACOM_PKGLEN_BBTOUCH3) &&
1473471d1714SBenjamin Tissoires 	    (features->device_type == BTN_TOOL_PEN)) {
1474471d1714SBenjamin Tissoires 		features->device_type = BTN_TOOL_FINGER;
1475471d1714SBenjamin Tissoires 
1476471d1714SBenjamin Tissoires 		features->x_max = 4096;
1477471d1714SBenjamin Tissoires 		features->y_max = 4096;
1478471d1714SBenjamin Tissoires 	}
1479471d1714SBenjamin Tissoires 
1480*8c97a765SBenjamin Tissoires 	/*
1481*8c97a765SBenjamin Tissoires 	 * Same thing for Bamboo PAD
1482*8c97a765SBenjamin Tissoires 	 */
1483*8c97a765SBenjamin Tissoires 	if (features->type == BAMBOO_PAD)
1484*8c97a765SBenjamin Tissoires 		features->device_type = BTN_TOOL_FINGER;
1485*8c97a765SBenjamin Tissoires 
1486f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
1487f81a1295SBenjamin Tissoires 		features->quirks |= WACOM_QUIRK_BATTERY;
1488f81a1295SBenjamin Tissoires 
1489471d1714SBenjamin Tissoires 	wacom_setup_device_quirks(features);
1490471d1714SBenjamin Tissoires 
1491471d1714SBenjamin Tissoires 	/* set unit to "100th of a mm" for devices not reported by HID */
1492471d1714SBenjamin Tissoires 	if (!features->unit) {
1493471d1714SBenjamin Tissoires 		features->unit = 0x11;
1494471d1714SBenjamin Tissoires 		features->unitExpo = -3;
1495471d1714SBenjamin Tissoires 	}
1496471d1714SBenjamin Tissoires 	wacom_calculate_res(features);
1497471d1714SBenjamin Tissoires 
1498471d1714SBenjamin Tissoires 	strlcpy(wacom_wac->name, features->name, sizeof(wacom_wac->name));
1499471d1714SBenjamin Tissoires 	snprintf(wacom_wac->pad_name, sizeof(wacom_wac->pad_name),
1500471d1714SBenjamin Tissoires 		"%s Pad", features->name);
1501471d1714SBenjamin Tissoires 
1502471d1714SBenjamin Tissoires 	if (features->quirks & WACOM_QUIRK_MULTI_INPUT) {
1503471d1714SBenjamin Tissoires 		/* Append the device type to the name */
1504471d1714SBenjamin Tissoires 		if (features->device_type != BTN_TOOL_FINGER)
1505471d1714SBenjamin Tissoires 			strlcat(wacom_wac->name, " Pen", WACOM_NAME_MAX);
1506471d1714SBenjamin Tissoires 		else if (features->touch_max)
1507471d1714SBenjamin Tissoires 			strlcat(wacom_wac->name, " Finger", WACOM_NAME_MAX);
1508471d1714SBenjamin Tissoires 		else
1509471d1714SBenjamin Tissoires 			strlcat(wacom_wac->name, " Pad", WACOM_NAME_MAX);
1510471d1714SBenjamin Tissoires 
1511471d1714SBenjamin Tissoires 		error = wacom_add_shared_data(hdev);
1512471d1714SBenjamin Tissoires 		if (error)
15137fefeec5SBenjamin Tissoires 			goto fail_shared_data;
1514471d1714SBenjamin Tissoires 	}
1515471d1714SBenjamin Tissoires 
1516f81a1295SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_MONITOR) &&
1517f81a1295SBenjamin Tissoires 	     (features->quirks & WACOM_QUIRK_BATTERY)) {
1518f81a1295SBenjamin Tissoires 		error = wacom_initialize_battery(wacom);
1519f81a1295SBenjamin Tissoires 		if (error)
15207fefeec5SBenjamin Tissoires 			goto fail_battery;
1521f81a1295SBenjamin Tissoires 	}
1522f81a1295SBenjamin Tissoires 
1523471d1714SBenjamin Tissoires 	if (!(features->quirks & WACOM_QUIRK_NO_INPUT)) {
1524494078b0SBenjamin Tissoires 		error = wacom_register_inputs(wacom);
1525471d1714SBenjamin Tissoires 		if (error)
15267fefeec5SBenjamin Tissoires 			goto fail_register_inputs;
1527f81a1295SBenjamin Tissoires 	}
1528f81a1295SBenjamin Tissoires 
1529f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH) {
1530f81a1295SBenjamin Tissoires 		error = device_create_file(&hdev->dev, &dev_attr_speed);
1531f81a1295SBenjamin Tissoires 		if (error)
1532f81a1295SBenjamin Tissoires 			hid_warn(hdev,
1533f81a1295SBenjamin Tissoires 				 "can't create sysfs speed attribute err: %d\n",
1534f81a1295SBenjamin Tissoires 				 error);
1535471d1714SBenjamin Tissoires 	}
1536471d1714SBenjamin Tissoires 
15377704ac93SBenjamin Tissoires 	if (features->type == HID_GENERIC)
15387704ac93SBenjamin Tissoires 		connect_mask |= HID_CONNECT_DRIVER;
15397704ac93SBenjamin Tissoires 
1540471d1714SBenjamin Tissoires 	/* Regular HID work starts now */
15417704ac93SBenjamin Tissoires 	error = hid_hw_start(hdev, connect_mask);
1542471d1714SBenjamin Tissoires 	if (error) {
1543471d1714SBenjamin Tissoires 		hid_err(hdev, "hw start failed\n");
15447fefeec5SBenjamin Tissoires 		goto fail_hw_start;
1545471d1714SBenjamin Tissoires 	}
1546471d1714SBenjamin Tissoires 
15475ae6e89fSBenjamin Tissoires 	/* Note that if query fails it is not a hard failure */
15485ae6e89fSBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
15495ae6e89fSBenjamin Tissoires 
1550471d1714SBenjamin Tissoires 	if (features->quirks & WACOM_QUIRK_MONITOR)
1551471d1714SBenjamin Tissoires 		error = hid_hw_open(hdev);
1552471d1714SBenjamin Tissoires 
1553471d1714SBenjamin Tissoires 	if (wacom_wac->features.type == INTUOSHT && wacom_wac->features.touch_max) {
1554471d1714SBenjamin Tissoires 		if (wacom_wac->features.device_type == BTN_TOOL_FINGER)
1555471d1714SBenjamin Tissoires 			wacom_wac->shared->touch_input = wacom_wac->input;
1556471d1714SBenjamin Tissoires 	}
1557471d1714SBenjamin Tissoires 
1558471d1714SBenjamin Tissoires 	return 0;
1559471d1714SBenjamin Tissoires 
15607fefeec5SBenjamin Tissoires fail_hw_start:
15617fefeec5SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
15627fefeec5SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
15637fefeec5SBenjamin Tissoires fail_register_inputs:
15642546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom);
15657fefeec5SBenjamin Tissoires 	wacom_destroy_battery(wacom);
15667fefeec5SBenjamin Tissoires fail_battery:
1567a97ac104SBenjamin Tissoires 	wacom_remove_shared_data(wacom);
15687fefeec5SBenjamin Tissoires fail_shared_data:
1569494078b0SBenjamin Tissoires 	wacom_clean_inputs(wacom);
1570494078b0SBenjamin Tissoires fail_allocate_inputs:
15717fefeec5SBenjamin Tissoires fail_type:
15727fefeec5SBenjamin Tissoires fail_pktlen:
15737fefeec5SBenjamin Tissoires fail_parse:
15747fefeec5SBenjamin Tissoires 	kfree(wacom);
1575471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
1576471d1714SBenjamin Tissoires 	return error;
1577471d1714SBenjamin Tissoires }
1578471d1714SBenjamin Tissoires 
1579471d1714SBenjamin Tissoires static void wacom_remove(struct hid_device *hdev)
1580471d1714SBenjamin Tissoires {
1581471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1582471d1714SBenjamin Tissoires 
1583471d1714SBenjamin Tissoires 	hid_hw_stop(hdev);
1584471d1714SBenjamin Tissoires 
1585471d1714SBenjamin Tissoires 	cancel_work_sync(&wacom->work);
15862546dacdSBenjamin Tissoires 	wacom_clean_inputs(wacom);
1587f81a1295SBenjamin Tissoires 	if (hdev->bus == BUS_BLUETOOTH)
1588f81a1295SBenjamin Tissoires 		device_remove_file(&hdev->dev, &dev_attr_speed);
1589471d1714SBenjamin Tissoires 	wacom_destroy_battery(wacom);
1590a97ac104SBenjamin Tissoires 	wacom_remove_shared_data(wacom);
1591471d1714SBenjamin Tissoires 
1592471d1714SBenjamin Tissoires 	hid_set_drvdata(hdev, NULL);
1593471d1714SBenjamin Tissoires 	kfree(wacom);
1594471d1714SBenjamin Tissoires }
1595471d1714SBenjamin Tissoires 
159641a74581SGeert Uytterhoeven #ifdef CONFIG_PM
1597471d1714SBenjamin Tissoires static int wacom_resume(struct hid_device *hdev)
1598471d1714SBenjamin Tissoires {
1599471d1714SBenjamin Tissoires 	struct wacom *wacom = hid_get_drvdata(hdev);
1600471d1714SBenjamin Tissoires 	struct wacom_features *features = &wacom->wacom_wac.features;
1601471d1714SBenjamin Tissoires 
1602471d1714SBenjamin Tissoires 	mutex_lock(&wacom->lock);
1603471d1714SBenjamin Tissoires 
1604471d1714SBenjamin Tissoires 	/* switch to wacom mode first */
1605471d1714SBenjamin Tissoires 	wacom_query_tablet_data(hdev, features);
1606471d1714SBenjamin Tissoires 	wacom_led_control(wacom);
1607471d1714SBenjamin Tissoires 
1608471d1714SBenjamin Tissoires 	mutex_unlock(&wacom->lock);
1609471d1714SBenjamin Tissoires 
1610471d1714SBenjamin Tissoires 	return 0;
1611471d1714SBenjamin Tissoires }
1612471d1714SBenjamin Tissoires 
1613471d1714SBenjamin Tissoires static int wacom_reset_resume(struct hid_device *hdev)
1614471d1714SBenjamin Tissoires {
1615471d1714SBenjamin Tissoires 	return wacom_resume(hdev);
1616471d1714SBenjamin Tissoires }
161741a74581SGeert Uytterhoeven #endif /* CONFIG_PM */
1618471d1714SBenjamin Tissoires 
1619471d1714SBenjamin Tissoires static struct hid_driver wacom_driver = {
1620471d1714SBenjamin Tissoires 	.name =		"wacom",
1621471d1714SBenjamin Tissoires 	.id_table =	wacom_ids,
1622471d1714SBenjamin Tissoires 	.probe =	wacom_probe,
1623471d1714SBenjamin Tissoires 	.remove =	wacom_remove,
16247704ac93SBenjamin Tissoires 	.event =	wacom_wac_event,
16257704ac93SBenjamin Tissoires 	.report =	wacom_wac_report,
1626471d1714SBenjamin Tissoires #ifdef CONFIG_PM
1627471d1714SBenjamin Tissoires 	.resume =	wacom_resume,
1628471d1714SBenjamin Tissoires 	.reset_resume =	wacom_reset_resume,
1629471d1714SBenjamin Tissoires #endif
1630471d1714SBenjamin Tissoires 	.raw_event =	wacom_raw_event,
1631471d1714SBenjamin Tissoires };
1632471d1714SBenjamin Tissoires module_hid_driver(wacom_driver);
1633f2e0a7d4SBenjamin Tissoires 
1634f2e0a7d4SBenjamin Tissoires MODULE_VERSION(DRIVER_VERSION);
1635f2e0a7d4SBenjamin Tissoires MODULE_AUTHOR(DRIVER_AUTHOR);
1636f2e0a7d4SBenjamin Tissoires MODULE_DESCRIPTION(DRIVER_DESC);
1637f2e0a7d4SBenjamin Tissoires MODULE_LICENSE(DRIVER_LICENSE);
1638